Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Let it work in SG (gras_init and gras_exit called only when needed)
[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_msgtype_t *msg_t; /* FIXME: not needed */
34
35   TRY(gras_msgtype_declare("ping", gras_datadesc_by_name("int"), &msg_t));
36   TRY(gras_msgtype_declare("pong", gras_datadesc_by_name("int"), &msg_t));
37
38   return no_error;
39 }
40
41 /* **********************************************************************
42  * Server code
43  * **********************************************************************/
44
45 /* Global private data */
46 typedef struct {
47   gras_socket_t *sock;
48   int endcondition;
49 } server_data_t;
50
51 /* Function prototypes */
52 int server_cb_ping_handler(gras_socket_t        *expeditor,
53                            void                 *payload_data);
54 int server (int argc,char *argv[]);
55
56
57 int server_cb_ping_handler(gras_socket_t        *expeditor,
58                            void                 *payload_data) {
59                              
60   gras_error_t errcode;
61   int msg=*(int*)payload_data;
62   gras_msgtype_t *pong_t=NULL;
63
64   server_data_t *g=(server_data_t*)gras_userdata_get();
65
66   g->endcondition = 0;
67   INFO3("SERVER: >>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<", 
68         msg, 
69         gras_socket_peer_name(expeditor),
70         gras_socket_peer_port(expeditor));
71   
72   msg = 4321;
73   TRY(gras_msgtype_by_name("pong",&pong_t));
74   errcode = gras_msg_send(expeditor, pong_t, &msg);
75
76   if (errcode != no_error) {
77     ERROR1("SERVER: Unable answer with PONG: %s\n", gras_error_name(errcode));
78     gras_socket_close(g->sock);
79     return 1;
80   }
81
82   INFO0("SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<");
83   g->endcondition = 1;
84   gras_socket_close(expeditor);
85   return 1;
86 }
87
88 int server (int argc,char *argv[]) {
89   gras_error_t errcode;
90   server_data_t *g=gras_userdata_new(server_data_t);  
91   gras_msgtype_t *ping_msg=NULL;
92
93   int port = 4000;
94   
95   if (argc == 2) {
96     port=atoi(argv[1]);
97   }
98
99   INFO1("Launch server (port=%d)", port);
100
101   if ((errcode=gras_socket_server(port,&(g->sock)))) { 
102     CRITICAL1("Error %s encountered while opening the server socket",
103               gras_error_name(errcode));
104     return 1;
105   }
106
107   TRYFAIL(register_messages());
108   TRYFAIL(register_messages());
109   TRYFAIL(gras_msgtype_by_name("ping",&ping_msg));
110   TRYFAIL(gras_cb_register(ping_msg,&server_cb_ping_handler));
111
112   INFO1("SERVER: >>>>>>>> Listening on port %d <<<<<<<<",
113         gras_socket_my_port(g->sock));
114   g->endcondition=0;
115
116   while (1) {
117     errcode = gras_msg_handle(10.0);
118     if (errcode != no_error && errcode != timeout_error) 
119       return errcode;
120     if (g->endcondition)
121       break;
122   }
123   
124   if (!gras_if_RL())
125     gras_sleep(5,0);
126   INFO0("SERVER: Done.");
127   gras_socket_close(g->sock);
128   free(g);
129   return no_error;
130 }
131
132 /* **********************************************************************
133  * Client code
134  * **********************************************************************/
135
136 /* Global private data */
137 typedef struct {
138   gras_socket_t *sock;
139 } client_data_t;
140
141 /* Function prototypes */
142 int client (int argc,char *argv[]);
143
144 int client(int argc,char *argv[]) {
145   gras_error_t errcode;
146   client_data_t *g=gras_userdata_new(client_data_t);
147
148   gras_socket_t  *from;
149   int ping, pong;
150   gras_msgtype_t *msg_ping_type, *msg_pong_type;
151
152   const char *host = "127.0.0.1";
153         int   port = 4000;
154
155   msg_ping_type = msg_pong_type = NULL;
156    
157   if (argc == 3) {
158     host=argv[1];
159     port=atoi(argv[2]);
160   } 
161
162   fprintf(stderr,"Launch client (server on %s:%d)",host,port);
163   if (!gras_if_RL())
164     gras_sleep(5,0); /* Wait for the server to be setup */
165   if ((errcode=gras_socket_client(host,port,&(g->sock)))) {
166     ERROR1("Client: Unable to connect to the server. Got %s",
167            gras_error_name(errcode));
168     return 1;
169   }
170   INFO2("Client: Connected to %s:%d.",host,port);    
171
172
173   TRY(register_messages());
174   TRY(gras_msgtype_by_name("ping",&msg_ping_type));
175   TRY(gras_msgtype_by_name("pong",&msg_pong_type));
176
177   INFO2("Client: >>>>>>>> Connected to server which is on %s:%d <<<<<<<<", 
178         gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
179
180   ping = 1234;
181   errcode = gras_msg_send(g->sock, msg_ping_type, &ping);
182   if (errcode != no_error) {
183     fprintf(stderr, "Client: Unable send PING to server (%s)\n",
184             gras_error_name(errcode));
185     gras_socket_close(g->sock);
186     return 1;
187   }
188   INFO3("Client: >>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
189         ping,
190         gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
191
192   if ((errcode=gras_msg_wait(6000,
193                              msg_pong_type,&from,&pong))) {
194     ERROR1("Client: Why can't I get my PONG message like everyone else (%s)?",
195            gras_error_name(errcode));
196     gras_socket_close(g->sock);
197     return 1;
198   }
199
200   INFO3("Client: >>>>>>>> Got PONG(%d) got from %s:%d <<<<<<<<", 
201         pong,
202         gras_socket_peer_name(from),gras_socket_peer_port(from));
203
204   gras_socket_close(g->sock);
205   free(g);
206   INFO0("Client: Done.");
207   return 0;
208 }