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 "xbt/xbt_socket_private.h"
13 #include "gras/Msg/msg_interface.h" /* gras_msg_listener_awake */
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_file, gras_trp,
16 "Pseudo-transport to write to/read from a file");
21 void gras_trp_file_close(xbt_socket_t sd);
23 void gras_trp_file_chunk_send_raw(xbt_socket_t sd,
25 unsigned long int size);
26 void gras_trp_file_chunk_send(xbt_socket_t sd, const char *data,
27 unsigned long int size, int stable_ignored);
29 int gras_trp_file_chunk_recv(xbt_socket_t sd,
30 char *data, unsigned long int size);
33 *** Specific plugin part
37 fd_set incoming_socks;
38 } gras_trp_file_plug_data_t;
41 *** Specific socket part
45 *** Info about who's speaking
47 static int gras_trp_file_my_port(xbt_socket_t s) {
50 static int gras_trp_file_peer_port(xbt_socket_t s) {
53 static const char* gras_trp_file_peer_name(xbt_socket_t s) {
56 static const char* gras_trp_file_peer_proc(xbt_socket_t s) {
59 static void gras_trp_file_peer_proc_set(xbt_socket_t s,char *name) {
66 void gras_trp_file_setup(xbt_trp_plugin_t plug)
69 gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t, 1);
71 FD_ZERO(&(file->incoming_socks));
73 plug->my_port = gras_trp_file_my_port;
74 plug->peer_port = gras_trp_file_peer_port;
75 plug->peer_name = gras_trp_file_peer_name;
76 plug->peer_proc = gras_trp_file_peer_proc;
77 plug->peer_proc_set = gras_trp_file_peer_proc_set;
79 plug->socket_close = gras_trp_file_close;
81 plug->raw_send = gras_trp_file_chunk_send_raw;
82 plug->send = gras_trp_file_chunk_send;
84 plug->raw_recv = plug->recv = gras_trp_file_chunk_recv;
86 plug->data = (void *) file;
90 * gras_socket_client_from_file:
92 * Create a client socket from a file path.
94 * This only possible in RL, and is mainly for debugging.
96 xbt_socket_t gras_socket_client_from_file(const char *path)
100 xbt_assert(gras_if_RL(), "Cannot use file as socket in the simulator");
102 xbt_socket_new_ext(0, &res, xbt_trp_plugin_get_by_name("file"), 0, 0);
104 if (strcmp("-", path)) {
106 open(path, O_TRUNC | O_WRONLY | O_CREAT | O_BINARY,
107 S_IRUSR | S_IWUSR | S_IRGRP);
110 THROWF(system_error, 0,
111 "Cannot create a client socket from file %s: %s",
112 path, strerror(errno));
115 res->sd = 1; /* stdout */
118 XBT_DEBUG("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
121 res->incoming ? 'y' : 'n',
122 res->outgoing ? 'y' : 'n', res->accepting ? 'y' : 'n');
124 xbt_dynar_push(((gras_trp_procdata_t)
125 gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
130 * gras_socket_server_from_file:
132 * Create a server socket from a file path.
134 * This only possible in RL, and is mainly for debugging.
136 xbt_socket_t gras_socket_server_from_file(const char *path)
140 xbt_assert(gras_if_RL(), "Cannot use file as socket in the simulator");
142 xbt_socket_new_ext(1, &res, xbt_trp_plugin_get_by_name("file"), 0, 0);
144 if (strcmp("-", path)) {
145 res->sd = open(path, O_RDONLY | O_BINARY);
148 THROWF(system_error, 0,
149 "Cannot create a server socket from file %s: %s",
150 path, strerror(errno));
153 res->sd = 0; /* stdin */
156 XBT_DEBUG("sd=%d in=%c out=%c accept=%c",
158 res->incoming ? 'y' : 'n',
159 res->outgoing ? 'y' : 'n', res->accepting ? 'y' : 'n');
161 xbt_dynar_push(((gras_trp_procdata_t)
162 gras_libdata_by_id(gras_trp_libdata_id))->sockets, &res);
163 gras_msg_listener_awake();
167 void gras_trp_file_close(xbt_socket_t sock)
169 gras_trp_file_plug_data_t *data;
172 return; /* close only once */
173 data = sock->plugin->data;
176 XBT_DEBUG("Do not close stdin");
177 } else if (sock->sd == 1) {
178 XBT_DEBUG("Do not close stdout");
180 XBT_DEBUG("close file connection %d", sock->sd);
182 /* forget about the socket */
183 FD_CLR(sock->sd, &(data->incoming_socks));
185 /* close the socket */
186 if (close(sock->sd) < 0) {
187 XBT_WARN("error while closing file %d: %s", sock->sd, strerror(errno));
193 * gras_trp_file_chunk_send:
195 * Send data on a file pseudo-socket
198 gras_trp_file_chunk_send(xbt_socket_t sock,
200 unsigned long int size, int stable_ignored)
202 gras_trp_file_chunk_send_raw(sock, data, size);
206 gras_trp_file_chunk_send_raw(xbt_socket_t sock,
207 const char *data, unsigned long int size)
210 xbt_assert(sock->outgoing, "Cannot write on client file socket");
211 xbt_assert(size >= 0, "Cannot send a negative amount of data");
216 XBT_DEBUG("write(%d, %p, %ld);", sock->sd, data, (long int) size);
217 status = write(sock->sd, data, (long int) size);
220 THROWF(system_error, 0, "write(%d,%p,%d) failed: %s",
221 sock->sd, data, (int) size, strerror(errno));
228 THROWF(system_error, 0, "file descriptor closed");
234 * gras_trp_file_chunk_recv:
236 * Receive data on a file pseudo-socket.
239 gras_trp_file_chunk_recv(xbt_socket_t sock,
240 char *data, unsigned long int size)
245 xbt_assert(sock, "Cannot recv on an NULL socket");
246 xbt_assert(sock->incoming, "Cannot recv on client file socket");
247 xbt_assert(size >= 0, "Cannot receive a negative amount of data");
250 data[0] = sock->recvd_val;
259 status = read(sock->sd, data + got, (long int) size);
260 XBT_DEBUG("read(%d, %p, %ld);", sock->sd, data + got, size);
263 THROWF(system_error, 0, "read(%d,%p,%d) failed: %s",
264 sock->sd, data + got, (int) size, strerror(errno));
271 THROWF(system_error, errno, "file descriptor closed after %d bytes",