Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix make dist.
[simgrid.git] / examples / gras / ping / ping_client.c
1 /* ping - ping/pong demo of GRAS features                                   */
2
3 /* Copyright (c) 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
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. */
8
9 #include "ping.h"
10 XBT_LOG_EXTERNAL_DEFAULT_CATEGORY(Ping);
11
12 static xbt_socket_t try_gras_socket_client(const char *host, int port)
13 {
14   volatile xbt_socket_t sock = NULL;
15   xbt_ex_t e;
16   TRY {
17     sock = gras_socket_client(host, port);
18   }
19   CATCH(e) {
20     if (e.category != system_error)
21       /* dunno what happened, let the exception go through */
22       RETHROWF("Unable to connect to the server: %s");
23     xbt_ex_free(e);
24   }
25   return sock;
26 }
27
28 int client(int argc, char *argv[])
29 {
30   xbt_socket_t toserver = NULL;        /* peer */
31
32   xbt_socket_t from;
33   int ping, pong;
34
35   const char *host = "127.0.0.1";
36   int port = 4000;
37
38   /* 1. Init the GRAS's infrastructure */
39   gras_init(&argc, argv);
40
41   /* 2. Get the server's address. The command line override defaults when specified */
42   if (argc == 3) {
43     host = argv[1];
44     port = atoi(argv[2]);
45   }
46
47   XBT_INFO("Launch client (server on %s:%d)", host, port);
48
49   /* 3. Create a socket to speak to the server */
50   while (!(toserver = try_gras_socket_client(host, port)))
51     gras_os_sleep(0.05);
52
53   XBT_INFO("Connected to %s:%d.", host, port);
54
55   /* 4. Register the messages.
56      See, it doesn't have to be done completely at the beginning, only before use */
57   ping_register_messages();
58
59   /* 5. Keep the user informed of what's going on */
60   XBT_INFO(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
61         xbt_socket_peer_name(toserver), xbt_socket_peer_port(toserver));
62
63   /* 6. Prepare and send the ping message to the server */
64   ping = 1234;
65   TRY {
66     gras_msg_send(toserver, "ping", &ping);
67   }
68   CATCH_ANONYMOUS {
69     gras_socket_close(toserver);
70     RETHROWF("Failed to send PING to server: %s");
71   }
72   XBT_INFO(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
73         ping, xbt_socket_peer_name(toserver), xbt_socket_peer_port(toserver));
74
75   /* 7. Wait for the answer from the server, and deal with issues */
76   TRY {
77     gras_msg_wait(6000, "pong", &from, &pong);
78   }
79   CATCH_ANONYMOUS {
80     gras_socket_close(toserver);
81     RETHROWF("Why can't I get my PONG message like everyone else: %s");
82   }
83
84   /* 8. Keep the user informed of what's going on, again */
85   XBT_INFO(">>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<",
86         pong, xbt_socket_peer_name(from), xbt_socket_peer_port(from));
87
88   /* 9. Free the allocated resources, and shut GRAS down */
89   gras_socket_close(toserver);
90   XBT_INFO("Done.");
91   gras_exit();
92   return 0;
93 }                               /* end_of_client */