Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
yet another cleaning pass
[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
9 XBT_LOG_NEW_DEFAULT_CATEGORY(msg_test_exception, "Messages specific for this msg example");
10
11 /** @addtogroup MSG_examples
12  * 
13  * - <b>exception/exception.c</b>: Demonstrates how to send an exception to a remote guy
14  */
15
16 /** Victim. This process gets a lot of remote exceptions  */
17 static int victim(int argc, char *argv[]) {
18   xbt_ex_t e;
19   msg_error_t res = MSG_OK;
20   
21   XBT_INFO("Let's work.");
22   TRY {  
23     res = MSG_task_execute(MSG_task_create("Task", 1e14, 0, NULL));
24     if (res != MSG_OK) {
25       XBT_INFO("The MSG_task_execute caught the exception for me and returned %d)",res);
26     } else {
27       xbt_die("I was expecting an exception during my execution!");
28     }
29   } CATCH(e) {
30     XBT_INFO("The received exception resumed my execution. Good. Here is it:  ----------------------->8----");
31     xbt_ex_display(&e);
32     XBT_INFO("(end of the first exception) ----8<------------------------");
33     xbt_ex_free(e);
34   }
35
36   XBT_INFO("Let's get suspended.");
37   int gotit = 0;
38   TRY {
39     MSG_process_suspend(MSG_process_self());
40   } CATCH(e) {
41     XBT_INFO("The received exception resumed my suspension. Good. Here is it:  ----------------------->8----");
42     xbt_ex_display(&e);
43     XBT_INFO("(end of the second exception) ----8<------------------------");
44     gotit = 1;
45     xbt_ex_free(e);
46   }
47   if(!gotit) {
48     xbt_die("I was expecting an exception during my suspension!");
49   }
50
51   XBT_INFO("Let's sleep for 10 seconds.");
52   TRY {
53     res = MSG_process_sleep(10);
54     if (res != MSG_OK) {
55       XBT_INFO("The MSG_process_sleep caught the exception for me and returned %d)",res);
56     } else {
57       xbt_die("I was expecting to get an exception during my nap.");
58     }
59   } CATCH(e) {
60     XBT_INFO("Got the second exception:   ----------------------->8----");
61     xbt_ex_display(&e);
62     XBT_INFO("(end of the third exception) ----8<------------------------");
63     xbt_ex_free(e);
64   }
65
66   XBT_INFO("Let's try a last time to do something on something");
67   MSG_process_sleep(10);
68
69   XBT_INFO("That's enough now. I quit.");
70   return 0;
71 }
72
73 /** Terrorist. This process sends a bunch of exceptions to the victim. */
74 static int terrorist(int argc, char *argv[])
75 {
76   msg_process_t victim_process = NULL;
77
78   XBT_INFO("Let's create a victim.");
79   victim_process = MSG_process_create("victim", victim, NULL, MSG_host_self());
80
81   XBT_INFO("Going to sleep for 1 second");
82   if (MSG_process_sleep(1) != MSG_OK)
83      xbt_die("What's going on??? I failed to sleep!");
84   XBT_INFO("Send a first exception (host failure)");
85   SIMIX_process_throw(victim_process, host_error, 0, "First Trick: Let's pretend that the host failed");
86
87   XBT_INFO("Sweet, let's prepare a second trick!");
88   XBT_INFO("Going to sleep for 2 seconds");
89
90   if (MSG_process_sleep(2) != MSG_OK)
91      xbt_die("What's going on??? I failed to sleep!");
92   XBT_INFO("Send a second exception (host failure)");
93   SIMIX_process_throw(victim_process, host_error, 0, "Second Trick: Let's pretend again that the host failed");
94
95   XBT_INFO("Sweet, let's prepare a third trick!");
96   XBT_INFO("Going to sleep for 3 seconds");
97
98   if (MSG_process_sleep(3) != MSG_OK)
99      xbt_die("What's going on??? I failed to sleep!");
100   XBT_INFO("Send a third exception (cancellation)");
101   SIMIX_process_throw(victim_process, cancel_error, 0, "Third Trick: Let's pretend this time that someone canceled something");
102
103   XBT_INFO("OK, goodbye now.");
104   return 0;
105 }
106
107 int main(int argc, char *argv[]) {
108   msg_error_t res = MSG_OK;
109
110   MSG_init(&argc, argv);
111   xbt_assert(argc > 2, "Usage: %s platform_file deployment_file\n"
112              "\tExample: %s msg_platform.xml msg_deployment.xml\n", argv[0], argv[0]);
113
114   MSG_function_register("terrorist", terrorist);
115   MSG_create_environment(argv[1]);
116   MSG_launch_application(argv[2]);
117
118   // Launch the simulation
119   res = MSG_main();
120
121   XBT_INFO("Simulation time %g", MSG_get_clock());
122   return res != MSG_OK;
123 }