Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
added a debug msg
[simgrid.git] / src / gras / Transport / transport_plugin_buf.c
index 46638be..dc3d81b 100644 (file)
@@ -2,42 +2,42 @@
 
 /* buf trp (transport) - buffered transport using the TCP one            */
 
-/* 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 <netinet/in.h>   /* htonl/ntohl */
 #include <stdlib.h>
 #include <string.h>       /* memset */
 
-#include "gras_private.h"
+#include "portable.h"
+#include "xbt/misc.h"
+#include "xbt/sysdep.h"
 #include "transport_private.h"
 
-GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_buf,transport,
+XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_buf,transport,
       "Generic buffered transport (works on top of TCP or SG)");
 
 /***
  *** Prototypes 
  ***/
-gras_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
-                                       /* OUT */ gras_socket_t *sock);
-gras_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
-                                       /* OUT */ gras_socket_t *sock);
-gras_error_t gras_trp_buf_socket_accept(gras_socket_t  *sock,
-                                       gras_socket_t **dst);
-
-void         gras_trp_buf_socket_close(gras_socket_t *sd);
+xbt_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
+                                       gras_socket_t sock);
+xbt_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
+                                       gras_socket_t sock);
+xbt_error_t gras_trp_buf_socket_accept(gras_socket_t sock,
+                                       gras_socket_t *dst);
+
+void         gras_trp_buf_socket_close(gras_socket_t sd);
   
-gras_error_t gras_trp_buf_chunk_send(gras_socket_t *sd,
+xbt_error_t gras_trp_buf_chunk_send(gras_socket_t sd,
                                     const char *data,
                                     long int size);
 
-gras_error_t gras_trp_buf_chunk_recv(gras_socket_t *sd,
+xbt_error_t gras_trp_buf_chunk_recv(gras_socket_t sd,
                                     char *data,
                                     long int size);
-gras_error_t gras_trp_buf_flush(gras_socket_t  *sock);
+xbt_error_t gras_trp_buf_flush(gras_socket_t sock);
 
 
 /***
@@ -53,7 +53,7 @@ typedef struct {
  ***/
 
 typedef struct {
-  uint32_t size;
+  int size;
   char *data;
   int pos; /* for receive; not exchanged over the net */
 } gras_trp_buf_t;
@@ -64,38 +64,33 @@ struct gras_trp_bufdata_{
   int buffsize;
 };
 
-gras_error_t gras_trp_buf_init_sock(gras_socket_t *sock) {
-  gras_trp_bufdata_t *data=gras_new(gras_trp_bufdata_t,1);
+void gras_trp_buf_init_sock(gras_socket_t sock) {
+  gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
   
-  GRAS_IN;
-  if (!data)
-    RAISE_MALLOC;
-  data->in.size  = 0;
+  XBT_IN;
   data->buffsize = 100 * 1024 ; /* 100k */ 
 
-  if (!(data->in.data = (char*)gras_malloc(data->buffsize)))
-    RAISE_MALLOC;
+  data->in.size  = 0;
+  data->in.data  = xbt_malloc(data->buffsize);
   data->in.pos   = 0; /* useless, indeed, since size==pos */
+   
   data->out.size = 0;
-  if (!(data->out.data = (char*)gras_malloc(data->buffsize)))
-    RAISE_MALLOC;
+  data->out.data = xbt_malloc(data->buffsize);
   data->out.pos  = 0;
+   
   sock->bufdata = data;
-  return no_error;
 }
 
 
 /***
  *** Code
  ***/
-gras_error_t
+xbt_error_t
 gras_trp_buf_setup(gras_trp_plugin_t *plug) {
-  gras_error_t errcode;
-  gras_trp_buf_plug_data_t *data =gras_new(gras_trp_buf_plug_data_t,1);
-  if (!data)
-    RAISE_MALLOC;
+  xbt_error_t errcode;
+  gras_trp_buf_plug_data_t *data =xbt_new(gras_trp_buf_plug_data_t,1);
 
-  GRAS_IN;
+  XBT_IN;
   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
                                  &(data->super)));
   DEBUG1("Derivate a buffer plugin from %s",gras_if_RL() ? "tcp" : "sg");
@@ -116,15 +111,15 @@ gras_trp_buf_setup(gras_trp_plugin_t *plug) {
   return no_error;
 }
 
-gras_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
-                                       /* OUT */ gras_socket_t *sock){
-  gras_error_t errcode;
+xbt_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
+                                       /* OUT */ gras_socket_t sock){
+  xbt_error_t errcode;
   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
 
-  GRAS_IN;
+  XBT_IN;
   TRY(super->socket_client(super,sock));
   sock->plugin = self;
-  TRY(gras_trp_buf_init_sock(sock));
+  gras_trp_buf_init_sock(sock);
     
   return no_error;
 }
@@ -134,43 +129,44 @@ gras_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
  *
  * Open a socket used to receive messages.
  */
-gras_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
-                                       /* OUT */ gras_socket_t *sock){
-  gras_error_t errcode;
+xbt_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
+                                       /* OUT */ gras_socket_t sock){
+  xbt_error_t errcode;
   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
 
-  GRAS_IN;
+  XBT_IN;
   TRY(super->socket_server(super,sock));
   sock->plugin = self;
-  TRY(gras_trp_buf_init_sock(sock));
+  gras_trp_buf_init_sock(sock);
   return no_error;
 }
 
