Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix make dist.
[simgrid.git] / src / gras / Transport / transport_plugin_file.c
1 /* File transport - send/receive a bunch of bytes from a file               */
2
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4  * All rights reserved.                                                     */
5
6 /* This program is free software; you can redistribute it and/or modify it
7  * under the terms of the license (GNU LGPL) which comes with this package. */
8
9 #include "portable.h"
10 #include "gras/Transport/transport_private.h"
11 #include "xbt/ex.h"
12 #include "xbt/xbt_socket_private.h"
13 #include "gras/Msg/msg_interface.h"     /* gras_msg_listener_awake */
14
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_file, gras_trp,
16                                 "Pseudo-transport to write to/read from a file");
17
18 /***
19  *** Prototypes
20  ***/
21 void gras_trp_file_close(xbt_socket_t sd);
22
23 void gras_trp_file_chunk_send_raw(xbt_socket_t sd,
24                                   const char *data,
25                                   unsigned long int size);
26 void gras_trp_file_chunk_send(xbt_socket_t sd, const char *data,
27                               unsigned long int size, int stable_ignored);
28
29 int gras_trp_file_chunk_recv(xbt_socket_t sd,
30                              char *data, unsigned long int size);
31
32 /***
33  *** Specific plugin part
34  ***/
35
36 typedef struct {
37   fd_set incoming_socks;
38 } gras_trp_file_plug_data_t;
39
40 /***
41  *** Specific socket part
42  ***/
43
44 /***
45  *** Info about who's speaking
46  ***/
47 static int gras_trp_file_my_port(xbt_socket_t s) {
48   THROW_UNIMPLEMENTED;
49 }
50 static int gras_trp_file_peer_port(xbt_socket_t s) {
51   THROW_UNIMPLEMENTED;
52 }
53 static const char* gras_trp_file_peer_name(xbt_socket_t s) {
54   THROW_UNIMPLEMENTED;
55 }
56 static const char* gras_trp_file_peer_proc(xbt_socket_t s) {
57   THROW_UNIMPLEMENTED;
58 }
59 static void gras_trp_file_peer_proc_set(xbt_socket_t s,char *name) {
60   THROW_UNIMPLEMENTED;
61 }
62
63 /***
64  *** Code
65  ***/
66 void gras_trp_file_setup(xbt_trp_plugin_t plug)
67 {
68
69   gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t, 1);
70
71   FD_ZERO(&(file->incoming_socks));
72
73   plug->my_port = gras_trp_file_my_port;
74   plug->peer_port = gras_trp_file_peer_port;
75   plug->peer_name = gras_trp_file_peer_name;
76   plug->peer_proc = gras_trp_file_peer_proc;
77   plug->peer_proc_set = gras_trp_file_peer_proc_set;
78
79   plug->socket_close = gras_trp_file_close;
80
81   plug->raw_send = gras_trp_file_chunk_send_raw;
82   plug->send = gras_trp_file_chunk_send;
83
84   plug->raw_recv = plug->recv = gras_trp_file_chunk_recv;
85
86   plug->data = (void *) file;
87 }
88
89 /**
90  * gras_socket_client_from_file:
91  *
92  * Create a client socket from a file path.
93  *
94  * This only possible in RL, and is mainly for debugging.
95  */
96 xbt_socket_t gras_socket_client_from_file(const char *path)
97 {
98   xbt_socket_t res;
99
100   xbt_assert(gras_if_RL(), "Cannot use file as socket in the simulator");
101
102   xbt_socket_new_ext(0, &res, xbt_trp_plugin_get_by_name("file"), 0, 0);
103
104   if (strcmp("-", path)) {
105     res->sd =
106         open(path, O_TRUNC | O_WRONLY | O_CREAT | O_BINARY,
107              S_IRUSR | S_IWUSR | S_IRGRP);
108
109     if (res->sd < 0) {
110       THROWF(system_error, 0,
111              "Cannot create a client socket from file %s: %s",
112              path, strerror(errno));
113     }
114   } else {
115     res->sd = 1;                /* stdout */
116   }
117
118   XBT_DEBUG("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
119          path,
120          res->sd,
121          res->incoming ? 'y' : 'n',
122          res->outgoing ? 'y' : 'n', res->accepting ? 'y' : 'n');
123
124   xbt_dynar_push(((gras_trp_procdata_t)
125                   gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
126   return res;
127 }
128
129 /**
130  * gras_socket_server_from_file:
131  *
132  * Create a server socket from a file path.
133  *
134  * This only possible in RL, and is mainly for debugging.
135  */
136 xbt_socket_t gras_socket_server_from_file(const char *path)
137 {
138   xbt_socket_t res;
139
140   xbt_assert(gras_if_RL(), "Cannot use file as socket in the simulator");
141
142   xbt_socket_new_ext(1, &res, xbt_trp_plugin_get_by_name("file"), 0, 0);
143
144   if (strcmp("-", path)) {
145     res->sd = open(path, O_RDONLY | O_BINARY);
146
147     if (res->sd < 0) {
148       THROWF(system_error, 0,
149              "Cannot create a server socket from file %s: %s",
150              path, strerror(errno));
151     }
152   } else {
153     res->sd = 0;                /* stdin */
154   }
155
156   XBT_DEBUG("sd=%d in=%c out=%c accept=%c",
157          res->sd,
158          res->incoming ? 'y' : 'n',
159          res->outgoing ? 'y' : 'n', res->accepting ? 'y' : 'n');
160
161   xbt_dynar_push(((gras_trp_procdata_t)
162                   gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
163   gras_msg_listener_awake();
164   return res;
165 }
166
167 void gras_trp_file_close(xbt_socket_t sock)
168 {
169   gras_trp_file_plug_data_t *data;
170
171   if (!sock)
172     return;                     /* close only once */
173   data = sock->plugin->data;
174
175   if (sock->sd == 0) {
176     XBT_DEBUG("Do not close stdin");
177   } else if (sock->sd == 1) {
178     XBT_DEBUG("Do not close stdout");
179   } else {
180     XBT_DEBUG("close file connection %d", sock->sd);
181
182     /* forget about the socket */
183     FD_CLR(sock->sd, &(data->incoming_socks));
184
185     /* close the socket */
186     if (close(sock->sd) < 0) {
187       XBT_WARN("error while closing file %d: %s", sock->sd, strerror(errno));
188     }
189   }
190 }
191
192 /**
193  * gras_trp_file_chunk_send:
194  *
195  * Send data on a file pseudo-socket
196  */
197 void
198 gras_trp_file_chunk_send(xbt_socket_t sock,
199                          const char *data,
200                          unsigned long int size, int stable_ignored)
201 {
202   gras_trp_file_chunk_send_raw(sock, data, size);
203 }
204
205 void
206 gras_trp_file_chunk_send_raw(xbt_socket_t sock,
207                              const char *data, unsigned long int size)
208 {
209
210   xbt_assert(sock->outgoing, "Cannot write on client file socket");
211
212   while (size) {
213     int status = 0;
214
215     XBT_DEBUG("write(%d, %p, %ld);", sock->sd, data, (long int) size);
216     status = write(sock->sd, data, (long int) size);
217
218     if (status == -1) {
219       THROWF(system_error, 0, "write(%d,%p,%d) failed: %s",
220              sock->sd, data, (int) size, strerror(errno));
221     }
222
223     if (status) {
224       size -= status;
225       data += status;
226     } else {
227       THROWF(system_error, 0, "file descriptor closed");
228     }
229   }
230 }
231
232 /**
233  * gras_trp_file_chunk_recv:
234  *
235  * Receive data on a file pseudo-socket.
236  */
237 int
238 gras_trp_file_chunk_recv(xbt_socket_t sock,
239                          char *data, unsigned long int size)
240 {
241
242   int got = 0;
243
244   xbt_assert(sock, "Cannot recv on an NULL socket");
245   xbt_assert(sock->incoming, "Cannot recv on client file socket");
246
247   if (sock->recvd) {
248     data[0] = sock->recvd_val;
249     sock->recvd = 0;
250     got++;
251     size--;
252   }
253
254   while (size) {
255     int status = 0;
256
257     status = read(sock->sd, data + got, (long int) size);
258     XBT_DEBUG("read(%d, %p, %lu);", sock->sd, data + got, size);
259
260     if (status < 0) {
261       THROWF(system_error, 0, "read(%d,%p,%d) failed: %s",
262              sock->sd, data + got, (int) size, strerror(errno));
263     }
264
265     if (status) {
266       size -= status;
267       got += status;
268     } else {
269       THROWF(system_error, errno, "file descriptor closed after %d bytes",
270              got);
271     }
272   }
273   return got;
274 }