Logo AND Algorithmique Numérique Distribuée

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