Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
It works. Isn't that great?
[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    
111   return no_error;
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_error_t
122 gras_socket_server_from_file(const char*path,
123                              /* OUT */ gras_socket_t **dst) {
124   gras_error_t errcode;
125   gras_trp_plugin_t *trp;
126
127   gras_assert0(gras_if_RL(),
128                "Cannot use file as socket in the simulator");
129
130   TRY(gras_trp_socket_new(1,dst));
131
132   TRY(gras_trp_plugin_get_by_name("file",&trp));
133   (*dst)->plugin=trp;
134
135
136   if (strcmp("-", path)) {
137     (*dst)->sd = open(path, O_RDONLY );
138
139     if ( (*dst)->sd < 0) {
140       RAISE2(system_error,
141              "Cannot create a server socket from file %s: %s",
142              path, strerror(errno));
143     }
144   } else {
145     (*dst)->sd = 0; /* stdin */
146   }
147
148   DEBUG4("sd=%d in=%c out=%c accept=%c",
149          (*dst)->sd,
150          (*dst)->incoming?'y':'n', 
151          (*dst)->outgoing?'y':'n',
152          (*dst)->accepting?'y':'n');
153
154   return no_error;
155 }
156
157 void gras_trp_file_close(gras_socket_t *sock){
158   gras_trp_file_plug_data_t *data;
159   
160   if (!sock) return; /* close only once */
161   data=sock->plugin->data;
162
163   if (sock->sd == 0) {
164     DEBUG0("Do not close stdin");
165   } else if (sock->sd == 1) {
166     DEBUG0("Do not close stdout");
167   } else {
168     DEBUG1("close file connection %d", sock->sd);
169
170     /* forget about the socket */
171     FD_CLR(sock->sd, &(data->incoming_socks));
172
173     /* close the socket */
174     if(close(sock->sd) < 0) {
175       WARN2("error while closing file %d: %s", 
176                sock->sd, strerror(errno));
177     }
178   }
179 }
180
181 /**
182  * gras_trp_file_chunk_send:
183  *
184  * Send data on a file pseudo-socket
185  */
186 gras_error_t 
187 gras_trp_file_chunk_send(gras_socket_t *sock,
188                          char *data,
189                          size_t size) {
190   
191   gras_assert0(sock->outgoing, "Cannot write on client file socket");
192   gras_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, (size_t)size);
198     status = write(sock->sd, data, (size_t)size);
199     
200     if (status == -1) {
201       RAISE4(system_error,"write(%d,%p,%d) failed: %s",
202              sock->sd, data, (int)size,
203              strerror(errno));
204     }
205     
206     if (status) {
207       size  -= status;
208       data  += status;
209     } else {
210       RAISE0(system_error,"file descriptor closed");
211     }
212   }
213
214   return no_error;
215 }
216 /**
217  * gras_trp_file_chunk_recv:
218  *
219  * Receive data on a file pseudo-socket.
220  */
221 gras_error_t 
222 gras_trp_file_chunk_recv(gras_socket_t *sock,
223                         char *data,
224                         size_t size) {
225
226   gras_assert0(sock, "Cannot recv on an NULL socket");
227   gras_assert0(sock->incoming, "Cannot recv on client file socket");
228   gras_assert0(size >= 0, "Cannot receive a negative amount of data");
229   
230   while (size) {
231     int status = 0;
232     
233     status = read(sock->sd, data, (size_t)size);
234     DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
235     
236     if (status == -1) {
237       RAISE4(system_error,"read(%d,%p,%d) failed: %s",
238              sock->sd, data, (int)size,
239              strerror(errno));
240     }
241     
242     if (status) {
243       size  -= status;
244       data  += status;
245     } else {
246       RAISE0(system_error,"file descriptor closed");
247     }
248   }
249   
250   return no_error;
251 }
252