Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
top_srcdir is not supposed to be a relative path
[simgrid.git] / examples / gras / rpc / rpc.c
index 47d577c..f40a4cf 100644 (file)
@@ -11,9 +11,6 @@
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(Rpc,"Messages specific to this example");
 
-int err=0; /* to make the message of the raised exception more informative and
-             even be able to follow their propagation from server to client*/
-
 /* register messages which may be sent (common to client and server) */
 static void register_messages(void) {
   gras_msgtype_declare_rpc("plain ping",
@@ -30,9 +27,9 @@ int server (int argc,char *argv[]);
 int forwarder (int argc,char *argv[]);
 int client (int argc,char *argv[]);
 
-static void exception_raising(void) {
-  THROW1(unknown_error,42,"Some error we will catch on client side %d",err++);
-}
+#define exception_raising() \
+  THROW0(unknown_error,42,"Some error we will catch on client side")
+
 static void exception_catching(void) {
   int gotit = 0,i;
   xbt_ex_t e;
@@ -67,13 +64,14 @@ int client(int argc,char *argv[]) {
   gras_socket_t toserver=NULL; /* peer */
   gras_socket_t toforwarder=NULL; /* peer */
 
-  memset(&e,0,sizeof(xbt_ex_t));
-
   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);
@@ -108,7 +106,7 @@ int client(int argc,char *argv[]) {
   register_messages();
 
   /* 6. Keep the user informed of what's going on */
-  INFO2("Connected to server which is on %s:%d ", 
+  INFO2("Connected to server which is on %s:%d",
        gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
 
   /* 7. Prepare and send the ping message to the server */
@@ -123,7 +121,7 @@ int client(int argc,char *argv[]) {
   exception_catching();
 
   /* 8. Keep the user informed of what's going on, again */
-  INFO4("The answer to PING(%d) on %s:%d is PONG(%d)  ", 
+  INFO4("The answer to PING(%d) on %s:%d is PONG(%d)",
        ping, 
        gras_socket_peer_name(toserver),gras_socket_peer_port(toserver),
        pong);
@@ -198,8 +196,8 @@ int client(int argc,char *argv[]) {
   /* 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 */
 
@@ -213,10 +211,11 @@ typedef struct {
 } s_forward_data_t, *forward_data_t;
 
 static int forwarder_cb_kill(gras_msg_cb_ctx_t ctx,
-                            void             *payload_data) {
+                                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));
-  forward_data_t fdata=gras_userdata_get();
+  fdata=gras_userdata_get();
   fdata->done = 1;
   return 0;
 }
@@ -260,8 +259,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;
 }
 
@@ -276,9 +275,11 @@ typedef struct {
 static int server_cb_kill(gras_msg_cb_ctx_t ctx,
                          void             *payload_data) {
   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));
 
-  server_data_t sdata=gras_userdata_get();
+  sdata=gras_userdata_get();
   sdata->done = 1;
   return 0;
 }
@@ -297,7 +298,7 @@ static int server_cb_ping(gras_msg_cb_ctx_t ctx,
   gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
    
   /* 2. Log which client connected */
-  INFO3("Got message PING(%d) from %s:%d ", 
+  INFO3("Got message PING(%d) from %s:%d",
        msg, 
        gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor));
   
@@ -342,7 +343,7 @@ int server (int argc,char *argv[]) {
   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));
+  INFO1("Listening on port %d", gras_socket_my_port(mysock));
 
   /* 5. Wait for the ping incomming messages */
   
@@ -378,9 +379,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 */