Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Oups, I changed the prototype of these functions
[simgrid.git] / examples / amok / bandwidth / bandwidth.c
index b231c72..5f1b720 100644 (file)
@@ -10,6 +10,7 @@
 
 #include "gras.h"
 #include "amok/bandwidth.h"
+#include "amok/peermanagement.h"
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(Bandwidth,"Messages specific to this example");
 
@@ -17,49 +18,41 @@ XBT_LOG_NEW_DEFAULT_CATEGORY(Bandwidth,"Messages specific to this example");
  * Sensor code
  * **********************************************************************/
 
-/* Global private data */
-typedef struct {
-  gras_socket_t sock;
-  int done;
-} s_sensor_data_t,*sensor_data_t;
-
-static int sensor_cb_quit(gras_socket_t  expeditor,
-                          void          *payload_data) {
-  sensor_data_t globals=(sensor_data_t)gras_userdata_get();
-                          
-  globals->done = 1;                  
-  return 1;         
-}
-
 /* Function prototypes */
 int sensor (int argc,char *argv[]);
 
 int sensor (int argc,char *argv[]) {
-  xbt_error_t errcode;
-  sensor_data_t g;
+  gras_socket_t mysock;
+  gras_socket_t master=NULL;
+  int connection_try = 10;
+  xbt_ex_t e;
 
   gras_init(&argc, argv);
-  g=gras_userdata_new(s_sensor_data_t);  
   amok_bw_init();
-   
-  if ((errcode=gras_socket_server(atoi(argv[1]),&(g->sock)))) { 
-    ERROR1("Error %s encountered while opening the server socket",xbt_error_name(errcode));
-    return 1;
+  amok_pm_init();
+  mysock = gras_socket_server_range(3000,9999,0,0);
+  INFO1("Sensor starting (on port %d)",gras_os_myport());
+  while (connection_try > 0 && master == NULL) {
+     int connected = 0;
+     TRY {
+       master = gras_socket_client_from_string(argv[1]);
+       connected = 1;
+     } CATCH(e) {
+       xbt_ex_free(e);
+     }
+     if (!connected) {
+       connection_try--;
+       gras_os_sleep(0.5); /* let the master get ready */
+     }
   }
-  g->done = 0;
-  
-  gras_msgtype_declare("quit",NULL);
-  gras_cb_register(gras_msgtype_by_name("quit"),&sensor_cb_quit);
-  
-  while (! g->done ) {
-    errcode=gras_msg_handle(60.0);
-    if (errcode != no_error) {
-       ERROR1("Error '%s' while handling message",xbt_error_name(errcode));
-       return errcode;
-    }  
-  }    
-
-  gras_socket_close(g->sock);
+                                             
+  amok_pm_group_join(master,"bandwidth",-1);
+  amok_pm_mainloop(60);
+
+  gras_socket_close(mysock);
+  gras_socket_close(master);
+  gras_exit();
   return 0;
 }
 
@@ -67,62 +60,72 @@ int sensor (int argc,char *argv[]) {
  * Maestro code
  * **********************************************************************/
 
-/* Global private data */
-typedef struct {
-  gras_socket_t sock;
-} s_maestro_data_t,*maestro_data_t;
-
 /* Function prototypes */
 int maestro (int argc,char *argv[]);
 
 int maestro(int argc,char *argv[]) {
-  xbt_error_t errcode;
-  maestro_data_t g;
   double sec, bw;
-  int buf_size=32;
-  int exp_size=1024*50;
-  int msg_size=512;
+  int buf_size=32  *1024;
+  int msg_size=512 *1024;
+  int msg_amount = 1;
+  double min_duration = 1;
+   
   gras_socket_t peer;
+  gras_socket_t mysock;
+  xbt_peer_t h1,h2,h_temp;
+  xbt_dynar_t group;
 
   gras_init(&argc, argv);
-  g=gras_userdata_new(s_maestro_data_t);
   amok_bw_init();
+  amok_pm_init();
 
-  if (argc != 5) {
-     ERROR0("Usage: maestro host port host port\n");
+  INFO0("Maestro starting");
+  if (argc != 2) {
+     ERROR0("Usage: maestro port\n");
      return 1;
   }
-
-  /* wait to ensure that all server sockets are there before starting the experiment */        
-  gras_os_sleep(0.5);
-  
-  if ((errcode=gras_socket_client(argv[1],atoi(argv[2]),&peer))) {
-     ERROR3("Unable to connect to my peer on %s:%s. Got %s",
-           argv[1],argv[2],xbt_error_name(errcode));
-     return 1;
+  mysock=gras_socket_server(atoi(argv[1]));
+  group=amok_pm_group_new("bandwidth");
+  INFO0("Wait for peers for 5 sec");
+  gras_msg_handleall(5); /* friends, we're ready. Come and play */
+   
+  if (xbt_dynar_length(group) < 2) {
+     char *msg;
+     asprintf(&msg,"Not enough peers arrived. Expected 2 got %ld",
+             xbt_dynar_length(group));
+     amok_pm_group_shutdown("bandwidth");
+     xbt_die(msg);
   }
+  h1 = *(xbt_peer_t*)xbt_dynar_get_ptr(group, 0);
+  h2 = *(xbt_peer_t*)xbt_dynar_get_ptr(group, 1);
+  /* sort peers in right order to keep output right */
+  if (strcmp(h1->name,h2->name) < 0 || h1->port > h2->port) {
+     h_temp = h1;
+     h1 = h2;
+     h2 = h_temp;
+  }
+
+  INFO2("Contact %s:%d",h1->name, h1->port);
+  peer = gras_socket_client(h1->name, h1->port);
 
   INFO0("Test the BW between me and one of the sensors");  
-  TRY(amok_bw_test(peer,buf_size,exp_size,msg_size,&sec,&bw));
-  INFO6("Experience between me and %s:%d (%d kb in msgs of %d kb) took %f sec, achieving %f kb/s",
-       argv[1],atoi(argv[2]),
-       exp_size,msg_size,
-       sec,bw);
-
-  INFO0("Test the BW between the two sensors");  
-  TRY(amok_bw_request(argv[1],atoi(argv[2]),argv[3],atoi(argv[4]),
-                      buf_size,exp_size,msg_size,&sec,&bw));   
-  INFO2("Experience took took %f sec, achieving %f kb/s",
-       sec,bw);
-
-  /* ask sensors to quit */                    
-  gras_msgtype_declare("quit",NULL);
-  TRY(gras_msg_send(peer,gras_msgtype_by_name("quit"), NULL));
-  gras_socket_close(peer);
-  TRY(gras_socket_client(argv[3],atoi(argv[4]),&peer));
-  TRY(gras_msg_send(peer,gras_msgtype_by_name("quit"), NULL));
-  gras_socket_close(peer);
-
-  gras_socket_close(g->sock);
+  amok_bw_test(peer,buf_size,msg_size,msg_amount,min_duration,&sec,&bw);
+  INFO7("Experience between me and %s:%d (initially %d msgs of %d bytes, maybe modified to fill the pipe at least %.1fs) took %f sec, achieving %f kb/s",
+       h1->name, h1->port,
+       msg_amount,msg_size,min_duration,
+       sec,((double)bw)/1024.0);
+
+  INFO4("Test the BW between %s:%d and %s:%d", h1->name, h1->port,     h2->name, h2->port);
+  amok_bw_request(h1->name, h1->port,  h2->name, h2->port,
+                 buf_size,msg_size,msg_amount,min_duration,&sec,&bw);
+  INFO6("Experience between %s:%d and %s:%d took took %f sec, achieving %f kb/s",
+       h1->name, h1->port,     h2->name, h2->port,
+       sec,((double)bw)/1024.0);
+
+  /* Game is over, friends */
+  amok_pm_group_shutdown ("bandwidth");
+
+  gras_socket_close(mysock);
+  gras_exit();
   return 0;
 }