Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Initial structure to implement gras using simix.
[simgrid.git] / src / gras_simix / Transport / gras_simix_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(gras_trp_file,gras_trp,
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_raw(gras_socket_t sd,
23                                   const char *data,
24                                   unsigned long int size);
25 void gras_trp_file_chunk_send(gras_socket_t sd,
26                               const char *data,
27                               unsigned long int size,
28                               int stable_ignored);
29
30 int gras_trp_file_chunk_recv(gras_socket_t sd,
31                              char *data,
32                              unsigned long int size);
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 void
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
60   plug->raw_send = gras_trp_file_chunk_send_raw;
61   plug->send = gras_trp_file_chunk_send;
62
63   plug->raw_recv = plug->recv = gras_trp_file_chunk_recv;
64
65   plug->data         = (void*)file;
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 gras_socket_t
76 gras_socket_client_from_file(const char*path) {
77   gras_socket_t res;
78
79   xbt_assert0(gras_if_RL(),
80                "Cannot use file as socket in the simulator");
81
82   gras_trp_socket_new(0,&res);
83
84   res->plugin=gras_trp_plugin_get_by_name("file");
85
86   if (strcmp("-", path)) {
87     res->sd = open(path, O_WRONLY|O_CREAT | O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP );
88     
89     if ( res->sd < 0) {
90       THROW2(system_error,0,
91              "Cannot create a client socket from file %s: %s",
92              path, strerror(errno));
93     }
94   } else {
95     res->sd = 1; /* stdout */
96   }
97
98   res->recv_ok=0;
99   DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
100          path,
101          res->sd,
102          res->incoming?'y':'n', 
103          res->outgoing?'y':'n',
104          res->accepting?'y':'n');
105
106   return res;
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 gras_socket_t gras_socket_server_from_file(const char*path) {
117   gras_socket_t res;
118
119   xbt_assert0(gras_if_RL(),
120                "Cannot use file as socket in the simulator");
121
122   gras_trp_socket_new(1,&res);
123
124   res->plugin=gras_trp_plugin_get_by_name("file");
125
126
127   if (strcmp("-", path)) {
128     res->sd = open(path, O_RDONLY | O_BINARY);
129
130     if ( res->sd < 0) {
131       THROW2(system_error,0,
132              "Cannot create a server socket from file %s: %s",
133              path, strerror(errno));
134     }
135   } else {
136     res->sd = 0; /* stdin */
137   }
138
139   DEBUG4("sd=%d in=%c out=%c accept=%c",
140          res->sd,
141          res->incoming?'y':'n', 
142          res->outgoing?'y':'n',
143          res->accepting?'y':'n');
144
145   res->recv_ok=0;
146   return res;
147 }
148
149 void gras_trp_file_close(gras_socket_t sock){
150   gras_trp_file_plug_data_t *data;
151   
152   if (!sock) return; /* close only once */
153   data=sock->plugin->data;
154
155   if (sock->sd == 0) {
156     DEBUG0("Do not close stdin");
157   } else if (sock->sd == 1) {
158     DEBUG0("Do not close stdout");
159   } else {
160     DEBUG1("close file connection %d", sock->sd);
161
162     /* forget about the socket */
163     FD_CLR(sock->sd, &(data->incoming_socks));
164
165     /* close the socket */
166     if(close(sock->sd) < 0) {
167       WARN2("error while closing file %d: %s", 
168                sock->sd, strerror(errno));
169     }
170   }
171 }
172
173 /**
174  * gras_trp_file_chunk_send:
175  *
176  * Send data on a file pseudo-socket
177  */
178 void
179 gras_trp_file_chunk_send(gras_socket_t sock,
180                          const char *data,
181                          unsigned long int size,
182                          int stable_ignored) {
183   gras_trp_file_chunk_send_raw(sock,data,size);
184 }
185 void
186 gras_trp_file_chunk_send_raw(gras_socket_t sock,
187                              const char *data,
188                              unsigned long int size) {
189   
190   xbt_assert0(sock->outgoing, "Cannot write on client file socket");
191   xbt_assert0(size >= 0, "Cannot send a negative amount of data");
192
193   while (size) {
194     int status = 0;
195     
196     DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
197     status = write(sock->sd, data, (long int)size);
198     
199     if (status == -1) {
200       THROW4(system_error,0,"write(%d,%p,%d) failed: %s",
201              sock->sd, data, (int)size,
202              strerror(errno));
203     }
204     
205     if (status) {
206       size  -= status;
207       data  += status;
208     } else {
209       THROW0(system_error,0,"file descriptor closed");
210     }
211   }
212 }
213 /**
214  * gras_trp_file_chunk_recv:
215  *
216  * Receive data on a file pseudo-socket.
217  */
218 int
219 gras_trp_file_chunk_recv(gras_socket_t sock,
220                          char *data,
221                          unsigned long int size) {
222
223   int got = 0;
224
225   xbt_assert0(sock, "Cannot recv on an NULL socket");
226   xbt_assert0(sock->incoming, "Cannot recv on client file socket");
227   xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
228
229   while (size) {
230     int status = 0;
231     
232     status = read(sock->sd, data+got, (long int)size);
233     DEBUG3("read(%d, %p, %ld);", sock->sd, data+got, size);
234     
235     if (status == -1) {
236       THROW4(system_error,0,"read(%d,%p,%d) failed: %s",
237              sock->sd, data+got, (int)size,
238              strerror(errno));
239     }
240     
241     if (status) {
242       size    -= status;
243       got    += status;
244     } else {
245       THROW0(system_error,0,"file descriptor closed");
246     }
247   }
248   return got;
249 }
250