Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Specify the buffer size before allocating them, to avoid the pain of malloc(0)
[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
34   TRY(gras_msgtype_declare("ping", gras_datadesc_by_name("int")));
35   TRY(gras_msgtype_declare("pong", gras_datadesc_by_name("int")));
36
37   return no_error;
38 }
39
40 /* **********************************************************************
41  * Server code
42  * **********************************************************************/
43
44 /* Global private data */
45 typedef struct {
46   gras_socket_t *sock;
47   int endcondition;
48 } server_data_t;
49
50 /* Function prototypes */
51 int server_cb_ping_handler(gras_socket_t        *expeditor,
52                            void                 *payload_data);
53 int server (int argc,char *argv[]);
54
55
56 int server_cb_ping_handler(gras_socket_t        *expeditor,
57                            void                 *payload_data) {
58                              
59   gras_error_t errcode;
60   int msg=*(int*)payload_data;
61   gras_msgtype_t *pong_t=NULL;
62
63   server_data_t *g=(server_data_t*)gras_userdata_get();
64
65   g->endcondition = 0;
66   INFO3("SERVER: >>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<", 
67         msg, 
68         gras_socket_peer_name(expeditor),
69         gras_socket_peer_port(expeditor));
70   
71   msg = 4321;
72   TRY(gras_msgtype_by_name("pong",&pong_t));
73   errcode = gras_msg_send(expeditor, pong_t, &msg);
74
75   if (errcode != no_error) {
76     ERROR1("SERVER: Unable answer with PONG: %s\n", gras_error_name(errcode));
77     gras_socket_close(g->sock);
78     return 1;
79   }
80
81   INFO0("SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<");
82   g->endcondition = 1;
83   gras_socket_close(expeditor);
84   return 1;
85 }
86
87 int server (int argc,char *argv[]) {
88   gras_error_t errcode;
89   server_data_t *g;
90   gras_msgtype_t *ping_msg=NULL;
91
92   int port = 4000;
93   
94   gras_init(&argc,argv);
95   g=gras_userdata_new(server_data_t);
96    
97   if (argc == 2) {
98     port=atoi(argv[1]);
99   }
100
101   INFO1("Launch server (port=%d)", port);
102
103   if ((errcode=gras_socket_server(port,&(g->sock)))) { 
104     CRITICAL1("Error %s encountered while opening the server socket",
105               gras_error_name(errcode));
106     return 1;
107   }
108
109   TRYFAIL(register_messages());
110   TRYFAIL(register_messages());
111   TRYFAIL(gras_msgtype_by_name("ping",&ping_msg));
112   TRYFAIL(gras_cb_register(ping_msg,&server_cb_ping_handler));
113
114   INFO1("SERVER: >>>>>>>> Listening on port %d <<<<<<<<",
115         gras_socket_my_port(g->sock));
116   g->endcondition=0;
117
118   errcode = gras_msg_handle(600.0);
119   if (errcode != no_error)
120     return errcode;
121   if (g->endcondition)
122   
123   if (!gras_if_RL())
124     gras_os_sleep(5,0);
125   gras_socket_close(g->sock);
126   free(g);
127   gras_exit();
128   INFO0("SERVER: Done.");
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;
147
148   gras_socket_t  *from;
149   int ping, pong;
150   gras_msgtype_t *msg_ping_type=NULL, *msg_pong_type=NULL;
151
152   const char *host = "127.0.0.1";
153         int   port = 4000;
154
155   gras_init(&argc, argv);
156   g=gras_userdata_new(client_data_t);
157    
158   if (argc == 3) {
159     host=argv[1];
160     port=atoi(argv[2]);
161   } 
162
163   INFO2("Launch client (server on %s:%d)",host,port);
164   gras_os_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   gras_exit();
207   INFO0("Client: Done.");
208   return 0;
209 }