Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ONGOING work on exceptions plus minor cleanups.
[simgrid.git] / src / gras / Transport / transport_plugin_buf.c
1 /* $Id$ */
2
3 /* buf trp (transport) - buffered transport using the TCP one            */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include <stdlib.h>
11 #include <string.h>       /* memset */
12
13 #include "portable.h"
14 #include "xbt/misc.h"
15 #include "xbt/sysdep.h"
16 #include "xbt/ex.h"
17 #include "transport_private.h"
18
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_buf,transport,
20       "Generic buffered transport (works on top of TCP or SG)");
21
22
23 static gras_trp_plugin_t _buf_super;
24
25 /***
26  *** Prototypes 
27  ***/
28 void hexa_print(const char*name, unsigned char *data, int size);   /* in gras.c */
29    
30 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
31                                 gras_socket_t sock);
32 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
33                                 gras_socket_t sock);
34 gras_socket_t gras_trp_buf_socket_accept(gras_socket_t sock);
35
36 void         gras_trp_buf_socket_close(gras_socket_t sd);
37   
38 void gras_trp_buf_chunk_send(gras_socket_t sd,
39                              const char *data,
40                              unsigned long int size);
41
42 void gras_trp_buf_chunk_recv(gras_socket_t sd,
43                              char *data,
44                              unsigned long int size);
45 void gras_trp_buf_flush(gras_socket_t sock);
46
47
48 /***
49  *** Specific plugin part
50  ***/
51
52 typedef struct {
53   int junk;
54 } gras_trp_buf_plug_data_t;
55
56 /***
57  *** Specific socket part
58  ***/
59
60 typedef struct {
61   int size;
62   char *data;
63   int pos; /* for receive; not exchanged over the net */
64 } gras_trp_buf_t;
65
66 struct gras_trp_bufdata_{
67   gras_trp_buf_t in;
68   gras_trp_buf_t out;
69   int buffsize;
70 };
71
72 void gras_trp_buf_init_sock(gras_socket_t sock) {
73   gras_trp_bufdata_t *data=xbt_new(gras_trp_bufdata_t,1);
74   
75   XBT_IN;
76   data->buffsize = 100 * 1024 ; /* 100k */ 
77
78   data->in.size  = 0;
79   data->in.data  = xbt_malloc(data->buffsize);
80   data->in.pos   = 0; /* useless, indeed, since size==pos */
81    
82    /* In SG, the 4 first bytes are for the chunk size as htonl'ed, so that we can send it in one shoot.
83     * This is mandatory in SG because all emissions go to the same channel, so if we split them,
84     * they can get mixed. */
85   data->out.size = gras_if_RL()?0:4;
86   data->out.data = xbt_malloc(data->buffsize);
87   data->out.pos  = data->out.size;
88    
89   sock->bufdata = data;
90 }
91
92 /***
93  *** Code
94  ***/
95 void
96 gras_trp_buf_setup(gras_trp_plugin_t plug) {
97   gras_trp_buf_plug_data_t *data =xbt_new(gras_trp_buf_plug_data_t,1);
98
99   XBT_IN;
100   _buf_super = gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg");
101
102   DEBUG1("Derivate a buffer plugin from %s",gras_if_RL() ? "tcp" : "sg");
103
104   plug->socket_client = gras_trp_buf_socket_client;
105   plug->socket_server = gras_trp_buf_socket_server;
106   plug->socket_accept = gras_trp_buf_socket_accept;
107   plug->socket_close  = gras_trp_buf_socket_close;
108
109   plug->chunk_send    = gras_trp_buf_chunk_send;
110   plug->chunk_recv    = gras_trp_buf_chunk_recv;
111
112   plug->flush         = gras_trp_buf_flush;
113
114   plug->data = (void*)data;
115   plug->exit = NULL;
116 }
117
118 void gras_trp_buf_socket_client(gras_trp_plugin_t self,
119                                 /* OUT */ gras_socket_t sock){
120
121   XBT_IN;
122   _buf_super->socket_client(_buf_super,sock);
123   sock->plugin = self;
124   gras_trp_buf_init_sock(sock);
125 }
126
127 /**
128  * gras_trp_buf_socket_server:
129  *
130  * Open a socket used to receive messages.
131  */
132 void gras_trp_buf_socket_server(gras_trp_plugin_t self,
133                                 /* OUT */ gras_socket_t sock){
134
135   XBT_IN;
136   _buf_super->socket_server(_buf_super,sock);
137   sock->plugin = self;
138   gras_trp_buf_init_sock(sock);
139 }
140
141 gras_socket_t
142 gras_trp_buf_socket_accept(gras_socket_t  sock) {
143
144   gras_socket_t res;
145       
146   XBT_IN;
147   res = _buf_super->socket_accept(sock);
148   res->plugin = sock->plugin;
149   gras_trp_buf_init_sock(res);
150   XBT_OUT;
151   return res;
152 }
153
154 void gras_trp_buf_socket_close(gras_socket_t sock){
155   gras_trp_bufdata_t *data=sock->bufdata;
156
157   XBT_IN;
158   if (data->in.size!=data->in.pos) {
159      WARN3("Socket closed, but %d bytes were unread (size=%d,pos=%d)",
160            data->in.size - data->in.pos,data->in.size, data->in.pos);
161   }
162    
163   if (data->out.size!=data->out.pos) {
164     DEBUG2("Flush the socket before closing (in=%d,out=%d)",data->in.size, data->out.size);
165     gras_trp_buf_flush(sock);
166   }
167    
168   if (data->in.data)
169     free(data->in.data);
170   if (data->out.data)
171     free(data->out.data);
172   free(data);
173
174   _buf_super->socket_close(sock);
175 }
176
177 /**
178  * gras_trp_buf_chunk_send:
179  *
180  * Send data on a buffered socket
181  */
182 void
183 gras_trp_buf_chunk_send(gras_socket_t sock,
184                         const char *chunk,
185                         unsigned long int size) {
186
187   gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
188   int chunk_pos=0;
189
190   XBT_IN;
191   /* Let underneath plugin check for direction, we work even in duplex */
192   xbt_assert0(size >= 0, "Cannot send a negative amount of data");
193
194   while (chunk_pos < size) {
195     /* size of the chunck to receive in that shot */
196     long int thissize = min(size-chunk_pos,data->buffsize - data->out.size);
197     DEBUG5("Set the chars %d..%ld into the buffer (size=%ld, ctn='%.*s')",
198            (int)data->out.size,
199            ((int)data->out.size) + thissize -1,
200            size, chunk_pos, chunk);
201
202     memcpy(data->out.data + data->out.size, chunk + chunk_pos, thissize);
203
204     data->out.size += thissize;
205     chunk_pos      += thissize;
206     DEBUG5("New pos = %d; Still to send = %ld of %ld; ctn sofar='%.*s'",
207            data->out.size,size-chunk_pos,size,(int)chunk_pos,chunk);
208
209     if (data->out.size == data->buffsize) /* out of space. Flush it */
210       gras_trp_buf_flush(sock);
211   }
212
213   XBT_OUT;
214 }
215
216 /**
217  * gras_trp_buf_chunk_recv:
218  *
219  * Receive data on a buffered socket.
220  */
221 void
222 gras_trp_buf_chunk_recv(gras_socket_t sock,
223                         char *chunk,
224                         unsigned long int size) {
225
226   xbt_ex_t e;
227   gras_trp_bufdata_t *data=sock->bufdata;
228   long int chunck_pos = 0;
229  
230   /* Let underneath plugin check for direction, we work even in duplex */
231   xbt_assert0(sock, "Cannot recv on an NULL socket");
232   xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
233   
234   XBT_IN;
235
236   while (chunck_pos < size) {
237     /* size of the chunck to receive in that shot */
238     long int thissize;
239
240     if (data->in.size == data->in.pos) { /* out of data. Get more */
241       int nextsize;
242       if (gras_if_RL()) {
243          DEBUG0("Recv the size");
244          TRY {
245            _buf_super->chunk_recv(sock,(char*)&nextsize, 4);
246          } CATCH(e) {
247            RETHROW3("Unable to get the chunk size on %p (peer = %s:%d): %s",
248                     sock,gras_socket_peer_name(sock),gras_socket_peer_port(sock));
249          }
250          data->in.size = (int)ntohl(nextsize);
251          VERB1("Recv the chunk (size=%d)",data->in.size);
252       } else {
253          data->in.size = -1;
254       }
255        
256       _buf_super->chunk_recv(sock, data->in.data, data->in.size);
257        
258       if (gras_if_RL()) {
259          data->in.pos=0;
260       } else {
261          memcpy((char*)&nextsize,data->in.data,4);
262          data->in.size = nextsize+4;
263          data->in.pos=4;
264          VERB3("Got the chunk (size=%d+4 for the size ifself)='%.*s'",
265                data->in.size-4, data->in.size,data->in.data);
266          if (XBT_LOG_ISENABLED(trp_buf,xbt_log_priority_debug))
267            hexa_print("chunck received",data->in.data,data->in.size);
268       }
269        
270     }
271      
272     thissize = min(size-chunck_pos ,  data->in.size - data->in.pos);
273     DEBUG2("Get the chars %d..%ld out of the buffer",
274            data->in.pos,
275            data->in.pos + thissize - 1);
276     memcpy(chunk+chunck_pos, data->in.data + data->in.pos, thissize);
277
278     data->in.pos += thissize;
279     chunck_pos   += thissize;
280     DEBUG5("New pos = %d; Still to receive = %ld of %ld. Ctn so far='%.*s'",
281            data->in.pos,size - chunck_pos,size,(int)chunck_pos,chunk);
282   }
283
284   XBT_OUT;
285 }
286
287 /**
288  * gras_trp_buf_flush:
289  *
290  * Make sure the data is sent
291  */
292 void
293 gras_trp_buf_flush(gras_socket_t sock) {
294   int size;
295   gras_trp_bufdata_t *data=sock->bufdata;
296   XBT_IN;    
297   
298   DEBUG0("Flush");
299   if (XBT_LOG_ISENABLED(trp_buf,xbt_log_priority_debug))
300      hexa_print("chunck to send ",data->out.data,data->out.size);
301   if ((data->out.size - data->out.pos) == (gras_if_RL()?0:4) ) { /* 4 first bytes=size in SG mode*/
302      DEBUG2("Nothing to flush (size=%d; pos=%d)",data->out.size,data->out.pos);
303      return;
304   }
305    
306   size = (int)data->out.size - data->out.pos;
307   DEBUG4("%s the size (=%d) to %s:%d",(gras_if_RL()?"Send":"Embeed"),data->out.size-data->out.pos,
308          gras_socket_peer_name(sock),gras_socket_peer_port(sock));
309   if (gras_if_RL()) {
310      size = (int)htonl(size);
311      _buf_super->chunk_send(sock,(char*) &size, 4);
312   } else {
313      memcpy(data->out.data, &size, 4);
314   }
315       
316
317   DEBUG3("Send the chunk (size=%d) to %s:%d",data->out.size,
318          gras_socket_peer_name(sock),gras_socket_peer_port(sock));
319   _buf_super->chunk_send(sock, data->out.data, data->out.size);
320   VERB1("Chunk sent (size=%d)",data->out.size);
321   if (XBT_LOG_ISENABLED(trp_buf,xbt_log_priority_debug))
322      hexa_print("chunck sent    ",data->out.data,data->out.size);
323   data->out.size = gras_if_RL()?0:4;
324 }