Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix miscasted logging arguments
[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   DEBUG1("Create a new socket (%p)", sock);
113
114   sock->plugin = NULL;
115   sock->sd     = -1;
116   sock->data   = NULL;
117
118   sock->incoming  = incoming ? 1:0;
119   sock->outgoing  = incoming ? 0:1;
120   sock->accepting = incoming ? 1:0;
121
122   sock->port      = -1;
123   sock->peer_port = -1;
124   sock->peer_name = NULL;
125   sock->raw = 0;
126
127   *dst = sock;
128
129   return gras_dynar_push(gras_socketset_get(),dst);
130 }
131
132
133 /**
134  * gras_socket_server:
135  *
136  * Opens a server socket and make it ready to be listened to.
137  * In real life, you'll get a TCP socket.
138  */
139 gras_error_t
140 gras_socket_server(unsigned short port,
141                    /* OUT */ gras_socket_t **dst) {
142  
143   gras_error_t errcode;
144   gras_trp_plugin_t *trp;
145   gras_socket_t *sock;
146
147   *dst = NULL;
148
149   DEBUG1("Create a server socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
150   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
151                                   &trp));
152
153   /* defaults settings */
154   TRY(gras_trp_socket_new(1,&sock));
155   sock->plugin= trp;
156   sock->port=port;
157
158   /* Call plugin socket creation function */
159   errcode = trp->socket_server(trp, port, sock);
160   DEBUG3("in=%c out=%c accept=%c",
161          sock->incoming?'y':'n', 
162          sock->outgoing?'y':'n',
163          sock->accepting?'y':'n');
164
165   if (errcode != no_error) {
166     free(sock);
167     return errcode;
168   }
169
170   *dst = sock;
171
172   return no_error;
173 }
174
175 /**
176  * gras_socket_client:
177  *
178  * Opens a client socket to a remote host.
179  * In real life, you'll get a TCP socket.
180  */
181 gras_error_t
182 gras_socket_client(const char *host,
183                    unsigned short port,
184                    /* OUT */ gras_socket_t **dst) {
185  
186   gras_error_t errcode;
187   gras_trp_plugin_t *trp;
188   gras_socket_t *sock;
189
190   *dst = NULL;
191
192   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "tcp" : "sg",
193                                   &trp));
194
195   DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
196   /* defaults settings */
197   TRY(gras_trp_socket_new(0,&sock));
198   sock->plugin= trp;
199   sock->peer_port = port;
200   sock->peer_name = strdup(host?host:"localhost");
201
202   /* plugin-specific */
203   errcode= (* trp->socket_client)(trp, 
204                                   host ? host : "localhost", port,
205                                   sock);
206   DEBUG3("in=%c out=%c accept=%c",
207          sock->incoming?'y':'n', 
208          sock->outgoing?'y':'n',
209          sock->accepting?'y':'n');
210
211   if (errcode != no_error) {
212     free(sock);
213     return errcode;
214   }
215
216   *dst = sock;
217
218   return no_error;
219 }
220
221 void gras_socket_close(gras_socket_t *sock) {
222   gras_dynar_t *sockets = gras_socketset_get();
223   gras_socket_t *sock_iter;
224   int cursor;
225
226   /* FIXME: Issue an event when the socket is closed */
227   if (sock) {
228     gras_dynar_foreach(sockets,cursor,sock_iter) {
229       if (sock == sock_iter) {
230         gras_dynar_cursor_rm(sockets,&cursor);
231         if ( sock->plugin->socket_close) 
232           (* sock->plugin->socket_close)(sock);
233
234         /* free the memory */
235         if (sock->peer_name)
236           free(sock->peer_name);
237         free(sock);
238         return;
239       }
240     }
241     WARN0("Ignoring request to free an unknown socket");
242   }
243 }
244
245 /**
246  * gras_trp_chunk_send:
247  *
248  * Send a bunch of bytes from on socket
249  */
250 gras_error_t
251 gras_trp_chunk_send(gras_socket_t *sd,
252                     char *data,
253                     long int size) {
254   gras_assert1(sd->outgoing,
255                "Socket not suited for data send (outgoing=%c)",
256                sd->outgoing?'y':'n');
257   gras_assert1(sd->plugin->chunk_send,
258                "No function chunk_send on transport plugin %s",
259                sd->plugin->name);
260   return (*sd->plugin->chunk_send)(sd,data,size);
261 }
262 /**
263  * gras_trp_chunk_recv:
264  *
265  * Receive a bunch of bytes from a socket
266  */
267 gras_error_t 
268 gras_trp_chunk_recv(gras_socket_t *sd,
269                     char *data,
270                     long int size) {
271   gras_assert0(sd->incoming,
272                "Socket not suited for data receive");
273   gras_assert1(sd->plugin->chunk_recv,
274                "No function chunk_recv on transport plugin %s",
275                sd->plugin->name);
276   return (sd->plugin->chunk_recv)(sd,data,size);
277 }
278
279
280 gras_error_t
281 gras_trp_plugin_get_by_name(const char *name,
282                             gras_trp_plugin_t **dst){
283
284   return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
285 }
286
287 int   gras_socket_my_port  (gras_socket_t *sock) {
288   return sock->port;
289 }
290 int   gras_socket_peer_port(gras_socket_t *sock) {
291   return sock->peer_port;
292 }
293 char *gras_socket_peer_name(gras_socket_t *sock) {
294   return sock->peer_name;
295 }