Logo AND Algorithmique Numérique Distribuée

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