Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
The server timeouts after a while instead of running for ever (useful for solaris...
[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;
91   gras_msgtype_t *ping_msg=NULL;
92
93   int port = 4000;
94   
95   gras_init(&argc,argv);
96   g=gras_userdata_new(server_data_t);
97    
98   if (argc == 2) {
99     port=atoi(argv[1]);
100   }
101
102   INFO1("Launch server (port=%d)", port);
103
104   if ((errcode=gras_socket_server(port,&(g->sock)))) { 
105     CRITICAL1("Error %s encountered while opening the server socket",
106               gras_error_name(errcode));
107     return 1;
108   }
109
110   TRYFAIL(register_messages());
111   TRYFAIL(register_messages());
112   TRYFAIL(gras_msgtype_by_name("ping",&ping_msg));
113   TRYFAIL(gras_cb_register(ping_msg,&server_cb_ping_handler));
114
115   INFO1("SERVER: >>>>>>>> Listening on port %d <<<<<<<<",
116         gras_socket_my_port(g->sock));
117   g->endcondition=0;
118
119   errcode = gras_msg_handle(600.0);
120   if (errcode != no_error)
121     return errcode;
122   if (g->endcondition)
123   
124   if (!gras_if_RL())
125     gras_sleep(5,0);
126   gras_socket_close(g->sock);
127   free(g);
128   gras_exit();
129   INFO0("SERVER: Done.");
130   return no_error;
131 }
132
133 /* **********************************************************************
134  * Client code
135  * **********************************************************************/
136
137 /* Global private data */
138 typedef struct {
139   gras_socket_t *sock;
140 } client_data_t;
141
142 /* Function prototypes */
143 int client (int argc,char *argv[]);
144
145 int client(int argc,char *argv[]) {
146   gras_error_t errcode;
147   client_data_t *g;
148
149   gras_socket_t  *from;
150   int ping, pong;
151   gras_msgtype_t *msg_ping_type=NULL, *msg_pong_type=NULL;
152
153   const char *host = "127.0.0.1";
154         int   port = 4000;
155
156   gras_init(&argc, argv);
157   g=gras_userdata_new(client_data_t);
158    
159   if (argc == 3) {
160     host=argv[1];
161     port=atoi(argv[2]);
162   } 
163
164   INFO2("Launch client (server on %s:%d)",host,port);
165   if (!gras_if_RL())
166     gras_sleep(5,0); /* Wait for the server to be setup */
167   if ((errcode=gras_socket_client(host,port,&(g->sock)))) {
168     ERROR1("Client: Unable to connect to the server. Got %s",
169            gras_error_name(errcode));
170     return 1;
171   }
172   INFO2("Client: Connected to %s:%d.",host,port);    
173
174
175   TRY(register_messages());
176   TRY(gras_msgtype_by_name("ping",&msg_ping_type));
177   TRY(gras_msgtype_by_name("pong",&msg_pong_type));
178
179   INFO2("Client: >>>>>>>> Connected to server which is on %s:%d <<<<<<<<", 
180         gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
181
182   ping = 1234;
183   errcode = gras_msg_send(g->sock, msg_ping_type, &ping);
184   if (errcode != no_error) {
185     fprintf(stderr, "Client: Unable send PING to server (%s)\n",
186             gras_error_name(errcode));
187     gras_socket_close(g->sock);
188     return 1;
189   }
190   INFO3("Client: >>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
191         ping,
192         gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
193
194   if ((errcode=gras_msg_wait(6000,
195                              msg_pong_type,&from,&pong))) {
196     ERROR1("Client: Why can't I get my PONG message like everyone else (%s)?",
197            gras_error_name(errcode));
198     gras_socket_close(g->sock);
199     return 1;
200   }
201
202   INFO3("Client: >>>>>>>> Got PONG(%d) got from %s:%d <<<<<<<<", 
203         pong,
204         gras_socket_peer_name(from),gras_socket_peer_port(from));
205
206   gras_socket_close(g->sock);
207   free(g);
208   gras_exit();
209   INFO0("Client: Done.");
210   return 0;
211 }