Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
7c78364b18ce7de3d76a81d9f124b4a72b35e2c9
[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,plug->name, plug, gras_trp_plugin_free));
45
46   return no_error;
47 }
48
49 void
50 gras_trp_exit(void){
51   gras_dict_free(&_gras_trp_plugins);
52   gras_dynar_free(_gras_trp_sockets);
53 }
54
55
56 void gras_trp_plugin_free(void *p) {
57   gras_trp_plugin_t *plug = p;
58
59   if (plug) {
60     if (plug->free_specific && plug->specific)
61       plug->free_specific(plug->specific);
62
63     free(plug->name);
64     free(plug);
65   }
66 }
67
68 void gras_socket_close(gras_socket_t *sock) {
69   gras_socket_t *sock_iter;
70   int cursor;
71
72   /* FIXME: Issue an event when the socket is closed */
73   if (sock) {
74     gras_dynar_foreach(_gras_trp_sockets,cursor,sock_iter) {
75       if (sock == sock_iter) {
76         gras_dynar_cursor_rm(_gras_trp_sockets,&cursor);
77         if (sock->plugin->socket_close) 
78           (*sock->plugin->socket_close)(sock);
79
80         /* free the memory */
81         free(sock);
82         return;
83       }
84     }
85     WARNING0("Ignoring request to free an unknown socket");
86   }
87 }
88
89 gras_error_t
90 gras_trp_plugin_get_by_name(const char *name,
91                             gras_trp_plugin_t **dst){
92
93   return gras_dict_retrieve(_gras_trp_plugins,name,(void**)dst);
94 }
95