Logo AND Algorithmique Numérique Distribuée

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