Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Biggest commit ever (SIMIX2): the user processes can now run in parallel
[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 int client(int argc, char *argv[])
13 {
14   xbt_ex_t e;
15   gras_socket_t toserver = NULL;        /* peer */
16   int connected = 0;
17
18   gras_socket_t from;
19   int ping, pong;
20
21   const char *host = "127.0.0.1";
22   int port = 4000;
23
24   /* 1. Init the GRAS's infrastructure */
25   gras_init(&argc, argv);
26
27   /* 2. Get the server's address. The command line override defaults when specified */
28   if (argc == 3) {
29     host = argv[1];
30     port = atoi(argv[2]);
31   }
32
33   INFO2("Launch client (server on %s:%d)", host, port);
34
35   /* 3. Create a socket to speak to the server */
36   while (!connected) {
37     TRY {
38       toserver = gras_socket_client(host, port);
39       connected = 1;
40     }
41     CATCH(e) {
42       if (e.category != system_error)
43         /* dunno what happened, let the exception go through */
44         RETHROW0("Unable to connect to the server: %s");
45       xbt_ex_free(e);
46       gras_os_sleep(0.05);
47     }
48   }
49
50   INFO2("Connected to %s:%d.", host, port);
51
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();
55
56   /* 5. Keep the user informed of what's going on */
57   INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
58         gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
59
60   /* 6. Prepare and send the ping message to the server */
61   ping = 1234;
62   TRY {
63     gras_msg_send(toserver, "ping", &ping);
64   }
65   CATCH(e) {
66     gras_socket_close(toserver);
67     RETHROW0("Failed to send PING to server: %s");
68   }
69   INFO3(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
70         ping, gras_socket_peer_name(toserver), gras_socket_peer_port(toserver));
71
72   /* 7. Wait for the answer from the server, and deal with issues */
73   TRY {
74     gras_msg_wait(6000, "pong", &from, &pong);
75   }
76   CATCH(e) {
77     gras_socket_close(toserver);
78     RETHROW0("Why can't I get my PONG message like everyone else: %s");
79   }
80
81   /* 8. Keep the user informed of what's going on, again */
82   INFO3(">>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<",
83         pong, gras_socket_peer_name(from), gras_socket_peer_port(from));
84
85   /* 9. Free the allocated resources, and shut GRAS down */
86   gras_socket_close(toserver);
87   INFO0("Done.");
88   gras_exit();
89   return 0;
90 }                               /* end_of_client */