Logo AND Algorithmique Numérique Distribuée

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