Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Work to port on new API, to be terminated
authormquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Mon, 26 Apr 2004 10:15:11 +0000 (10:15 +0000)
committermquinson <mquinson@48e7efb5-ca39-0410-a469-dd3cf9ba447f>
Mon, 26 Apr 2004 10:15:11 +0000 (10:15 +0000)
git-svn-id: svn+ssh://scm.gforge.inria.fr/svn/simgrid/simgrid/trunk@86 48e7efb5-ca39-0410-a469-dd3cf9ba447f

examples/ping/ping.c
examples/ping/ping_deployment.txt

index 6395d51..c9d87ec 100644 (file)
 
 #include <gras.h>
 
+GRAS_LOG_NEW_DEFAULT_CATEGORY(Ping);
+
 /* **********************************************************************
  * 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);
+gras_error_t register_messages(void);
 
 /* Code */
-gras_error_t register_messages(const char *prefix) {
+gras_error_t register_messages(void) {
   gras_error_t errcode;
+  gras_datadesc_type_t *payload_t;
+  gras_msgtype_t *msg_t;
+
+  TRY(gras_datadesc_declare_struct("msg_ping_t",&payload_t));
+  TRY(gras_datadesc_declare_struct_append_name(payload_t,
+                                              "dummy","msg_ping_t"));
+
+  TRY(gras_msgtype_declare("ping", payload_t, &msg_t)); msg_t=NULL;
+  TRY(gras_msgtype_declare("pong", payload_t, &msg_t));
 
-  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;
 }
 
@@ -56,32 +49,46 @@ gras_error_t register_messages(const char *prefix) {
 
 /* 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,
+                          gras_datadesc_type_t *payload_type,
+                          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,
+                          gras_datadesc_type_t *payload_type,
+                          void                 *payload_data) {
+                             
+  gras_error_t errcode;
+  msg_ping_t *msg=payload_data;
+  gras_msgtype_t *pong_t;
+
   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 <<<<<<<<\n", 
+       msg->dummy, 
+       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->dummy = 4321;
+  TRY(gras_msgtype_by_name("pong",&pong_t));
+  errcode = gras_msg_send(expeditor, pong_t, payload_data);
+
+  free(payload_data);
+  if (errcode != no_error) {
+    ERROR1("SERVER: Unable answer with PONG: %s\n", gras_error_name(errcode));
+    gras_socket_close(&(g->sock));
     return 1;
   }
 
-  gras_msg_free(msg);
-  fprintf(stderr,"SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<\n");
+  free(msg);
+  INFO0("SERVER: >>>>>>>> Answed with PONG(4321) <<<<<<<<\n");
   g->endcondition = 1;
   return 1;
 }
@@ -89,32 +96,40 @@ int server_cbPingHandler(gras_msg_t *msg) {
 int server (int argc,char *argv[]) {
   gras_error_t errcode;
   server_data_t *g=gras_userdata_new(server_data_t);  
+  gras_msgtype_t *ping_msg;
 
-  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;
+  
+  if (argc == 2) {
+    port=atoi(argv[1]);
   }
 
-  if (register_messages("SERVER") || 
-      gras_cb_register(MSG_PING,-1,&server_cbPingHandler)) {
-    gras_sock_close(g->sock);
+  if ((errcode=gras_socket_server(port,&(g->sock)))) { 
+    CRITICAL1("Error %s encountered while opening the server socket",
+             gras_error_name(errcode));
     return 1;
   }
 
-  fprintf(stderr,"SERVER: >>>>>>>> Listening on port %d <<<<<<<<\n",gras_sock_get_my_port(g->sock));
+  TRYFAIL(register_messages());
+  TRYFAIL(gras_msgtype_by_name("ping",&ping_msg));
+  TRYFAIL(gras_cb_register(ping_msg,&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);
+  while (1) {
+    errcode = gras_msg_handle(60.0);
+    if (errcode != no_error && errcode != timeout_error) 
       return errcode;
-    }
+    if (g->endcondition)
+      break;
   }
 
   gras_sleep(5,0);
-  fprintf(stderr,"SERVER: Done.\n");
-  return gras_sock_close(g->sock);
+  INFO0("SERVER: Done.");
+  gras_socket_close(&(g->sock));
+  return no_error;
 }
 
 /* **********************************************************************
@@ -123,53 +138,76 @@ int server (int argc,char *argv[]) {
 
 /* 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)))) {
+  gras_socket_t  *from;
+  msg_ping_t     *msg_ping_data, *msg_pong_data;
+  gras_msgtype_t *msg_ping_type, *msg_pong_type;
+
+  const char *host = "127.0.0.1";
+        int   port = 4000;
+
+  if (argc == 3) {
+    host=argv[1];
+    port=atoi(argv[2]);
+  } 
+
+  if ((errcode=gras_socket_client(host,port,&(g->sock)))) {
     fprintf(stderr,"Client: Unable to connect to the server. Got %s\n",
            gras_error_name(errcode));
     return 1;
   }
+  fprintf(stderr,"Client: Connected to %s:%d.\n",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));
+  TRY(register_messages());
+  TRY(gras_msgtype_by_name("ping",&msg_ping_type));
+  TRY(gras_msgtype_by_name("pong",&msg_pong_type));
+
+  fprintf(stderr,
+         "Client: >>>>>>>> Connected to server which is on %s:%d <<<<<<<<\n", 
+         gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
 
-  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);
+  msg_ping_data = malloc(sizeof(msg_ping_t));
+  msg_ping_data->dummy = 1234;
+  errcode = gras_msg_send(g->sock, msg_ping_type, msg_ping_data);
+  if (errcode != no_error) {
+    fprintf(stderr, "Client: Unable send PING to server (%s)\n",
+           gras_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",
+         msg_ping_data->dummy,
+         gras_socket_peer_name(g->sock),gras_socket_peer_port(g->sock));
+
+  msg_pong_data = NULL;
+  if ((errcode=gras_msg_wait(6000,
+                            msg_pong_type,&from,(void**)&msg_pong_data))) {
+    fprintf(stderr,
+         "Client: Why can't I get my PONG message like everyone else (%s)?\n",
            gras_error_name(errcode));
-    gras_sock_close(g->sock);
+    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);
+  fprintf(stderr,"Client: >>>>>>>> Got PONG(%d) got from %s:%d <<<<<<<<\n", 
+         msg_pong_data->dummy,
+         gras_socket_peer_name(from),gras_socket_peer_port(from));
+
+  free(msg_ping_data);  
+  free(msg_pong_data);
 
   gras_sleep(5,0);
-  gras_sock_close(g->sock);
+  gras_socket_close(&(g->sock));
   fprintf(stderr,"Client: Done.\n");
   return 0;
 }
index 7ac9899..020a829 100644 (file)
@@ -1,2 +1,2 @@
-102 server
-161 client 102 0
+125 server
+165 client 125 0