Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
API cleanup: s/dict_insert/dict_set/ and s/dict_retrieve/dict_get/ for consistency...
[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
16 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(transport,GRAS);
17
18 static gras_dict_t  *_gras_trp_plugins;     /* All registered plugins */
19 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
20
21
22 gras_dynar_t *_gras_trp_sockets; /* all existing sockets */
23 static void gras_trp_socket_free(void *s); /* free one socket */
24
25 static fd_set FDread;
26
27 gras_error_t 
28 gras_trp_init(void){
29   gras_error_t errcode;
30   gras_trp_plugin_t *plug;
31   
32   /* make room for all socket ownership descriptions */
33   TRY(gras_dynar_new(&_gras_trp_sockets, sizeof(gras_socket_t*), NULL));
34
35   /* We do not ear for any socket for now */
36   FD_ZERO(&FDread);
37   
38   /* make room for all plugins */
39   TRY(gras_dict_new(&_gras_trp_plugins));
40
41   /* TCP */
42   TRY(gras_trp_tcp_init(&plug));
43   TRY(gras_dict_set(_gras_trp_plugins, 
44                     plug->name, plug, gras_trp_plugin_free));
45
46   /* FILE */
47   TRY(gras_trp_file_init(&plug));
48   TRY(gras_dict_set(_gras_trp_plugins, 
49                     plug->name, plug, gras_trp_plugin_free));
50
51   return no_error;
52 }
53
54 void
55 gras_trp_exit(void){
56   gras_dict_free(&_gras_trp_plugins);
57   gras_dynar_free(_gras_trp_sockets);
58 }
59
60
61 void gras_trp_plugin_free(void *p) {
62   gras_trp_plugin_t *plug = p;
63
64   if (plug) {
65     if (plug->free_specific && plug->specific)
66       plug->free_specific(plug->specific);
67
68     free(plug->name);
69     free(plug);
70   }
71 }
72
73
74 /**
75  * gras_trp_socket_new:
76  *
77  * Malloc a new socket, and initialize it with defaults
78  */
79 gras_error_t gras_trp_socket_new(int incoming,
80                                  gras_socket_t **dst) {
81
82   gras_socket_t *sock;
83
84   if (! (sock=malloc(sizeof(gras_socket_t))) )
85     RAISE_MALLOC;
86
87   sock->plugin = NULL;
88   sock->sd     = -1;
89
90   sock->incoming  = incoming? 1:0;
91   sock->outgoing  = incoming? 0:1;
92   sock->accepting = incoming? 1:0;
93   DEBUG3("in=%c out=%c accept=%c",
94          sock->incoming?'y':'n', 
95          sock->outgoing?'y':'n',
96          sock->accepting?'y':'n');
97
98   sock->port      = -1;
99   sock->peer_port = -1;
100   sock->peer_name = NULL;
101
102   *dst = sock;
103   return no_error;
104 }
105
106
107 /**
108  * gras_socket_server:
109  *
110  * Opens a server socket and make it ready to be listened to.
111  * In real life, you'll get a TCP socket.
112  */
113 gras_error_t
114 gras_socket_server(unsigned short port,
115                    /* OUT */ gras_socket_t **dst) {
116  
117   gras_error_t errcode;
118   gras_trp_plugin_t *trp;
119   gras_socket_t *sock;
120
121   *dst = NULL;
122
123   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "TCP" : "SG",
124                                   &trp));
125
126   /* defaults settings */
127   TRY(gras_trp_socket_new(1,&sock));
128   sock->plugin= trp;
129   sock->port=port;
130
131   /* Call plugin socket creation function */
132   errcode = trp->socket_server(trp, port, sock);
133   if (errcode != no_error) {
134     free(sock);
135     return errcode;
136   }
137
138   *dst = sock;
139   /* Register this socket */
140   errcode = gras_dynar_push(_gras_trp_sockets,dst);
141   if (errcode != no_error) {
142     free(sock);
143     *dst = NULL;
144     return errcode;
145   }
146
147   return no_error;
148 }
149
150 /**
151  * gras_socket_client:
152  *
153  * Opens a client socket to a remote host.
154  * In real life, you'll get a TCP socket.
155  */
156 gras_error_t
157 gras_socket_client(const char *host,
158                    unsigned short port,
159                    /* OUT */ gras_socket_t **dst) {
160  
161   gras_error_t errcode;
162   gras_trp_plugin_t *trp;
163   gras_socket_t *sock;
164
165   *dst = NULL;
166
167   TRY(gras_trp_plugin_get_by_name(gras_if_RL() ? "TCP" : "SG",
168                                   &trp));
169
170   /* defaults settings */
171   TRY(gras_trp_socket_new(0,&sock));
172   sock->plugin= trp;
173   sock->peer_port = port;
174   sock->peer_name = strdup(host?host:"localhost");
175
176   /* plugin-specific */
177   errcode= (* trp->socket_client)(trp, 
178                                   host ? host : "localhost", port,
179                                   sock);
180   if (errcode != no_error) {
181     free(sock);
182     return errcode;
183   }
184
185   /* register socket */
186   *dst = sock;
187   errcode = gras_dynar_push(_gras_trp_sockets,dst);
188   if (errcode != no_error) {
189     free(sock);
190     *dst = NULL;
191     return errcode;
192   }
193
194   return no_error;
195 }
196
197 void gras_socket_close(gras_socket_t **sock) {
198   gras_socket_t *sock_iter;
199   int cursor;
200
201   /* FIXME: Issue an event when the socket is closed */
202   if (sock && *sock) {
203     gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
204       if (*sock == sock_iter) {
205         gras_dynar_cursor_rm(_gras_trp_sockets,&cursor);
206         if ( (*sock)->plugin->socket_close) 
207           (* (*sock)->plugin->socket_close)(*sock);
208
209         /* free the memory */
210         free(*sock);
211         *sock=NULL;
212         return;
213       }
214     }
215     WARN0("Ignoring request to free an unknown socket");
216   }
217 }
218
219 /**
220  * gras_trp_chunk_send:
221  *
222  * Send a bunch of bytes from on socket
223  */
224 gras_error_t
225 gras_trp_chunk_send(gras_socket_t *sd,
226                     char *data,
227                     size_t size) {
228   gras_assert1(sd->outgoing,
229                "Socket not suited for data send (outgoing=%c)",
230                sd->outgoing?'y':'n');
231   gras_assert1(sd->plugin->chunk_send,
232                "No function chunk_send on transport plugin %s",
233                sd->plugin->name);
234   return (*sd->plugin->chunk_send)(sd,data,size);
235 }
236 /**
237  * gras_trp_chunk_recv:
238  *
239  * Receive a bunch of bytes from a socket
240  */
241 gras_error_t 
242 gras_trp_chunk_recv(gras_socket_t *sd,
243                     char *data,
244                     size_t size) {
245   gras_assert0(sd->incoming,
246                "Socket not suited for data receive");
247   gras_assert1(sd->plugin->chunk_recv,
248                "No function chunk_recv on transport plugin %s",
249                sd->plugin->name);
250   return (sd->plugin->chunk_recv)(sd,data,size);
251 }
252
253
254 gras_error_t
255 gras_trp_plugin_get_by_name(const char *name,
256                             gras_trp_plugin_t **dst){
257
258   return gras_dict_get(_gras_trp_plugins,name,(void**)dst);
259 }
260
261 int   gras_socket_my_port  (gras_socket_t *sock) {
262   return sock->port;
263 }
264 int   gras_socket_peer_port(gras_socket_t *sock) {
265   return sock->peer_port;
266 }
267 char *gras_socket_peer_name(gras_socket_t *sock) {
268   return sock->peer_name;
269 }