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. */
12 #include <sys/types.h>
17 #include "transport_private.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_file,transport,
20 "Pseudo-transport to write to/read from a file");
25 void gras_trp_file_close(gras_socket_t sd);
27 xbt_error_t gras_trp_file_chunk_send(gras_socket_t sd,
31 xbt_error_t gras_trp_file_chunk_recv(gras_socket_t sd,
37 *** Specific plugin part
41 fd_set incoming_socks;
42 } gras_trp_file_plug_data_t;
45 *** Specific socket part
54 gras_trp_file_setup(gras_trp_plugin_t *plug) {
56 gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t,1);
58 FD_ZERO(&(file->incoming_socks));
60 plug->socket_close = gras_trp_file_close;
61 plug->chunk_send = gras_trp_file_chunk_send;
62 plug->chunk_recv = gras_trp_file_chunk_recv;
63 plug->data = (void*)file;
69 * gras_socket_client_from_file:
71 * Create a client socket from a file path.
73 * This only possible in RL, and is mainly for debugging.
76 gras_socket_client_from_file(const char*path,
77 /* OUT */ gras_socket_t *dst) {
79 gras_trp_plugin_t *trp;
81 xbt_assert0(gras_if_RL(),
82 "Cannot use file as socket in the simulator");
84 gras_trp_socket_new(0,dst);
86 TRY(gras_trp_plugin_get_by_name("file",&trp));
89 if (strcmp("-", path)) {
90 (*dst)->sd = open(path, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP );
92 if ( (*dst)->sd < 0) {
94 "Cannot create a client socket from file %s: %s",
95 path, strerror(errno));
98 (*dst)->sd = 1; /* stdout */
101 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
104 (*dst)->incoming?'y':'n',
105 (*dst)->outgoing?'y':'n',
106 (*dst)->accepting?'y':'n');
112 * gras_socket_server_from_file:
114 * Create a server socket from a file path.
116 * This only possible in RL, and is mainly for debugging.
119 gras_socket_server_from_file(const char*path,
120 /* OUT */ gras_socket_t *dst) {
122 gras_trp_plugin_t *trp;
124 xbt_assert0(gras_if_RL(),
125 "Cannot use file as socket in the simulator");
127 gras_trp_socket_new(1,dst);
129 TRY(gras_trp_plugin_get_by_name("file",&trp));
133 if (strcmp("-", path)) {
134 (*dst)->sd = open(path, O_RDONLY );
136 if ( (*dst)->sd < 0) {
138 "Cannot create a server socket from file %s: %s",
139 path, strerror(errno));
142 (*dst)->sd = 0; /* stdin */
145 DEBUG4("sd=%d in=%c out=%c accept=%c",
147 (*dst)->incoming?'y':'n',
148 (*dst)->outgoing?'y':'n',
149 (*dst)->accepting?'y':'n');
154 void gras_trp_file_close(gras_socket_t sock){
155 gras_trp_file_plug_data_t *data;
157 if (!sock) return; /* close only once */
158 data=sock->plugin->data;
161 DEBUG0("Do not close stdin");
162 } else if (sock->sd == 1) {
163 DEBUG0("Do not close stdout");
165 DEBUG1("close file connection %d", sock->sd);
167 /* forget about the socket */
168 FD_CLR(sock->sd, &(data->incoming_socks));
170 /* close the socket */
171 if(close(sock->sd) < 0) {
172 WARN2("error while closing file %d: %s",
173 sock->sd, strerror(errno));
179 * gras_trp_file_chunk_send:
181 * Send data on a file pseudo-socket
184 gras_trp_file_chunk_send(gras_socket_t sock,
188 xbt_assert0(sock->outgoing, "Cannot write on client file socket");
189 xbt_assert0(size >= 0, "Cannot send a negative amount of data");
194 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
195 status = write(sock->sd, data, (long int)size);
198 RAISE4(system_error,"write(%d,%p,%d) failed: %s",
199 sock->sd, data, (int)size,
207 RAISE0(system_error,"file descriptor closed");
214 * gras_trp_file_chunk_recv:
216 * Receive data on a file pseudo-socket.
219 gras_trp_file_chunk_recv(gras_socket_t sock,
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");
230 status = read(sock->sd, data, (long int)size);
231 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
234 RAISE4(system_error,"read(%d,%p,%d) failed: %s",
235 sock->sd, data, (int)size,
243 RAISE0(system_error,"file descriptor closed");