1 /* File transport - send/receive a bunch of bytes from a file */
3 /* Copyright (c) 2004, 2005, 2006, 2007, 2009, 2010. The SimGrid Team.
4 * All rights reserved. */
6 /* This program is free software; you can redistribute it and/or modify it
7 * under the terms of the license (GNU LGPL) which comes with this package. */
10 #include "gras/Transport/transport_private.h"
12 #include "gras/Msg/msg_interface.h" /* gras_msg_listener_awake */
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_file, gras_trp,
15 "Pseudo-transport to write to/read from a file");
20 void gras_trp_file_close(gras_socket_t sd);
22 void gras_trp_file_chunk_send_raw(gras_socket_t sd,
23 const char *data, unsigned long int size);
24 void gras_trp_file_chunk_send(gras_socket_t sd,
26 unsigned long int size, int stable_ignored);
28 int gras_trp_file_chunk_recv(gras_socket_t sd,
29 char *data, unsigned long int size);
32 *** Specific plugin part
36 fd_set incoming_socks;
37 } gras_trp_file_plug_data_t;
40 *** Specific socket part
48 void gras_trp_file_setup(gras_trp_plugin_t plug)
51 gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t, 1);
53 FD_ZERO(&(file->incoming_socks));
55 plug->socket_close = gras_trp_file_close;
57 plug->raw_send = gras_trp_file_chunk_send_raw;
58 plug->send = gras_trp_file_chunk_send;
60 plug->raw_recv = plug->recv = gras_trp_file_chunk_recv;
62 plug->data = (void *) file;
66 * gras_socket_client_from_file:
68 * Create a client socket from a file path.
70 * This only possible in RL, and is mainly for debugging.
72 gras_socket_t gras_socket_client_from_file(const char *path)
76 xbt_assert0(gras_if_RL(), "Cannot use file as socket in the simulator");
78 gras_trp_socket_new(0, &res);
80 res->plugin = gras_trp_plugin_get_by_name("file");
82 if (strcmp("-", path)) {
84 open(path, O_TRUNC | O_WRONLY | O_CREAT | O_BINARY,
85 S_IRUSR | S_IWUSR | S_IRGRP);
88 THROW2(system_error, 0,
89 "Cannot create a client socket from file %s: %s",
90 path, strerror(errno));
93 res->sd = 1; /* stdout */
96 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
99 res->incoming ? 'y' : 'n',
100 res->outgoing ? 'y' : 'n', res->accepting ? 'y' : 'n');
102 xbt_dynar_push(((gras_trp_procdata_t)
103 gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
108 * gras_socket_server_from_file:
110 * Create a server socket from a file path.
112 * This only possible in RL, and is mainly for debugging.
114 gras_socket_t gras_socket_server_from_file(const char *path)
118 xbt_assert0(gras_if_RL(), "Cannot use file as socket in the simulator");
120 gras_trp_socket_new(1, &res);
122 res->plugin = gras_trp_plugin_get_by_name("file");
125 if (strcmp("-", path)) {
126 res->sd = open(path, O_RDONLY | O_BINARY);
129 THROW2(system_error, 0,
130 "Cannot create a server socket from file %s: %s",
131 path, strerror(errno));
134 res->sd = 0; /* stdin */
137 DEBUG4("sd=%d in=%c out=%c accept=%c",
139 res->incoming ? 'y' : 'n',
140 res->outgoing ? 'y' : 'n', res->accepting ? 'y' : 'n');
142 xbt_dynar_push(((gras_trp_procdata_t)
143 gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
144 gras_msg_listener_awake();
148 void gras_trp_file_close(gras_socket_t sock)
150 gras_trp_file_plug_data_t *data;
153 return; /* close only once */
154 data = sock->plugin->data;
157 DEBUG0("Do not close stdin");
158 } else if (sock->sd == 1) {
159 DEBUG0("Do not close stdout");
161 DEBUG1("close file connection %d", sock->sd);
163 /* forget about the socket */
164 FD_CLR(sock->sd, &(data->incoming_socks));
166 /* close the socket */
167 if (close(sock->sd) < 0) {
168 WARN2("error while closing file %d: %s", sock->sd, strerror(errno));
174 * gras_trp_file_chunk_send:
176 * Send data on a file pseudo-socket
179 gras_trp_file_chunk_send(gras_socket_t sock,
181 unsigned long int size, int stable_ignored)
183 gras_trp_file_chunk_send_raw(sock, data, size);
187 gras_trp_file_chunk_send_raw(gras_socket_t sock,
188 const char *data, unsigned long int size)
191 xbt_assert0(sock->outgoing, "Cannot write on client file socket");
192 xbt_assert0(size >= 0, "Cannot send a negative amount of data");
197 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int) size);
198 status = write(sock->sd, data, (long int) size);
201 THROW4(system_error, 0, "write(%d,%p,%d) failed: %s",
202 sock->sd, data, (int) size, strerror(errno));
209 THROW0(system_error, 0, "file descriptor closed");
215 * gras_trp_file_chunk_recv:
217 * Receive data on a file pseudo-socket.
220 gras_trp_file_chunk_recv(gras_socket_t sock,
221 char *data, unsigned long int size)
226 xbt_assert0(sock, "Cannot recv on an NULL socket");
227 xbt_assert0(sock->incoming, "Cannot recv on client file socket");
228 xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
231 data[0] = sock->recvd_val;
240 status = read(sock->sd, data + got, (long int) size);
241 DEBUG3("read(%d, %p, %ld);", sock->sd, data + got, size);
244 THROW4(system_error, 0, "read(%d,%p,%d) failed: %s",
245 sock->sd, data + got, (int) size, strerror(errno));
252 THROW1(system_error, errno, "file descriptor closed after %d bytes",