Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
b477365ba1ac47a02014e87b22756c280c25a720
[simgrid.git] / examples / ping / ping.c
1 /* $Id$ */
2
3 /* ping - ping/pong demo of GRAS features                                   */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2003 the OURAGAN project.                                  */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <signal.h>
14
15 #include <gras.h>
16
17 GRAS_LOG_NEW_DEFAULT_CATEGORY(Ping);
18
19 /* **********************************************************************
20  * Comon code
21  * **********************************************************************/
22
23 typedef struct {
24   int dummy;
25 } msg_ping_t;
26
27 /* Function prototypes */
28 gras_error_t register_messages(void);
29
30 /* Code */
31 gras_error_t register_messages(void) {
32   gras_error_t errcode;
33   gras_datadesc_type_t *payload_t;
34   gras_msgtype_t *msg_t;
35
36   TRY(gras_datadesc_declare_struct("msg_ping_t",&payload_t));
37   TRY(gras_datadesc_declare_struct_append(payload_t,"dummy",
38                                           gras_datadesc_by_name("int")));
39
40   TRY(gras_msgtype_declare("ping", payload_t, &msg_t)); msg_t=NULL;
41   TRY(gras_msgtype_declare("pong", payload_t, &msg_t));
42
43   return no_error;
44 }
45
46 /* **********************************************************************
47  * Server code
48  * **********************************************************************/
49
50 /* Global private data */
51 typedef struct {
52   gras_socket_t *sock;
53   int endcondition;
54 } server_data_t;
55
56 /* Function prototypes */
57 int server_cb_ping_handler(gras_socket_t        *expeditor,
58                            gras_datadesc_type_t *payload_type,
59                            void                 *payload_data);
60 int server (int argc,char *argv[]);
61
62
63 int server_cb_ping_handler(gras_socket_t        *expeditor,
64                            gras_datadesc_type_t *payload_type,
65                            void                 *payload_data) {
66                               
67   gras_error_t errcode;
68   msg_ping_t *msg=payload_data;
69   gras_msgtype_t *pong_t=NULL;
70
71   server_data_t *g=(server_data_t*)gras_userdata_get();
72
73   g->endcondition = 0;
74   INFO3("SERVER: >>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<", 
75         msg->dummy, 
76         gras_socket_peer_name(expeditor),
77         gras_socket_peer_port(expeditor));
78   
79   msg->dummy = 4321;
80   TRY(gras_msgtype_by_name("pong",&pong_t));
81   errcode = gras_msg_send(expeditor, pong_t, payload_data);
82
83   if (errcode != no_error) {
84     ERROR1("SERVER: Unable answer with PONG: %s\n", gras_error_name(errcode));
85     gras_socket_close(&(g->sock));
86     return 1;
87   }
88
89   free(msg);
90   INFO0("SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<");
91   g->endcondition = 1;
92   return 1;
93 }
94
95 int server (int argc,char *argv[]) {
96   gras_error_t errcode;
97   server_data_t *g=gras_userdata_new(server_data_t);  
98   gras_msgtype_t *ping_msg=NULL;
99
100   int port = 4000;
101   
102   if (argc == 2) {
103     port=atoi(argv[1]);
104   }
105
106   INFO1("Launch server (port=%d)", port);
107
108   if ((errcode=gras_socket_server(port,&(g->sock)))) { 
109     CRITICAL1("Error %s encountered while opening the server socket",
110               gras_error_name(errcode));
111     return 1;
112   }
113
114   TRYFAIL(register_messages());
115   TRYFAIL(register_messages());
116   TRYFAIL(gras_msgtype_by_name("ping",&ping_msg));
117   TRYFAIL(gras_cb_register(ping_msg,&server_cb_ping_handler));
118
119   INFO1("SERVER: >>>>>>>> Listening on port %d <<<<<<<<",
120         gras_socket_my_port(g->sock));
121   g->endcondition=0;
122
123   while (1) {
124     errcode = gras_msg_handle(10.0);
125     if (errcode != no_error && errcode != timeout_error) 
126       return errcode;
127     if (g->endcondition)
128       break;
129   }
130   
131   if (!gras_if_RL())
132     gras_sleep(5,0);
133   INFO0("SERVER: Done.");
134   gras_socket_close(&(g->sock));
135   return no_error;
136 }
137
138 /* **********************************************************************
139  * Client code
140  * **********************************************************************/
141
142 /* Global private data */
143 typedef struct {
144   gras_socket_t *sock;
145 } client_data_t;
146
147 /* Function prototypes */
148 int client (int argc,char *argv[]);
149
150 int client(int argc,char *argv[]) {
151   gras_error_t errcode;
152   client_data_t *g=gras_userdata_new(client_data_t);
153
154   gras_socket_t  *from;
155   msg_ping_t     *msg_ping_data, *msg_pong_data;
156   gras_msgtype_t *msg_ping_type, *msg_pong_type;
157
158   const char *host = "127.0.0.1";
159         int   port = 4000;
160
161   msg_ping_type = msg_pong_type = NULL;
162    
163   if (argc == 3) {
164     host=argv[1];
165     port=atoi(argv[2]);
166   } 
167
168   fprintf(stderr,"Launch client (server on %s:%d)",host,port);
169   if (!gras_if_RL())
170     gras_sleep(5,0); /* Wait for the server to be setup */
171   if ((errcode=gras_socket_client(host,port,&(g->sock)))) {
172     ERROR1("Client: Unable to connect to the server. Got %s",
173            gras_error_name(errcode));
174     return 1;
175   }
176   INFO2("Client: Connected to %s:%d.",host,port);    
177
178
179   TRY(register_messages());
180   TRY(gras_msgtype_by_name("ping",&msg_ping_type));
181   TRY(gras_msgtype_by_name("pong",&msg_pong_type));
182
183   INFO2("Client: >>>>>>>> Connected to server which is on %s:%d <<<<<<<<", 
184         gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
185
186   msg_ping_data = malloc(sizeof(msg_ping_t));
187   msg_ping_data->dummy = 1234;
188   errcode = gras_msg_send(g->sock, msg_ping_type, msg_ping_data);
189   if (errcode != no_error) {
190     fprintf(stderr, "Client: Unable send PING to server (%s)\n",
191             gras_error_name(errcode));
192     gras_socket_close(&(g->sock));
193     return 1;
194   }
195   INFO3("Client: >>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
196         msg_ping_data->dummy,
197         gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
198
199   msg_pong_data = NULL;
200   if ((errcode=gras_msg_wait(6000,
201                              msg_pong_type,&from,(void**)&msg_pong_data))) {
202     ERROR1("Client: Why can't I get my PONG message like everyone else (%s)?",
203            gras_error_name(errcode));
204     gras_socket_close(&(g->sock));
205     return 1;
206   }
207
208   INFO3("Client: >>>>>>>> Got PONG(%d) got from %s:%d <<<<<<<<", 
209         msg_pong_data->dummy,
210         gras_socket_peer_name(from),gras_socket_peer_port(from));
211
212   free(msg_ping_data);  
213   free(msg_pong_data);
214
215   gras_socket_close(&(g->sock));
216   INFO0("Client: Done.");
217   return 0;
218 }