Logo AND Algorithmique Numérique Distribuée

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