Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
port the SG part to the new stuff intended to work on AIX
[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,"Messages specific to this example");
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   errcode = gras_msg_send(expeditor, gras_msgtype_by_name("pong"), &msg);
73
74   if (errcode != no_error) {
75     ERROR1("SERVER: Unable answer with PONG: %s\n", gras_error_name(errcode));
76     gras_socket_close(g->sock);
77     return 1;
78   }
79
80   INFO0("SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<");
81   g->endcondition = 1;
82   gras_socket_close(expeditor);
83   return 1;
84 }
85
86 int server (int argc,char *argv[]) {
87   gras_error_t errcode;
88   server_data_t *g;
89   gras_msgtype_t *ping_msg=NULL;
90
91   int port = 4000;
92   
93   gras_init(&argc,argv);
94   g=gras_userdata_new(server_data_t);
95    
96   if (argc == 2) {
97     port=atoi(argv[1]);
98   }
99
100   INFO1("Launch server (port=%d)", port);
101
102   if ((errcode=gras_socket_server(port,&(g->sock)))) { 
103     CRITICAL1("Error %s encountered while opening the server socket",
104               gras_error_name(errcode));
105     return 1;
106   }
107
108   TRYFAIL(register_messages());
109   TRYFAIL(register_messages());
110   TRYFAIL(gras_cb_register(gras_msgtype_by_name("ping"),&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   errcode = gras_msg_handle(600.0);
117   if (errcode != no_error)
118     return errcode;
119   if (g->endcondition)
120   
121   if (!gras_if_RL())
122     gras_os_sleep(5,0);
123   gras_socket_close(g->sock);
124   free(g);
125   gras_exit();
126   INFO0("SERVER: Done.");
127   return no_error;
128 }
129
130 /* **********************************************************************
131  * Client code
132  * **********************************************************************/
133
134 /* Global private data */
135 typedef struct {
136   gras_socket_t *sock;
137 } client_data_t;
138
139 /* Function prototypes */
140 int client (int argc,char *argv[]);
141
142 int client(int argc,char *argv[]) {
143   gras_error_t errcode;
144   client_data_t *g;
145
146   gras_socket_t  *from;
147   int ping, pong;
148
149   const char *host = "127.0.0.1";
150         int   port = 4000;
151
152   gras_init(&argc, argv);
153   g=gras_userdata_new(client_data_t);
154    
155   if (argc == 3) {
156     host=argv[1];
157     port=atoi(argv[2]);
158   } 
159
160   INFO2("Launch client (server on %s:%d)",host,port);
161   gras_os_sleep(5,0); /* Wait for the server to be setup */
162   if ((errcode=gras_socket_client(host,port,&(g->sock)))) {
163     ERROR1("Client: Unable to connect to the server. Got %s",
164            gras_error_name(errcode));
165     return 1;
166   }
167   INFO2("Client: Connected to %s:%d.",host,port);    
168
169
170   TRY(register_messages());
171
172   INFO2("Client: >>>>>>>> Connected to server which is on %s:%d <<<<<<<<", 
173         gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
174
175   ping = 1234;
176   errcode = gras_msg_send(g->sock, gras_msgtype_by_name("ping"), &ping);
177   if (errcode != no_error) {
178     fprintf(stderr, "Client: Unable send PING to server (%s)\n",
179             gras_error_name(errcode));
180     gras_socket_close(g->sock);
181     return 1;
182   }
183   INFO3("Client: >>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
184         ping,
185         gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
186
187   if ((errcode=gras_msg_wait(6000,gras_msgtype_by_name("pong"),
188                              &from,&pong))) {
189     ERROR1("Client: Why can't I get my PONG message like everyone else (%s)?",
190            gras_error_name(errcode));
191     gras_socket_close(g->sock);
192     return 1;
193   }
194
195   INFO3("Client: >>>>>>>> Got PONG(%d) got from %s:%d <<<<<<<<", 
196         pong,
197         gras_socket_peer_name(from),gras_socket_peer_port(from));
198
199   gras_socket_close(g->sock);
200   free(g);
201   gras_exit();
202   INFO0("Client: Done.");
203   return 0;
204 }