Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
reduce verbosity
[simgrid.git] / examples / gras / tokenS / tokenS.c
index d22444c..27b4471 100644 (file)
@@ -12,6 +12,7 @@
 #include "gras.h"
  
 #define NBLOOPS 100
+/*#define NBLOOPS 30000*/
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(Token,"Messages specific to this example");
 
@@ -40,8 +41,8 @@ typedef struct {
 /* Callback function */
 static int node_cb_stoken_handler(gras_socket_t  expeditor,
                                  void          *payload_data) {
-                            
-  xbt_error_t errcode;
+  
+  xbt_ex_t e;
   
   /* 1. Get the payload into the msg variable */
   int msg=*(int*)payload_data;
@@ -58,7 +59,7 @@ static int node_cb_stoken_handler(gras_socket_t  expeditor,
     supersteps = 10;
   } 
   if (globals->create && (! (globals->remaining_loop % supersteps))) {
-    INFO1("Begin a new loop. Still to do: %d", globals->remaining_loop);
+    VERB1("Begin a new loop. Still to do: %d", globals->remaining_loop);
   } else if (! (globals->remaining_loop % supersteps)) {
     VERB3("Got token(%d) from %s remaining_loop=%d", 
          msg, gras_socket_peer_name(expeditor),globals->remaining_loop);
@@ -76,14 +77,14 @@ static int node_cb_stoken_handler(gras_socket_t  expeditor,
      
      
     /* 6. Send it as payload of a stoken message to the successor */
-    errcode = gras_msg_send(globals->tosuccessor, 
-                           gras_msgtype_by_name("stoken"), &msg);
+    TRY {
+      gras_msg_send(globals->tosuccessor, 
+                   gras_msgtype_by_name("stoken"), &msg);
      
     /* 7. Deal with errors */
-    if (errcode != no_error) {
-      ERROR1("Unable to forward token: %s\n", xbt_error_name(errcode));
+    } CATCH(e) {
       gras_socket_close(globals->sock);
-      return 1;
+      RETHROW0("Unable to forward token: %s");
     }
   
   }
@@ -92,26 +93,27 @@ static int node_cb_stoken_handler(gras_socket_t  expeditor,
      reused by our predecessor.
      Closing this side would thus create troubles */
   
-  /* 9. Decrease the remaining_loop integer. */
+  /* 8. Decrease the remaining_loop integer. */
   globals->remaining_loop -= 1;
    
-  /* 10. Repport the hop number to the user at the end */
+  /* 9. Repport the hop number to the user at the end */
   if (globals->remaining_loop == -1 && globals->create) {
     INFO1("Shut down the token-ring. There was %d hops.",msg);
   }
 
-  /* 11. Tell GRAS that we consummed this message */
+  /* 10. Tell GRAS that we consummed this message */
   return 1;
 } /* end_of_node_cb_stoken_handler */
 
 
 int node (int argc,char *argv[]) {
-  xbt_error_t errcode;
   node_data_t *globals;
   
   const char *host;
   int   myport;
   int   peerport;
+    
+  xbt_ex_t e;
   
   /* 1. Init the GRAS infrastructure and declare my globals */
   gras_init(&argc,argv);
@@ -138,24 +140,14 @@ int node (int argc,char *argv[]) {
        gras_os_getpid(), host,peerport, myport);
 
   /* 4. Create my master socket for listening */
-  if ((errcode=gras_socket_server(myport,&(globals->sock)))) { 
-    CRITICAL1("Error %s encountered while opening the server socket. Bailing out.",
-             xbt_error_name(errcode));
-    return 1;
-  }
+  globals->sock = gras_socket_server(myport);
   gras_os_sleep(1.0); /* Make sure all server sockets are created */
 
 
   /* 5. Create socket to the successor on the ring */
   DEBUG2("Connect to my successor on %s:%d",host,peerport);
 
-  if ((errcode=gras_socket_client(host,peerport,
-                                 &(globals->tosuccessor)))) {
-    ERROR1("Unable to connect to the node (got %s). Bailing out.",
-          xbt_error_name(errcode));
-    return 1;
-  } 
+  globals->tosuccessor = gras_socket_client(host,peerport);
   
   /* 6. Register the known messages. This function is called twice here,
         but it's because this file also acts as regression test.
@@ -180,26 +172,18 @@ int node (int argc,char *argv[]) {
     INFO3("Create the token (with value %d) and send it to %s:%d",
          token, host, peerport);
 
-    errcode = gras_msg_send(globals->tosuccessor,
-                           gras_msgtype_by_name("stoken"), &token);
-      
-    if (errcode != no_error) {
-      fprintf(stderr, "Unable send 'stoken' to successor (%s)\n",
-             xbt_error_name(errcode));
-      return 1;
+    TRY {        
+      gras_msg_send(globals->tosuccessor,
+                 gras_msgtype_by_name("stoken"), &token);
+    } CATCH(e) {
+      RETHROW0("Unable to send the freshly created token: %s");
     }
   } 
   
   /* 8. Wait up to 10 seconds for an incomming message to handle */
   while (globals->remaining_loop > (globals->create ? -1 : 0)) {
-    errcode = gras_msg_handle(10.0);
+    gras_msg_handle(10.0);
   
-    /* 9. Housekeeping */
-    if (errcode != no_error) {
-      ERROR1("Got error %s in msg_handle", xbt_error_name(errcode));
-      return errcode;
-    }
-       
     DEBUG1("looping (remaining_loop=%d)", globals->remaining_loop);
   }
 
@@ -211,5 +195,5 @@ int node (int argc,char *argv[]) {
   free(globals);
   gras_exit();
   
-  return no_error;
+  return 0;
 } /* end_of_node */