Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
0733dcdcf44d6cc20ebc30fc5e935ab790a1b8f7
[simgrid.git] / teshsuite / msg / host_on_off / host_on_off_wait.c
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <stdio.h>
8 #include "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10 #include "xbt/ex.h"
11
12 /* Create a log channel to have nice outputs. */
13 #include "xbt/log.h"
14 #include "xbt/asserts.h"
15 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
16                              "Messages specific for this msg example");
17
18 int master(int argc, char *argv[]);
19 int slave(int argc, char *argv[]);
20
21 /** Emitter function  */
22 int master(int argc, char *argv[])
23 {
24   xbt_ex_t e;
25   TRY {
26     msg_host_t jupiter = MSG_host_by_name("Jupiter");
27     XBT_INFO("Master waiting");
28     if(MSG_process_sleep(1)) {
29       XBT_ERROR("Unexpected error while sleeping");
30       return 1;
31     }
32
33     XBT_INFO("Turning off the slave host");
34     MSG_host_off(jupiter);
35     XBT_INFO("Master has finished");
36   }
37   CATCH(e) {
38     xbt_die("Exception caught in the master");
39     return 1;
40   }
41   return 0;
42 }
43
44 /** Receiver function  */
45 int slave(int argc, char *argv[])
46 {
47   xbt_ex_t e;
48   TRY {
49     XBT_INFO("Slave waiting");
50     // TODO, This should really be MSG_HOST_FAILURE
51     MSG_process_sleep(5);
52     XBT_ERROR("Slave should be off already.");
53     return 1;
54   }
55   CATCH(e) {
56     XBT_ERROR("Exception caught in the slave");
57     return 1;
58   }
59   return 0;
60 }
61
62 /** Main function */
63 int main(int argc, char *argv[])
64 {
65   msg_error_t res;
66   const char *platform_file;
67   const char *application_file;
68
69   MSG_init(&argc, argv);
70   if (argc != 3) {
71     printf("Usage: %s platform_file deployment_file\n", argv[0]);
72     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
73     exit(1);
74   }
75   platform_file = argv[1];
76   application_file = argv[2];
77
78   {                             /*  Simulation setting */
79     MSG_create_environment(platform_file);
80   }
81   {                             /*   Application deployment */
82     MSG_function_register("master", master);
83     MSG_function_register("slave", slave);
84
85     MSG_launch_application(application_file);
86   }
87   res = MSG_main();
88
89   XBT_INFO("Simulation time %g", MSG_get_clock());
90
91   if (res == MSG_OK)
92     return 0;
93   else
94     return 1;
95 }                               /* end_of_main */