Logo AND Algorithmique Numérique Distribuée

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