Logo AND Algorithmique Numérique Distribuée

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