Logo AND Algorithmique Numérique Distribuée

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