Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
bb3a4c345d12d6eb0d948c3c96319b9d4269990e
[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 /* Authors: Martin Quinson                                                  */
6 /* Copyright (C) 2004 Martin Quinson.                                       */
7
8 /* This program is free software; you can redistribute it and/or modify it
9    under the terms of the license (GNU LGPL) which comes with this package. */
10
11 #include <errno.h>
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <fcntl.h>
15
16 #include "gras_private.h"
17 #include "transport_private.h"
18
19 GRAS_LOG_NEW_DEFAULT_SUBCATEGORY(trp_file,transport);
20
21 typedef struct {
22   int buffsize;
23 } gras_trp_tcp_sock_specific_t;
24
25 /***
26  *** Prototypes 
27  ***/
28 void         gras_trp_file_close(gras_socket_t *sd);
29   
30 gras_error_t gras_trp_file_chunk_send(gras_socket_t *sd,
31                                       char *data,
32                                       size_t size);
33
34 gras_error_t gras_trp_file_chunk_recv(gras_socket_t *sd,
35                                       char *data,
36                                       size_t size);
37
38 void         gras_trp_file_free_specific(void *s);
39
40 /***
41  *** Specific plugin part
42  ***/
43
44 typedef struct {
45   fd_set incoming_socks;
46 } gras_trp_file_specific_t;
47
48 /***
49  *** Specific socket part
50  ***/
51
52
53 /***
54  *** Code
55  ***/
56 gras_error_t
57 gras_trp_file_init(gras_trp_plugin_t **dst) {
58
59   gras_trp_plugin_t *res=malloc(sizeof(gras_trp_plugin_t));
60   gras_trp_file_specific_t *specif = malloc(sizeof(gras_trp_file_specific_t));
61   if (!res || !specif)
62     RAISE_MALLOC;
63
64   FD_ZERO(&(specif->incoming_socks));
65
66   res->name = strdup("file");
67   res->socket_client = NULL;
68   res->socket_server = NULL;
69   res->socket_accept = NULL;
70   res->socket_close  = gras_trp_file_close;
71
72   res->chunk_send    = gras_trp_file_chunk_send;
73   res->chunk_recv    = gras_trp_file_chunk_recv;
74
75   res->specific      = (void*)specif;
76   res->free_specific = gras_trp_file_free_specific;
77
78   *dst = res;
79   return no_error;
80 }
81
82 void gras_trp_file_free_specific(void *s) {
83   gras_trp_file_specific_t *specific = s;
84   free(specific);
85 }
86
87 /**
88  * gras_socket_client_from_file:
89  *
90  * Create a client socket from a file path.
91  *
92  * This only possible in RL, and is mainly for debugging.
93  */
94 gras_error_t
95 gras_socket_client_from_file(const char*path,
96                              /* OUT */ gras_socket_t **dst) {
97   gras_error_t errcode;
98   gras_trp_plugin_t *trp;
99
100   gras_assert0(gras_if_RL(),
101                "Cannot use file as socket in the simulator");
102
103   TRY(gras_trp_socket_new(0,dst));
104
105   TRY(gras_trp_plugin_get_by_name("file",&trp));
106   (*dst)->plugin=trp;
107
108   if (strcmp("-", path)) {
109     (*dst)->sd = open(path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP );
110     
111     if ( (*dst)->sd < 0) {
112       RAISE2(system_error,
113              "Cannot create a client socket from file %s: %s",
114              path, strerror(errno));
115     }
116   } else {
117     (*dst)->sd = 1; /* stdout */
118   }
119
120   DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
121          path,
122          (*dst)->sd,
123          (*dst)->incoming?'y':'n', 
124          (*dst)->outgoing?'y':'n',
125          (*dst)->accepting?'y':'n');
126   /* register socket */
127   errcode = gras_dynar_push(_gras_trp_sockets,dst);
128   if (errcode != no_error) {
129     free(*dst);
130     *dst = NULL;
131     return errcode;
132   }
133
134   return no_error;
135 }
136
137 /**
138  * gras_socket_server_from_file:
139  *
140  * Create a server socket from a file path.
141  *
142  * This only possible in RL, and is mainly for debugging.
143  */
144 gras_error_t
145 gras_socket_server_from_file(const char*path,
146                              /* OUT */ gras_socket_t **dst) {
147   gras_error_t errcode;
148   gras_trp_plugin_t *trp;
149
150   gras_assert0(gras_if_RL(),
151                "Cannot use file as socket in the simulator");
152
153   TRY(gras_trp_socket_new(1,dst));
154
155   TRY(gras_trp_plugin_get_by_name("file",&trp));
156   (*dst)->plugin=trp;
157
158
159   if (strcmp("-", path)) {
160     (*dst)->sd = open(path, O_RDONLY );
161
162     if ( (*dst)->sd < 0) {
163       RAISE2(system_error,
164              "Cannot create a server socket from file %s: %s",
165              path, strerror(errno));
166     }
167   } else {
168     (*dst)->sd = 0; /* stdin */
169   }
170
171   DEBUG4("sd=%d in=%c out=%c accept=%c",
172          (*dst)->sd,
173          (*dst)->incoming?'y':'n', 
174          (*dst)->outgoing?'y':'n',
175          (*dst)->accepting?'y':'n');
176
177   /* register socket */
178   errcode = gras_dynar_push(_gras_trp_sockets,dst);
179   if (errcode != no_error) {
180     free(*dst);
181     *dst = NULL;
182     return errcode;
183   }
184
185   return no_error;
186 }
187
188 void gras_trp_file_close(gras_socket_t *sock){
189   gras_trp_file_specific_t *specific;
190   
191   if (!sock) return; /* close only once */
192   specific=sock->plugin->specific;
193
194   if (sock->sd == 0) {
195     DEBUG0("Do not close stdin");
196   } else if (sock->sd == 1) {
197     DEBUG0("Do not close stdout");
198   } else {
199     DEBUG1("close file connection %d", sock->sd);
200
201     /* forget about the socket */
202     FD_CLR(sock->sd, &(specific->incoming_socks));
203
204     /* close the socket */
205     if(close(sock->sd) < 0) {
206       WARNING2("error while closing file %d: %s", 
207                sock->sd, strerror(errno));
208     }
209   }
210 }
211
212 /**
213  * gras_trp_file_chunk_send:
214  *
215  * Send data on a file pseudo-socket
216  */
217 gras_error_t 
218 gras_trp_file_chunk_send(gras_socket_t *sock,
219                          char *data,
220                          size_t size) {
221   
222   gras_assert0(size >= 0, "Cannot send a negative amount of data");
223
224   while (size) {
225     int status = 0;
226     
227     DEBUG3("write(%d, %p, %ld);", sock->sd, data, (size_t)size);
228     status = write(sock->sd, data, (size_t)size);
229     
230     if (status == -1) {
231       RAISE4(system_error,"write(%d,%p,%d) failed: %s",
232              sock->sd, data, (int)size,
233              strerror(errno));
234     }
235     
236     if (status) {
237       size  -= status;
238       data  += status;
239     } else {
240       RAISE0(system_error,"file descriptor closed");
241     }
242   }
243
244   return no_error;
245 }
246 /**
247  * gras_trp_file_chunk_recv:
248  *
249  * Receive data on a file pseudo-socket.
250  */
251 gras_error_t 
252 gras_trp_file_chunk_recv(gras_socket_t *sock,
253                         char *data,
254                         size_t size) {
255
256   /* TCP sockets are in duplex mode, don't check direction */
257   gras_assert0(sock, "Cannot recv on an NULL socket");
258   gras_assert0(size >= 0, "Cannot receive a negative amount of data");
259   
260   while (size) {
261     int status = 0;
262     
263     status = read(sock->sd, data, (size_t)size);
264     DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
265     
266     if (status == -1) {
267       RAISE4(system_error,"read(%d,%p,%d) failed: %s",
268              sock->sd, data, (int)size,
269              strerror(errno));
270     }
271     
272     if (status) {
273       size  -= status;
274       data  += status;
275     } else {
276       RAISE0(system_error,"file descriptor closed");
277     }
278   }
279   
280   return no_error;
281 }
282