Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix compilation with tracing and new network_element_t
[simgrid.git] / examples / gras / console / 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   xbt_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   /* 1. Get the payload into the msg variable, and retrieve my caller */
23   int msg = *(int *) payload;
24   xbt_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
25
26   /* 2. Retrieve the server's state (globals) */
27
28   server_data_t *globals = (server_data_t *) gras_userdata_get();
29   globals->endcondition = 0;
30
31   /* 3. Log which client connected */
32   XBT_INFO(">>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<",
33         msg,
34         xbt_socket_peer_name(expeditor),
35         xbt_socket_peer_port(expeditor));
36
37   /* 4. Change the value of the msg variable */
38   msg = 4321;
39   /* 5. Send it back as payload of a pong message to the expeditor */
40   TRY {
41     gras_msg_send(expeditor, "pong", &msg);
42
43     /* 6. Deal with errors: add some details to the exception */
44   }
45   CATCH_ANONYMOUS {
46     gras_socket_close(globals->sock);
47     RETHROWF("Unable answer with PONG: %s");
48   }
49
50   XBT_INFO(">>>>>>>> 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. Tell GRAS that we consummed this message */
56   return 0;
57 }                               /* end_of_server_cb_ping_handler */
58
59 int server(int argc, char *argv[])
60 {
61   server_data_t *globals;
62
63   int port = 4000;
64
65   /* 1. Init the GRAS infrastructure and declare my globals */
66   gras_init(&argc, argv);
67   globals = gras_userdata_new(server_data_t);
68
69   /* 2. Get the port I should listen on from the command line, if specified */
70   if (argc == 2) {
71     port = atoi(argv[1]);
72   }
73
74   XBT_INFO("Launch server (port=%d)", port);
75
76   /* 3. Create my master socket */
77   globals->sock = gras_socket_server(port);
78
79   /* 4. Register the known messages. This function is called twice here, but it's because
80      this file also acts as regression test, no need to do so yourself of course. */
81   ping_register_messages();
82   ping_register_messages();     /* just to make sure it works ;) */
83
84   /* 5. Register my callback */
85   gras_cb_register("ping", &server_cb_ping_handler);
86
87   XBT_INFO(">>>>>>>> Listening on port %d <<<<<<<<",
88         xbt_socket_my_port(globals->sock));
89   globals->endcondition = 0;
90
91   /* 6. Wait up to 20 minutes for an incomming message to handle */
92   gras_msg_handle(20.0);
93
94   /* 7. Housekeeping */
95   if (!globals->endcondition)
96     XBT_WARN
97         ("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   XBT_INFO("Done.");
103   gras_exit();
104
105   return 0;
106 }                               /* end_of_server */