Logo AND Algorithmique Numérique Distribuée

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