Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
plug some memleaks
[simgrid.git] / examples / gras / all2all / all2all.c
index badb068..9efad71 100644 (file)
@@ -29,35 +29,35 @@ int receiver (int argc,char *argv[]) {
   int myport; /* port on which I receive stuff */
   int todo; /* amount of messages I should get */
   char *data; /* message content */
+
   gras_socket_t mysock;  /* socket on which other people contact me */
   gras_socket_t expeditor;  /* to notice who wrote me */
+
   /* Init the GRAS infrastructure and declare my globals */
   gras_init(&argc,argv);
+
   /* Get my settings from the command line */
   myport=atoi(argv[1]);
   todo=atoi(argv[2]);
 
-  /* Create my master socket */
-  mysock = gras_socket_server(myport);
-
   /* Register the known messages */
   gras_msgtype_declare("data", gras_datadesc_by_name("string"));
 
-  /* Get the data */
+  /* Create my master socket */
+  mysock = gras_socket_server(myport);
 
+  /* Get the data */
   INFO2("Listening on port %d (expecting %d messages)",
        gras_socket_my_port(mysock),
        todo);
   while (todo>0) {
      gras_msg_wait(60 /* wait up to one minute */,
-                  gras_msgtype_by_name("data"),
+                  "data",
                   &expeditor,
                   &data);
      todo--;
-     
+     free(data);
+
      INFO3("Got Data from %s:%d (still %d to go)",
           gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor),
           todo);
@@ -77,28 +77,29 @@ int receiver (int argc,char *argv[]) {
 
 int sender (int argc,char *argv[]) {
 
-  int i; /* iterator */
+  unsigned int iter; /* iterator */
   char *data; /* data exchanged */
   int datasize; /* size of message */
-  xbt_host_t h; /* iterator */
-  
-  gras_socket_t peer;  /* socket to node */
-  
-  /* xbt_dynar for hosts */
-  xbt_dynar_t hosts = xbt_dynar_new(sizeof(xbt_host_t),&xbt_host_free_voidp);
+  xbt_peer_t h; /* iterator */
+  int connected = 0;
+
+  gras_socket_t peer=NULL;  /* socket to node */
+
+
+  /* xbt_dynar for peers */
+  xbt_dynar_t peers = xbt_dynar_new(sizeof(xbt_peer_t),&xbt_peer_free_voidp);
+
   /* Init the GRAS infrastructure and declare my globals */
   gras_init(&argc,argv);
+
   /* Get the node location from argc/argv */
-  for (i=1; i<argc-1; i++){
-    xbt_host_t host = xbt_host_from_string(argv[i]);
-    xbt_dynar_push(hosts,&host);
+  for (iter=1; iter<argc-1; iter++){
+    xbt_peer_t peer = xbt_peer_from_string(argv[iter]);
+    xbt_dynar_push(peers,&peer);
   }
-  
+
   datasize=atoi(argv[argc-1]);
-  
+
   data=(char *) malloc(datasize+1);  // allocation of datasize octets
   memset(data, 32, datasize);
   data[datasize] = '\0';
@@ -109,22 +110,35 @@ int sender (int argc,char *argv[]) {
   gras_msgtype_declare("data", gras_datadesc_by_name("string"));
 
 
-  /* Wait for receivers to startup */
-  gras_os_sleep(1);
+  /* write to the receivers */
+  xbt_dynar_foreach(peers,iter,h) {
+     connected = 0;
+     while (!connected) {
+       xbt_ex_t e;
+       TRY {
+          peer = gras_socket_client(h->name,h->port);
+          connected=1;
+       } CATCH(e) {
+          if (e.category != system_error /*in RL*/&& e.category != mismatch_error/*in SG*/)
+            RETHROW;
+          xbt_ex_free(e);
+          gras_os_sleep(0.01);
+       }
+     }
+     gras_msg_send(peer,"data",&data);
+     if (gras_if_SG()) {
+       INFO2("  Sent Data from %s to %s", gras_os_myname(),h->name);
+     } else {
+       INFO0("  Sent Data");
+     }
 
-  /* write 'em */
-  xbt_dynar_foreach(hosts,i,h) {
-     
-     peer = gras_socket_client(h->name,h->port);
-     gras_msg_send(peer,gras_msgtype_by_name("data"),&data);
-     INFO2("  Sent Data from %s to %s",
-          gras_os_myname(),h->name);
      gras_socket_close(peer);
   }
 
   /* Free the allocated resources, and shut GRAS down */
-  xbt_dynar_free(&hosts);
-     
+  free(data);
+  xbt_dynar_free(&peers);
+
   gras_exit();
   return 0;
 } /* end_of_sender */