Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Update Doc.
[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   gras_socket_t client = gras_msg_cb_ctx_from(ctx);
18   server_data_t *globals=(server_data_t*)gras_userdata_get();
19    
20   CRITICAL2("Argh, killed by %s:%d! Bye folks, I'm out of here...",
21         gras_socket_peer_name(client), gras_socket_peer_port(client));
22   
23   globals->killed = 1;
24    
25   return 0;
26 } /* end_of_kill_callback */
27
28 int server(int argc, char *argv[]) {
29   gras_socket_t mysock;   /* socket on which I listen */
30   server_data_t *globals;
31   
32   gras_init(&argc,argv);
33
34   globals=gras_userdata_new(server_data_t*);
35   globals->killed=0;
36
37   gras_msgtype_declare("kill", NULL);
38   gras_cb_register("kill",&server_kill_cb);
39    
40   if (argc>1 && !strcmp(argv[1],"--cheat")) {
41      mysock = gras_socket_server(9999);
42      INFO0("Hi! hi! I'm not in the search range, but in 9999...");
43   } else {
44      mysock = gras_socket_server((rand() % 10) + 3000);   
45      INFO1("Ok, I'm hidden on port %d. Hope for the best.", gras_socket_my_port(mysock));
46   } 
47    
48   while (!globals->killed) {
49      gras_msg_handle(-1); /* blocking */
50   }
51     
52   gras_exit();
53   return 0;
54 }
55
56 int client(int argc, char *argv[]) {
57   gras_socket_t mysock;   /* socket on which I listen */
58   gras_socket_t toserver; /* socket used to write to the server */
59   int found;              /* whether we found peer */
60   int port;               /* where we think that the server is */
61   xbt_ex_t e;
62
63   gras_init(&argc,argv);
64
65   gras_msgtype_declare("kill", NULL);
66   mysock = gras_socket_server_range(1024, 10000, 0, 0);
67   
68   VERB0("Run little server, run. I'll get you. (sleep 1.5 sec)");
69   gras_os_sleep(1.5);
70    
71   for (port=3000, found=0; port<3010 && !found; port++) {
72      TRY {
73         toserver = gras_socket_client(argv[1], port);
74         gras_msg_send(toserver,"kill", NULL);
75         gras_socket_close(toserver);
76         found = 1;
77         INFO1("Yeah! I found the server on %d! It's eradicated by now.",port);
78      } CATCH(e) {
79         xbt_ex_free(e);
80      }
81      if (!found)
82        INFO1("Damn, the server is not on %d",port);
83   } /* end_of_loop */
84    
85   if(!found) 
86      THROW0(not_found_error, 0, "Damn, I failed to find the server! I cannot survive this humilliation.");
87   
88      
89   gras_exit();
90   return 0;
91 }