Logo AND Algorithmique Numérique Distribuée

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