Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Sanitize the log channels naming scheme in GRAS too
[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(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   DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
99          path,
100          res->sd,
101          res->incoming?'y':'n', 
102          res->outgoing?'y':'n',
103          res->accepting?'y':'n');
104
105   return res;
106 }
107
108 /**
109  * gras_socket_server_from_file:
110  *
111  * Create a server socket from a file path.
112  *
113  * This only possible in RL, and is mainly for debugging.
114  */
115 gras_socket_t gras_socket_server_from_file(const char*path) {
116   gras_socket_t res;
117
118   xbt_assert0(gras_if_RL(),
119                "Cannot use file as socket in the simulator");
120
121   gras_trp_socket_new(1,&res);
122
123   res->plugin=gras_trp_plugin_get_by_name("file");
124
125
126   if (strcmp("-", path)) {
127     res->sd = open(path, O_RDONLY | O_BINARY);
128
129     if ( res->sd < 0) {
130       THROW2(system_error,0,
131              "Cannot create a server socket from file %s: %s",
132              path, strerror(errno));
133     }
134   } else {
135     res->sd = 0; /* stdin */
136   }
137
138   DEBUG4("sd=%d in=%c out=%c accept=%c",
139          res->sd,
140          res->incoming?'y':'n', 
141          res->outgoing?'y':'n',
142          res->accepting?'y':'n');
143
144   return res;
145 }
146
147 void gras_trp_file_close(gras_socket_t sock){
148   gras_trp_file_plug_data_t *data;
149   
150   if (!sock) return; /* close only once */
151   data=sock->plugin->data;
152
153   if (sock->sd == 0) {
154     DEBUG0("Do not close stdin");
155   } else if (sock->sd == 1) {
156     DEBUG0("Do not close stdout");
157   } else {
158     DEBUG1("close file connection %d", sock->sd);
159
160     /* forget about the socket */
161     FD_CLR(sock->sd, &(data->incoming_socks));
162
163     /* close the socket */
164     if(close(sock->sd) < 0) {
165       WARN2("error while closing file %d: %s", 
166                sock->sd, strerror(errno));
167     }
168   }
169 }
170
171 /**
172  * gras_trp_file_chunk_send:
173  *
174  * Send data on a file pseudo-socket
175  */
176 void
177 gras_trp_file_chunk_send(gras_socket_t sock,
178                          const char *data,
179                          unsigned long int size,
180                          int stable_ignored) {
181   gras_trp_file_chunk_send_raw(sock,data,size);
182 }
183 void
184 gras_trp_file_chunk_send_raw(gras_socket_t sock,
185                              const char *data,
186                              unsigned long int size) {
187   
188   xbt_assert0(sock->outgoing, "Cannot write on client file socket");
189   xbt_assert0(size >= 0, "Cannot send a negative amount of data");
190
191   while (size) {
192     int status = 0;
193     
194     DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
195     status = write(sock->sd, data, (long int)size);
196     
197     if (status == -1) {
198       THROW4(system_error,0,"write(%d,%p,%d) failed: %s",
199              sock->sd, data, (int)size,
200              strerror(errno));
201     }
202     
203     if (status) {
204       size  -= status;
205       data  += status;
206     } else {
207       THROW0(system_error,0,"file descriptor closed");
208     }
209   }
210 }
211 /**
212  * gras_trp_file_chunk_recv:
213  *
214  * Receive data on a file pseudo-socket.
215  */
216 int
217 gras_trp_file_chunk_recv(gras_socket_t sock,
218                          char *data,
219                          unsigned long int size) {
220
221   int got = 0;
222
223   xbt_assert0(sock, "Cannot recv on an NULL socket");
224   xbt_assert0(sock->incoming, "Cannot recv on client file socket");
225   xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
226
227   while (size) {
228     int status = 0;
229     
230     status = read(sock->sd, data+got, (long int)size);
231     DEBUG3("read(%d, %p, %ld);", sock->sd, data+got, size);
232     
233     if (status == -1) {
234       THROW4(system_error,0,"read(%d,%p,%d) failed: %s",
235              sock->sd, data+got, (int)size,
236              strerror(errno));
237     }
238     
239     if (status) {
240       size    -= status;
241       got    += status;
242     } else {
243       THROW0(system_error,0,"file descriptor closed");
244     }
245   }
246   return got;
247 }
248