3 /* ping - ping/pong demo of GRAS features */
5 /* Authors: Martin Quinson */
6 /* Copyright (C) 2003 the OURAGAN project. */
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. */
17 GRAS_LOG_NEW_DEFAULT_CATEGORY(Ping,"Messages specific to this example");
19 /* **********************************************************************
21 * **********************************************************************/
27 /* Function prototypes */
28 gras_error_t register_messages(void);
31 gras_error_t register_messages(void) {
34 TRY(gras_msgtype_declare("ping", gras_datadesc_by_name("int")));
35 TRY(gras_msgtype_declare("pong", gras_datadesc_by_name("int")));
40 /* **********************************************************************
42 * **********************************************************************/
44 /* Global private data */
50 /* Function prototypes */
51 int server_cb_ping_handler(gras_socket_t *expeditor,
53 int server (int argc,char *argv[]);
56 int server_cb_ping_handler(gras_socket_t *expeditor,
60 int msg=*(int*)payload_data;
61 gras_msgtype_t *pong_t=NULL;
63 server_data_t *g=(server_data_t*)gras_userdata_get();
66 INFO3("SERVER: >>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<",
68 gras_socket_peer_name(expeditor),
69 gras_socket_peer_port(expeditor));
72 errcode = gras_msg_send(expeditor, gras_msgtype_by_name("pong"), &msg);
74 if (errcode != no_error) {
75 ERROR1("SERVER: Unable answer with PONG: %s\n", gras_error_name(errcode));
76 gras_socket_close(g->sock);
80 INFO0("SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<");
82 gras_socket_close(expeditor);
86 int server (int argc,char *argv[]) {
89 gras_msgtype_t *ping_msg=NULL;
93 gras_init(&argc,argv);
94 g=gras_userdata_new(server_data_t);
100 INFO1("Launch server (port=%d)", port);
102 if ((errcode=gras_socket_server(port,&(g->sock)))) {
103 CRITICAL1("Error %s encountered while opening the server socket",
104 gras_error_name(errcode));
108 TRYFAIL(register_messages());
109 TRYFAIL(register_messages());
110 TRYFAIL(gras_cb_register(gras_msgtype_by_name("ping"),&server_cb_ping_handler));
112 INFO1("SERVER: >>>>>>>> Listening on port %d <<<<<<<<",
113 gras_socket_my_port(g->sock));
116 errcode = gras_msg_handle(600.0);
117 if (errcode != no_error)
123 gras_socket_close(g->sock);
126 INFO0("SERVER: Done.");
130 /* **********************************************************************
132 * **********************************************************************/
134 /* Global private data */
139 /* Function prototypes */
140 int client (int argc,char *argv[]);
142 int client(int argc,char *argv[]) {
143 gras_error_t errcode;
149 const char *host = "127.0.0.1";
152 gras_init(&argc, argv);
153 g=gras_userdata_new(client_data_t);
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));
167 INFO2("Client: Connected to %s:%d.",host,port);
170 TRY(register_messages());
172 INFO2("Client: >>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
173 gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
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);
183 INFO3("Client: >>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
185 gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
187 if ((errcode=gras_msg_wait(6000,gras_msgtype_by_name("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);
195 INFO3("Client: >>>>>>>> Got PONG(%d) got from %s:%d <<<<<<<<",
197 gras_socket_peer_name(from),gras_socket_peer_port(from));
199 gras_socket_close(g->sock);
202 INFO0("Client: Done.");