Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c8a13d0dd200ed8a42a4fa70fe400477ba541494
[simgrid.git] / src / gras / Transport / transport_plugin_file.c
1 /* $Id$ */
2
3 /* File transport - send/receive a bunch of bytes from a file               */
4
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved.                  */
6
7 /* This program is free software; you can redistribute it and/or modify it
8  * under the terms of the license (GNU LGPL) which comes with this package. */
9
10 #include "portable.h"
11 #include "gras/Transport/transport_private.h"
12 #include "xbt/ex.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(gras_socket_t sd);
22
23 void gras_trp_file_chunk_send_raw(gras_socket_t sd,
24                                   const char *data, unsigned long int size);
25 void gras_trp_file_chunk_send(gras_socket_t sd,
26                               const char *data,
27                               unsigned long int size, int stable_ignored);
28
29 int gras_trp_file_chunk_recv(gras_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
46 /***
47  *** Code
48  ***/
49 void gras_trp_file_setup(gras_trp_plugin_t plug)
50 {
51
52   gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t, 1);
53
54   FD_ZERO(&(file->incoming_socks));
55
56   plug->socket_close = gras_trp_file_close;
57
58   plug->raw_send = gras_trp_file_chunk_send_raw;
59   plug->send = gras_trp_file_chunk_send;
60
61   plug->raw_recv = plug->recv = gras_trp_file_chunk_recv;
62
63   plug->data = (void *) file;
64 }
65
66 /**
67  * gras_socket_client_from_file:
68  *
69  * Create a client socket from a file path.
70  *
71  * This only possible in RL, and is mainly for debugging.
72  */
73 gras_socket_t gras_socket_client_from_file(const char *path)
74 {
75   gras_socket_t res;
76
77   xbt_assert0(gras_if_RL(), "Cannot use file as socket in the simulator");
78
79   gras_trp_socket_new(0, &res);
80
81   res->plugin = gras_trp_plugin_get_by_name("file");
82
83   if (strcmp("-", path)) {
84     res->sd =
85       open(path, O_TRUNC | O_WRONLY | O_CREAT | O_BINARY,
86            S_IRUSR | S_IWUSR | S_IRGRP);
87
88     if (res->sd < 0) {
89       THROW2(system_error, 0,
90              "Cannot create a client socket from file %s: %s",
91              path, strerror(errno));
92     }
93   } else {
94     res->sd = 1;                /* stdout */
95   }
96
97   DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
98          path,
99          res->sd,
100          res->incoming ? 'y' : 'n',
101          res->outgoing ? 'y' : 'n', res->accepting ? 'y' : 'n');
102
103   xbt_dynar_push(((gras_trp_procdata_t)
104                   gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
105   return res;
106 }
107
108 /**
109  * gras_socket_server_from_file:
110  *
111  * Create a server socket from a file path.
112  *
113  * This only possible in RL, and is mainly for debugging.
114  */
115 gras_socket_t gras_socket_server_from_file(const char *path)
116 {
117   gras_socket_t res;
118
119   xbt_assert0(gras_if_RL(), "Cannot use file as socket in the simulator");
120
121   gras_trp_socket_new(1, &res);
122
123   res->plugin = gras_trp_plugin_get_by_name("file");
124
125
126   if (strcmp("-", path)) {
127     res->sd = open(path, O_RDONLY | O_BINARY);
128
129     if (res->sd < 0) {
130       THROW2(system_error, 0,
131              "Cannot create a server socket from file %s: %s",
132              path, strerror(errno));
133     }
134   } else {
135     res->sd = 0;                /* stdin */
136   }
137
138   DEBUG4("sd=%d in=%c out=%c accept=%c",
139          res->sd,
140          res->incoming ? 'y' : 'n',
141          res->outgoing ? 'y' : 'n', res->accepting ? 'y' : 'n');
142
143   xbt_dynar_push(((gras_trp_procdata_t)
144                   gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
145   gras_msg_listener_awake();
146   return res;
147 }
148
149 void gras_trp_file_close(gras_socket_t sock)
150 {
151   gras_trp_file_plug_data_t *data;
152
153   if (!sock)
154     return;                     /* close only once */
155   data = sock->plugin->data;
156
157   if (sock->sd == 0) {
158     DEBUG0("Do not close stdin");
159   } else if (sock->sd == 1) {
160     DEBUG0("Do not close stdout");
161   } else {
162     DEBUG1("close file connection %d", sock->sd);
163
164     /* forget about the socket */
165     FD_CLR(sock->sd, &(data->incoming_socks));
166
167     /* close the socket */
168     if (close(sock->sd) < 0) {
169       WARN2("error while closing file %d: %s", sock->sd, strerror(errno));
170     }
171   }
172 }
173
174 /**
175  * gras_trp_file_chunk_send:
176  *
177  * Send data on a file pseudo-socket
178  */
179 void
180 gras_trp_file_chunk_send(gras_socket_t sock,
181                          const char *data,
182                          unsigned long int size, int stable_ignored)
183 {
184   gras_trp_file_chunk_send_raw(sock, data, size);
185 }
186
187 void
188 gras_trp_file_chunk_send_raw(gras_socket_t sock,
189                              const char *data, unsigned long int size)
190 {
191
192   xbt_assert0(sock->outgoing, "Cannot write on client file socket");
193   xbt_assert0(size >= 0, "Cannot send a negative amount of data");
194
195   while (size) {
196     int status = 0;
197
198     DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int) size);
199     status = write(sock->sd, data, (long int) size);
200
201     if (status == -1) {
202       THROW4(system_error, 0, "write(%d,%p,%d) failed: %s",
203              sock->sd, data, (int) size, strerror(errno));
204     }
205
206     if (status) {
207       size -= status;
208       data += status;
209     } else {
210       THROW0(system_error, 0, "file descriptor closed");
211     }
212   }
213 }
214
215 /**
216  * gras_trp_file_chunk_recv:
217  *
218  * Receive data on a file pseudo-socket.
219  */
220 int
221 gras_trp_file_chunk_recv(gras_socket_t sock,
222                          char *data, unsigned long int size)
223 {
224
225   int got = 0;
226
227   xbt_assert0(sock, "Cannot recv on an NULL socket");
228   xbt_assert0(sock->incoming, "Cannot recv on client file socket");
229   xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
230
231   if (sock->recvd) {
232     data[0] = sock->recvd_val;
233     sock->recvd = 0;
234     got++;
235     size--;
236   }
237
238   while (size) {
239     int status = 0;
240
241     status = read(sock->sd, data + got, (long int) size);
242     DEBUG3("read(%d, %p, %ld);", sock->sd, data + got, size);
243
244     if (status < 0) {
245       THROW4(system_error, 0, "read(%d,%p,%d) failed: %s",
246              sock->sd, data + got, (int) size, strerror(errno));
247     }
248
249     if (status) {
250       size -= status;
251       got += status;
252     } else {
253       THROW1(system_error, errno, "file descriptor closed after %d bytes",
254              got);
255     }
256   }
257   return got;
258 }