Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Current state. See changelog, sorry, I'm out of time
[simgrid.git] / src / gras / Transport / transport.c
1 /* $Id$ */
2
3 /* transport - low level communication                                      */
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 "gras/Transport/transport_private.h"
12
13 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(transport,gras,"Conveying bytes over the network");
14
15 static gras_dict_t  *_gras_trp_plugins;     /* All registered plugins */
16 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
17
18 static void gras_trp_socket_free(void *s); /* free one socket */
19
20 static void
21 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
22   gras_error_t errcode;
23
24   gras_trp_plugin_t *plug = gras_new0(gras_trp_plugin_t, 1);
25   
26   DEBUG1("Create plugin %s",name);
27
28   plug->name=gras_strdup(name);
29
30   errcode = setup(plug);
31   switch (errcode) {
32   case mismatch_error:
33     /* SG plugin return mismatch when in RL mode (and vice versa) */
34     gras_free(plug->name);
35     gras_free(plug);
36     break;
37
38   case no_error:
39     gras_dict_set(_gras_trp_plugins,
40                   name, plug, gras_trp_plugin_free);
41     break;
42
43   default:
44     DIE_IMPOSSIBLE;
45   }
46
47 }
48
49 void gras_trp_init(void){
50   /* make room for all plugins */
51   _gras_trp_plugins=gras_dict_new();
52
53   /* Add them */
54   gras_trp_plugin_new("tcp", gras_trp_tcp_setup);
55   gras_trp_plugin_new("file",gras_trp_file_setup);
56   gras_trp_plugin_new("sg",gras_trp_sg_setup);
57
58   /* buf is composed, so it must come after the others */
59   gras_trp_plugin_new("buf", gras_trp_buf_setup);
60
61 }
62
63 void
64 gras_trp_exit(void){
65   gras_dict_free(&_gras_trp_plugins);
66 }
67
68
69 void gras_trp_plugin_free(void *p) {
70   gras_trp_plugin_t *plug = p;
71
72   if (plug) {
73     if (plug->exit) {
74       plug->exit(plug);
75     } else if (plug->data) {
76       DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
77       gras_free(plug->data);
78     }
79
80     gras_free(plug->name);
81     gras_free(plug);
82   }
83 }
84
85
86 /**
87  * gras_trp_socket_new:
88  *
89  * Malloc a new socket, and initialize it with defaults
90  */
91 void gras_trp_socket_new(int incoming,
92                          gras_socket_t **dst) {
93
94   gras_socket_t *sock=gras_new(gras_socket_t,1);
95
96   DEBUG1("Create a new socket (%p)", (void*)sock);
97
98   sock->plugin = NULL;
99   sock->sd     = -1;
100   sock->data   = NULL;
101
102   sock->incoming  = incoming ? 1:0;
103   sock->outgoing  = incoming ? 0:1;
104   sock->accepting = incoming ? 1:0;
105
106   sock->port      = -1;
107   sock->peer_port = -1;
108   sock->peer_name = NULL;
109   sock->raw = 0;
110
111   *dst = sock;
112
113   gras_dynar_push(gras_socketset_get(),dst);
114 }
115
116
117 /**
118  * gras_socket_server_ext:
119  *
120  * Opens a server socket and make it ready to be listened to.
121  * In real life, you'll get a TCP socket.
122  */
123 gras_error_t
124 gras_socket_server_ext(unsigned short port,
125                        
126                        unsigned long int bufSize,
127                        int raw,
128                        
129                        /* OUT */ gras_socket_t **dst) {
130  
131   gras_error_t errcode;
132   gras_trp_plugin_t *trp;
133   gras_socket_t *sock;
134
135   *dst = NULL;
136
137   DEBUG1("Create a server socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
138   TRY(gras_trp_plugin_get_by_name("buf",&trp));
139
140   /* defaults settings */
141   gras_trp_socket_new(1,&sock);
142   sock->plugin= trp;
143   sock->port=port;
144   sock->bufSize = bufSize;
145   sock->raw = raw;
146
147   /* Call plugin socket creation function */
148   errcode = trp->socket_server(trp, sock);
149   DEBUG3("in=%c out=%c accept=%c",
150          sock->incoming?'y':'n', 
151          sock->outgoing?'y':'n',
152          sock->accepting?'y':'n');
153
154   if (errcode != no_error) {
155     gras_free(sock);
156     return errcode;
157   }
158
159   *dst = sock;
160
161   return no_error;
162 }
163    
164 /**
165  * gras_socket_client_ext:
166  *
167  * Opens a client socket to a remote host.
168  * In real life, you'll get a TCP socket.
169  */
170 gras_error_t
171 gras_socket_client_ext(const char *host,
172                        unsigned short port,
173                        
174                        unsigned long int bufSize,
175                        int raw,
176                        
177                        /* OUT */ gras_socket_t **dst) {
178  
179   gras_error_t errcode;
180   gras_trp_plugin_t *trp;
181   gras_socket_t *sock;
182
183   *dst = NULL;
184
185   TRY(gras_trp_plugin_get_by_name("buf",&trp));
186
187   DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
188   /* defaults settings */
189   gras_trp_socket_new(0,&sock);
190   sock->plugin= trp;
191   sock->peer_port = port;
192   sock->peer_name = (char*)strdup(host?host:"localhost");
193   sock->bufSize = bufSize;
194   sock->raw = raw;
195
196   /* plugin-specific */
197   errcode= (*trp->socket_client)(trp, sock);
198   DEBUG3("in=%c out=%c accept=%c",
199          sock->incoming?'y':'n', 
200          sock->outgoing?'y':'n',
201          sock->accepting?'y':'n');
202
203   if (errcode != no_error) {
204     gras_free(sock);
205     return errcode;
206   }
207
208   *dst = sock;
209
210   return no_error;
211 }
212
213 /**
214  * gras_socket_server:
215  *
216  * Opens a server socket and make it ready to be listened to.
217  * In real life, you'll get a TCP socket.
218  */
219 gras_error_t
220 gras_socket_server(unsigned short port,
221                    /* OUT */ gras_socket_t **dst) {
222    return gras_socket_server_ext(port,32,0,dst);
223 }
224
225 /**
226  * gras_socket_client:
227  *
228  * Opens a client socket to a remote host.
229  * In real life, you'll get a TCP socket.
230  */
231 gras_error_t
232 gras_socket_client(const char *host,
233                    unsigned short port,
234                    /* OUT */ gras_socket_t **dst) {
235    return gras_socket_client_ext(host,port,32,0,dst);
236 }
237
238
239 void gras_socket_close(gras_socket_t *sock) {
240   gras_dynar_t *sockets = gras_socketset_get();
241   gras_socket_t *sock_iter;
242   int cursor;
243
244   /* FIXME: Issue an event when the socket is closed */
245   if (sock) {
246     gras_dynar_foreach(sockets,cursor,sock_iter) {
247       if (sock == sock_iter) {
248         gras_dynar_cursor_rm(sockets,&cursor);
249         if ( sock->plugin->socket_close) 
250           (* sock->plugin->socket_close)(sock);
251
252         /* free the memory */
253         if (sock->peer_name)
254           gras_free(sock->peer_name);
255         gras_free(sock);
256         return;
257       }
258     }
259     WARN0("Ignoring request to free an unknown socket");
260   }
261 }
262
263 /**
264  * gras_trp_chunk_send:
265  *
266  * Send a bunch of bytes from on socket
267  */
268 gras_error_t
269 gras_trp_chunk_send(gras_socket_t *sd,
270                     char *data,
271                     long int size) {
272   gras_assert1(sd->outgoing,
273                "Socket not suited for data send (outgoing=%c)",
274                sd->outgoing?'y':'n');
275   gras_assert1(sd->plugin->chunk_send,
276                "No function chunk_send on transport plugin %s",
277                sd->plugin->name);
278   return (*sd->plugin->chunk_send)(sd,data,size);
279 }
280 /**
281  * gras_trp_chunk_recv:
282  *
283  * Receive a bunch of bytes from a socket
284  */
285 gras_error_t 
286 gras_trp_chunk_recv(gras_socket_t *sd,
287                     char *data,
288                     long int size) {
289   gras_assert0(sd->incoming,
290                "Socket not suited for data receive");
291   gras_assert1(sd->plugin->chunk_recv,
292                "No function chunk_recv on transport plugin %s",
293                sd->plugin->name);
294   return (sd->plugin->chunk_recv)(sd,data,size);
295 }
296
297 /**
298  * gras_trp_flush:
299  *
300  * Make sure all pending communications are done
301  */
302 gras_error_t 
303 gras_trp_flush(gras_socket_t *sd) {
304   return (sd->plugin->flush)(sd);
305 }
306
307 gras_error_t
308 gras_trp_plugin_get_by_name(const char *name,
309                             gras_trp_plugin_t **dst){
310
311   return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
312 }
313
314 int   gras_socket_my_port  (gras_socket_t *sock) {
315   return sock->port;
316 }
317 int   gras_socket_peer_port(gras_socket_t *sock) {
318   return sock->peer_port;
319 }
320 char *gras_socket_peer_name(gras_socket_t *sock) {
321   return sock->peer_name;
322 }
323
324 gras_error_t gras_socket_raw_send(gras_socket_t *peer, 
325                                   unsigned int timeout,
326                                   unsigned long int exp_size, 
327                                   unsigned long int msg_size) {
328   gras_error_t errcode;
329   char *chunk = gras_malloc(msg_size);
330   int exp_sofar;
331    
332   gras_assert0(peer->raw,"Asked to send raw data on a regular socket\n");
333   for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
334      TRY(gras_trp_chunk_send(peer,chunk,msg_size));
335   }
336              
337   gras_free(chunk);
338   return no_error;//gras_socket_raw_exchange(peer,1,timeout,expSize,msgSize);   
339 }
340
341 gras_error_t gras_socket_raw_recv(gras_socket_t *peer, 
342                                   unsigned int timeout,
343                                   unsigned long int exp_size, 
344                                   unsigned long int msg_size){
345   
346   gras_error_t errcode;
347   char *chunk = gras_malloc(msg_size);
348   int exp_sofar;
349
350   gras_assert0(peer->raw,"Asked to recveive raw data on a regular socket\n");
351   for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
352      TRY(gras_trp_chunk_send(peer,chunk,msg_size));
353   }
354
355   gras_free(chunk);
356   return no_error;//gras_socket_raw_exchange(peer,0,timeout,expSize,msgSize);   
357 }