Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge pull request #2 from mquinson/master
[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   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
118            "\tExample: %s msg_platform.xml msg_deployment.xml\n", 
119            argv[0], argv[0]);
120
121   MSG_function_register("terrorist", terrorist);
122   MSG_create_environment(argv[1]);
123   MSG_launch_application(argv[2]);
124
125
126   // Launch the simulation
127   res = MSG_main();
128
129   XBT_INFO("Simulation time %g", MSG_get_clock());
130   return res != MSG_OK;
131 }