Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
121d0b9a3efa95234cc1e22fab8cce352eca51d9
[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 "simgrid/msg.h"            /* Yeah! If you want to use msg, you need to include simgrid/msg.h */
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test, "Messages specific for this msg example");
10
11 static const char* mailbox = "comm";
12
13 static int master(int argc, char *argv[])
14 {
15   xbt_ex_t e;
16   TRY {
17     msg_host_t jupiter = MSG_host_by_name("Jupiter");
18     
19     XBT_INFO("Master starting");
20     MSG_process_sleep(0.5);
21
22     msg_comm_t comm = NULL;
23     {
24       msg_task_t task = MSG_task_create("COMM", 0, 100000000, NULL);
25       comm = MSG_task_isend(task, mailbox);
26     }
27
28     MSG_process_sleep(0.5);
29
30     XBT_INFO("Turning off the slave host");
31     MSG_host_off(jupiter);
32
33     if (comm) {
34       MSG_comm_wait(comm, -1);
35       MSG_comm_destroy(comm);
36     }
37     XBT_INFO("Master has finished");
38   }
39   CATCH(e) {
40     xbt_die("Exception caught in the master");
41     return 1;
42   }
43   return 0;
44 }
45
46 static int slave(int argc, char *argv[])
47 {
48   xbt_ex_t e;
49   TRY {
50     XBT_INFO("Slave receiving");
51     msg_task_t task = NULL;
52     msg_error_t error = MSG_task_receive(&(task), mailbox);
53     if (error) {
54       XBT_ERROR("Error while receiving message");
55       return 1;
56     }
57
58     XBT_ERROR("Slave should be off already.");
59     return 1;
60   }
61   CATCH(e) {
62     XBT_ERROR("Exception caught in the slave");
63     return 1;
64   }
65   return 0;
66 }
67
68 int main(int argc, char *argv[])
69 {
70   msg_error_t res;
71
72   MSG_init(&argc, argv);
73   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
74        "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
75
76   MSG_create_environment(argv[1]);
77
78   MSG_function_register("master", master);
79   MSG_function_register("slave", slave);
80   MSG_launch_application(argv[2]);
81
82   res = MSG_main();
83
84   XBT_INFO("Simulation time %g", MSG_get_clock());
85
86   return res != MSG_OK;
87 }