Logo AND Algorithmique Numérique Distribuée

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