Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New XBT module: file
[simgrid.git] / teshsuite / msg / host_on_off / host_on_off_recv.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 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_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       MSG_comm_destroy(comm);
49     }
50     XBT_INFO("Master has finished");
51   }
52   CATCH(e) {
53     xbt_die("Exception caught in the master");
54     return 1;
55   }
56   return 0;
57 }
58
59 /** Receiver function  */
60 int slave(int argc, char *argv[])
61 {
62   xbt_ex_t e;
63   TRY {
64     XBT_INFO("Slave receiving");
65     msg_task_t task = NULL;
66     msg_error_t error = MSG_task_receive(&(task), mailbox);
67     if (error) {
68       XBT_ERROR("Error while receiving message");
69       return 1;
70     }
71
72     XBT_ERROR("Slave should be off already.");
73     return 1;
74   }
75   CATCH(e) {
76     XBT_ERROR("Exception caught in the slave");
77     return 1;
78   }
79   return 0;
80 }
81
82 /** Main function */
83 int main(int argc, char *argv[])
84 {
85   msg_error_t res;
86   const char *platform_file;
87   const char *application_file;
88
89   MSG_init(&argc, argv);
90   if (argc != 3) {
91     printf("Usage: %s platform_file deployment_file\n", argv[0]);
92     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
93     exit(1);
94   }
95   platform_file = argv[1];
96   application_file = argv[2];
97
98   {                             /*  Simulation setting */
99     MSG_create_environment(platform_file);
100   }
101   {                             /*   Application deployment */
102     MSG_function_register("master", master);
103     MSG_function_register("slave", slave);
104
105     MSG_launch_application(application_file);
106   }
107   res = MSG_main();
108
109   XBT_INFO("Simulation time %g", MSG_get_clock());
110
111   if (res == MSG_OK)
112     return 0;
113   else
114     return 1;
115 }                               /* end_of_main */