Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
sockets is now part of the process data
[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 <time.h>       /* time() */
12
13 #include "Transport/transport_private.h"
14
15 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(transport,GRAS);
16
17 static gras_dict_t  *_gras_trp_plugins;     /* All registered plugins */
18 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
19
20 static void gras_trp_socket_free(void *s); /* free one socket */
21
22 gras_error_t
23 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup);
24
25 gras_error_t
26 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
27   gras_error_t errcode;
28
29   gras_trp_plugin_t *plug = malloc(sizeof(gras_trp_plugin_t));
30   
31   DEBUG1("Create plugin %s",name);
32
33   if (!plug) 
34     RAISE_MALLOC;
35
36   memset(plug,0,sizeof(gras_trp_plugin_t));
37
38   plug->name=strdup(name);
39   if (!plug->name)
40     RAISE_MALLOC;
41
42   errcode = setup(plug);
43   switch (errcode) {
44   case mismatch_error:
45     /* SG plugin return mismatch when in RL mode (and vice versa) */
46     free(plug->name);
47     free(plug);
48     break;
49
50   case no_error:
51     TRY(gras_dict_set(_gras_trp_plugins, 
52                       name, plug, gras_trp_plugin_free));
53     break;
54
55   default:
56     free(plug);
57     return errcode;
58   }
59   return no_error;
60 }
61
62 gras_error_t 
63 gras_trp_init(void){
64   gras_error_t errcode;
65   
66   /* make room for all plugins */
67   TRY(gras_dict_new(&_gras_trp_plugins));
68
69   /* Add them */
70   TRY(gras_trp_plugin_new("tcp", gras_trp_tcp_setup));
71   TRY(gras_trp_plugin_new("file",gras_trp_file_setup));
72   TRY(gras_trp_plugin_new("sg",gras_trp_sg_setup));
73
74   return no_error;
75 }
76
77 void
78 gras_trp_exit(void){
79   gras_dict_free(&_gras_trp_plugins);
80 }
81
82
83 void gras_trp_plugin_free(void *p) {
84   gras_trp_plugin_t *plug = p;
85
86   if (plug) {
87     if (plug->exit) {
88       plug->exit(plug);
89     } else if (plug->data) {
90       DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
91       free(plug->data);
92     }
93
94     free(plug->name);
95     free(plug);
96   }
97 }
98
99
100 /**
101  * gras_trp_socket_new:
102  *
103  * Malloc a new socket, and initialize it with defaults
104  */
105 gras_error_t gras_trp_socket_new(int incoming,
106                                  gras_socket_t **dst) {
107
108   gras_socket_t *sock;
109
110   if (! (sock=malloc(sizeof(gras_socket_t))) )
111     RAISE_MALLOC;
112
113   sock->plugin = NULL;
114   sock->sd     = -1;
115
116   sock->incoming  = incoming ? 1:0;
117   sock->outgoing  = incoming ? 0:1;
118   sock->accepting = incoming ? 1:0;
119
120   sock->port      = -1;
121   sock->peer_port = -1;
122   sock->peer_name = NULL;
123   sock->raw = 0;
124
125   *dst = sock;
126   return no_error;
127 }
128
129
130 /**
131  * gras_socket_server:
132  *
133  * Opens a server socket and make it ready to be listened to.
134  * In real life, you'll get a TCP socket.
135  */
136 gras_error_t
137 gras_socket_server(unsigned short port,
138                    /* OUT */ gras_socket_t **dst) {
139  
140   gras_error_t errcode;
141   gras_trp_plugin_t *trp;
142   gras_socket_t *sock;
143
144   *dst = NULL;
145
146   DEBUG1("Create a server socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
147   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
148                                   &trp));
149
150   /* defaults settings */
151   TRY(gras_trp_socket_new(1,&sock));
152   sock->plugin= trp;
153   sock->port=port;
154
155   /* Call plugin socket creation function */
156   errcode = trp->socket_server(trp, port, sock);
157   DEBUG3("in=%c out=%c accept=%c",
158          sock->incoming?'y':'n', 
159          sock->outgoing?'y':'n',
160          sock->accepting?'y':'n');
161
162   if (errcode != no_error) {
163     free(sock);
164     return errcode;
165   }
166
167   *dst = sock;
168   /* Register this socket */
169   errcode = gras_dynar_push(gras_socketset_get(),dst);
170   if (errcode != no_error) {
171     free(sock);
172     *dst = NULL;
173     return errcode;
174   }
175
176   return no_error;
177 }
178
179 /**
180  * gras_socket_client:
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(const char *host,
187                    unsigned short port,
188                    /* OUT */ gras_socket_t **dst) {
189  
190   gras_error_t errcode;
191   gras_trp_plugin_t *trp;
192   gras_socket_t *sock;
193
194   *dst = NULL;
195
196   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
197                                   &trp));
198
199   /* defaults settings */
200   TRY(gras_trp_socket_new(0,&sock));
201   sock->plugin= trp;
202   sock->peer_port = port;
203   sock->peer_name = strdup(host?host:"localhost");
204
205   /* plugin-specific */
206   errcode= (* trp->socket_client)(trp, 
207                                   host ? host : "localhost", port,
208                                   sock);
209   DEBUG3("in=%c out=%c accept=%c",
210          sock->incoming?'y':'n', 
211          sock->outgoing?'y':'n',
212          sock->accepting?'y':'n');
213
214   if (errcode != no_error) {
215     free(sock);
216     return errcode;
217   }
218
219   /* register socket */
220   *dst = sock;
221   errcode = gras_dynar_push(gras_socketset_get(),dst);
222   if (errcode != no_error) {
223     free(sock);
224     *dst = NULL;
225     return errcode;
226   }
227
228   return no_error;
229 }
230
231 void gras_socket_close(gras_socket_t *sock) {
232   gras_dynar_t *sockets = gras_socketset_get();
233   gras_socket_t *sock_iter;
234   int cursor;
235
236   /* FIXME: Issue an event when the socket is closed */
237   if (sock) {
238     gras_dynar_foreach(sockets,cursor,sock_iter) {
239       if (sock == sock_iter) {
240         gras_dynar_cursor_rm(sockets,&cursor);
241         if ( sock->plugin->socket_close) 
242           (* sock->plugin->socket_close)(sock);
243
244         /* free the memory */
245         if (sock->peer_name)
246           free(sock->peer_name);
247         free(sock);
248         return;
249       }
250     }
251     WARN0("Ignoring request to free an unknown socket");
252   }
253 }
254
255 /**
256  * gras_trp_chunk_send:
257  *
258  * Send a bunch of bytes from on socket
259  */
260 gras_error_t
261 gras_trp_chunk_send(gras_socket_t *sd,
262                     char *data,
263                     size_t size) {
264   gras_assert1(sd->outgoing,
265                "Socket not suited for data send (outgoing=%c)",
266                sd->outgoing?'y':'n');
267   gras_assert1(sd->plugin->chunk_send,
268                "No function chunk_send on transport plugin %s",
269                sd->plugin->name);
270   return (*sd->plugin->chunk_send)(sd,data,size);
271 }
272 /**
273  * gras_trp_chunk_recv:
274  *
275  * Receive a bunch of bytes from a socket
276  */
277 gras_error_t 
278 gras_trp_chunk_recv(gras_socket_t *sd,
279                     char *data,
280                     size_t size) {
281   gras_assert0(sd->incoming,
282                "Socket not suited for data receive");
283   gras_assert1(sd->plugin->chunk_recv,
284                "No function chunk_recv on transport plugin %s",
285                sd->plugin->name);
286   return (sd->plugin->chunk_recv)(sd,data,size);
287 }
288
289
290 gras_error_t
291 gras_trp_plugin_get_by_name(const char *name,
292                             gras_trp_plugin_t **dst){
293
294   return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
295 }
296
297 int   gras_socket_my_port  (gras_socket_t *sock) {
298   return sock->port;
299 }
300 int   gras_socket_peer_port(gras_socket_t *sock) {
301   return sock->peer_port;
302 }
303 char *gras_socket_peer_name(gras_socket_t *sock) {
304   return sock->peer_name;
305 }