Logo AND Algorithmique Numérique Distribuée

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