3 /* ping - ping/pong demo of GRAS features */
5 /* Copyright (c) 2003, 2004, 2005 Martin Quinson. All rights reserved. */
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. */
12 XBT_LOG_NEW_DEFAULT_CATEGORY(Ping,"Messages specific to this example");
14 /* register messages which may be sent (common to client and server) */
15 static void register_messages(void) {
16 gras_msgtype_declare("ping", gras_datadesc_by_name("int"));
17 gras_msgtype_declare("pong", gras_datadesc_by_name("int"));
20 /* Function prototypes */
21 int server (int argc,char *argv[]);
22 int client (int argc,char *argv[]);
24 /* **********************************************************************
26 * **********************************************************************/
28 /* Global private data */
35 static int server_cb_ping_handler(gras_socket_t expeditor,
39 /* 1. Get the payload into the msg variable */
40 int msg=*(int*)payload_data;
42 /* 2. Retrieve the server's state (globals) */
44 server_data_t *globals=(server_data_t*)gras_userdata_get();
45 globals->endcondition = 0;
47 /* 3. Log which client connected */
48 INFO3(">>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<",
50 gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor));
52 /* 4. Change the value of the msg variable */
54 /* 5. Send it back as payload of a pong message to the expeditor */
55 errcode = gras_msg_send(expeditor, gras_msgtype_by_name("pong"), &msg);
57 /* 6. Deal with errors */
58 if (errcode != no_error) {
59 ERROR1("Unable answer with PONG: %s\n", xbt_error_name(errcode));
60 gras_socket_close(globals->sock);
64 INFO0(">>>>>>>> Answered with PONG(4321) <<<<<<<<");
66 /* 7. Set the endcondition boolean to true (and make sure the server stops after receiving it). */
67 globals->endcondition = 1;
69 /* 8. Make sure we don't leak sockets */
70 gras_socket_close(expeditor);
72 /* 9. Tell GRAS that we consummed this message */
74 } /* end_of_server_cb_ping_handler */
76 int server (int argc,char *argv[]) {
78 server_data_t *globals;
82 /* 1. Init the GRAS infrastructure and declare my globals */
83 gras_init(&argc,argv, NULL);
84 globals=gras_userdata_new(server_data_t);
86 /* 2. Get the port I should listen on from the command line, if specified */
91 INFO1("Launch server (port=%d)", port);
93 /* 3. Create my master socket */
94 if ((errcode=gras_socket_server(port,&(globals->sock)))) {
95 CRITICAL1("Error %s encountered while opening the server socket",
96 xbt_error_name(errcode));
100 /* 4. Register the known messages. This function is called twice here, but it's because
101 this file also acts as regression test, no need to do so yourself of course. */
103 register_messages(); /* just to make sure it works ;) */
105 /* 5. Register my callback */
106 gras_cb_register(gras_msgtype_by_name("ping"),&server_cb_ping_handler);
108 INFO1(">>>>>>>> Listening on port %d <<<<<<<<", gras_socket_my_port(globals->sock));
109 globals->endcondition=0;
111 /* 6. Wait up to 10 minutes for an incomming message to handle */
112 errcode = gras_msg_handle(600.0);
114 /* 7. Housekeeping */
115 if (errcode != no_error)
117 if (!globals->endcondition)
118 WARN0("An error occured, the endcondition was not set by the callback");
120 /* 8. Free the allocated resources, and shut GRAS down */
121 gras_socket_close(globals->sock);
127 } /* end_of_server */
129 /* **********************************************************************
131 * **********************************************************************/
133 /* Function prototypes */
135 int client(int argc,char *argv[]) {
137 gras_socket_t toserver; /* peer */
142 const char *host = "127.0.0.1";
145 /* 1. Init the GRAS's infrastructure */
146 gras_init(&argc, argv, NULL);
148 /* 2. Get the server's address. The command line override defaults when specified */
154 INFO2("Launch client (server on %s:%d)",host,port);
156 /* 3. Wait for the server startup */
159 /* 4. Create a socket to speak to the server */
160 if ((errcode=gras_socket_client(host,port,&toserver))) {
161 ERROR1("Unable to connect to the server. Got %s",
162 xbt_error_name(errcode));
165 INFO2("Connected to %s:%d.",host,port);
168 /* 5. Register the messages.
169 See, it doesn't have to be done completely at the beginning, only before use */
172 /* 6. Keep the user informed of what's going on */
173 INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
174 gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
176 /* 7. Prepare and send the ping message to the server */
178 errcode = gras_msg_send(toserver, gras_msgtype_by_name("ping"), &ping);
179 if (errcode != no_error) {
180 fprintf(stderr, "Unable send PING to server (%s)\n",
181 xbt_error_name(errcode));
182 gras_socket_close(toserver);
185 INFO3(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
187 gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
189 /* 8. Wait for the answer from the server, and deal with issues */
190 if ((errcode=gras_msg_wait(6000,gras_msgtype_by_name("pong"),
192 ERROR1("Why can't I get my PONG message like everyone else (%s)?",
193 xbt_error_name(errcode));
194 gras_socket_close(toserver);
198 /* 9. Keep the user informed of what's going on, again */
199 INFO3(">>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<",
201 gras_socket_peer_name(from),gras_socket_peer_port(from));
203 /* 10. Free the allocated resources, and shut GRAS down */
204 gras_socket_close(toserver);
208 } /* end_of_client */