Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
more debuging
[simgrid.git] / src / gras / Transport / transport.c
index 1aa3413..6cb3f1e 100644 (file)
@@ -2,16 +2,17 @@
 
 /* transport - low level communication                                      */
 
-/* Authors: Martin Quinson                                                  */
-/* Copyright (C) 2004 Martin Quinson.                                       */
+/* Copyright (c) 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 "portable.h"
 #include "gras/Transport/transport_private.h"
 
 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(transport,gras,"Conveying bytes over the network");
 XBT_LOG_NEW_SUBCATEGORY(raw_trp,transport,"Conveying bytes over the network without formating");
+static short int _gras_trp_started = 0;
 
 static xbt_dict_t _gras_trp_plugins;      /* All registered plugins */
 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
@@ -30,8 +31,8 @@ gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
   switch (errcode) {
   case mismatch_error:
     /* SG plugin return mismatch when in RL mode (and vice versa) */
-    xbt_free(plug->name);
-    xbt_free(plug);
+    free(plug->name);
+    free(plug);
     break;
 
   case no_error:
@@ -46,22 +47,69 @@ gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
 }
 
 void gras_trp_init(void){
-  /* make room for all plugins */
-  _gras_trp_plugins=xbt_dict_new();
-
-  /* Add them */
-  gras_trp_plugin_new("tcp", gras_trp_tcp_setup);
-  gras_trp_plugin_new("file",gras_trp_file_setup);
-  gras_trp_plugin_new("sg",gras_trp_sg_setup);
-
-  /* buf is composed, so it must come after the others */
-  gras_trp_plugin_new("buf", gras_trp_buf_setup);
+  if (!_gras_trp_started) {
+     /* make room for all plugins */
+     _gras_trp_plugins=xbt_dict_new();
+
+#ifdef HAVE_WINSOCK2_H
+     /* initialize the windows mechanism */
+     {  
+       WORD wVersionRequested;
+       WSADATA wsaData;
+       
+       wVersionRequested = MAKEWORD( 2, 0 );
+       xbt_assert0(WSAStartup( wVersionRequested, &wsaData ) == 0,
+                   "Cannot find a usable WinSock DLL");
+       
+       /* Confirm that the WinSock DLL supports 2.0.*/
+       /* Note that if the DLL supports versions greater    */
+       /* than 2.0 in addition to 2.0, it will still return */
+       /* 2.0 in wVersion since that is the version we      */
+       /* requested.                                        */
+       
+       xbt_assert0(LOBYTE( wsaData.wVersion ) == 2 &&
+                   HIBYTE( wsaData.wVersion ) == 0,
+                   "Cannot find a usable WinSock DLL");
+       INFO0("Found and initialized winsock2");
+     }       /* The WinSock DLL is acceptable. Proceed. */
+#elif HAVE_WINSOCK_H
+     {       WSADATA wsaData;
+       xbt_assert0(WSAStartup( 0x0101, &wsaData ) == 0,
+                   "Cannot find a usable WinSock DLL");
+       INFO0("Found and initialized winsock");
+     }
+#endif
+   
+     /* Add plugins */
+     gras_trp_plugin_new("tcp", gras_trp_tcp_setup);
+     gras_trp_plugin_new("file",gras_trp_file_setup);
+     gras_trp_plugin_new("sg",gras_trp_sg_setup);
 
+     /* buf is composed, so it must come after the others */
+     gras_trp_plugin_new("buf", gras_trp_buf_setup);
+  }
+   
+  _gras_trp_started++;
 }
 
 void
 gras_trp_exit(void){
-  xbt_dict_free(&_gras_trp_plugins);
+   if (_gras_trp_started == 0) {
+      return;
+   }
+   
+   if ( --_gras_trp_started == 0 ) {
+#ifdef HAVE_WINSOCK_H
+      if ( WSACleanup() == SOCKET_ERROR ) {
+        if ( WSAGetLastError() == WSAEINPROGRESS ) {
+           WSACancelBlockingCall();
+           WSACleanup();
+        }
+       }
+#endif
+
+      xbt_dict_free(&_gras_trp_plugins);
+   }
 }
 
 
