Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Define THROWF and RETHROWF as variadic macros.
[simgrid.git] / doc / gtut-files / 08-exceptions.c
1 /* Copyright (c) 2006, 2007, 2010. 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 <gras.h>
8
9 XBT_LOG_NEW_DEFAULT_CATEGORY(test, "My little example");
10
11 typedef struct {
12   int killed;
13 } server_data_t;
14
15
16 int server_kill_cb(gras_msg_cb_ctx_t ctx, void *payload)
17 {
18   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
19   server_data_t *globals = (server_data_t *) gras_userdata_get();
20
21   XBT_CRITICAL("Argh, killed by %s:%d! Bye folks, I'm out of here...",
22             gras_socket_peer_name(client), gras_socket_peer_port(client));
23
24   globals->killed = 1;
25
26   return 0;
27 }                               /* end_of_kill_callback */
28
29 int server(int argc, char *argv[])
30 {
31   gras_socket_t mysock;         /* socket on which I listen */
32   server_data_t *globals;
33
34   gras_init(&argc, argv);
35
36   globals = gras_userdata_new(server_data_t *);
37   globals->killed = 0;
38
39   gras_msgtype_declare("kill", NULL);
40   gras_cb_register("kill", &server_kill_cb);
41
42   if (argc > 1 && !strcmp(argv[1], "--cheat")) {
43     mysock = gras_socket_server(9999);
44     XBT_INFO("Hi! hi! I'm not in the search range, but in 9999...");
45   } else {
46     mysock = gras_socket_server((rand() % 10) + 3000);
47     XBT_INFO("Ok, I'm hidden on port %d. Hope for the best.",
48           gras_socket_my_port(mysock));
49   }
50
51   while (!globals->killed) {
52     gras_msg_handle(-1);        /* blocking */
53   }
54
55   gras_exit();
56   return 0;
57 }
58
59 int client(int argc, char *argv[])
60 {
61   gras_socket_t mysock;         /* socket on which I listen */
62   gras_socket_t toserver;       /* socket used to write to the server */
63   int found;                    /* whether we found peer */
64   int port;                     /* where we think that the server is */
65   xbt_ex_t e;
66
67   gras_init(&argc, argv);
68
69   gras_msgtype_declare("kill", NULL);
70   mysock = gras_socket_server_range(1024, 10000, 0, 0);
71
72   XBT_VERB("Run little server, run. I'll get you. (sleep 1.5 sec)");
73   gras_os_sleep(1.5);
74
75   for (port = 3000, found = 0; port < 3010 && !found; port++) {
76     TRY {
77       toserver = gras_socket_client(argv[1], port);
78       gras_msg_send(toserver, "kill", NULL);
79       gras_socket_close(toserver);
80       found = 1;
81       XBT_INFO("Yeah! I found the server on %d! It's eradicated by now.",
82             port);
83     }
84     CATCH(e) {
85       xbt_ex_free(e);
86     }
87     if (!found)
88       XBT_INFO("Damn, the server is not on %d", port);
89   }                             /* end_of_loop */
90
91   if (!found)
92     THROWF(not_found_error, 0,
93            "Damn, I failed to find the server! I cannot survive this humilliation.");
94
95
96   gras_exit();
97   return 0;
98 }