Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
6395d51eb4f51022cca231ae795c235312c5cc1b
[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 /* **********************************************************************
18  * Comon code
19  * **********************************************************************/
20 /* message definition */
21 #define MSG_PING 1024
22 #define MSG_PONG 1025
23
24 typedef struct {
25   int dummy;
26 } msgPing_t;
27
28 static const DataDescriptor msgPingDesc[] = 
29   {SIMPLE_MEMBER(INT_TYPE,1,offsetof(msgPing_t,dummy))};
30 #define msgPingLength 1
31
32 typedef msgPing_t msgPong_t;
33 static const DataDescriptor msgPongDesc[] = 
34   {SIMPLE_MEMBER(INT_TYPE,1,offsetof(msgPong_t,dummy))};
35 #define msgPongLength 1
36
37 /* Function prototypes */
38 gras_error_t register_messages(const char *prefix);
39
40 /* Code */
41 gras_error_t register_messages(const char *prefix) {
42   gras_error_t errcode;
43
44   if ((errcode=gras_msgtype_register(MSG_PING,"ping",1,msgPingDesc,msgPingLength)) ||
45       (errcode=gras_msgtype_register(MSG_PONG,"pong",1,msgPongDesc,msgPongLength))) {
46     fprintf(stderr,"%s: Unable register the messages (got error %s)\n",
47             prefix,gras_error_name(errcode));
48     return errcode;
49   }
50   return no_error;
51 }
52
53 /* **********************************************************************
54  * Server code
55  * **********************************************************************/
56
57 /* Global private data */
58 typedef struct {
59   gras_sock_t *sock;
60   int endcondition;
61 } server_data_t;
62
63 /* Function prototypes */
64 int server_cbPingHandler(gras_msg_t *msg);
65 int server (int argc,char *argv[]);
66
67
68 int server_cbPingHandler(gras_msg_t *msg) {
69   int *dummy=malloc(sizeof(int*));
70   server_data_t *g=(server_data_t*)gras_userdata_get();
71
72   g->endcondition = 0;
73   *dummy=4321;
74   fprintf (stderr,"SERVER: >>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<\n", 
75            gras_msg_ctn(msg,0,0,int), gras_sock_get_peer_name(msg->sock),gras_sock_get_peer_port(msg->sock));
76   
77   if (gras_msg_new_and_send(msg->sock,MSG_PONG,1,   dummy,1)) {
78     fprintf(stderr,"SERVER: Unable answer with PONG\n");
79     gras_sock_close(g->sock);
80     return 1;
81   }
82
83   gras_msg_free(msg);
84   fprintf(stderr,"SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<\n");
85   g->endcondition = 1;
86   return 1;
87 }
88
89 int server (int argc,char *argv[]) {
90   gras_error_t errcode;
91   server_data_t *g=gras_userdata_new(server_data_t);  
92
93   if ((errcode=gras_sock_server_open(4000,4000,&(g->sock)))) { 
94     fprintf(stderr,"SERVER: Error %s encountered while opening the server socket\n",gras_error_name(errcode));
95     return 1;
96   }
97
98   if (register_messages("SERVER") || 
99       gras_cb_register(MSG_PING,-1,&server_cbPingHandler)) {
100     gras_sock_close(g->sock);
101     return 1;
102   }
103
104   fprintf(stderr,"SERVER: >>>>>>>> Listening on port %d <<<<<<<<\n",gras_sock_get_my_port(g->sock));
105   g->endcondition=0;
106
107   while (!g->endcondition) {
108     if ((errcode=gras_msg_handle(60.0))) {
109       fprintf(stderr,"SERVER: Error '%s' while handling message\n",gras_error_name(errcode));
110       gras_sock_close(g->sock);
111       return errcode;
112     }
113   }
114
115   gras_sleep(5,0);
116   fprintf(stderr,"SERVER: Done.\n");
117   return gras_sock_close(g->sock);
118 }
119
120 /* **********************************************************************
121  * Client code
122  * **********************************************************************/
123
124 /* Global private data */
125 typedef struct {
126   gras_sock_t *sock;
127 } client_data_t;
128
129 /* Function prototypes */
130 int client (int argc,char *argv[]);
131
132 int client(int argc,char *argv[]) {
133   int dummy=1234;
134   gras_msg_t *msg;
135   gras_error_t errcode;
136   client_data_t *g=gras_userdata_new(client_data_t);
137
138   if ((errcode=gras_sock_client_open("102",4000,&(g->sock)))) {
139     fprintf(stderr,"Client: Unable to connect to the server. Got %s\n",
140             gras_error_name(errcode));
141     return 1;
142   }
143
144   if (register_messages("Client")) {
145     //    grasCloseSocket(g->sock);
146     return 1;
147   }
148
149   fprintf(stderr,"Client: >>>>>>>> Connected to server which is on %s:%d <<<<<<<<\n", 
150           gras_sock_get_peer_name(g->sock),gras_sock_get_peer_port(g->sock));
151
152   if (gras_msg_new_and_send(g->sock,MSG_PING, 1,   &dummy,1)) {
153     fprintf(stderr,"Client: Unable send PING to server\n");
154     gras_sock_close(g->sock);
155     return 1;
156   }
157   fprintf(stderr,"Client: >>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<\n",
158           dummy, gras_sock_get_peer_name(g->sock),gras_sock_get_peer_port(g->sock));
159
160   if ((errcode=gras_msg_wait(6000,MSG_PONG,&msg))) {
161     fprintf(stderr,"Client: Why can't I get my PONG message like everyone else (%s error)?!\n",
162             gras_error_name(errcode));
163     gras_sock_close(g->sock);
164     return 1;
165   }
166
167   fprintf(stderr,"Client: >>>>>>>> Message PONG(%d) got from %s:%d <<<<<<<<\n", 
168           gras_msg_ctn(msg,0,0,int),gras_sock_get_peer_name(msg->sock),gras_sock_get_peer_port(msg->sock));
169   gras_msg_free(msg);
170
171   gras_sleep(5,0);
172   gras_sock_close(g->sock);
173   fprintf(stderr,"Client: Done.\n");
174   return 0;
175 }