Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Destroy tasks if sending has failed.
[simgrid.git] / teshsuite / msg / host_on_off.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 "msg/msg.h"            /* Yeah! If you want to use msg, you need to include msg/msg.h */
9 #include "xbt/sysdep.h"         /* calloc, printf */
10
11 /* Create a log channel to have nice outputs. */
12 #include "xbt/log.h"
13 #include "xbt/asserts.h"
14 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test,
15                              "Messages specific for this msg example");
16
17 int master(int argc, char *argv[]);
18 int slave(int argc, char *argv[]);
19
20 /** Emitter function  */
21 int master(int argc, char *argv[])
22 {
23   double task_comp_size = 5E7;
24   double task_comm_size = 1E6;
25
26   char mailbox[256];
27   msg_task_t task = NULL;
28
29   sprintf(mailbox, "jupi");
30
31   task = MSG_task_create("task on", task_comp_size, task_comm_size, NULL);
32   XBT_INFO("Sending \"%s\"", task->name);
33   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
34     MSG_task_destroy(task);
35
36   MSG_process_sleep(1);
37   MSG_host_off(MSG_get_host_by_name("Jupiter"));
38
39   task = MSG_task_create("task off", task_comp_size, task_comm_size, NULL);
40   XBT_INFO("Sending \"%s\"", task->name);
41   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
42     MSG_task_destroy(task);
43
44   MSG_host_on(MSG_get_host_by_name("Jupiter"));
45
46   task = MSG_task_create("task on without proc", task_comp_size, task_comm_size, NULL);
47   XBT_INFO("Sending \"%s\"", task->name);
48   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
49     MSG_task_destroy(task);
50
51   char **argvF = xbt_new(char*, 2);
52   argvF[0] = xbt_strdup("slave");
53   MSG_process_create_with_arguments("slave", slave, NULL, MSG_get_host_by_name("Jupiter"), 1, argvF);
54
55   task = MSG_task_create("task on with proc", task_comp_size, task_comm_size, NULL);
56   XBT_INFO("Sending \"%s\"", task->name);
57   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
58     MSG_task_destroy(task);
59
60   task = MSG_task_create("finalize", 0, 0, 0);
61   XBT_INFO("Sending \"%s\"", task->name);
62   if (MSG_task_send_with_timeout(task, mailbox, 1) != MSG_OK)
63     MSG_task_destroy(task);
64
65   XBT_INFO("Goodbye now!");
66   return 0;
67 }                               /* end_of_master */
68
69 /** Receiver function  */
70 int slave(int argc, char *argv[])
71 {
72   msg_task_t task = NULL;
73   _XBT_GNUC_UNUSED int res;
74   int id = -1;
75   char mailbox[80];
76
77   sprintf(mailbox, "jupi");
78
79   while (1) {
80     res = MSG_task_receive(&(task), mailbox);
81     xbt_assert(res == MSG_OK, "MSG_task_get failed");
82
83     if (!strcmp(MSG_task_get_name(task), "finalize")) {
84       MSG_task_destroy(task);
85       break;
86     }
87     MSG_task_execute(task);
88     XBT_INFO("Task \"%s\" done", MSG_task_get_name(task));
89
90     MSG_task_destroy(task);
91     task = NULL;
92     id--;
93   }
94   XBT_INFO("I'm done. See you!");
95   return 0;
96 }                               /* end_of_slave */
97
98 /** Main function */
99 int main(int argc, char *argv[])
100 {
101   msg_error_t res;
102   const char *platform_file;
103   const char *application_file;
104
105   MSG_init(&argc, argv);
106   if (argc != 3) {
107     printf("Usage: %s platform_file deployment_file\n", argv[0]);
108     printf("example: %s msg_platform.xml msg_deployment.xml\n", argv[0]);
109     exit(1);
110   }
111   platform_file = argv[1];
112   application_file = argv[2];
113
114   /* MSG_config("workstation/model","KCCFLN05"); */
115   {                             /*  Simulation setting */
116     MSG_create_environment(platform_file);
117   }
118   {                             /*   Application deployment */
119     MSG_function_register("master", master);
120     MSG_function_register("slave", slave);
121
122     MSG_launch_application(application_file);
123   }
124   res = MSG_main();
125
126   XBT_INFO("Simulation time %g", MSG_get_clock());
127
128   if (res == MSG_OK)
129     return 0;
130   else
131     return 1;
132 }                               /* end_of_main */