Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix warnings about clobbered variables in gras/ping example.
[simgrid.git] / examples / gras / ping / ping_client.c
index 1c34881..d9aab2c 100644 (file)
@@ -9,11 +9,25 @@
 #include "ping.h"
 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Ping);
 
-int client(int argc, char *argv[])
+static gras_socket_t try_gras_socket_client(const char *host, int port)
 {
+  volatile gras_socket_t sock = NULL;
   xbt_ex_t e;
+  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;
+}
+
+int client(int argc, char *argv[])
+{
   gras_socket_t toserver = NULL;        /* peer */
-  int connected = 0;
 
   gras_socket_t from;
   int ping, pong;
@@ -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 */
-        RETHROW0("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);
 
@@ -62,9 +65,9 @@ int client(int argc, char *argv[])
   TRY {
     gras_msg_send(toserver, "ping", &ping);
   }
-  CATCH(e) {
+  CATCH_ANONYMOUS {
     gras_socket_close(toserver);
-    RETHROW0("Failed to send PING to server: %s");
+    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));
@@ -73,9 +76,9 @@ int client(int argc, char *argv[])
   TRY {
     gras_msg_wait(6000, "pong", &from, &pong);
   }
-  CATCH(e) {
+  CATCH_ANONYMOUS {
     gras_socket_close(toserver);
-    RETHROW0("Why can't I get my PONG message like everyone else: %s");
+    RETHROWF("Why can't I get my PONG message like everyone else: %s");
   }
 
   /* 8. Keep the user informed of what's going on, again */