1 /* ping - ping/pong demo of GRAS features */
3 /* Copyright (c) 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
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. */
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Ping);
12 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 XBT_INFO("Launch client (server on %s:%d)", host, port);
35 /* 3. Create a socket to speak to the server */
38 toserver = gras_socket_client(host, port);
42 if (e.category != system_error)
43 /* dunno what happened, let the exception go through */
44 RETHROWF("Unable to connect to the server: %s");
50 XBT_INFO("Connected to %s:%d.", host, port);
52 /* 4. Register the messages.
53 See, it doesn't have to be done completely at the beginning, only before use */
54 ping_register_messages();
56 /* 5. Keep the user informed of what's going on */
57 XBT_INFO(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
58 gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
60 /* 6. Prepare and send the ping message to the server */
63 gras_msg_send(toserver, "ping", &ping);
66 gras_socket_close(toserver);
67 RETHROWF("Failed to send PING to server: %s");
69 XBT_INFO(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
71 gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
73 /* 7. Wait for the answer from the server, and deal with issues */
75 gras_msg_wait(6000, "pong", &from, &pong);
78 gras_socket_close(toserver);
79 RETHROWF("Why can't I get my PONG message like everyone else: %s");
82 /* 8. Keep the user informed of what's going on, again */
83 XBT_INFO(">>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<",
84 pong, gras_socket_peer_name(from), gras_socket_peer_port(from));
86 /* 9. Free the allocated resources, and shut GRAS down */
87 gras_socket_close(toserver);