Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
you fucking windows. Don't even think about translating my bytes
[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 "transport_private.h"
12
13 #if 0
14 #  include <errno.h>
15 #  include <sys/time.h>
16 #  include <sys/types.h>
17 #  include <sys/stat.h>
18 #  include <fcntl.h>
19 #  include <unistd.h>
20 #endif 
21
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_file,transport,
23         "Pseudo-transport to write to/read from a file");
24
25 /***
26  *** Prototypes 
27  ***/
28 void         gras_trp_file_close(gras_socket_t sd);
29   
30 xbt_error_t gras_trp_file_chunk_send(gras_socket_t sd,
31                                       const char *data,
32                                       long int size);
33
34 xbt_error_t gras_trp_file_chunk_recv(gras_socket_t sd,
35                                       char *data,
36                                       long int size);
37
38
39 /***
40  *** Specific plugin part
41  ***/
42
43 typedef struct {
44   fd_set incoming_socks;
45 } gras_trp_file_plug_data_t;
46
47 /***
48  *** Specific socket part
49  ***/
50
51
52
53 /***
54  *** Code
55  ***/
56 xbt_error_t
57 gras_trp_file_setup(gras_trp_plugin_t *plug) {
58
59   gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t,1);
60
61   FD_ZERO(&(file->incoming_socks));
62
63   plug->socket_close = gras_trp_file_close;
64   plug->chunk_send   = gras_trp_file_chunk_send;
65   plug->chunk_recv   = gras_trp_file_chunk_recv;
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 xbt_error_t
79 gras_socket_client_from_file(const char*path,
80                              /* OUT */ gras_socket_t *dst) {
81   xbt_error_t errcode;
82   gras_trp_plugin_t *trp;
83
84   xbt_assert0(gras_if_RL(),
85                "Cannot use file as socket in the simulator");
86
87   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 | O_BINARY, 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 xbt_error_t
122 gras_socket_server_from_file(const char*path,
123                              /* OUT */ gras_socket_t *dst) {
124   xbt_error_t errcode;
125   gras_trp_plugin_t *trp;
126
127   xbt_assert0(gras_if_RL(),
128                "Cannot use file as socket in the simulator");
129
130   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 | O_BINARY);
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 xbt_error_t 
187 gras_trp_file_chunk_send(gras_socket_t sock,
188                          const char *data,
189                          long int size) {
190   
191   xbt_assert0(sock->outgoing, "Cannot write on client file socket");
192   xbt_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, (long int)size);
198     status = write(sock->sd, data, (long int)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 xbt_error_t 
222 gras_trp_file_chunk_recv(gras_socket_t sock,
223                         char *data,
224                         long int size) {
225
226   xbt_assert0(sock, "Cannot recv on an NULL socket");
227   xbt_assert0(sock->incoming, "Cannot recv on client file socket");
228   xbt_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, (long int)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