Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
9fb3bde0d3bbe73480dc6aba9398a8d3d40019e9
[simgrid.git] / teshsuite / msg / host_on_off / host_on_off_recv.c
1 /* Copyright (c) 2010-2014. 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 static const char* mailbox = "comm";
22
23 /** Emitter function  */
24 int master(int argc, char *argv[])
25 {
26   xbt_ex_t e;
27   TRY {
28     msg_host_t jupiter = MSG_get_host_by_name("Jupiter");
29     
30     XBT_INFO("Master starting");
31     MSG_process_sleep(0.5);
32
33     msg_comm_t comm = NULL;
34     if (1) {
35       msg_task_t task = MSG_task_create("COMM", 0, 100000000, NULL);
36       comm = MSG_task_isend(task, mailbox);
37     }
38
39     if(MSG_process_sleep(0.5)) {
40       XBT_ERROR("Unexpected error while sleeping");
41       return 1;
42     }
43     XBT_INFO("Turning off the slave host");
44     MSG_host_off(jupiter);
45
46     if (comm) {
47       MSG_comm_wait(comm, -1);
48     }
49     XBT_INFO("Master has finished");
50   }
51   CATCH(e) {
52     xbt_die("Exception caught in the master");
53     return 1;
54   }
55   return 0;
56 }
57
58 /** Receiver function  */
59 int slave(int argc, char *argv[])
60 {
61   xbt_ex_t e;
62   TRY {
63     XBT_INFO("Slave receiving");
64     msg_task_t task = NULL;
65     msg_error_t error = MSG_task_receive(&(task), mailbox);
66     if (error) {
67       XBT_ERROR("Error while receiving message");
68       return 1;
69     }
70
71     XBT_ERROR("Slave should be off already.");
72     return 1;
73   }
74   CATCH(e) {
75     XBT_ERROR("Exception caught in the slave");
76     return 1;
77   }
78   return 0;
79 }
80
81 /** Main function */
82 int main(int argc, char *argv[])
83 {
84   msg_error_t res;
85   const char *platform_file;
86   const char *application_file;
87
88   MSG_init(&argc, argv);
89   if (argc != 3) {
90     printf("Usage: %s platform_file deployment_file\n", argv[0]);
91     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
92     exit(1);
93   }
94   platform_file = argv[1];
95   application_file = argv[2];
96
97   {                             /*  Simulation setting */
98     MSG_create_environment(platform_file);
99   }
100   {                             /*   Application deployment */
101     MSG_function_register("master", master);
102     MSG_function_register("slave", slave);
103
104     MSG_launch_application(application_file);
105   }
106   res = MSG_main();
107
108   XBT_INFO("Simulation time %g", MSG_get_clock());
109
110   if (res == MSG_OK)
111     return 0;
112   else
113     return 1;
114 }                               /* end_of_main */