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"
14 # include <sys/types.h>
17 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_file,transport,
18 "Pseudo-transport to write to/read from a file");
23 void gras_trp_file_close(gras_socket_t sd);
25 xbt_error_t gras_trp_file_chunk_send(gras_socket_t sd,
29 xbt_error_t gras_trp_file_chunk_recv(gras_socket_t sd,
35 *** Specific plugin part
39 fd_set incoming_socks;
40 } gras_trp_file_plug_data_t;
43 *** Specific socket part
52 gras_trp_file_setup(gras_trp_plugin_t *plug) {
54 gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t,1);
56 FD_ZERO(&(file->incoming_socks));
58 plug->socket_close = gras_trp_file_close;
59 plug->chunk_send = gras_trp_file_chunk_send;
60 plug->chunk_recv = gras_trp_file_chunk_recv;
61 plug->data = (void*)file;
67 * gras_socket_client_from_file:
69 * Create a client socket from a file path.
71 * This only possible in RL, and is mainly for debugging.
74 gras_socket_client_from_file(const char*path,
75 /* OUT */ gras_socket_t *dst) {
77 gras_trp_plugin_t *trp;
79 xbt_assert0(gras_if_RL(),
80 "Cannot use file as socket in the simulator");
82 gras_trp_socket_new(0,dst);
84 TRY(gras_trp_plugin_get_by_name("file",&trp));
87 if (strcmp("-", path)) {
88 (*dst)->sd = open(path, O_WRONLY|O_CREAT | O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP );
90 if ( (*dst)->sd < 0) {
92 "Cannot create a client socket from file %s: %s",
93 path, strerror(errno));
96 (*dst)->sd = 1; /* stdout */
99 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
102 (*dst)->incoming?'y':'n',
103 (*dst)->outgoing?'y':'n',
104 (*dst)->accepting?'y':'n');
110 * gras_socket_server_from_file:
112 * Create a server socket from a file path.
114 * This only possible in RL, and is mainly for debugging.
117 gras_socket_server_from_file(const char*path,
118 /* OUT */ gras_socket_t *dst) {
120 gras_trp_plugin_t *trp;
122 xbt_assert0(gras_if_RL(),
123 "Cannot use file as socket in the simulator");
125 gras_trp_socket_new(1,dst);
127 TRY(gras_trp_plugin_get_by_name("file",&trp));
131 if (strcmp("-", path)) {
132 (*dst)->sd = open(path, O_RDONLY | O_BINARY);
134 if ( (*dst)->sd < 0) {
136 "Cannot create a server socket from file %s: %s",
137 path, strerror(errno));
140 (*dst)->sd = 0; /* stdin */
143 DEBUG4("sd=%d in=%c out=%c accept=%c",
145 (*dst)->incoming?'y':'n',
146 (*dst)->outgoing?'y':'n',
147 (*dst)->accepting?'y':'n');
152 void gras_trp_file_close(gras_socket_t sock){
153 gras_trp_file_plug_data_t *data;
155 if (!sock) return; /* close only once */
156 data=sock->plugin->data;
159 DEBUG0("Do not close stdin");
160 } else if (sock->sd == 1) {
161 DEBUG0("Do not close stdout");
163 DEBUG1("close file connection %d", sock->sd);
165 /* forget about the socket */
166 FD_CLR(sock->sd, &(data->incoming_socks));
168 /* close the socket */
169 if(close(sock->sd) < 0) {
170 WARN2("error while closing file %d: %s",
171 sock->sd, strerror(errno));
177 * gras_trp_file_chunk_send:
179 * Send data on a file pseudo-socket
182 gras_trp_file_chunk_send(gras_socket_t sock,
186 xbt_assert0(sock->outgoing, "Cannot write on client file socket");
187 xbt_assert0(size >= 0, "Cannot send a negative amount of data");
192 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
193 status = write(sock->sd, data, (long int)size);
196 RAISE4(system_error,"write(%d,%p,%d) failed: %s",
197 sock->sd, data, (int)size,
205 RAISE0(system_error,"file descriptor closed");
212 * gras_trp_file_chunk_recv:
214 * Receive data on a file pseudo-socket.
217 gras_trp_file_chunk_recv(gras_socket_t sock,
221 xbt_assert0(sock, "Cannot recv on an NULL socket");
222 xbt_assert0(sock->incoming, "Cannot recv on client file socket");
223 xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
228 status = read(sock->sd, data, (long int)size);
229 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
232 RAISE4(system_error,"read(%d,%p,%d) failed: %s",
233 sock->sd, data, (int)size,
241 RAISE0(system_error,"file descriptor closed");