Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Providing up-to-date xml files... Version 1 with units in Bytes, Flops and Seconds.
[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   xbt_ex_t e; 
15   gras_socket_t toserver=NULL; /* peer */
16
17   gras_socket_t from;
18   int ping, pong;
19
20   const char *host = "127.0.0.1";
21         int   port = 4000;
22
23   /* 1. Init the GRAS's infrastructure */
24   gras_init(&argc, argv);
25    
26   /* 2. Get the server's address. The command line override defaults when specified */
27   if (argc == 3) {
28     host=argv[1];
29     port=atoi(argv[2]);
30   } 
31
32   INFO2("Launch client (server on %s:%d)",host,port);
33    
34   /* 3. Wait for the server startup */
35   gras_os_sleep(1);
36    
37   /* 4. Create a socket to speak to the server */
38   TRY {
39     toserver=gras_socket_client(host,port);
40   } CATCH(e) {
41     RETHROW0("Unable to connect to the server: %s");
42   }
43   INFO2("Connected to %s:%d.",host,port);    
44
45
46   /* 5. Register the messages. 
47         See, it doesn't have to be done completely at the beginning, only before use */
48   ping_register_messages();
49
50   /* 6. Keep the user informed of what's going on */
51   INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<", 
52         gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
53
54   /* 7. Prepare and send the ping message to the server */
55   ping = 1234;
56   TRY {
57     gras_msg_send(toserver, gras_msgtype_by_name("ping"), &ping);
58   } CATCH(e) {
59     gras_socket_close(toserver);
60     RETHROW0("Failed to send PING to server: %s");
61   }
62   INFO3(">>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
63         ping,
64         gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
65
66   /* 8. Wait for the answer from the server, and deal with issues */
67   TRY {
68     gras_msg_wait(6000,gras_msgtype_by_name("pong"),
69                   &from,&pong);
70   } CATCH(e) {
71     gras_socket_close(toserver);
72     RETHROW0("Why can't I get my PONG message like everyone else: %s");
73   }
74
75   /* 9. Keep the user informed of what's going on, again */
76   INFO3(">>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<", 
77         pong,
78         gras_socket_peer_name(from),gras_socket_peer_port(from));
79
80   /* 10. Free the allocated resources, and shut GRAS down */
81   gras_socket_close(toserver);
82   gras_exit();
83   INFO0("Done.");
84   return 0;
85 } /* end_of_client */