#include "ping.h"
XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Ping);
-int client(int argc, char *argv[])
+static xbt_socket_t try_gras_socket_client(const char *host, int port)
{
+ volatile xbt_socket_t sock = NULL;
xbt_ex_t e;
- gras_socket_t toserver = NULL; /* peer */
- int connected = 0;
+ TRY {
+ sock = gras_socket_client(host, port);
+ }
+ CATCH(e) {
+ if (e.category != system_error)
+ /* dunno what happened, let the exception go through */
+ RETHROWF("Unable to connect to the server: %s");
+ xbt_ex_free(e);
+ }
+ return sock;
+}
- gras_socket_t from;
+int client(int argc, char *argv[])
+{
+ xbt_socket_t toserver = NULL; /* peer */
+
+ xbt_socket_t from;
int ping, pong;
const char *host = "127.0.0.1";
XBT_INFO("Launch client (server on %s:%d)", host, port);
/* 3. Create a socket to speak to the server */
- while (!connected) {
- TRY {
- toserver = gras_socket_client(host, port);
- connected = 1;
- }
- CATCH(e) {
- if (e.category != system_error)
- /* dunno what happened, let the exception go through */
- RETHROWF("Unable to connect to the server: %s");
- xbt_ex_free(e);
- gras_os_sleep(0.05);
- }
- }
+ while (!(toserver = try_gras_socket_client(host, port)))
+ gras_os_sleep(0.05);
XBT_INFO("Connected to %s:%d.", host, port);
/* 5. Keep the user informed of what's going on */
XBT_INFO(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
- gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
+ xbt_socket_peer_name(toserver), xbt_socket_peer_port(toserver));
/* 6. Prepare and send the ping message to the server */
ping = 1234;
}
XBT_INFO(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
ping,
- gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
+ xbt_socket_peer_name(toserver), xbt_socket_peer_port(toserver));
/* 7. Wait for the answer from the server, and deal with issues */
TRY {
/* 8. Keep the user informed of what's going on, again */
XBT_INFO(">>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<",
- pong, gras_socket_peer_name(from), gras_socket_peer_port(from));
+ pong, xbt_socket_peer_name(from), xbt_socket_peer_port(from));
/* 9. Free the allocated resources, and shut GRAS down */
gras_socket_close(toserver);