Logo AND Algorithmique Numérique Distribuée

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