Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
add a test trying to access the properties of a remote host -- it works
[simgrid.git] / examples / msg / tracing / procmig.c
1 /*      $Id$     */
2
3 /* Copyright (c) 2009 The SimGrid team. All rights reserved.                */
4
5 /* This program is free software; you can redistribute it and/or modify it
6  * under the terms of the license (GNU LGPL) which comes with this package. */
7
8 #include "msg/msg.h"            /* core library */
9 #include "xbt/sysdep.h"         /* calloc */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
14                              "Messages specific for this msg example");
15
16 /** The guy we will move from host to host. It move alone and then is moved by policeman back  */
17 static int emigrant(int argc, char *argv[])
18 {
19   m_task_t task = NULL;
20   char *destination = NULL;
21
22   MSG_process_sleep(2);
23
24   while (1){ // I am an eternal emigrant
25     MSG_task_receive(&(task), "master_mailbox");
26     destination = (char*)MSG_task_get_data (task);
27     if (!destination) break; //there is no destination, die
28     XBT_INFO("Migrating to %s", destination);
29     MSG_process_migrate(MSG_process_self(), MSG_get_host_by_name(destination));
30     MSG_process_sleep(2); // I am tired, have to sleep for 2 seconds
31     MSG_task_destroy (task);
32     task = NULL;
33   }
34   return 0;
35 }
36
37 static int master(int argc, char *argv[])
38 {
39   m_task_t task = NULL;
40
41   // I am the master of emigrant process,
42   // I tell it where it must emigrate to.
43   xbt_dynar_t destinations = xbt_dynar_new (sizeof(char*), &xbt_free_ref);
44   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
45   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Jupiter"));
46   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
47   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
48   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Bourassa"));
49   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Fafard"));
50   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Tremblay"));
51   xbt_dynar_push_as (destinations, char*, xbt_strdup ("Ginette"));
52   xbt_dynar_push_as (destinations, char*, NULL);
53
54   char *destination;
55   unsigned int i;
56   xbt_dynar_foreach(destinations, i, destination){
57     task = MSG_task_create("task", 0, 0, NULL);
58     if (destination){
59       MSG_task_set_data(task, xbt_strdup (destination));
60     }
61     TRACE_msg_set_task_category(task, "migration_order");
62     MSG_task_send (task, "master_mailbox");
63     task = NULL;
64   }
65   xbt_dynar_free (&destinations);
66   return 0;
67 }
68
69 /** Main function */
70 int main(int argc, char *argv[])
71 {
72   MSG_error_t res = MSG_OK;
73
74   /* Argument checking */
75   MSG_global_init(&argc, argv);
76   if (argc < 3) {
77     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
78     XBT_CRITICAL("example: %s msg_platform.xml msg_deployment_suspend.xml\n",
79               argv[0]);
80     exit(1);
81   }
82
83   /* Simulation setting */
84   MSG_create_environment(argv[1]);
85
86   TRACE_category ("migration_order");
87
88   /* Application deployment */
89   MSG_function_register("emigrant", emigrant);
90   MSG_function_register("master", master);
91   MSG_launch_application(argv[2]);
92
93   /* Run the simulation */
94   res = MSG_main();
95   XBT_INFO("Simulation time %g", MSG_get_clock());
96   if (res == MSG_OK)
97     res = MSG_clean();
98
99   if (res == MSG_OK)
100     return 0;
101   else
102     return 1;
103 }                               /* end_of_main */