Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Regenerate tesh files and make the tests gras-{all2all,ping,rpc}-rl succeed.
[simgrid.git] / examples / gras / ping / ping_server.c
1 /* ping - ping/pong demo of GRAS features                                   */
2
3 /* Copyright (c) 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "ping.h"
10
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Ping);
12
13 /* Global private data */
14 typedef struct {
15   gras_socket_t sock;
16   int endcondition;
17 } server_data_t;
18
19
20 static int server_cb_ping_handler(gras_msg_cb_ctx_t ctx, void *payload)
21 {
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),
37         gras_socket_peer_port(expeditor));
38
39   /* 4. Change the value of the msg variable */
40   msg = 4321;
41   /* 5. Send it back as payload of a pong message to the expeditor */
42   TRY {
43     gras_msg_send(expeditor, "pong", &msg);
44
45     /* 6. Deal with errors: add some details to the exception */
46   } CATCH(e) {
47     gras_socket_close(globals->sock);
48     RETHROW0("Unable answer with PONG: %s");
49   }
50
51   INFO0(">>>>>>>> Answered with PONG(4321) <<<<<<<<");
52
53   /* 7. Set the endcondition boolean to true (and make sure the server stops after receiving it). */
54   globals->endcondition = 1;
55
56   /* 8. Tell GRAS that we consummed this message */
57   return 0;
58 }                               /* end_of_server_cb_ping_handler */
59
60 int server(int argc, char *argv[])
61 {
62   server_data_t *globals;
63
64   int port = 4000;
65
66   /* 1. Init the GRAS infrastructure and declare my globals */
67   gras_init(&argc, argv);
68   globals = gras_userdata_new(server_data_t);
69
70   /* 2. Get the port I should listen on from the command line, if specified */
71   if (argc == 2) {
72     port = atoi(argv[1]);
73   }
74
75   INFO1("Launch server (port=%d)", port);
76
77   /* 3. Create my master socket */
78   globals->sock = gras_socket_server(port);
79
80   /* 4. Register the known messages. This function is called twice here, but it's because
81      this file also acts as regression test, no need to do so yourself of course. */
82   ping_register_messages();
83   ping_register_messages();     /* just to make sure it works ;) */
84
85   /* 5. Register my callback */
86   gras_cb_register("ping", &server_cb_ping_handler);
87
88   INFO1(">>>>>>>> Listening on port %d <<<<<<<<",
89         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(10.0);
94
95   /* 7. Housekeeping */
96   if (!globals->endcondition)
97     WARN0
98         ("An error occured, the endcondition was not set by the callback");
99
100   /* 8. Free the allocated resources, and shut GRAS down */
101   gras_socket_close(globals->sock);
102   free(globals);
103   INFO0("Done.");
104   gras_exit();
105
106   return 0;
107 }                               /* end_of_server */