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. */
11 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Ping);
13 int client(int argc,char *argv[]) {
15 gras_socket_t toserver=NULL; /* peer */
21 const char *host = "127.0.0.1";
24 /* 1. Init the GRAS's infrastructure */
25 gras_init(&argc, argv);
27 /* 2. Get the server's address. The command line override defaults when specified */
33 INFO2("Launch client (server on %s:%d)",host,port);
35 /* 3. Wait for the server startup */
38 /* 4. Create a socket to speak to the server */
41 toserver=gras_socket_client(host,port);
44 if (e.category != system_error)
45 /* dunno what happened, let the exception go through */
46 RETHROW0("Unable to connect to the server: %s");
52 INFO2("Connected to %s:%d.",host,port);
54 /* 5. Register the messages.
55 See, it doesn't have to be done completely at the beginning, only before use */
56 ping_register_messages();
58 /* 6. Keep the user informed of what's going on */
59 INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
60 gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
62 /* 7. Prepare and send the ping message to the server */
65 gras_msg_send(toserver, "ping", &ping);
67 gras_socket_close(toserver);
68 RETHROW0("Failed to send PING to server: %s");
70 INFO3(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
72 gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
74 /* 8. Wait for the answer from the server, and deal with issues */
76 gras_msg_wait(6000,"pong", &from,&pong);
78 gras_socket_close(toserver);
79 RETHROW0("Why can't I get my PONG message like everyone else: %s");
82 /* 9. Keep the user informed of what's going on, again */
83 INFO3(">>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<",
85 gras_socket_peer_name(from),gras_socket_peer_port(from));
87 /* 10. Free the allocated resources, and shut GRAS down */
88 gras_socket_close(toserver);