Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
stupid old gcc. That's initialized.
[simgrid.git] / examples / msg / exception / exception.c
1 /* Copyright (c) 2007, 2009-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 "msg/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   XBT_INFO("Let's get suspended.");
23   TRY { 
24     MSG_process_suspend(MSG_process_self());
25   } CATCH(e) {
26     XBT_INFO("The received exception resumed my execution. Good. Here is it:  ----------------------->8----");
27     xbt_ex_display(&e);
28     XBT_INFO("(end of the first exception) ----8<------------------------");
29     xbt_ex_free(e);
30   }
31
32   msg_error_t res = MSG_OK;
33   int gotit = 0;
34   XBT_INFO("Let's sleep for 10 seconds.");
35   TRY {
36     res = MSG_process_sleep(10);
37   } CATCH(e) {
38     XBT_INFO("Got the second exception:   ----------------------->8----");
39     xbt_ex_display(&e);
40     XBT_INFO("(end of the second exception) ----8<------------------------");
41     xbt_ex_free(e);
42   }
43
44   if (res != MSG_TASK_CANCELED)
45     xbt_die("Sleep action not canceled through the exception. This is not a method. (retval: %d)",res);
46   if (!gotit)
47     xbt_die("I was expecting to get an exception during my nap.");
48   XBT_INFO("My little nap got canceled through a raw exception. Excellent.");
49
50   XBT_INFO("That's enough now. I quit.");
51   return 0;
52 }
53
54 /** Terrorist. This process sends a bunch of exceptions to the victim. */
55 static int terrorist(int argc, char *argv[])
56 {
57   msg_process_t victim_process = NULL;
58
59   XBT_INFO("Let's create a victim.");
60   victim_process = MSG_process_create("victim", victim, NULL, MSG_host_self());
61   if (MSG_process_sleep(1) != MSG_OK)
62      xbt_die("What's going on??? I failed to sleep!");
63   XBT_INFO("Send a first exception (host failure)");
64   SIMIX_process_throw(victim_process, host_error, 0, "Let's pretend that the host failed");
65
66   if (MSG_process_sleep(3) != MSG_OK)
67      xbt_die("What's going on??? I failed to sleep!");
68   XBT_INFO("Send a second exception (cancellation)");
69   SIMIX_process_throw(victim_process, cancel_error, 0, "Let's pretend this time that someone canceled something");
70
71   XBT_INFO("OK, goodbye now.");
72   return 0;
73 }
74
75 int main(int argc, char *argv[]) {
76   msg_error_t res = MSG_OK;
77
78   MSG_init(&argc, argv);
79   if (argc < 3) {
80     XBT_CRITICAL("Usage: %s platform_file deployment_file\n", argv[0]);
81     exit(1);
82   }
83
84   MSG_function_register("terrorist", terrorist);
85   MSG_create_environment(argv[1]);
86   MSG_launch_application(argv[2]);
87
88   /*
89   // Simplistic platform with only one host
90   sg_platf_begin();
91   s_sg_platf_AS_cbarg_t AS = SG_PLATF_AS_INITIALIZER;
92   sg_platf_new_AS_begin(&AS);
93
94   s_sg_platf_host_cbarg_t host = SG_PLATF_HOST_INITIALIZER;
95   host.id = "host0";
96   sg_platf_new_host(&host);
97
98   sg_platf_new_AS_end();
99   sg_platf_end();
100
101   // Add one process -- super heavy just to launch an application!
102   SIMIX_init_application();
103   sg_platf_begin();
104
105   s_sg_platf_process_cbarg_t process = SG_PLATF_PROCESS_INITIALIZER;
106   process.argc=1;
107   process.argv = malloc(sizeof(char*)*2);
108   process.argv[0] = "terrorist";
109   process.argv[1] = NULL;
110   process.host = "host0";
111   process.function = "terrorist";
112   process.start_time = 0;
113   sg_platf_new_process(&process);
114   sg_platf_end();
115 */
116
117   // Launch the simulation
118   res = MSG_main();
119
120   XBT_INFO("Simulation time %g", MSG_get_clock());
121   if (res == MSG_OK)
122     return 0;
123   else
124     return 1;
125 }