Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Use new style logging macros.
[simgrid.git] / src / gras / Msg / gras_msg_listener.c
index ef7057b..dbe87a0 100644 (file)
@@ -18,15 +18,17 @@ XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_msg_read, gras_msg,
 #include "gras/Transport/transport_interface.h" /* gras_select */
 
 typedef struct s_gras_msg_listener_ {
+  xbt_thread_t listener; /* keep this first, gras_socket_im_the_server() does funky transtyping in sg_msg.c */
   xbt_queue_t incomming_messages;       /* messages received from the wire and still to be used by master */
   xbt_queue_t socks_to_close;   /* let the listener close the sockets, since it may be selecting on them. Darwin don't like this trick */
   gras_socket_t wakeup_sock_listener_side;
   gras_socket_t wakeup_sock_master_side;
+  int port; /* The port on which the listener opened the command socket */
   xbt_mutex_t init_mutex;       /* both this mutex and condition are used at initialization to make sure that */
   xbt_cond_t init_cond;         /* the main thread speaks to the listener only once it is started (FIXME: It would be easier using a semaphore, if only semaphores were in xbt_synchro) */
-  xbt_thread_t listener;
 } s_gras_msg_listener_t;
 
+#include "gras/Virtu/virtu_private.h" /* gras_procdata_t */
 static void listener_function(void *p)
 {
   gras_msg_listener_t me = (gras_msg_listener_t) p;
@@ -34,11 +36,22 @@ static void listener_function(void *p)
   xbt_ex_t e;
   gras_msgtype_t msg_wakeup_listener_t =
       gras_msgtype_by_name("_wakeup_listener");
-  DEBUG0("I'm the listener");
+  XBT_DEBUG("I'm the listener");
 
   /* get a free socket for the receiving part of the listener */
-  me->wakeup_sock_listener_side =
-      gras_socket_server_range(5000, 6000, -1, 0);
+  me->wakeup_sock_listener_side =NULL;
+  for (me->port = 5000; me->port < 6000; me->port++) {
+    TRY {
+      me->wakeup_sock_listener_side = gras_socket_server_ext(me->port, -1, 0);
+    }
+    CATCH(e) {
+      if (me->port == 6000)
+        RETHROW;
+      xbt_ex_free(e);
+    }
+    if (me->wakeup_sock_listener_side)
+      break;
+  }
 
   /* wake up the launcher */
   xbt_mutex_acquire(me->init_mutex);
@@ -49,17 +62,19 @@ static void listener_function(void *p)
   while (1) {
     msg = gras_msg_recv_any();
     if (msg->type != msg_wakeup_listener_t) {
-      VERB1("Got a '%s' message. Queue it for handling by main thread",
-            gras_msgtype_get_name(msg->type));
+               /* Cannot display who sent this since in SG, gras_socket_peer_* wont work:
+                  I'm not the user process but I'm just the listener. Too bad */
+      XBT_VERB("Got a '%s' message (%s) from sock %p. Queue it for handling by main thread",
+            gras_msgtype_get_name(msg->type),e_gras_msg_kind_names[msg->kind],msg->expe);
       xbt_queue_push(me->incomming_messages, msg);
     } else {
       char got = *(char *) msg->payl;
       if (got == '1') {
-        VERB0("Asked to get awake");
+        XBT_VERB("Asked to get awake");
         free(msg->payl);
         free(msg);
       } else {
-        VERB0("Asked to die");
+        XBT_VERB("Asked to die");
         //gras_socket_close(me->wakeup_sock_listener_side);
         free(msg->payl);
         free(msg);
@@ -73,10 +88,10 @@ static void listener_function(void *p)
         xbt_queue_shift_timed(me->socks_to_close, &sock, 0);
         if (tcp_close(sock) < 0) {
 #ifdef _XBT_WIN32
-          WARN2("error while closing tcp socket %d: %d\n", sock,
+          XBT_WARN("error while closing tcp socket %d: %d\n", sock,
                 sock_errno);
 #else
-          WARN3("error while closing tcp socket %d: %d (%s)\n",
+          XBT_WARN("error while closing tcp socket %d: %d (%s)\n",
                 sock, sock_errno, sock_errstr(sock_errno));
 #endif
         }
@@ -94,7 +109,7 @@ gras_msg_listener_t gras_msg_listener_launch(xbt_queue_t msg_received)
 {
   gras_msg_listener_t arg = xbt_new0(s_gras_msg_listener_t, 1);
 
-  VERB0("Launch listener");
+  XBT_VERB("Launch listener");
   arg->incomming_messages = msg_received;
   arg->socks_to_close = xbt_queue_new(0, sizeof(int));
   arg->init_mutex = xbt_mutex_init();
@@ -117,8 +132,7 @@ gras_msg_listener_t gras_msg_listener_launch(xbt_queue_t msg_received)
   /* Connect the other part of the socket */
   arg->wakeup_sock_master_side =
       gras_socket_client(gras_os_myname(),
-                         gras_socket_my_port
-                         (arg->wakeup_sock_listener_side));
+                         arg->port);
   return arg;
 }
 
@@ -127,7 +141,7 @@ void gras_msg_listener_shutdown()
 {
   gras_procdata_t *pd = gras_procdata_get();
   char kill = '0';
-  DEBUG0("Listener quit");
+  XBT_DEBUG("Listener quit");
 
   if (pd->listener)
     gras_msg_send(pd->listener->wakeup_sock_master_side,
@@ -146,7 +160,7 @@ void gras_msg_listener_awake()
   gras_procdata_t *pd;
   char c = '1';
 
-  DEBUG0("Awaking the listener");
+  XBT_DEBUG("Awaking the listener");
   pd = gras_procdata_get();
   if (pd->listener) {
     gras_msg_send(pd->listener->wakeup_sock_master_side,