Logo AND Algorithmique Numérique Distribuée

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