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,
24 unsigned long int size);
25 void gras_trp_file_chunk_send(gras_socket_t sd, const char *data,
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
44 *** Info about who's speaking
46 static int gras_trp_file_my_port(gras_socket_t s) {
49 static int gras_trp_file_peer_port(gras_socket_t s) {
52 static const char* gras_trp_file_peer_name(gras_socket_t s) {
55 static const char* gras_trp_file_peer_proc(gras_socket_t s) {
58 static void gras_trp_file_peer_proc_set(gras_socket_t s,char *name) {
65 void gras_trp_file_setup(gras_trp_plugin_t plug)
68 gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t, 1);
70 FD_ZERO(&(file->incoming_socks));
72 plug->my_port = gras_trp_file_my_port;
73 plug->peer_port = gras_trp_file_peer_port;
74 plug->peer_name = gras_trp_file_peer_name;
75 plug->peer_proc = gras_trp_file_peer_proc;
76 plug->peer_proc_set = gras_trp_file_peer_proc_set;
78 plug->socket_close = gras_trp_file_close;
80 plug->raw_send = gras_trp_file_chunk_send_raw;
81 plug->send = gras_trp_file_chunk_send;
83 plug->raw_recv = plug->recv = gras_trp_file_chunk_recv;
85 plug->data = (void *) file;
89 * gras_socket_client_from_file:
91 * Create a client socket from a file path.
93 * This only possible in RL, and is mainly for debugging.
95 gras_socket_t gras_socket_client_from_file(const char *path)
99 xbt_assert(gras_if_RL(), "Cannot use file as socket in the simulator");
101 gras_trp_socket_new(0, &res);
103 res->plugin = gras_trp_plugin_get_by_name("file");
105 if (strcmp("-", path)) {
107 open(path, O_TRUNC | O_WRONLY | O_CREAT | O_BINARY,
108 S_IRUSR | S_IWUSR | S_IRGRP);
111 THROWF(system_error, 0,
112 "Cannot create a client socket from file %s: %s",
113 path, strerror(errno));
116 res->sd = 1; /* stdout */
119 XBT_DEBUG("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
122 res->incoming ? 'y' : 'n',
123 res->outgoing ? 'y' : 'n', res->accepting ? 'y' : 'n');
125 xbt_dynar_push(((gras_trp_procdata_t)
126 gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
131 * gras_socket_server_from_file:
133 * Create a server socket from a file path.
135 * This only possible in RL, and is mainly for debugging.
137 gras_socket_t gras_socket_server_from_file(const char *path)
141 xbt_assert(gras_if_RL(), "Cannot use file as socket in the simulator");
143 gras_trp_socket_new(1, &res);
145 res->plugin = gras_trp_plugin_get_by_name("file");
148 if (strcmp("-", path)) {
149 res->sd = open(path, O_RDONLY | O_BINARY);
152 THROWF(system_error, 0,
153 "Cannot create a server socket from file %s: %s",
154 path, strerror(errno));
157 res->sd = 0; /* stdin */
160 XBT_DEBUG("sd=%d in=%c out=%c accept=%c",
162 res->incoming ? 'y' : 'n',
163 res->outgoing ? 'y' : 'n', res->accepting ? 'y' : 'n');
165 xbt_dynar_push(((gras_trp_procdata_t)
166 gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
167 gras_msg_listener_awake();
171 void gras_trp_file_close(gras_socket_t sock)
173 gras_trp_file_plug_data_t *data;
176 return; /* close only once */
177 data = sock->plugin->data;
180 XBT_DEBUG("Do not close stdin");
181 } else if (sock->sd == 1) {
182 XBT_DEBUG("Do not close stdout");
184 XBT_DEBUG("close file connection %d", sock->sd);
186 /* forget about the socket */
187 FD_CLR(sock->sd, &(data->incoming_socks));
189 /* close the socket */
190 if (close(sock->sd) < 0) {
191 XBT_WARN("error while closing file %d: %s", sock->sd, strerror(errno));
197 * gras_trp_file_chunk_send:
199 * Send data on a file pseudo-socket
202 gras_trp_file_chunk_send(gras_socket_t sock,
204 unsigned long int size, int stable_ignored)
206 gras_trp_file_chunk_send_raw(sock, data, size);
210 gras_trp_file_chunk_send_raw(gras_socket_t sock,
211 const char *data, unsigned long int size)
214 xbt_assert(sock->outgoing, "Cannot write on client file socket");
215 xbt_assert(size >= 0, "Cannot send a negative amount of data");
220 XBT_DEBUG("write(%d, %p, %ld);", sock->sd, data, (long int) size);
221 status = write(sock->sd, data, (long int) size);
224 THROWF(system_error, 0, "write(%d,%p,%d) failed: %s",
225 sock->sd, data, (int) size, strerror(errno));
232 THROWF(system_error, 0, "file descriptor closed");
238 * gras_trp_file_chunk_recv:
240 * Receive data on a file pseudo-socket.
243 gras_trp_file_chunk_recv(gras_socket_t sock,
244 char *data, unsigned long int size)
249 xbt_assert(sock, "Cannot recv on an NULL socket");
250 xbt_assert(sock->incoming, "Cannot recv on client file socket");
251 xbt_assert(size >= 0, "Cannot receive a negative amount of data");
254 data[0] = sock->recvd_val;
263 status = read(sock->sd, data + got, (long int) size);
264 XBT_DEBUG("read(%d, %p, %ld);", sock->sd, data + got, size);
267 THROWF(system_error, 0, "read(%d,%p,%d) failed: %s",
268 sock->sd, data + got, (int) size, strerror(errno));
275 THROWF(system_error, errno, "file descriptor closed after %d bytes",