Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetics
[simgrid.git] / src / gras / Transport / transport_plugin_tcp.c
index 180e284..2683d7a 100644 (file)
 #include "portable.h"
 
 #if 0
-#  include <unistd.h>       /* close() pipe() read() write() */
 #  include <signal.h>       /* close() pipe() read() write() */
-#  include <netinet/in.h>   /* sometimes required for #include <arpa/inet.h> */
-#  include <netinet/tcp.h>  /* TCP_NODELAY */
-#  include <arpa/inet.h>    /* inet_ntoa() */
-#  include <netdb.h>        /* getprotobyname() */
-#  include <sys/time.h>     /* struct timeval */
-#  include <errno.h>        /* errno */
 #  include <sys/wait.h>     /* waitpid() */
-#  include <sys/socket.h>   /* getpeername() socket() */
-#  include <stdlib.h>
-#  include <string.h>       /* memset */
 #endif
 
 
@@ -59,7 +49,7 @@ static int TcpProtoNumber(void);
 
 typedef struct {
   fd_set msg_socks;
-  fd_set raw_socks;
+  fd_set meas_socks;
 } gras_trp_tcp_plug_data_t;
 
 /***
@@ -79,7 +69,7 @@ xbt_error_t gras_trp_tcp_setup(gras_trp_plugin_t *plug) {
   gras_trp_tcp_plug_data_t *data = xbt_new(gras_trp_tcp_plug_data_t,1);
 
   FD_ZERO(&(data->msg_socks));
-  FD_ZERO(&(data->raw_socks));
+  FD_ZERO(&(data->meas_socks));
 
   plug->socket_client = gras_trp_tcp_socket_client;
   plug->socket_server = gras_trp_tcp_socket_server;
@@ -99,7 +89,7 @@ xbt_error_t gras_trp_tcp_setup(gras_trp_plugin_t *plug) {
 
 void gras_trp_tcp_exit(gras_trp_plugin_t *plug) {
   DEBUG1("Exit plugin TCP (free %p)", plug->data);
-  xbt_free(plug->data);
+  free(plug->data);
 }
 
 xbt_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
@@ -139,6 +129,7 @@ xbt_error_t gras_trp_tcp_socket_client(gras_trp_plugin_t *self,
   addr.sin_family = AF_INET;
   addr.sin_port = htons (sock->peer_port);
 
+  DEBUG2("Connect to %s:%d",sock->peer_name, sock->peer_port);
   if (connect (sock->sd, (struct sockaddr*) &addr, sizeof (addr)) < 0) {
     tcp_close(sock->sd);
     RAISE3(system_error,
@@ -187,13 +178,14 @@ xbt_error_t gras_trp_tcp_socket_server(gras_trp_plugin_t *self,
     RAISE2(system_error,"Cannot bind to port %d: %s",sock->port, sock_errstr);
   }
 
+  DEBUG1("Listen on port %d",sock->port);
   if (listen(sock->sd, 5) < 0) {
     tcp_close(sock->sd);
-    RAISE2(system_error,"Cannot listen to port %d: %s",sock->port,sock_errstr);
+    RAISE2(system_error,"Cannot listen on port %d: %s",sock->port,sock_errstr);
   }
 
-  if (sock->raw)
-    FD_SET(sock->sd, &(tcp->raw_socks));
+  if (sock->meas)
+    FD_SET(sock->sd, &(tcp->meas_socks));
   else
     FD_SET(sock->sd, &(tcp->msg_socks));
 
@@ -220,7 +212,7 @@ gras_trp_tcp_socket_accept(gras_socket_t  sock,
   sd = accept(sock->sd, (struct sockaddr *)&peer_in, &peer_in_len);
   tmp_errno = errno;
 
-  if(sd == -1) {
+  if (sd == -1) {
     gras_socket_close(sock);
     RAISE1(system_error,
           "Accept failed (%s). Droping server socket.", sock_errstr);
@@ -267,7 +259,7 @@ gras_trp_tcp_socket_accept(gras_socket_t  sock,
       }
     }
 
-    VERB3("accepted socket %d to %s:%d", sd, res->peer_name,res->peer_port);
+    VERB3("Accepted socket %d to %s:%d", sd, res->peer_name,res->peer_port);
     
     *dst = res;
 
@@ -302,8 +294,8 @@ void gras_trp_tcp_socket_close(gras_socket_t sock){
   /* forget about the socket 
      ... but not when using winsock since accept'ed socket can not fit 
      into the fd_set*/
-  if (sock->raw){
-    FD_CLR(sock->sd, &(tcp->raw_socks));
+  if (sock->meas){
+    FD_CLR(sock->sd, &(tcp->meas_socks));
   } else {
     FD_CLR(sock->sd, &(tcp->msg_socks));
   }
@@ -336,7 +328,7 @@ gras_trp_tcp_chunk_send(gras_socket_t sock,
     status = tcp_write(sock->sd, data, (size_t)size);
     DEBUG3("write(%d, %p, %ld);", sock->sd, data, size);
     
-    if (status <= 0) {
+    if (status < 0) {
       RAISE4(system_error,"write(%d,%p,%ld) failed: %s",
             sock->sd, data, size,
             sock_errstr);
@@ -346,7 +338,8 @@ gras_trp_tcp_chunk_send(gras_socket_t sock,
       size  -= status;
       data  += status;
     } else {
-      RAISE0(system_error,"file descriptor closed");
+      RAISE1(system_error,"file descriptor closed (%s)",
+             sock_errstr);
     }
   }
 
@@ -372,7 +365,7 @@ gras_trp_tcp_chunk_recv(gras_socket_t sock,
     status = tcp_read(sock->sd, data, (size_t)size);
     DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
     
-    if (status <= 0) {
+    if (status < 0) {
       RAISE4(system_error,"read(%d,%p,%d) failed: %s",
             sock->sd, data, (int)size,
             sock_errstr);
@@ -382,7 +375,7 @@ gras_trp_tcp_chunk_recv(gras_socket_t sock,
       size  -= status;
       data  += status;
     } else {
-      RAISE0(system_error,"file descriptor closed");
+      RAISE0(system_error,"file descriptor closed (nothing read on the socket)");
     }
   }
   
@@ -408,12 +401,13 @@ static int TcpProtoNumber(void) {
   return returnValue;
 }
 
-/* Data exchange over raw sockets. Placing this in there is a kind of crude hack.
-   It means that the only possible raw are TCP where we may want to do UDP for them. 
+#if 0 /* KILLME */
+/* Data exchange over measurement sockets. Placing this in there is a kind of crude hack.
+   It means that the only possible measurement sockets are TCP where we may want to do UDP for them. 
    But I fail to find a good internal organization for now. We may want to split 
-   raw and regular sockets more efficiently.
+   meas and regular sockets more efficiently.
 */
-xbt_error_t gras_socket_raw_exchange(gras_socket_t peer,
+xbt_error_t gras_socket_meas_exchange(gras_socket_t peer,
                                      int sender,
                                      unsigned int timeout,
                                      unsigned long int exp_size,
@@ -446,16 +440,16 @@ xbt_error_t gras_socket_raw_exchange(gras_socket_t peer,
         }
         if (res_last == 0) {
           /* No progress done, bail out */
-          xbt_free(chunk);
+          free(chunk);
           RAISE0(unknown_error,"Not exchanged a single byte, bailing out");
         }
       }
    }
    
-   xbt_free(chunk);
+   free(chunk);
    return no_error;
 }
-
+#endif
 
 #ifdef HAVE_WINSOCK_H
 #define RETSTR( x ) case x: return #x