Logo AND Algorithmique Numérique Distribuée

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