3 /* File transport - send/receive a bunch of bytes from a file */
5 /* Copyright (c) 2004 Martin Quinson. All rights reserved. */
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. */
11 #include "transport_private.h"
15 # include <sys/time.h>
16 # include <sys/types.h>
17 # include <sys/stat.h>
22 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_file,transport,
23 "Pseudo-transport to write to/read from a file");
28 void gras_trp_file_close(gras_socket_t sd);
30 xbt_error_t gras_trp_file_chunk_send(gras_socket_t sd,
34 xbt_error_t gras_trp_file_chunk_recv(gras_socket_t sd,
40 *** Specific plugin part
44 fd_set incoming_socks;
45 } gras_trp_file_plug_data_t;
48 *** Specific socket part
57 gras_trp_file_setup(gras_trp_plugin_t *plug) {
59 gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t,1);
61 FD_ZERO(&(file->incoming_socks));
63 plug->socket_close = gras_trp_file_close;
64 plug->chunk_send = gras_trp_file_chunk_send;
65 plug->chunk_recv = gras_trp_file_chunk_recv;
66 plug->data = (void*)file;
72 * gras_socket_client_from_file:
74 * Create a client socket from a file path.
76 * This only possible in RL, and is mainly for debugging.
79 gras_socket_client_from_file(const char*path,
80 /* OUT */ gras_socket_t *dst) {
82 gras_trp_plugin_t *trp;
84 xbt_assert0(gras_if_RL(),
85 "Cannot use file as socket in the simulator");
87 gras_trp_socket_new(0,dst);
89 TRY(gras_trp_plugin_get_by_name("file",&trp));
92 if (strcmp("-", path)) {
93 (*dst)->sd = open(path, O_WRONLY|O_CREAT | O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP );
95 if ( (*dst)->sd < 0) {
97 "Cannot create a client socket from file %s: %s",
98 path, strerror(errno));
101 (*dst)->sd = 1; /* stdout */
104 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
107 (*dst)->incoming?'y':'n',
108 (*dst)->outgoing?'y':'n',
109 (*dst)->accepting?'y':'n');
115 * gras_socket_server_from_file:
117 * Create a server socket from a file path.
119 * This only possible in RL, and is mainly for debugging.
122 gras_socket_server_from_file(const char*path,
123 /* OUT */ gras_socket_t *dst) {
125 gras_trp_plugin_t *trp;
127 xbt_assert0(gras_if_RL(),
128 "Cannot use file as socket in the simulator");
130 gras_trp_socket_new(1,dst);
132 TRY(gras_trp_plugin_get_by_name("file",&trp));
136 if (strcmp("-", path)) {
137 (*dst)->sd = open(path, O_RDONLY | O_BINARY);
139 if ( (*dst)->sd < 0) {
141 "Cannot create a server socket from file %s: %s",
142 path, strerror(errno));
145 (*dst)->sd = 0; /* stdin */
148 DEBUG4("sd=%d in=%c out=%c accept=%c",
150 (*dst)->incoming?'y':'n',
151 (*dst)->outgoing?'y':'n',
152 (*dst)->accepting?'y':'n');
157 void gras_trp_file_close(gras_socket_t sock){
158 gras_trp_file_plug_data_t *data;
160 if (!sock) return; /* close only once */
161 data=sock->plugin->data;
164 DEBUG0("Do not close stdin");
165 } else if (sock->sd == 1) {
166 DEBUG0("Do not close stdout");
168 DEBUG1("close file connection %d", sock->sd);
170 /* forget about the socket */
171 FD_CLR(sock->sd, &(data->incoming_socks));
173 /* close the socket */
174 if(close(sock->sd) < 0) {
175 WARN2("error while closing file %d: %s",
176 sock->sd, strerror(errno));
182 * gras_trp_file_chunk_send:
184 * Send data on a file pseudo-socket
187 gras_trp_file_chunk_send(gras_socket_t sock,
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 RAISE4(system_error,"write(%d,%p,%d) failed: %s",
202 sock->sd, data, (int)size,
210 RAISE0(system_error,"file descriptor closed");
217 * gras_trp_file_chunk_recv:
219 * Receive data on a file pseudo-socket.
222 gras_trp_file_chunk_recv(gras_socket_t sock,
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");
233 status = read(sock->sd, data, (long int)size);
234 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
237 RAISE4(system_error,"read(%d,%p,%d) failed: %s",
238 sock->sd, data, (int)size,
246 RAISE0(system_error,"file descriptor closed");