Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
getting rid of deprecated functions
[simgrid.git] / examples / msg / msg_test.c
index 06adbb9..388e087 100644 (file)
@@ -5,24 +5,26 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
-#include "msg/msg.h"
+#include "msg/msg.h" /* Yeah! If you want to use msg, you need to include msg/msg.h */
 
-#define VERBOSE
-#include "messages.h"
+/* Create a log channel to have nice outputs. */
+#include "xbt/log.h"
+XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,"Messages specific for this msg example");
 
 int master(int argc, char *argv[]);
 int slave(int argc, char *argv[]);
 int forwarder(int argc, char *argv[]);
 void test_all(const char *platform_file, const char *application_file);
 
-
 typedef enum {
   PORT_22 = 0,
   MAX_CHANNEL
 } channel_t;
 
-void print_args(int argc, char** argv);
-void print_args(int argc, char** argv)
+/* This function is just used so that users can check that each process
+ *  has received the arguments it was supposed to receive.
+ */
+static void print_args(int argc, char** argv)
 {
   int i ; 
 
@@ -32,6 +34,7 @@ void print_args(int argc, char** argv)
   fprintf(stderr,">\n");
 }
 
+/** Emitter function  */
 int master(int argc, char *argv[])
 {
   int slaves_count = 0;
@@ -46,11 +49,11 @@ int master(int argc, char *argv[])
 
   print_args(argc,argv);
 
-  ASSERT(sscanf(argv[1],"%d", &number_of_tasks),
+  xbt_assert1(sscanf(argv[1],"%d", &number_of_tasks),
         "Invalid argument %s\n",argv[1]);
-  ASSERT(sscanf(argv[2],"%lg", &task_comp_size),
+  xbt_assert1(sscanf(argv[2],"%lg", &task_comp_size),
         "Invalid argument %s\n",argv[2]);
-  ASSERT(sscanf(argv[3],"%lg", &task_comm_size),
+  xbt_assert1(sscanf(argv[3],"%lg", &task_comm_size),
         "Invalid argument %s\n",argv[3]);
 
   {                  /*  Task creation */
@@ -71,36 +74,40 @@ int master(int argc, char *argv[])
     for (i = 4; i < argc; i++) {
       slaves[i-4] = MSG_get_host_by_name(argv[i]);
       if(slaves[i-4]==NULL) {
-       PRINT_MESSAGE("Unknown host %s. Stopping Now! \n", argv[i]);
+       INFO1("Unknown host %s. Stopping Now! ", argv[i]);
        abort();
       }
     }
   }
 
-  PRINT_MESSAGE("Got %d slave(s) :\n", slaves_count);
+  INFO1("Got %d slave(s) :", slaves_count);
   for (i = 0; i < slaves_count; i++)
-    PRINT_MESSAGE("\t %s\n", slaves[i]->name);
+    INFO1("\t %s", slaves[i]->name);
 
-  PRINT_MESSAGE("Got %d task to process :\n", number_of_tasks);
+  INFO1("Got %d task to process :", number_of_tasks);
 
   for (i = 0; i < number_of_tasks; i++)
-    PRINT_MESSAGE("\t\"%s\"\n", todo[i]->name);
+    INFO1("\t\"%s\"", todo[i]->name);
 
   for (i = 0; i < number_of_tasks; i++) {
-    PRINT_MESSAGE("Sending \"%s\" to \"%s\"\n",
+    INFO2("Sending \"%s\" to \"%s\"",
                   todo[i]->name,
                   slaves[i % slaves_count]->name);
+    if(MSG_host_self()==slaves[i % slaves_count]) {
+      INFO0("Hey ! It's me ! :)");
+    }
     MSG_task_put(todo[i], slaves[i % slaves_count],
                  PORT_22);
-    PRINT_MESSAGE("Send completed\n");
+    INFO0("Send completed");
   }
   
-  PRINT_MESSAGE("All tasks have been dispatched. Bye!\n");
+  INFO0("All tasks have been dispatched. Bye!");
   free(slaves);
   free(todo);
   return 0;
-}
+} /* end_of_master */
 
+/** Receiver function  */
 int slave(int argc, char *argv[])
 {
   print_args(argc,argv);
@@ -110,21 +117,21 @@ int slave(int argc, char *argv[])
     int a;
     a = MSG_task_get(&(task), PORT_22);
     if (a == MSG_OK) {
-      PRINT_MESSAGE("Received \"%s\" \n", task->name);
-      PRINT_MESSAGE("Processing \"%s\" \n", task->name);
+      INFO1("Received \"%s\" ", task->name);
+      INFO1("Processing \"%s\" ", task->name);
       MSG_task_execute(task);
-      PRINT_MESSAGE("\"%s\" done \n", task->name);
+      INFO1("\"%s\" done ", task->name);
       MSG_task_destroy(task);
     } else {
-      PRINT_MESSAGE("Hey ?! What's up ? \n");
-      DIE("Unexpected behaviour");
+      INFO0("Hey ?! What's up ? ");
+      xbt_assert0(0,"Unexpected behaviour");
     }
   }
-  PRINT_MESSAGE("I'm done. See you!\n");
+  INFO0("I'm done. See you!");
   return 0;
-}
-
+} /* end_of_slave */
 
+/** Receiver function */
 int forwarder(int argc, char *argv[])
 {
   int i;
@@ -140,39 +147,43 @@ int forwarder(int argc, char *argv[])
     for (i = 1; i < argc; i++) {
       slaves[i-1] = MSG_get_host_by_name(argv[i]);
       if(slaves[i-1]==NULL) {
-       PRINT_MESSAGE("Unknown host %s. Stopping Now! \n", argv[i]);
+       INFO1("Unknown host %s. Stopping Now! ", argv[i]);
        abort();
       }
     }
   }
 
-
+  i=0;
   while(1) {
     m_task_t task = NULL;
     int a;
     a = MSG_task_get(&(task), PORT_22);
     if (a == MSG_OK) {
-      PRINT_MESSAGE("Received \"%s\" \n", task->name);
-      PRINT_MESSAGE("Sending \"%s\" to \"%s\"\n",
+      INFO1("Received \"%s\" ", task->name);
+      INFO2("Sending \"%s\" to \"%s\"",
                    task->name,
                    slaves[i % slaves_count]->name);
       MSG_task_put(task, slaves[i % slaves_count],
                   PORT_22);
     } else {
-      PRINT_MESSAGE("Hey ?! What's up ? \n");
-      DIE("Unexpected behaviour");
+      INFO0("Hey ?! What's up ? ");
+      xbt_assert0(0,"Unexpected behaviour");
     }
   }
 
-  PRINT_MESSAGE("I'm done. See you!\n");
+  INFO0("I'm done. See you!");
   return 0;
-}
+} /* end_of_forwarder */
 
 
+/** Test function */
 void test_all(const char *platform_file,const char *application_file)
 {
+
+  /* MSG_config("surf_workstation_model","KCCFLN05"); */
   {                            /*  Simulation setting */
     MSG_set_channel_number(MAX_CHANNEL);
+    MSG_paje_output("msg_test.trace");
     MSG_create_environment(platform_file);
   }
   {                            /*   Application deployment */
@@ -182,13 +193,21 @@ void test_all(const char *platform_file,const char *application_file)
     MSG_launch_application(application_file);
   }
   MSG_main();
-  printf("Simulation time %Lg\n",MSG_getClock());
-}
+  
+  INFO1("Simulation time %g",MSG_getClock());
+} /* end_of_test_all */
 
+
+/** Main function */
 int main(int argc, char *argv[])
 {
-  MSG_global_init_args(&argc,argv);
-  test_all("msg_platform.xml","msg_deployment.xml");
+  MSG_global_init(&argc,argv);
+  if (argc < 3) {
+     printf ("Usage: %s platform_file deployment_file\n",argv[0]);
+     printf ("example: %s msg_platform.xml msg_deployment.xml\n",argv[0]);
+     exit(1);
+  }
+  test_all(argv[1],argv[2]);
   MSG_clean();
   return (0);
-}
+} /* end_of_main */