1 /* ping - ping/pong demo of GRAS features */
3 /* Copyright (c) 2003, 2004, 2005 Martin Quinson. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
9 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Ping);
11 int client(int argc, char *argv[])
14 gras_socket_t toserver = NULL; /* peer */
20 const char *host = "127.0.0.1";
23 /* 1. Init the GRAS's infrastructure */
24 gras_init(&argc, argv);
26 /* 2. Get the server's address. The command line override defaults when specified */
32 INFO2("Launch client (server on %s:%d)", host, port);
34 /* 3. Create a socket to speak to the server */
37 toserver = gras_socket_client(host, port);
41 if (e.category != system_error)
42 /* dunno what happened, let the exception go through */
43 RETHROW0("Unable to connect to the server: %s");
49 INFO2("Connected to %s:%d.", host, port);
51 /* 4. Register the messages.
52 See, it doesn't have to be done completely at the beginning, only before use */
53 ping_register_messages();
55 /* 5. Keep the user informed of what's going on */
56 INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
57 gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
59 /* 6. Prepare and send the ping message to the server */
62 gras_msg_send(toserver, "ping", &ping);
65 gras_socket_close(toserver);
66 RETHROW0("Failed to send PING to server: %s");
68 INFO3(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
70 gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
72 /* 7. Wait for the answer from the server, and deal with issues */
74 gras_msg_wait(6000, "pong", &from, &pong);
77 gras_socket_close(toserver);
78 RETHROW0("Why can't I get my PONG message like everyone else: %s");
81 /* 8. Keep the user informed of what's going on, again */
82 INFO3(">>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<",
83 pong, gras_socket_peer_name(from), gras_socket_peer_port(from));
85 /* 9. Free the allocated resources, and shut GRAS down */
86 gras_socket_close(toserver);