Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Gras_stub_generator does not produce the same files as it used to
[simgrid.git] / examples / gras / ping / ping_server.c
1 /* $Id$ */
2
3 /* ping - ping/pong demo of GRAS features                                   */
4
5 /* Copyright (c) 2003, 2004, 2005 Martin Quinson. All rights reserved.      */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "ping.h"
11
12 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Ping);
13
14 /* Global private data */
15 typedef struct {
16   gras_socket_t sock;
17   int endcondition;
18 } server_data_t;
19
20
21 static int server_cb_ping_handler(gras_msg_cb_ctx_t ctx, void *payload) {
22                              
23   xbt_ex_t e;
24   /* 1. Get the payload into the msg variable, and retrieve my caller */
25   int msg=*(int*)payload;
26   gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
27
28   /* 2. Retrieve the server's state (globals) */
29
30   server_data_t *globals=(server_data_t*)gras_userdata_get();
31   globals->endcondition = 0;
32    
33   /* 3. Log which client connected */
34   INFO3(">>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<", 
35         msg, 
36         gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor));
37   
38   /* 4. Change the value of the msg variable */
39   msg = 4321;
40   /* 5. Send it back as payload of a pong message to the expeditor */
41   TRY {
42     gras_msg_send(expeditor, gras_msgtype_by_name("pong"), &msg);
43
44   /* 6. Deal with errors: add some details to the exception */
45   } CATCH(e) {
46     gras_socket_close(globals->sock);
47     RETHROW0("Unable answer with PONG: %s");
48   }
49
50   INFO0(">>>>>>>> Answered with PONG(4321) <<<<<<<<");
51    
52   /* 7. Set the endcondition boolean to true (and make sure the server stops after receiving it). */
53   globals->endcondition = 1;
54   
55   /* 8. Make sure we don't leak sockets */
56   gras_socket_close(expeditor);
57    
58   /* 9. Tell GRAS that we consummed this message */
59   return 1;
60 } /* end_of_server_cb_ping_handler */
61
62 int server (int argc,char *argv[]) {
63   server_data_t *globals;
64
65   int port = 4000;
66   
67   /* 1. Init the GRAS infrastructure and declare my globals */
68   gras_init(&argc,argv);
69   globals=gras_userdata_new(server_data_t);
70    
71   /* 2. Get the port I should listen on from the command line, if specified */
72   if (argc == 2) {
73     port=atoi(argv[1]);
74   }
75
76   INFO1("Launch server (port=%d)", port);
77
78   /* 3. Create my master socket */
79   globals->sock = gras_socket_server(port);
80
81   /* 4. Register the known messages. This function is called twice here, but it's because
82         this file also acts as regression test, no need to do so yourself of course. */
83   ping_register_messages();
84   ping_register_messages(); /* just to make sure it works ;) */
85    
86   /* 5. Register my callback */
87   gras_cb_register(gras_msgtype_by_name("ping"),&server_cb_ping_handler);
88
89   INFO1(">>>>>>>> Listening on port %d <<<<<<<<", gras_socket_my_port(globals->sock));
90   globals->endcondition=0;
91
92   /* 6. Wait up to 10 minutes for an incomming message to handle */
93   gras_msg_handle(60.0);
94    
95   /* 7. Housekeeping */
96   if (!globals->endcondition)
97      WARN0("An error occured, the endcondition was not set by the callback");
98   
99   /* 8. Free the allocated resources, and shut GRAS down */
100   gras_socket_close(globals->sock);
101   free(globals);
102   gras_exit();
103    
104   INFO0("Done.");
105   return 0;
106 } /* end_of_server */