@@ -73,11 +121,11 @@ void gras_trp_plugin_free(void *p) {
       plug->exit(plug);
     } else if (plug->data) {
       DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
-      xbt_free(plug->data);
+      free(plug->data);
     }
 
-    xbt_free(plug->name);
-    xbt_free(plug);
+    free(plug->name);
+    free(plug);
   }
 }
 
@@ -110,6 +158,7 @@ void gras_trp_socket_new(int incoming,
   *dst = sock;
 
   xbt_dynar_push(gras_socketset_get(),dst);
+  XBT_OUT;
 }
 
 
@@ -146,6 +195,7 @@ gras_socket_server_ext(unsigned short port,
   sock->raw = raw;
 
   /* Call plugin socket creation function */
+  DEBUG1("Prepare socket with plugin (fct=%p)",trp->socket_server);
   errcode = trp->socket_server(trp, sock);
   DEBUG3("in=%c out=%c accept=%c",
         sock->incoming?'y':'n', 
@@ -153,7 +203,7 @@ gras_socket_server_ext(unsigned short port,
         sock->accepting?'y':'n');
 
   if (errcode != no_error) {
-    xbt_free(sock);
+    free(sock);
     return errcode;
   }
 
@@ -202,7 +252,7 @@ gras_socket_client_ext(const char *host,
         sock->accepting?'y':'n');
 
   if (errcode != no_error) {
-    xbt_free(sock);
+    free(sock);
     return errcode;
   }
 
@@ -242,23 +292,26 @@ void gras_socket_close(gras_socket_t sock) {
   gras_socket_t sock_iter;
   int cursor;
 
+  XBT_IN;
   /* FIXME: Issue an event when the socket is closed */
   if (sock) {
     xbt_dynar_foreach(sockets,cursor,sock_iter) {
       if (sock == sock_iter) {
        xbt_dynar_cursor_rm(sockets,&cursor);
-       if ( sock->plugin->socket_close) 
+       if (sock->plugin->socket_close) 
          (* sock->plugin->socket_close)(sock);
 
        /* free the memory */
        if (sock->peer_name)
-         xbt_free(sock->peer_name);
-       xbt_free(sock);
+         free(sock->peer_name);
+       free(sock);
+       XBT_OUT;
        return;
       }
     }
     WARN0("Ignoring request to free an unknown socket");
   }
+  XBT_OUT;
 }
 
 /**
@@ -341,7 +394,7 @@ xbt_error_t gras_socket_raw_send(gras_socket_t peer,
          exp_sofar,exp_size,msg_size,
          gras_socket_peer_name(peer), gras_socket_peer_port(peer));
             
-  xbt_free(chunk);
+  free(chunk);
   return no_error;/* gras_socket_raw_exchange(peer,1,timeout,expSize,msgSize);    */
 }
 
@@ -365,6 +418,40 @@ xbt_error_t gras_socket_raw_recv(gras_socket_t peer,
          exp_sofar,exp_size,msg_size,
          gras_socket_peer_name(peer), gras_socket_peer_port(peer));
 
-  xbt_free(chunk);
+  free(chunk);
   return no_error;/* gras_socket_raw_exchange(peer,0,timeout,expSize,msgSize);    */
 }
+
+/*
+ * Creating procdata for this module
+ */
+static void *gras_trp_procdata_new() {
+   gras_trp_procdata_t res = xbt_new(s_gras_trp_procdata_t,1);
+   
+   res->sockets   = xbt_dynar_new(sizeof(gras_socket_t*), NULL);
+   
+   return (void*)res;
+}
+
+/*
+ * Freeing procdata for this module
+ */
+static void gras_trp_procdata_free(void *data) {
+   gras_trp_procdata_t res = (gras_trp_procdata_t)data;
+   
+   xbt_dynar_free(&( res->sockets ));
+}
+
+/*
+ * Module registration
+ */
+void gras_trp_register() {
+   gras_procdata_add("gras_trp",gras_trp_procdata_new, gras_trp_procdata_free);
+}
+
+
+xbt_dynar_t 
+gras_socketset_get(void) {
+   /* FIXME: KILLME */
+   return ((gras_trp_procdata_t) gras_libdata_get("gras_trp"))->sockets;
+}