Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
do not use malloc directly since AIX does not like it. Use the gras stuff instead
[simgrid.git] / src / gras / Transport / transport_plugin_buf.c
1 /* $Id$ */
2
3 /* buf trp (transport) - buffered transport using the TCP one            */
4
5 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2004 Martin Quinson.                                       */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <netinet/in.h>   /* htonl/ntohl */
12 #include <stdlib.h>
13 #include <string.h>       /* memset */
14
15 #include "gras_private.h"
16 #include "transport_private.h"
17
18 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_buf,transport,
19       "Generic buffered transport (works on top of TCP or SG)");
20
21 /***
22  *** Prototypes 
23  ***/
24 gras_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
25                                         const char *host,
26                                         unsigned short port,
27                                         /* OUT */ gras_socket_t *sock);
28 gras_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
29                                         unsigned short port,
30                                         /* OUT */ gras_socket_t *sock);
31 gras_error_t gras_trp_buf_socket_accept(gras_socket_t  *sock,
32                                         gras_socket_t **dst);
33
34 void         gras_trp_buf_socket_close(gras_socket_t *sd);
35   
36 gras_error_t gras_trp_buf_chunk_send(gras_socket_t *sd,
37                                      const char *data,
38                                      long int size);
39
40 gras_error_t gras_trp_buf_chunk_recv(gras_socket_t *sd,
41                                      char *data,
42                                      long int size);
43 gras_error_t gras_trp_buf_flush(gras_socket_t  *sock);
44
45
46 /***
47  *** Specific plugin part
48  ***/
49
50 typedef struct {
51   gras_trp_plugin_t *super;
52 } gras_trp_buf_plug_data_t;
53
54 /***
55  *** Specific socket part
56  ***/
57
58 typedef struct {
59   uint32_t size;
60   char *data;
61   int pos; /* for receive; not exchanged over the net */
62 } gras_trp_buf_t;
63
64 struct gras_trp_bufdata_{
65   gras_trp_buf_t in;
66   gras_trp_buf_t out;
67   int buffsize;
68 };
69
70 gras_error_t gras_trp_buf_init_sock(gras_socket_t *sock) {
71   gras_trp_bufdata_t *data=gras_new(gras_trp_bufdata_t,1);
72   
73   GRAS_IN;
74   if (!data)
75     RAISE_MALLOC;
76   data->in.size  = 0;
77   //  data->buffsize = 32 * 1024 - 4; /* default socket buffsize (32k) - headers */ 
78   data->buffsize = 100 * 1024 ; /* 100k */ 
79
80   if (!(data->in.data = (char*)gras_malloc(data->buffsize)))
81     RAISE_MALLOC;
82   data->in.pos   = 0; /* useless, indeed, since size==pos */
83   data->out.size = 0;
84   if (!(data->out.data = (char*)gras_malloc(data->buffsize)))
85     RAISE_MALLOC;
86   data->out.pos  = 0;
87   sock->bufdata = data;
88   return no_error;
89 }
90
91
92 /***
93  *** Code
94  ***/
95 gras_error_t
96 gras_trp_buf_setup(gras_trp_plugin_t *plug) {
97   gras_error_t errcode;
98   gras_trp_buf_plug_data_t *data =gras_new(gras_trp_buf_plug_data_t,1);
99   if (!data)
100     RAISE_MALLOC;
101
102   GRAS_IN;
103   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
104                                   &(data->super)));
105   DEBUG1("Derivate a buffer plugin from %s",gras_if_RL() ? "tcp" : "sg");
106
107   plug->socket_client = gras_trp_buf_socket_client;
108   plug->socket_server = gras_trp_buf_socket_server;
109   plug->socket_accept = gras_trp_buf_socket_accept;
110   plug->socket_close  = gras_trp_buf_socket_close;
111
112   plug->chunk_send    = gras_trp_buf_chunk_send;
113   plug->chunk_recv    = gras_trp_buf_chunk_recv;
114
115   plug->flush         = gras_trp_buf_flush;
116
117   plug->data = (void*)data;
118   plug->exit = NULL;
119   
120   return no_error;
121 }
122
123 gras_error_t gras_trp_buf_socket_client(gras_trp_plugin_t *self,
124                                         const char *host,
125                                         unsigned short port,
126                                         /* OUT */ gras_socket_t *sock){
127   gras_error_t errcode;
128   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
129
130   GRAS_IN;
131   TRY(super->socket_client(super,host,port,sock));
132   sock->plugin = self;
133   TRY(gras_trp_buf_init_sock(sock));
134     
135   return no_error;
136 }
137
138 /**
139  * gras_trp_buf_socket_server:
140  *
141  * Open a socket used to receive messages.
142  */
143 gras_error_t gras_trp_buf_socket_server(gras_trp_plugin_t *self,
144                                         unsigned short port,
145                                         /* OUT */ gras_socket_t *sock){
146   gras_error_t errcode;
147   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)self->data)->super;
148
149   GRAS_IN;
150   TRY(super->socket_server(super,port,sock));
151   sock->plugin = self;
152   TRY(gras_trp_buf_init_sock(sock));
153   return no_error;
154 }
155
156 gras_error_t
157 gras_trp_buf_socket_accept(gras_socket_t  *sock,
158                            gras_socket_t **dst) {
159   gras_error_t errcode;
160   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
161       
162   GRAS_IN;
163   TRY(super->socket_accept(sock,dst));
164   (*dst)->plugin = sock->plugin;
165   TRY(gras_trp_buf_init_sock(*dst));
166   return no_error;
167 }
168
169 void gras_trp_buf_socket_close(gras_socket_t *sock){
170   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
171   gras_trp_bufdata_t *data=sock->bufdata;
172
173   GRAS_IN;
174   if (data->in.size || data->out.size)
175     gras_trp_buf_flush(sock);
176   if (data->in.data)
177     free(data->in.data);
178   if (data->out.data)
179     free(data->out.data);
180   free(data);
181
182   return super->socket_close(sock);
183 }
184
185 /**
186  * gras_trp_buf_chunk_send:
187  *
188  * Send data on a TCP socket
189  */
190 gras_error_t 
191 gras_trp_buf_chunk_send(gras_socket_t *sock,
192                         const char *chunk,
193                         long int size) {
194
195   gras_error_t errcode;
196   gras_trp_bufdata_t *data=(gras_trp_bufdata_t*)sock->bufdata;
197   int chunk_pos=0;
198
199   GRAS_IN;
200   /* Let underneath plugin check for direction, we work even in duplex */
201   gras_assert0(size >= 0, "Cannot send a negative amount of data");
202
203   while (chunk_pos < size) {
204     /* size of the chunck to receive in that shot */
205     long int thissize = min(size-chunk_pos,data->buffsize - data->out.size);
206     DEBUG5("Set the chars %d..%ld into the buffer (size=%ld, ctn='%.*s')",
207            (int)data->out.size,
208            ((int)data->out.size) + thissize -1,
209            size, chunk_pos, chunk);
210
211     memcpy(data->out.data + data->out.size, chunk + chunk_pos, thissize);
212
213     data->out.size += thissize;
214     chunk_pos      += thissize;
215     DEBUG5("New pos = %d; Still to send = %ld of %ld; ctn sofar='%.*s'",
216            data->out.size,size-chunk_pos,size,(int)chunk_pos,chunk);
217
218     if (data->out.size == data->buffsize) /* out of space. Flush it */
219       TRY(gras_trp_buf_flush(sock));
220   }
221
222   GRAS_OUT;
223   return no_error;
224 }
225
226 /**
227  * gras_trp_buf_chunk_recv:
228  *
229  * Receive data on a TCP socket.
230  */
231 gras_error_t 
232 gras_trp_buf_chunk_recv(gras_socket_t *sock,
233                         char *chunk,
234                         long int size) {
235
236   gras_error_t errcode;
237   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
238   gras_trp_bufdata_t *data=sock->bufdata;
239   long int chunck_pos = 0;
240
241   /* Let underneath plugin check for direction, we work even in duplex */
242   gras_assert0(sock, "Cannot recv on an NULL socket");
243   gras_assert0(size >= 0, "Cannot receive a negative amount of data");
244   
245   GRAS_IN;
246
247   while (chunck_pos < size) {
248     /* size of the chunck to receive in that shot */
249     long int thissize;
250
251     if (data->in.size == data->in.pos) { /* out of data. Get more */
252       uint32_t nextsize;
253       DEBUG0("Recv the size");
254       TRY(super->chunk_recv(sock,(char*)&nextsize, 4));
255       data->in.size = ntohl(nextsize);
256
257       VERB1("Recv the chunk (size=%d)",data->in.size);
258       TRY(super->chunk_recv(sock, data->in.data, data->in.size));
259       data->in.pos=0;
260     }
261     
262     thissize = min(size-chunck_pos ,  data->in.size - data->in.pos);
263     DEBUG2("Get the chars %d..%ld out of the buffer",
264            data->in.pos,
265            data->in.pos + thissize - 1);
266     memcpy(chunk+chunck_pos, data->in.data + data->in.pos, thissize);
267
268     data->in.pos += thissize;
269     chunck_pos   += thissize;
270     DEBUG5("New pos = %d; Still to receive = %ld of %ld. Ctn so far='%.*s'",
271            data->in.pos,size - chunck_pos,size,(int)chunck_pos,chunk);
272   }
273
274   GRAS_OUT;
275   return no_error;
276 }
277
278 /**
279  * gras_trp_buf_flush:
280  *
281  * Make sure the data is sent
282  */
283 gras_error_t 
284 gras_trp_buf_flush(gras_socket_t *sock) {
285   gras_error_t errcode;
286   uint32_t size;
287   gras_trp_plugin_t *super=((gras_trp_buf_plug_data_t*)sock->plugin->data)->super;
288   gras_trp_bufdata_t *data=sock->bufdata;
289
290   GRAS_IN;
291   size = htonl(data->out.size);
292   DEBUG1("Send the size (=%d)",data->out.size);
293   TRY(super->chunk_send(sock,(char*) &size, 4));
294
295   DEBUG1("Send the chunk (size=%d)",data->out.size);
296   TRY(super->chunk_send(sock, data->out.data, data->out.size));
297   VERB1("Chunk sent (size=%d)",data->out.size);
298   data->out.size = 0;
299   return no_error;
300 }