Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ea28d6896b9630728cc2a89fe091f021aad3f5f9
[simgrid.git] / examples / gras / ping / ping.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 "gras.h"
11
12 XBT_LOG_NEW_DEFAULT_CATEGORY(Ping,"Messages specific to this example");
13
14 /* register messages which may be sent (common to client and server) */
15 static void register_messages(void) {
16   gras_msgtype_declare("ping", gras_datadesc_by_name("int"));
17   gras_msgtype_declare("pong", gras_datadesc_by_name("int"));
18 }
19
20 /* Function prototypes */
21 int server (int argc,char *argv[]);
22 int client (int argc,char *argv[]);
23
24 /* **********************************************************************
25  * Server code
26  * **********************************************************************/
27
28 /* Global private data */
29 typedef struct {
30   gras_socket_t sock;
31   int endcondition;
32 } server_data_t;
33
34
35 static int server_cb_ping_handler(gras_socket_t  expeditor,
36                                   void          *payload_data) {
37                              
38   xbt_error_t errcode;
39   /* 1. Get the payload into the msg variable */
40   int msg=*(int*)payload_data;
41
42   /* 2. Retrieve the server's state (globals) */
43
44   server_data_t *globals=(server_data_t*)gras_userdata_get();
45   globals->endcondition = 0;
46    
47   /* 3. Log which client connected */
48   INFO3("SERVER: >>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<", 
49         msg, 
50         gras_socket_peer_name(expeditor), gras_socket_peer_port(expeditor));
51   
52   /* 4. Change the value of the msg variable */
53   msg = 4321;
54   /* 5. Send it back as payload of a pong message to the expeditor */
55   errcode = gras_msg_send(expeditor, gras_msgtype_by_name("pong"), &msg);
56
57   /* 6. Deal with errors */
58   if (errcode != no_error) {
59     ERROR1("SERVER: Unable answer with PONG: %s\n", xbt_error_name(errcode));
60     gras_socket_close(globals->sock);
61     return 1;
62   }
63
64   INFO0("SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<");
65    
66   /* 7. Set the endcondition boolean to true (and make sure the server stops after receiving it). */
67   globals->endcondition = 1;
68   
69   /* 8. Make sure we don't leak sockets */
70   gras_socket_close(expeditor);
71    
72   /* 9. Tell GRAS that we consummed this message */
73   return 1;
74 } /* end_of_server_cb_ping_handler */
75
76 int server (int argc,char *argv[]) {
77   xbt_error_t errcode;
78   server_data_t *globals;
79
80   int port = 4000;
81   
82   /* 1. Init the GRAS infrastructure and declare my globals */
83   gras_init(&argc,argv, NULL);
84   globals=gras_userdata_new(server_data_t);
85    
86   /* 2. Get the port I should listen on from the command line, if specified */
87   if (argc == 2) {
88     port=atoi(argv[1]);
89   }
90
91   INFO1("Launch server (port=%d)", port);
92
93   /* 3. Create my master socket */
94   if ((errcode=gras_socket_server(port,&(globals->sock)))) { 
95     CRITICAL1("Error %s encountered while opening the server socket",
96               xbt_error_name(errcode));
97     return 1;
98   }
99
100   /* 4. Register the known messages. This function is called twice here, but it's because
101         this file also acts as regression test, no need to do so yourself of course. */
102   register_messages();
103   register_messages(); /* just to make sure it works ;) */
104    
105   /* 5. Register my callback */
106   gras_cb_register(gras_msgtype_by_name("ping"),&server_cb_ping_handler);
107
108   INFO1("SERVER: >>>>>>>> Listening on port %d <<<<<<<<", gras_socket_my_port(globals->sock));
109   globals->endcondition=0;
110
111   /* 6. Wait up to 10 minutes for an incomming message to handle */
112   errcode = gras_msg_handle(600.0);
113    
114   /* 7. Housekeeping */
115   if (errcode != no_error)
116     return errcode;
117   if (!globals->endcondition)
118      WARN0("An error occured, the endcondition was not set by the callback");
119   
120   /* 8. Sleep one second, but only in real life, not in simulation */
121   if (!gras_if_RL())
122     gras_os_sleep(1, 0);
123
124   /* 9. Free the allocated resources, and shut GRAS down */
125   gras_socket_close(globals->sock);
126   free(globals);
127   gras_exit();
128    
129   INFO0("SERVER: Done.");
130   return no_error;
131 } /* end_of_server */
132
133 /* **********************************************************************
134  * Client code
135  * **********************************************************************/
136
137 /* Function prototypes */
138
139 int client(int argc,char *argv[]) {
140   xbt_error_t errcode; 
141   gras_socket_t toserver; /* peer */
142
143   gras_socket_t from;
144   int ping, pong;
145
146   const char *host = "127.0.0.1";
147         int   port = 4000;
148
149   /* 1. Init the GRAS's infrastructure */
150   gras_init(&argc, argv, NULL);
151    
152   /* 2. Get the server's address. The command line override defaults when specified */
153   if (argc == 3) {
154     host=argv[1];
155     port=atoi(argv[2]);
156   } 
157
158   INFO2("Launch client (server on %s:%d)",host,port);
159    
160   /* 3. Wait for the server startup */
161   gras_os_sleep(1,0);
162    
163   /* 4. Create a socket to speak to the server */
164   if ((errcode=gras_socket_client(host,port,&toserver))) {
165     ERROR1("Client: Unable to connect to the server. Got %s",
166            xbt_error_name(errcode));
167     return 1;
168   }
169   INFO2("Client: Connected to %s:%d.",host,port);    
170
171
172   /* 5. Register the messages. 
173         See, it doesn't have to be done completely at the beginning, only before use */
174   register_messages();
175
176   /* 6. Keep the user informed of what's going on */
177   INFO2("Client: >>>>>>>> Connected to server which is on %s:%d <<<<<<<<", 
178         gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
179
180   /* 7. Prepare and send the ping message to the server */
181   ping = 1234;
182   errcode = gras_msg_send(toserver, gras_msgtype_by_name("ping"), &ping);
183   if (errcode != no_error) {
184     fprintf(stderr, "Client: Unable send PING to server (%s)\n",
185             xbt_error_name(errcode));
186     gras_socket_close(toserver);
187     return 1;
188   }
189   INFO3("Client: >>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
190         ping,
191         gras_socket_peer_name(toserver),gras_socket_peer_port(toserver));
192
193   /* 8. Wait for the answer from the server, and deal with issues */
194   if ((errcode=gras_msg_wait(6000,gras_msgtype_by_name("pong"),
195                              &from,&pong))) {
196     ERROR1("Client: Why can't I get my PONG message like everyone else (%s)?",
197            xbt_error_name(errcode));
198     gras_socket_close(toserver);
199     return 1;
200   }
201
202   /* 9. Keep the user informed of what's going on, again */
203   INFO3("Client: >>>>>>>> Got PONG(%d) from %s:%d <<<<<<<<", 
204         pong,
205         gras_socket_peer_name(from),gras_socket_peer_port(from));
206
207   /* 10. Free the allocated resources, and shut GRAS down */
208   gras_socket_close(toserver);
209   gras_exit();
210   INFO0("Client: Done.");
211   return 0;
212 } /* end_of_client */