Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Continuation of the internal cleanup; Copy/paste of the SG implementation (not workin...
[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 /***
22  *** Prototypes 
23  ***/
24 void         gras_trp_file_close(gras_socket_t *sd);
25   
26 gras_error_t gras_trp_file_chunk_send(gras_socket_t *sd,
27                                       char *data,
28                                       size_t size);
29
30 gras_error_t gras_trp_file_chunk_recv(gras_socket_t *sd,
31                                       char *data,
32                                       size_t size);
33
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 gras_error_t
53 gras_trp_file_setup(gras_trp_plugin_t *plug) {
54
55   gras_trp_file_plug_data_t *file = malloc(sizeof(gras_trp_file_plug_data_t));
56   if (!file)
57     RAISE_MALLOC;
58
59   FD_ZERO(&(file->incoming_socks));
60
61   plug->socket_close = gras_trp_file_close;
62
63   plug->chunk_send   = gras_trp_file_chunk_send;
64   plug->chunk_recv   = gras_trp_file_chunk_recv;
65
66   plug->data         = (void*)file;
67
68   return no_error;
69 }
70
71 /**
72  * gras_socket_client_from_file:
73  *
74  * Create a client socket from a file path.
75  *
76  * This only possible in RL, and is mainly for debugging.
77  */
78 gras_error_t
79 gras_socket_client_from_file(const char*path,
80                              /* OUT */ gras_socket_t **dst) {
81   gras_error_t errcode;
82   gras_trp_plugin_t *trp;
83
84   gras_assert0(gras_if_RL(),
85                "Cannot use file as socket in the simulator");
86
87   TRY(gras_trp_socket_new(0,dst));
88
89   TRY(gras_trp_plugin_get_by_name("file",&trp));
90   (*dst)->plugin=trp;
91
92   if (strcmp("-", path)) {
93     (*dst)->sd = open(path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP );
94     
95     if ( (*dst)->sd < 0) {
96       RAISE2(system_error,
97              "Cannot create a client socket from file %s: %s",
98              path, strerror(errno));
99     }
100   } else {
101     (*dst)->sd = 1; /* stdout */
102   }
103
104   DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
105          path,
106          (*dst)->sd,
107          (*dst)->incoming?'y':'n', 
108          (*dst)->outgoing?'y':'n',
109          (*dst)->accepting?'y':'n');
110   /* register socket */
111   errcode = gras_dynar_push(_gras_trp_sockets,dst);
112   if (errcode != no_error) {
113     free(*dst);
114     *dst = NULL;
115     return errcode;
116   }
117
118   return no_error;
119 }
120
121 /**
122  * gras_socket_server_from_file:
123  *
124  * Create a server socket from a file path.
125  *
126  * This only possible in RL, and is mainly for debugging.
127  */
128 gras_error_t
129 gras_socket_server_from_file(const char*path,
130                              /* OUT */ gras_socket_t **dst) {
131   gras_error_t errcode;
132   gras_trp_plugin_t *trp;
133
134   gras_assert0(gras_if_RL(),
135                "Cannot use file as socket in the simulator");
136
137   TRY(gras_trp_socket_new(1,dst));
138
139   TRY(gras_trp_plugin_get_by_name("file",&trp));
140   (*dst)->plugin=trp;
141
142
143   if (strcmp("-", path)) {
144     (*dst)->sd = open(path, O_RDONLY );
145
146     if ( (*dst)->sd < 0) {
147       RAISE2(system_error,
148              "Cannot create a server socket from file %s: %s",
149              path, strerror(errno));
150     }
151   } else {
152     (*dst)->sd = 0; /* stdin */
153   }
154
155   DEBUG4("sd=%d in=%c out=%c accept=%c",
156          (*dst)->sd,
157          (*dst)->incoming?'y':'n', 
158          (*dst)->outgoing?'y':'n',
159          (*dst)->accepting?'y':'n');
160
161   /* register socket */
162   errcode = gras_dynar_push(_gras_trp_sockets,dst);
163   if (errcode != no_error) {
164     free(*dst);
165     *dst = NULL;
166     return errcode;
167   }
168
169   return no_error;
170 }
171
172 void gras_trp_file_close(gras_socket_t *sock){
173   gras_trp_file_plug_data_t *data;
174   
175   if (!sock) return; /* close only once */
176   data=sock->plugin->data;
177
178   if (sock->sd == 0) {
179     DEBUG0("Do not close stdin");
180   } else if (sock->sd == 1) {
181     DEBUG0("Do not close stdout");
182   } else {
183     DEBUG1("close file connection %d", sock->sd);
184
185     /* forget about the socket */
186     FD_CLR(sock->sd, &(data->incoming_socks));
187
188     /* close the socket */
189     if(close(sock->sd) < 0) {
190       WARN2("error while closing file %d: %s", 
191                sock->sd, strerror(errno));
192     }
193   }
194 }
195
196 /**
197  * gras_trp_file_chunk_send:
198  *
199  * Send data on a file pseudo-socket
200  */
201 gras_error_t 
202 gras_trp_file_chunk_send(gras_socket_t *sock,
203                          char *data,
204                          size_t size) {
205   
206   gras_assert0(sock->outgoing, "Cannot write on client file socket");
207   gras_assert0(size >= 0, "Cannot send a negative amount of data");
208
209   while (size) {
210     int status = 0;
211     
212     DEBUG3("write(%d, %p, %ld);", sock->sd, data, (size_t)size);
213     status = write(sock->sd, data, (size_t)size);
214     
215     if (status == -1) {
216       RAISE4(system_error,"write(%d,%p,%d) failed: %s",
217              sock->sd, data, (int)size,
218              strerror(errno));
219     }
220     
221     if (status) {
222       size  -= status;
223       data  += status;
224     } else {
225       RAISE0(system_error,"file descriptor closed");
226     }
227   }
228
229   return no_error;
230 }
231 /**
232  * gras_trp_file_chunk_recv:
233  *
234  * Receive data on a file pseudo-socket.
235  */
236 gras_error_t 
237 gras_trp_file_chunk_recv(gras_socket_t *sock,
238                         char *data,
239                         size_t size) {
240
241   gras_assert0(sock, "Cannot recv on an NULL socket");
242   gras_assert0(sock->incoming, "Cannot recv on client file socket");
243   gras_assert0(size >= 0, "Cannot receive a negative amount of data");
244   
245   while (size) {
246     int status = 0;
247     
248     status = read(sock->sd, data, (size_t)size);
249     DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
250     
251     if (status == -1) {
252       RAISE4(system_error,"read(%d,%p,%d) failed: %s",
253              sock->sd, data, (int)size,
254              strerror(errno));
255     }
256     
257     if (status) {
258       size  -= status;
259       data  += status;
260     } else {
261       RAISE0(system_error,"file descriptor closed");
262     }
263   }
264   
265   return no_error;
266 }
267