Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
a8224d5cd56c42709d66f7edd1b542b6f21d80d9
[simgrid.git] / src / gras / DataDesc / net_driver_fd.c
1 /* gs_fd_net_driver.c */
2
3 #include "DataDesc/gs_private.h"
4
5 struct s_gs_fd_net_driver {
6         /**/
7         int dummy;
8 };
9
10 struct s_gs_fd_connection {
11         int fd;
12 };
13
14 static
15 struct s_gs_net_driver_ops *net_driver_ops = NULL;
16
17 static
18 struct s_gs_connection_ops *connection_ops = NULL;
19
20 struct s_gs_net_driver_ops *
21 gs_fd_net_driver(void) {
22         if (!connection_ops) {
23                 connection_ops = calloc(1, sizeof(struct s_gs_connection_ops));
24
25                 connection_ops->_init   = gs_fd_connection__init;
26                 connection_ops->_exit   = gs_fd_connection__exit;
27                 connection_ops->write   = gs_fd_connection_write;
28                 connection_ops->read    = gs_fd_connection_read;
29                 connection_ops->flush   = gs_fd_connection_flush;
30         }
31
32         if (!net_driver_ops) {
33                 net_driver_ops = calloc(1, sizeof(struct s_gs_net_driver_ops));
34
35                 net_driver_ops->_init   = gs_fd__init;
36                 net_driver_ops->_exit   = gs_fd__exit;
37         }
38
39         return net_driver_ops;
40 }
41
42 void
43 gs_fd__init(struct s_gs_net_driver      *p_net_driver) {
44
45         struct s_gs_fd_net_driver *p_fd = NULL;
46
47         p_fd = calloc(1, sizeof(struct s_gs_fd_net_driver));
48         p_net_driver->connection_ops    = connection_ops;
49         p_net_driver->specific          = p_fd;
50 }
51
52 void
53 gs_fd__exit(struct s_gs_net_driver      *p_net_driver) {
54
55         struct s_gs_fd_net_driver *p_fd = p_net_driver->specific;
56
57         free(p_fd);
58
59         p_net_driver->specific = NULL;
60 }
61
62
63 void
64 gs_fd_connection__init(struct s_gs_connection   *p_connection,
65                        void                     *arg) {
66
67         struct s_gs_fd_net_connection_arg       *p_fd_arg = arg;
68         struct s_gs_fd_connection               *p_fd_cnx = NULL;
69
70         p_fd_cnx                = calloc(1, sizeof(struct s_gs_fd_connection));
71         p_fd_cnx->fd            = p_fd_arg->fd;
72         p_connection->specific  = p_fd_cnx;
73 }
74
75 void
76 gs_fd_connection__exit(struct s_gs_connection   *p_connection) {
77         struct s_gs_fd_connection *p_fd_cnx = p_connection->specific;
78
79         free(p_fd_cnx);
80
81         p_connection->specific = NULL;
82 }
83
84 void
85 gs_fd_connection_write(struct s_gs_connection   *p_connection,
86                        void                     *_ptr,
87                        long int                  length) {
88         struct s_gs_fd_connection *p_fd_cnx = p_connection->specific;
89         unsigned char             *ptr      = _ptr;
90
91         if (p_connection->direction != e_gs_connection_direction_outgoing)
92                 GS_FAILURE("invalid operation");
93
94         while (length) {
95                 int status = 0;
96
97                 status = write(p_fd_cnx->fd, ptr, (size_t)length);
98                 fprintf(stderr, "write(%d, %p, %ld);\n", p_fd_cnx->fd, ptr, length);
99
100                 if (status == -1) {
101                         perror("write");
102                         GS_FAILURE("system call failed");
103                 }
104
105                 if (status) {
106                         length  -= status;
107                         ptr     += status;
108                 } else {
109                         GS_FAILURE("file descriptor closed");
110                 }
111         }
112 }
113
114 void
115 gs_fd_connection_read(struct s_gs_connection    *p_connection,
116                       void                      *_ptr,
117                       long int                   length) {
118         struct s_gs_fd_connection *p_fd_cnx = p_connection->specific;
119         unsigned char             *ptr      = _ptr;
120
121         if (p_connection->direction != e_gs_connection_direction_incoming)
122                 GS_FAILURE("invalid operation");
123
124         while (length) {
125                 int status = 0;
126
127                 status = read(p_fd_cnx->fd, ptr, (size_t)length);
128                 fprintf(stderr, "read(%d, %p, %ld);\n", p_fd_cnx->fd, ptr, length);
129
130                 if (status == -1) {
131                         perror("write");
132                         GS_FAILURE("system call failed");
133                 }
134
135                 if (status) {
136                         length  -= status;
137                         ptr     += status;
138                 } else {
139                         GS_FAILURE("file descriptor closed");
140                 }
141         }
142 }
143 void
144 gs_fd_connection_flush(struct s_gs_connection   *p_connection) {
145         (void)p_connection;
146
147         /* */
148 }
149