-gras_error_t
-gras_trp_buf_socket_accept(gras_socket_t  *sock,
-                          gras_socket_t **dst) {
-  gras_error_t errcode;
+xbt_error_t
+gras_trp_buf_socket_accept(gras_socket_t  sock,
+                          gras_socket_t *dst) {
+  xbt_error_t errcode;
   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
       
-  GRAS_IN;
+  XBT_IN;
   TRY(super->socket_accept(sock,dst));
   (*dst)->plugin = sock->plugin;
-  TRY(gras_trp_buf_init_sock(*dst));
+  gras_trp_buf_init_sock(*dst);
+  XBT_OUT;
   return no_error;
 }
 
-void gras_trp_buf_socket_close(gras_socket_t *sock){
+void gras_trp_buf_socket_close(gras_socket_t sock){
   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
   gras_trp_bufdata_t *data=sock->bufdata;
 
-  GRAS_IN;
+  XBT_IN;
   if (data->in.size || data->out.size)
     gras_trp_buf_flush(sock);
   if (data->in.data)
-    free(data->in.data);
+    xbt_free(data->in.data);
   if (data->out.data)
-    free(data->out.data);
-  free(data);
+    xbt_free(data->out.data);
+  xbt_free(data);
 
   super->socket_close(sock);
 }
@@ -180,18 +176,18 @@ void gras_trp_buf_socket_close(gras_socket_t *sock){
  *
  * Send data on a TCP socket
  */
-gras_error_t 
-gras_trp_buf_chunk_send(gras_socket_t *sock,
+xbt_error_t 
+gras_trp_buf_chunk_send(gras_socket_t sock,
                        const char *chunk,
                        long int size) {
 
-  gras_error_t errcode;
+  xbt_error_t errcode;
   gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
   int chunk_pos=0;
 
-  GRAS_IN;
+  XBT_IN;
   /* Let underneath plugin check for direction, we work even in duplex */
-  gras_assert0(size >= 0, "Cannot send a negative amount of data");
+  xbt_assert0(size >= 0, "Cannot send a negative amount of data");
 
   while (chunk_pos < size) {
     /* size of the chunck to receive in that shot */
@@ -212,7 +208,7 @@ gras_trp_buf_chunk_send(gras_socket_t *sock,
       TRY(gras_trp_buf_flush(sock));
   }
 
-  GRAS_OUT;
+  XBT_OUT;
   return no_error;
 }
 
@@ -221,31 +217,31 @@ gras_trp_buf_chunk_send(gras_socket_t *sock,
  *
  * Receive data on a TCP socket.
  */
-gras_error_t 
-gras_trp_buf_chunk_recv(gras_socket_t *sock,
+xbt_error_t 
+gras_trp_buf_chunk_recv(gras_socket_t sock,
                        char *chunk,
                        long int size) {
 
-  gras_error_t errcode;
+  xbt_error_t errcode;
   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
   gras_trp_bufdata_t *data=sock->bufdata;
   long int chunck_pos = 0;
 
   /* Let underneath plugin check for direction, we work even in duplex */
-  gras_assert0(sock, "Cannot recv on an NULL socket");
-  gras_assert0(size >= 0, "Cannot receive a negative amount of data");
+  xbt_assert0(sock, "Cannot recv on an NULL socket");
+  xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
   
-  GRAS_IN;
+  XBT_IN;
 
   while (chunck_pos < size) {
     /* size of the chunck to receive in that shot */
     long int thissize;
 
     if (data->in.size == data->in.pos) { /* out of data. Get more */
-      uint32_t nextsize;
+      int nextsize;
       DEBUG0("Recv the size");
       TRY(super->chunk_recv(sock,(char*)&nextsize, 4));
-      data->in.size = ntohl(nextsize);
+      data->in.size = (int)ntohl(nextsize);
 
       VERB1("Recv the chunk (size=%d)",data->in.size);
       TRY(super->chunk_recv(sock, data->in.data, data->in.size));
@@ -264,7 +260,7 @@ gras_trp_buf_chunk_recv(gras_socket_t *sock,
           data->in.pos,size - chunck_pos,size,(int)chunck_pos,chunk);
   }
 
-  GRAS_OUT;
+  XBT_OUT;
   return no_error;
 }
 
@@ -273,15 +269,15 @@ gras_trp_buf_chunk_recv(gras_socket_t *sock,
  *
  * Make sure the data is sent
  */
-gras_error_t 
-gras_trp_buf_flush(gras_socket_t *sock) {
-  gras_error_t errcode;
-  uint32_t size;
+xbt_error_t 
+gras_trp_buf_flush(gras_socket_t sock) {
+  xbt_error_t errcode;
+  int size;
   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
   gras_trp_bufdata_t *data=sock->bufdata;
 
-  GRAS_IN;
-  size = htonl(data->out.size);
+  XBT_IN;
+  size = (int)htonl(data->out.size);
   DEBUG1("Send the size (=%d)",data->out.size);
   TRY(super->chunk_send(sock,(char*) &size, 4));