Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
today is doomsday: platform.xml is sacrificed for the greater good
[simgrid.git] / examples / msg / platform-failures / platform-failures.c
index 4065d0f..710b5f1 100644 (file)
@@ -23,30 +23,35 @@ static int master(int argc, char *argv[])
 
   for (i = 0; i < number_of_tasks; i++) {
     char mailbox[256];
 
   for (i = 0; i < number_of_tasks; i++) {
     char mailbox[256];
-    sprintf(mailbox, "worker-%ld", i % workers_count);
+    snprintf(mailbox, 255, "worker-%ld", i % workers_count);
 
     msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, xbt_new0(double, 1));
     *((double *) task->data) = MSG_get_clock();
 
 
     msg_task_t task = MSG_task_create("Task", task_comp_size, task_comm_size, xbt_new0(double, 1));
     *((double *) task->data) = MSG_get_clock();
 
-    msg_error_t a = MSG_task_send_with_timeout(task,mailbox,10.0);
-
-    if (a == MSG_OK) {
+    switch ( MSG_task_send_with_timeout(task,mailbox,10.0) ) {
+    case MSG_OK:
       XBT_INFO("Send completed");
       XBT_INFO("Send completed");
-    } else if (a == MSG_HOST_FAILURE) {
+      break;
+
+    case MSG_HOST_FAILURE:
       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
       free(task->data);
       MSG_task_destroy(task);
       return 0;
       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
       free(task->data);
       MSG_task_destroy(task);
       return 0;
-    } else if (a == MSG_TRANSFER_FAILURE) {
+
+    case MSG_TRANSFER_FAILURE:
       XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox);
       free(task->data);
       MSG_task_destroy(task);
       XBT_INFO("Mmh. Something went wrong with '%s'. Nevermind. Let's keep going!", mailbox);
       free(task->data);
       MSG_task_destroy(task);
-    } else if (a == MSG_TIMEOUT) {
+      break;
+
+    case MSG_TIMEOUT:
       XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
       free(task->data);
       MSG_task_destroy(task);
       XBT_INFO ("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
       free(task->data);
       MSG_task_destroy(task);
-    } else {
-      XBT_INFO("Hey ?! What's up ? ");
+      break;
+
+    default:
       xbt_die( "Unexpected behavior");
     }
   }
       xbt_die( "Unexpected behavior");
     }
   }
@@ -54,24 +59,31 @@ static int master(int argc, char *argv[])
   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
   for (i = 0; i < workers_count; i++) {
     char mailbox[256];
   XBT_INFO("All tasks have been dispatched. Let's tell everybody the computation is over.");
   for (i = 0; i < workers_count; i++) {
     char mailbox[256];
-    sprintf(mailbox, "worker-%ld", i % workers_count);
+    snprintf(mailbox, 255, "worker-%ld", i % workers_count);
     msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
     msg_task_t task = MSG_task_create("finalize", 0, 0, FINALIZE);
-    int a = MSG_task_send_with_timeout(task,mailbox,1.0);
-    if (a == MSG_OK)
-      continue;
-    if (a == MSG_HOST_FAILURE) {
+
+    switch (MSG_task_send_with_timeout(task,mailbox,1.0)) {
+    case MSG_HOST_FAILURE:
       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
       MSG_task_destroy(task);
       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
       MSG_task_destroy(task);
-      return 0;
-    } else if (a == MSG_TRANSFER_FAILURE) {
+      break;
+
+    case MSG_TRANSFER_FAILURE:
       XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", mailbox);
       MSG_task_destroy(task);
       XBT_INFO("Mmh. Can't reach '%s'! Nevermind. Let's keep going!", mailbox);
       MSG_task_destroy(task);
-    } else if (a == MSG_TIMEOUT) {
+      break;
+
+    case MSG_TIMEOUT:
       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
       MSG_task_destroy(task);
       XBT_INFO("Mmh. Got timeouted while speaking to '%s'. Nevermind. Let's keep going!", mailbox);
       MSG_task_destroy(task);
-    } else {
-      XBT_INFO("Hey ?! What's up ? ");
-      xbt_die("Unexpected behavior with '%s': %d", mailbox, a);
+      break;
+
+    case MSG_OK:
+      /* nothing */
+      break;
+
+    default:
+      xbt_die("Unexpected behavior with '%s'", mailbox);
     }
   }
 
     }
   }
 
@@ -86,16 +98,13 @@ static int worker(int argc, char *argv[])
 
   long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
 
 
   long id= xbt_str_parse_int(argv[1], "Invalid argument %s");
 
-  sprintf(mailbox, "worker-%ld", id);
+  snprintf(mailbox, 79,"worker-%ld", id);
 
   while (1) {
 
   while (1) {
-    int a;
-    double time1, time2;
-
-    time1 = MSG_get_clock();
-    a = MSG_task_receive( &(task), mailbox);
-    time2 = MSG_get_clock();
-    if (a == MSG_OK) {
+    double time1 = MSG_get_clock();
+    int retcode = MSG_task_receive( &(task), mailbox);
+    double time2 = MSG_get_clock();
+    if (retcode == MSG_OK) {
       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
       if (MSG_task_get_data(task) == FINALIZE) {
         MSG_task_destroy(task);
       XBT_INFO("Received \"%s\"", MSG_task_get_name(task));
       if (MSG_task_get_data(task) == FINALIZE) {
         MSG_task_destroy(task);
@@ -106,15 +115,13 @@ static int worker(int argc, char *argv[])
         time1 = *((double *) task->data);
       XBT_INFO("Communication time : \"%f\"", time2 - time1);
       XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
         time1 = *((double *) task->data);
       XBT_INFO("Communication time : \"%f\"", time2 - time1);
       XBT_INFO("Processing \"%s\"", MSG_task_get_name(task));
-      a = MSG_task_execute(task);
-      if (a == MSG_OK) {
+      retcode = MSG_task_execute(task);
+      if (retcode == MSG_OK) {
         XBT_INFO("\"%s\" done", MSG_task_get_name(task));
         XBT_INFO("\"%s\" done", MSG_task_get_name(task));
-        free(task->data);
         MSG_task_destroy(task);
         task = NULL;
         MSG_task_destroy(task);
         task = NULL;
-      } else if (a == MSG_HOST_FAILURE) {
+      } else if (retcode == MSG_HOST_FAILURE) {
         XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
         XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
-        free(task->data);
         MSG_task_destroy(task);
         task = NULL;
         return 0;
         MSG_task_destroy(task);
         task = NULL;
         return 0;
@@ -122,13 +129,12 @@ static int worker(int argc, char *argv[])
         XBT_INFO("Hey ?! What's up ? ");
         xbt_die("Unexpected behavior");
       }
         XBT_INFO("Hey ?! What's up ? ");
         xbt_die("Unexpected behavior");
       }
-    } else if (a == MSG_HOST_FAILURE) {
+    } else if (retcode == MSG_HOST_FAILURE) {
       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
       return 0;
       XBT_INFO("Gloups. The cpu on which I'm running just turned off!. See you!");
       return 0;
-    } else if (a == MSG_TRANSFER_FAILURE) {
+    } else if (retcode == MSG_TRANSFER_FAILURE) {
       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
     } else {
       XBT_INFO("Mmh. Something went wrong. Nevermind. Let's keep going!");
     } else {
-      XBT_INFO("Hey ?! What's up ? ");
       xbt_die("Unexpected behavior");
     }
   }
       xbt_die("Unexpected behavior");
     }
   }