Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Remove extern declaration for gras_opt_trp_nomoredata_on_close.
[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   xbt_assert(size >= 0, "Cannot send a negative amount of data");
212
213   while (size) {
214     int status = 0;
215
216     XBT_DEBUG("write(%d, %p, %ld);", sock->sd, data, (long int) size);
217     status = write(sock->sd, data, (long int) size);
218
219     if (status == -1) {
220       THROWF(system_error, 0, "write(%d,%p,%d) failed: %s",
221              sock->sd, data, (int) size, strerror(errno));
222     }
223
224     if (status) {
225       size -= status;
226       data += status;
227     } else {
228       THROWF(system_error, 0, "file descriptor closed");
229     }
230   }
231 }
232
233 /**
234  * gras_trp_file_chunk_recv:
235  *
236  * Receive data on a file pseudo-socket.
237  */
238 int
239 gras_trp_file_chunk_recv(xbt_socket_t sock,
240                          char *data, unsigned long int size)
241 {
242
243   int got = 0;
244
245   xbt_assert(sock, "Cannot recv on an NULL socket");
246   xbt_assert(sock->incoming, "Cannot recv on client file socket");
247   xbt_assert(size >= 0, "Cannot receive a negative amount of data");
248
249   if (sock->recvd) {
250     data[0] = sock->recvd_val;
251     sock->recvd = 0;
252     got++;
253     size--;
254   }
255
256   while (size) {
257     int status = 0;
258
259     status = read(sock->sd, data + got, (long int) size);
260     XBT_DEBUG("read(%d, %p, %ld);", sock->sd, data + got, size);
261
262     if (status < 0) {
263       THROWF(system_error, 0, "read(%d,%p,%d) failed: %s",
264              sock->sd, data + got, (int) size, strerror(errno));
265     }
266
267     if (status) {
268       size -= status;
269       got += status;
270     } else {
271       THROWF(system_error, errno, "file descriptor closed after %d bytes",
272              got);
273     }
274   }
275   return got;
276 }