Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix make dist.
[simgrid.git] / examples / gras / ping / ping_client.c
index e362b44..c60456d 100644 (file)
@@ -9,13 +9,27 @@
 #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";
@@ -33,19 +47,8 @@ int client(int argc, char *argv[])
   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);
 
@@ -55,7 +58,7 @@ int client(int argc, char *argv[])
 
   /* 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;
@@ -67,7 +70,7 @@ int client(int argc, char *argv[])
     RETHROWF("Failed to send PING to server: %s");
   }
   XBT_INFO(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
-        ping, gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
+        ping, xbt_socket_peer_name(toserver), xbt_socket_peer_port(toserver));
 
   /* 7. Wait for the answer from the server, and deal with issues */
   TRY {
@@ -80,7 +83,7 @@ int client(int argc, char *argv[])
 
   /* 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);