Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New function: gras_agent_spawn
[simgrid.git] / examples / gras / ping / ping.c
index a6c84d2..5095e30 100644 (file)
@@ -1,30 +1,8 @@
-/* $Id$ */
+#include "ping.h"
 
-/* ping - ping/pong demo of GRAS features                                   */
-
-/* Copyright (c) 2003, 2004, 2005 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. */
-
-#include "gras.h"
 
 XBT_LOG_NEW_DEFAULT_CATEGORY(Ping,"Messages specific to this example");
 
-/* register messages which may be sent (common to client and server) */
-static void register_messages(void) {
-  gras_msgtype_declare("ping", gras_datadesc_by_name("int"));
-  gras_msgtype_declare("pong", gras_datadesc_by_name("int"));
-}
-
-/* Function prototypes */
-int server (int argc,char *argv[]);
-int client (int argc,char *argv[]);
-
-/* **********************************************************************
- * Server code
- * **********************************************************************/
-
 /* Global private data */
 typedef struct {
   gras_socket_t sock;
@@ -32,12 +10,19 @@ typedef struct {
 } server_data_t;
 
 
-static int server_cb_ping_handler(gras_socket_t  expeditor,
-                                 void          *payload_data) {
+/* register messages which may be sent (common to client and server) */
+void ping_register_messages(void) {
+  gras_msgtype_declare("ping", gras_datadesc_by_name("int"));
+  gras_msgtype_declare("pong", gras_datadesc_by_name("int"));
+  INFO0("Messages registered");
+}
+
+static int server_cb_ping_handler(gras_msg_cb_ctx_t ctx, void* payload) {
                             
   xbt_ex_t e;
-  /* 1. Get the payload into the msg variable */
-  int msg=*(int*)payload_data;
+  /* 1. Get the payload into the msg variable, and retrieve my caller */
+  int msg=*(int*)payload;
+  gras_socket_t expeditor = gras_msg_cb_ctx_from(ctx);
 
   /* 2. Retrieve the server's state (globals) */
 
@@ -53,7 +38,7 @@ static int server_cb_ping_handler(gras_socket_t  expeditor,
   msg = 4321;
   /* 5. Send it back as payload of a pong message to the expeditor */
   TRY {
-    gras_msg_send(expeditor, gras_msgtype_by_name("pong"), &msg);
+    gras_msg_send(expeditor, "pong", &msg);
 
   /* 6. Deal with errors: add some details to the exception */
   } CATCH(e) {
@@ -70,7 +55,7 @@ static int server_cb_ping_handler(gras_socket_t  expeditor,
   gras_socket_close(expeditor);
    
   /* 9. Tell GRAS that we consummed this message */
-  return 1;
+  return 0;
 } /* end_of_server_cb_ping_handler */
 
 int server (int argc,char *argv[]) {
@@ -94,17 +79,17 @@ int server (int argc,char *argv[]) {
 
   /* 4. Register the known messages. This function is called twice here, but it's because
         this file also acts as regression test, no need to do so yourself of course. */
-  register_messages();
-  register_messages(); /* just to make sure it works ;) */
+  ping_register_messages();
+  ping_register_messages(); /* just to make sure it works ;) */
    
   /* 5. Register my callback */
-  gras_cb_register(gras_msgtype_by_name("ping"),&server_cb_ping_handler);
+  gras_cb_register("ping",&server_cb_ping_handler);
 
   INFO1(">>>>>>>> Listening on port %d <<<<<<<<", gras_socket_my_port(globals->sock));
   globals->endcondition=0;
 
   /* 6. Wait up to 10 minutes for an incomming message to handle */
-  gras_msg_handle(600.0);
+  gras_msg_handle(60.0);
    
   /* 7. Housekeeping */
   if (!globals->endcondition)
@@ -119,11 +104,6 @@ int server (int argc,char *argv[]) {
   return 0;
 } /* end_of_server */
 
-/* **********************************************************************
- * Client code
- * **********************************************************************/
-
-/* Function prototypes */
 
 int client(int argc,char *argv[]) {
   xbt_ex_t e; 
@@ -160,7 +140,7 @@ int client(int argc,char *argv[]) {
 
   /* 5. Register the messages. 
         See, it doesn't have to be done completely at the beginning, only before use */
-  register_messages();
+  ping_register_messages();
 
   /* 6. Keep the user informed of what's going on */
   INFO2(">>>>>>>> Connected to server which is on %s:%d <<<<<<<<", 
@@ -169,7 +149,7 @@ int client(int argc,char *argv[]) {
   /* 7. Prepare and send the ping message to the server */
   ping = 1234;
   TRY {
-    gras_msg_send(toserver, gras_msgtype_by_name("ping"), &ping);
+    gras_msg_send(toserver, "ping", &ping);
   } CATCH(e) {
     gras_socket_close(toserver);
     RETHROW0("Failed to send PING to server: %s");
@@ -180,8 +160,7 @@ int client(int argc,char *argv[]) {
 
   /* 8. Wait for the answer from the server, and deal with issues */
   TRY {
-    gras_msg_wait(6000,gras_msgtype_by_name("pong"),
-                 &from,&pong);
+    gras_msg_wait(6000,"pong", &from,&pong);
   } CATCH(e) {
     gras_socket_close(toserver);
     RETHROW0("Why can't I get my PONG message like everyone else: %s");