Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Prevent warning message 'config elem ... registered twice'
[simgrid.git] / examples / msg / exception / exception.c
1 /* Copyright (c) 2007, 2009-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"
8 #include "xbt.h"
9
10 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test_exception,
11                              "Messages specific for this msg example");
12
13 /** @addtogroup MSG_examples
14  * 
15  * - <b>exception/exception.c</b>: Demonstrates how to send an exception to a remote guy
16  */
17
18 /** Victim. This process gets a lot of remote exceptions  */
19 static int victim(int argc, char *argv[]) {
20
21   xbt_ex_t e;
22   msg_error_t res = MSG_OK;
23   
24   XBT_INFO("Let's work.");
25   TRY { 
26     res = MSG_task_execute(MSG_task_create("Task", 1e14, 0, NULL));
27     if (res != MSG_OK) {
28       XBT_INFO("The MSG_task_execute caught the exception for me and returned %d)",res);
29     } else {
30       xbt_die("I was expecting an exception during my execution!");
31     }
32   } CATCH(e) {
33     XBT_INFO("The received exception resumed my execution. Good. Here is it:  ----------------------->8----");
34     xbt_ex_display(&e);
35     XBT_INFO("(end of the first exception) ----8<------------------------");
36     xbt_ex_free(e);
37   }
38
39
40   XBT_INFO("Let's get suspended.");
41   int gotit = 0;
42   TRY {
43     MSG_process_suspend(MSG_process_self());
44   } CATCH(e) {
45     XBT_INFO("The received exception resumed my suspension. Good. Here is it:  ----------------------->8----");
46     xbt_ex_display(&e);
47     XBT_INFO("(end of the second exception) ----8<------------------------");
48     gotit = 1;
49     xbt_ex_free(e);
50   }
51   if(!gotit) {
52     xbt_die("I was expecting an exception during my suspension!");
53   }
54   
55   XBT_INFO("Let's sleep for 10 seconds.");
56   TRY {
57     res = MSG_process_sleep(10);
58     if (res != MSG_OK) {
59       XBT_INFO("The MSG_process_sleep caught the exception for me and returned %d)",res);
60     } else {
61       xbt_die("I was expecting to get an exception during my nap.");
62     }
63   } CATCH(e) {
64     XBT_INFO("Got the second exception:   ----------------------->8----");
65     xbt_ex_display(&e);
66     XBT_INFO("(end of the third exception) ----8<------------------------");
67     xbt_ex_free(e);
68   }
69
70   XBT_INFO("Let's try a last time to do something on something");
71   MSG_process_sleep(10);
72
73   XBT_INFO("That's enough now. I quit.");
74   
75   return 0;
76 }
77
78 /** Terrorist. This process sends a bunch of exceptions to the victim. */
79 static int terrorist(int argc, char *argv[])
80 {
81   msg_process_t victim_process = NULL;
82
83   XBT_INFO("Let's create a victim.");
84   victim_process = MSG_process_create("victim", victim, NULL, MSG_host_self());
85
86   XBT_INFO("Going to sleep for 1 second");
87   if (MSG_process_sleep(1) != MSG_OK)
88      xbt_die("What's going on??? I failed to sleep!");
89   XBT_INFO("Send a first exception (host failure)");
90   SIMIX_process_throw(victim_process, host_error, 0, "First Trick: Let's pretend that the host failed");
91
92
93   XBT_INFO("Sweet, let's prepare a second trick!");
94   XBT_INFO("Going to sleep for 2 seconds");
95
96   if (MSG_process_sleep(2) != MSG_OK)
97      xbt_die("What's going on??? I failed to sleep!");
98   XBT_INFO("Send a second exception (host failure)");
99   SIMIX_process_throw(victim_process, host_error, 0, "Second Trick: Let's pretend again that the host failed");
100
101   XBT_INFO("Sweet, let's prepare a third trick!");
102   XBT_INFO("Going to sleep for 3 seconds");
103
104   if (MSG_process_sleep(3) != MSG_OK)
105      xbt_die("What's going on??? I failed to sleep!");
106   XBT_INFO("Send a third exception (cancellation)");
107   SIMIX_process_throw(victim_process, cancel_error, 0, "Third Trick: Let's pretend this time that someone canceled something");
108
109   XBT_INFO("OK, goodbye now.");
110   return 0;
111 }
112
113 int main(int argc, char *argv[]) {
114   msg_error_t res = MSG_OK;
115
116   MSG_init(&argc, argv);
117   if (argc < 3) {
118     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
119     exit(1);
120   }
121
122   MSG_function_register("terrorist", terrorist);
123   MSG_create_environment(argv[1]);
124   MSG_launch_application(argv[2]);
125
126   /*
127   // Simplistic platform with only one host
128   sg_platf_begin();
129   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
130   sg_platf_new_AS_begin(&AS);
131
132   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
133   host.id = "host0";
134   sg_platf_new_host(&host);
135
136   sg_platf_new_AS_end();
137   sg_platf_end();
138
139   // Add one process -- super heavy just to launch an application!
140   SIMIX_init_application();
141   sg_platf_begin();
142
143   s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
144   process.argc=1;
145   process.argv = malloc(sizeof(char*)*2);
146   process.argv[0] = "terrorist";
147   process.argv[1] = NULL;
148   process.host = "host0";
149   process.function = "terrorist";
150   process.start_time = 0;
151   sg_platf_new_process(&process);
152   sg_platf_end();
153 */
154
155   // Launch the simulation
156   res = MSG_main();
157
158   XBT_INFO("Simulation time %g", MSG_get_clock());
159   if (res == MSG_OK)
160     return 0;
161   else
162     return 1;
163 }