/* ping - ping/pong demo of GRAS features */
-/* Authors: Martin Quinson */
-/* Copyright (C) 2003 the OURAGAN project. */
+/* Copyright (c) 2003, 2004 Martin Quinson. All rights reserved. */
/* This program is free software; you can redistribute it and/or modify it
- under the terms of the license (GNU LGPL) which comes with this package. */
+ * under the terms of the license (GNU LGPL) which comes with this package. */
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
-#include <gras.h>
+#include "gras.h"
+
+XBT_LOG_NEW_DEFAULT_CATEGORY(Ping,"Messages specific to this example");
/* **********************************************************************
* Comon code
* **********************************************************************/
-/* message definition */
-#define MSG_PING 1024
-#define MSG_PONG 1025
typedef struct {
int dummy;
-} msgPing_t;
-
-static const DataDescriptor msgPingDesc[] =
- {SIMPLE_MEMBER(INT_TYPE,1,offsetof(msgPing_t,dummy))};
-#define msgPingLength 1
-
-typedef msgPing_t msgPong_t;
-static const DataDescriptor msgPongDesc[] =
- {SIMPLE_MEMBER(INT_TYPE,1,offsetof(msgPong_t,dummy))};
-#define msgPongLength 1
+} msg_ping_t;
/* Function prototypes */
-gras_error_t register_messages(const char *prefix);
+void register_messages(void);
/* Code */
-gras_error_t register_messages(const char *prefix) {
- gras_error_t errcode;
+void register_messages(void) {
- if ((errcode=gras_msgtype_register(MSG_PING,"ping",1,msgPingDesc,msgPingLength)) ||
- (errcode=gras_msgtype_register(MSG_PONG,"pong",1,msgPongDesc,msgPongLength))) {
- fprintf(stderr,"%s: Unable register the messages (got error %s)\n",
- prefix,gras_error_name(errcode));
- return errcode;
- }
- return no_error;
+ gras_msgtype_declare("ping", gras_datadesc_by_name("int"));
+ gras_msgtype_declare("pong", gras_datadesc_by_name("int"));
}
/* **********************************************************************
/* Global private data */
typedef struct {
- gras_sock_t *sock;
+ gras_socket_t sock;
int endcondition;
} server_data_t;
/* Function prototypes */
-int server_cbPingHandler(gras_msg_t *msg);
+int server_cb_ping_handler(gras_socket_t expeditor,
+ void *payload_data);
int server (int argc,char *argv[]);
-int server_cbPingHandler(gras_msg_t *msg) {
- int *dummy=malloc(sizeof(int*));
+int server_cb_ping_handler(gras_socket_t expeditor,
+ void *payload_data) {
+
+ xbt_error_t errcode;
+ int msg=*(int*)payload_data;
+ gras_msgtype_t *pong_t=NULL;
+
server_data_t *g=(server_data_t*)gras_userdata_get();
g->endcondition = 0;
- *dummy=4321;
- fprintf (stderr,"SERVER: >>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<\n",
- gras_msg_ctn(msg,0,0,int), gras_sock_get_peer_name(msg->sock),gras_sock_get_peer_port(msg->sock));
+ INFO3("SERVER: >>>>>>>> Got message PING(%d) from %s:%d <<<<<<<<",
+ msg,
+ gras_socket_peer_name(expeditor),
+ gras_socket_peer_port(expeditor));
- if (gras_msg_new_and_send(msg->sock,MSG_PONG,1, dummy,1)) {
- fprintf(stderr,"SERVER: Unable answer with PONG\n");
- gras_sock_close(g->sock);
+ msg = 4321;
+ errcode = gras_msg_send(expeditor, gras_msgtype_by_name("pong"), &msg);
+
+ if (errcode != no_error) {
+ ERROR1("SERVER: Unable answer with PONG: %s\n", xbt_error_name(errcode));
+ gras_socket_close(g->sock);
return 1;
}
- gras_msg_free(msg);
- fprintf(stderr,"SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<\n");
+ INFO0("SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<");
g->endcondition = 1;
+ gras_socket_close(expeditor);
return 1;
}
int server (int argc,char *argv[]) {
- gras_error_t errcode;
- server_data_t *g=gras_userdata_new(server_data_t);
+ xbt_error_t errcode;
+ server_data_t *g;
+ gras_msgtype_t *ping_msg=NULL;
- if ((errcode=gras_sock_server_open(4000,4000,&(g->sock)))) {
- fprintf(stderr,"SERVER: Error %s encountered while opening the server socket\n",gras_error_name(errcode));
- return 1;
+ int port = 4000;
+
+ gras_init(&argc,argv, NULL);
+ g=gras_userdata_new(server_data_t);
+
+ if (argc == 2) {
+ port=atoi(argv[1]);
}
- if (register_messages("SERVER") ||
- gras_cb_register(MSG_PING,-1,&server_cbPingHandler)) {
- gras_sock_close(g->sock);
+ INFO1("Launch server (port=%d)", port);
+
+ if ((errcode=gras_socket_server(port,&(g->sock)))) {
+ CRITICAL1("Error %s encountered while opening the server socket",
+ xbt_error_name(errcode));
return 1;
}
- fprintf(stderr,"SERVER: >>>>>>>> Listening on port %d <<<<<<<<\n",gras_sock_get_my_port(g->sock));
+ register_messages();
+ register_messages();
+ gras_cb_register(gras_msgtype_by_name("ping"),&server_cb_ping_handler);
+
+ INFO1("SERVER: >>>>>>>> Listening on port %d <<<<<<<<",
+ gras_socket_my_port(g->sock));
g->endcondition=0;
- while (!g->endcondition) {
- if ((errcode=gras_msg_handle(60.0))) {
- fprintf(stderr,"SERVER: Error '%s' while handling message\n",gras_error_name(errcode));
- gras_sock_close(g->sock);
- return errcode;
- }
- }
+ errcode = gras_msg_handle(600.0);
+ if (errcode != no_error)
+ return errcode;
+ if (g->endcondition)
+
+ if (!gras_if_RL())
+ gras_os_sleep(1,0);
- gras_sleep(5,0);
- fprintf(stderr,"SERVER: Done.\n");
- return gras_sock_close(g->sock);
+ gras_socket_close(g->sock);
+ free(g);
+ gras_exit();
+ INFO0("SERVER: Done.");
+ return no_error;
}
/* **********************************************************************
/* Global private data */
typedef struct {
- gras_sock_t *sock;
+ gras_socket_t sock;
} client_data_t;
/* Function prototypes */
int client (int argc,char *argv[]);
int client(int argc,char *argv[]) {
- int dummy=1234;
- gras_msg_t *msg;
- gras_error_t errcode;
- client_data_t *g=gras_userdata_new(client_data_t);
-
- if ((errcode=gras_sock_client_open("102",4000,&(g->sock)))) {
- fprintf(stderr,"Client: Unable to connect to the server. Got %s\n",
- gras_error_name(errcode));
+ xbt_error_t errcode;
+ client_data_t *g;
+
+ gras_socket_t from;
+ int ping, pong;
+
+ const char *host = "127.0.0.1";
+ int port = 4000;
+
+ gras_init(&argc, argv, NULL);
+ g=gras_userdata_new(client_data_t);
+
+ if (argc == 3) {
+ host=argv[1];
+ port=atoi(argv[2]);
+ }
+
+ INFO2("Launch client (server on %s:%d)",host,port);
+ gras_os_sleep(1,0); /* Wait for the server startup */
+ if ((errcode=gras_socket_client(host,port,&(g->sock)))) {
+ ERROR1("Client: Unable to connect to the server. Got %s",
+ xbt_error_name(errcode));
return 1;
}
+ INFO2("Client: Connected to %s:%d.",host,port);
- if (register_messages("Client")) {
- // grasCloseSocket(g->sock);
- return 1;
- }
- fprintf(stderr,"Client: >>>>>>>> Connected to server which is on %s:%d <<<<<<<<\n",
- gras_sock_get_peer_name(g->sock),gras_sock_get_peer_port(g->sock));
+ register_messages();
- if (gras_msg_new_and_send(g->sock,MSG_PING, 1, &dummy,1)) {
- fprintf(stderr,"Client: Unable send PING to server\n");
- gras_sock_close(g->sock);
+ INFO2("Client: >>>>>>>> Connected to server which is on %s:%d <<<<<<<<",
+ gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
+
+ ping = 1234;
+ errcode = gras_msg_send(g->sock, gras_msgtype_by_name("ping"), &ping);
+ if (errcode != no_error) {
+ fprintf(stderr, "Client: Unable send PING to server (%s)\n",
+ xbt_error_name(errcode));
+ gras_socket_close(g->sock);
return 1;
}
- fprintf(stderr,"Client: >>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<\n",
- dummy, gras_sock_get_peer_name(g->sock),gras_sock_get_peer_port(g->sock));
-
- if ((errcode=gras_msg_wait(6000,MSG_PONG,&msg))) {
- fprintf(stderr,"Client: Why can't I get my PONG message like everyone else (%s error)?!\n",
- gras_error_name(errcode));
- gras_sock_close(g->sock);
+ INFO3("Client: >>>>>>>> Message PING(%d) sent to %s:%d <<<<<<<<",
+ ping,
+ gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
+
+ if ((errcode=gras_msg_wait(6000,gras_msgtype_by_name("pong"),
+ &from,&pong))) {
+ ERROR1("Client: Why can't I get my PONG message like everyone else (%s)?",
+ xbt_error_name(errcode));
+ gras_socket_close(g->sock);
return 1;
}
- fprintf(stderr,"Client: >>>>>>>> Message PONG(%d) got from %s:%d <<<<<<<<\n",
- gras_msg_ctn(msg,0,0,int),gras_sock_get_peer_name(msg->sock),gras_sock_get_peer_port(msg->sock));
- gras_msg_free(msg);
+ INFO3("Client: >>>>>>>> Got PONG(%d) got from %s:%d <<<<<<<<",
+ pong,
+ gras_socket_peer_name(from),gras_socket_peer_port(from));
- gras_sleep(5,0);
- gras_sock_close(g->sock);
- fprintf(stderr,"Client: Done.\n");
+ gras_socket_close(g->sock);
+ free(g);
+ gras_exit();
+ INFO0("Client: Done.");
return 0;
}