Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
ba7adad0b12ef847e97fa5aa21b69f32c35cdcc5
[simgrid.git] / src / gras / gs / net_driver.c
1 /* gs_net_driver.c */
2
3 #include "gs/gs_private.h"
4
5 static
6 gras_dict_t *p_net_driver_dict = NULL;
7
8 void
9 gs_net_drivers_init(void) {
10         gras_dict_insert(p_net_driver_dict, "fd", gs_fd_net_driver(), NULL);
11 }
12
13
14 struct s_gs_net_driver *
15 gs_net_driver_init(const char *name) {
16
17         struct s_gs_net_driver_ops      *net_driver_ops = NULL;
18         struct s_gs_net_driver          *p_net_driver   = NULL;
19
20         gras_dict_retrieve(p_net_driver_dict, name, (void **)&net_driver_ops);
21         if (!net_driver_ops)
22                 GS_FAILURE("net driver not found");
23
24         p_net_driver                    = calloc(1, sizeof (struct s_gs_net_driver));
25         p_net_driver->net_ops           = net_driver_ops;
26         p_net_driver->connection_ops    = NULL;
27         p_net_driver->specific          = NULL;
28
29         if (net_driver_ops->_init) {
30                 net_driver_ops->_init(p_net_driver);
31         }
32
33         return p_net_driver;
34 }
35
36
37
38 void
39 gs_net_driver_exit(struct s_gs_net_driver       *p_net_driver){
40
41         struct s_gs_net_driver_ops      *net_driver_ops = NULL;
42
43         net_driver_ops = p_net_driver->net_ops;
44         if (net_driver_ops->_exit) {
45                 net_driver_ops->_exit(p_net_driver);
46         }
47
48         p_net_driver->net_ops = NULL;
49
50         if (p_net_driver->specific) {
51                 free(p_net_driver->specific);
52                 p_net_driver->specific = NULL;
53         }
54
55         free(p_net_driver);
56 }
57
58 struct s_gs_connection *
59 gs_net_connection_connect(struct s_gs_net_driver        *p_net_driver,
60                           void                          *arg) {
61
62         struct s_gs_connection_ops      *connection_ops = p_net_driver->connection_ops;
63         struct s_gs_connection          *p_connection   = NULL;
64
65         p_connection = malloc(sizeof (struct s_gs_connection));
66         p_connection->connection_ops    = connection_ops;
67         p_connection->p_net_driver      = p_net_driver;
68         p_connection->direction         = e_gs_connection_direction_outgoing;
69
70         if (connection_ops->_init) {
71                 connection_ops->_init(p_connection, arg);
72         }
73
74         return p_connection;
75 }
76
77 struct s_gs_connection *
78 gs_net_connection_accept(struct s_gs_net_driver *p_net_driver,
79                          void                   *arg) {
80
81         struct s_gs_connection_ops      *connection_ops = p_net_driver->connection_ops;
82         struct s_gs_connection          *p_connection   = NULL;
83
84         p_connection = malloc(sizeof (struct s_gs_connection));
85         p_connection->connection_ops    = connection_ops;
86         p_connection->p_net_driver      = p_net_driver;
87         p_connection->direction         = e_gs_connection_direction_incoming;
88
89         if (connection_ops->_init) {
90                 connection_ops->_init(p_connection, arg);
91         }
92
93         return p_connection;
94 }
95
96 void
97 gs_net_connection_close(struct s_gs_connection  *p_connection) {
98         if (p_connection->connection_ops->_exit) {
99                 p_connection->connection_ops->_exit(p_connection);
100         }
101
102         if (p_connection->specific) {
103                 free(p_connection->specific);
104                 p_connection->specific = NULL;
105         }
106
107         free(p_connection);
108 }