Logo AND Algorithmique Numérique Distribuée

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