Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Stop using the logs when they are not ready anymore
[simgrid.git] / examples / gras / rpc / rpc.c
index 3805fc9..3248639 100644 (file)
@@ -53,7 +53,7 @@ static void exception_catching(void) {
     xbt_assert1(!strncmp(e.msg,"Some error we will catch on client side",
                         strlen("Some error we will catch on client side")), 
                "Got wrong message: %s", e.msg);
-    xbt_ex_free(&e);
+    xbt_ex_free(e);
   }
 }
 
@@ -63,17 +63,18 @@ static void exception_catching(void) {
 
 
 int client(int argc,char *argv[]) {
-  xbt_ex_t e; 
+  xbt_ex_t e;
   gras_socket_t toserver=NULL; /* peer */
   gras_socket_t toforwarder=NULL; /* peer */
 
-  
-
   int ping, pong, i;
   volatile int gotit=0;
 
+
   const char *host = "127.0.0.1";
-        int   port = 4000;
+               int   port = 4000;
+
+  memset(&e,0,sizeof(xbt_ex_t));
 
   /* 1. Init the GRAS's infrastructure */
   gras_init(&argc, argv);
@@ -115,8 +116,7 @@ int client(int argc,char *argv[]) {
   ping = 1234;
   TRY {
     exception_catching();
-    gras_msg_rpccall(toserver, 6000.0,
-                    gras_msgtype_by_name("plain ping"), &ping, &pong);
+    gras_msg_rpccall(toserver, 6000.0, "plain ping", &ping, &pong);
   } CATCH(e) {
     gras_socket_close(toserver);
     RETHROW0("Failed to execute a PING rpc on the server: %s");
@@ -132,8 +132,7 @@ int client(int argc,char *argv[]) {
   /* 9. Call a RPC which raises an exception (to test exception propagation) */
   INFO0("Call the exception raising RPC");
   TRY {
-    gras_msg_rpccall(toserver, 6000.0,
-                    gras_msgtype_by_name("raise exception"), NULL, NULL);
+    gras_msg_rpccall(toserver, 6000.0, "raise exception", NULL, NULL);
   } CATCH(e) {
     gotit = 1; 
     xbt_assert2(e.category == unknown_error, 
@@ -144,7 +143,7 @@ int client(int argc,char *argv[]) {
                         strlen("Some error we will catch on client side")), 
                "Got wrong message: %s", e.msg);
     INFO0("Got the expected exception when calling the exception raising RPC");
-    xbt_ex_free(&e);
+    xbt_ex_free(e);
   }
 
   if (!gotit)
@@ -158,11 +157,10 @@ int client(int argc,char *argv[]) {
        
      INFO1("Call the exception raising RPC (i=%d)",i);
      TRY {
-       gras_msg_rpccall(toserver, 6000.0,
-                        gras_msgtype_by_name("raise exception"), NULL, NULL);
+       gras_msg_rpccall(toserver, 6000.0, "raise exception", NULL, NULL);
      } CATCH(e) {
        gotit = 1;
-       xbt_ex_free(&e);
+       xbt_ex_free(e);
      }
      if (!gotit) {
        THROW0(unknown_error,0,"Didn't got the remote exception!");
@@ -174,8 +172,7 @@ int client(int argc,char *argv[]) {
   for (i=0;i<5;i++) {
     INFO1("Call the exception raising RPC on the forwarder (i=%d)",i);
     TRY {
-      gras_msg_rpccall(toforwarder, 6000.0,
-                      gras_msgtype_by_name("forward exception"), NULL, NULL);
+      gras_msg_rpccall(toforwarder, 6000.0, "forward exception", NULL, NULL);
     } CATCH(e) {
       gotit = 1;
     }
@@ -190,18 +187,20 @@ int client(int argc,char *argv[]) {
                "Got wrong category: %d (instead of %d)", 
                e.category,unknown_error);
     INFO0("Got the expected exception when calling the exception raising RPC");
-    xbt_ex_free(&e);
+    xbt_ex_free(e);
     exception_catching();
   }
-  
-  gras_msg_send(toserver,gras_msgtype_by_name("kill"),NULL);
-  gras_msg_send(toforwarder,gras_msgtype_by_name("kill"),NULL);
+
+  INFO2("Ask %s:%d to die",gras_socket_peer_name(toforwarder),gras_socket_peer_port(toforwarder));
+  gras_msg_send(toforwarder,"kill",NULL);
+  INFO2("Ask %s:%d to die",gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
+  gras_msg_send(toserver,"kill",NULL);
 
   /* 11. Cleanup the place before leaving */
   gras_socket_close(toserver);
   gras_socket_close(toforwarder);
-  gras_exit();
   INFO0("Done.");
+  gras_exit();
   return 0;
 } /* end_of_client */
 
@@ -215,19 +214,22 @@ typedef struct {
 } s_forward_data_t, *forward_data_t;
 
 static int forwarder_cb_kill(gras_msg_cb_ctx_t ctx,
-                            void             *payload_data) {
-  forward_data_t fdata=gras_userdata_get();
+                                void             *payload_data) {
+  forward_data_t fdata;
+  gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
+  INFO2("Asked to die by %s:%d",gras_socket_peer_name(expeditor),gras_socket_peer_port(expeditor));
+  fdata=gras_userdata_get();
   fdata->done = 1;
-  return 1;
+  return 0;
 }
 
 static int forwarder_cb_forward_ex(gras_msg_cb_ctx_t ctx,
                                   void             *payload_data) {
   forward_data_t fdata=gras_userdata_get();
 
-  gras_msg_rpccall(fdata->server, 60,
-                  gras_msgtype_by_name("raise exception"),NULL,NULL);
-  return 1;
+  INFO0("Forward a request");
+  gras_msg_rpccall(fdata->server, 60, "raise exception",NULL,NULL);
+  return 0;
 }
 
 int forwarder (int argc,char *argv[]) {
@@ -250,10 +252,8 @@ int forwarder (int argc,char *argv[]) {
   fdata->server=gras_socket_client(argv[2],atoi(argv[3]));
 
   register_messages();
-  gras_cb_register(gras_msgtype_by_name("forward exception"),
-                  &forwarder_cb_forward_ex);
-  gras_cb_register(gras_msgtype_by_name("kill"),
-                  &forwarder_cb_kill);
+  gras_cb_register("forward exception", &forwarder_cb_forward_ex);
+  gras_cb_register("kill",              &forwarder_cb_kill);
 
   while (!fdata->done) {
     gras_msg_handle(600.0);
@@ -262,8 +262,8 @@ int forwarder (int argc,char *argv[]) {
   gras_socket_close(mysock);
   gras_socket_close(fdata->server);
   free(fdata);
-  gras_exit();
   INFO0("Done.");
+  gras_exit();
   return 0;
 }
 
@@ -277,15 +277,20 @@ typedef struct {
 
 static int server_cb_kill(gras_msg_cb_ctx_t ctx,
                          void             *payload_data) {
-  server_data_t sdata=gras_userdata_get();
+  gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
+  server_data_t sdata;
+
+  INFO2("Asked to die by %s:%d",gras_socket_peer_name(expeditor),gras_socket_peer_port(expeditor));
+
+  sdata=gras_userdata_get();
   sdata->done = 1;
-  return 1;
+  return 0;
 }
 
 static int server_cb_raise_ex(gras_msg_cb_ctx_t ctx,
                              void             *payload_data) {
   exception_raising();
-  return 1;
+  return 0;
 }
 
 static int server_cb_ping(gras_msg_cb_ctx_t ctx,
@@ -310,7 +315,7 @@ static int server_cb_ping(gras_msg_cb_ctx_t ctx,
   /* 6. Cleanups, if any */
 
   /* 7. Tell GRAS that we consummed this message */
-  return 1;
+  return 0;
 } /* end_of_server_cb_ping */
 
 
@@ -337,16 +342,18 @@ int server (int argc,char *argv[]) {
 
   /* 4. Register the known messages and register my callback */
   register_messages();
-  gras_cb_register(gras_msgtype_by_name("plain ping"),&server_cb_ping);
-  gras_cb_register(gras_msgtype_by_name("raise exception"),&server_cb_raise_ex);
-  gras_cb_register(gras_msgtype_by_name("kill"),&server_cb_kill);
+  gras_cb_register("plain ping",&server_cb_ping);
+  gras_cb_register("raise exception",&server_cb_raise_ex);
+  gras_cb_register("kill",&server_cb_kill);
 
   INFO1("Listening on port %d ", gras_socket_my_port(mysock));
 
   /* 5. Wait for the ping incomming messages */
   
-  /* BUG: if the server is gone before the forwarder tries to connect, 
-     it dies awfully with :
+  /** \bug if the server is gone before the forwarder tries to connect, 
+     it dies awfully with the following message. The problem stands somewhere
+     at the interface between the gras_socket_t and the msg mess. There is thus
+     no way for me to dive into this before this interface is rewritten 
 ==15875== Invalid read of size 4
 ==15875==    at 0x408B805: find_port (transport_plugin_sg.c:68)
 ==15875==    by 0x408BD64: gras_trp_sg_socket_client (transport_plugin_sg.c:115)
@@ -375,9 +382,9 @@ int server (int argc,char *argv[]) {
   /* 8. Free the allocated resources, and shut GRAS down */
   free(sdata);
   gras_socket_close(mysock);
+  INFO0("Done.");
   gras_exit();
    
-  INFO0("Done.");
   return 0;
 } /* end_of_server */