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 "gras/Transport/transport_private.h"
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_file,gras_trp,
19 "Pseudo-transport to write to/read from a file");
24 void gras_trp_file_close(gras_socket_t sd);
26 void gras_trp_file_chunk_send_raw(gras_socket_t sd,
28 unsigned long int size);
29 void gras_trp_file_chunk_send(gras_socket_t sd,
31 unsigned long int size,
34 int gras_trp_file_chunk_recv(gras_socket_t sd,
36 unsigned long int size);
39 *** Specific plugin part
43 fd_set incoming_socks;
44 } gras_trp_file_plug_data_t;
47 *** Specific socket part
56 gras_trp_file_setup(gras_trp_plugin_t plug) {
58 gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t,1);
60 FD_ZERO(&(file->incoming_socks));
62 plug->socket_close = gras_trp_file_close;
64 plug->raw_send = gras_trp_file_chunk_send_raw;
65 plug->send = gras_trp_file_chunk_send;
67 plug->raw_recv = plug->recv = gras_trp_file_chunk_recv;
69 plug->data = (void*)file;
73 * gras_socket_client_from_file:
75 * Create a client socket from a file path.
77 * This only possible in RL, and is mainly for debugging.
80 gras_socket_client_from_file(const char*path) {
83 xbt_assert0(gras_if_RL(),
84 "Cannot use file as socket in the simulator");
86 gras_trp_socket_new(0,&res);
88 res->plugin=gras_trp_plugin_get_by_name("file");
90 if (strcmp("-", path)) {
91 res->sd = open(path, O_TRUNC|O_WRONLY|O_CREAT | O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP );
94 THROW2(system_error,0,
95 "Cannot create a client socket from file %s: %s",
96 path, strerror(errno));
99 res->sd = 1; /* stdout */
102 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
105 res->incoming?'y':'n',
106 res->outgoing?'y':'n',
107 res->accepting?'y':'n');
109 xbt_dynar_push(((gras_trp_procdata_t)
110 gras_libdata_by_id(gras_trp_libdata_id))->sockets,&res);
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.
121 gras_socket_t gras_socket_server_from_file(const char*path) {
124 xbt_assert0(gras_if_RL(),
125 "Cannot use file as socket in the simulator");
127 gras_trp_socket_new(1,&res);
129 res->plugin=gras_trp_plugin_get_by_name("file");
132 if (strcmp("-", path)) {
133 res->sd = open(path, O_RDONLY | O_BINARY);
136 THROW2(system_error,0,
137 "Cannot create a server socket from file %s: %s",
138 path, strerror(errno));
141 res->sd = 0; /* stdin */
144 DEBUG4("sd=%d in=%c out=%c accept=%c",
146 res->incoming?'y':'n',
147 res->outgoing?'y':'n',
148 res->accepting?'y':'n');
150 xbt_dynar_push(((gras_trp_procdata_t)
151 gras_libdata_by_id(gras_trp_libdata_id))->sockets,&res);
155 void gras_trp_file_close(gras_socket_t sock){
156 gras_trp_file_plug_data_t *data;
158 if (!sock) return; /* close only once */
159 data=sock->plugin->data;
162 DEBUG0("Do not close stdin");
163 } else if (sock->sd == 1) {
164 DEBUG0("Do not close stdout");
166 DEBUG1("close file connection %d", sock->sd);
168 /* forget about the socket */
169 FD_CLR(sock->sd, &(data->incoming_socks));
171 /* close the socket */
172 if(close(sock->sd) < 0) {
173 WARN2("error while closing file %d: %s",
174 sock->sd, strerror(errno));
180 * gras_trp_file_chunk_send:
182 * Send data on a file pseudo-socket
185 gras_trp_file_chunk_send(gras_socket_t sock,
187 unsigned long int size,
188 int stable_ignored) {
189 gras_trp_file_chunk_send_raw(sock,data,size);
192 gras_trp_file_chunk_send_raw(gras_socket_t sock,
194 unsigned long int size) {
196 xbt_assert0(sock->outgoing, "Cannot write on client file socket");
197 xbt_assert0(size >= 0, "Cannot send a negative amount of data");
202 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
203 status = write(sock->sd, data, (long int)size);
206 THROW4(system_error,0,"write(%d,%p,%d) failed: %s",
207 sock->sd, data, (int)size,
215 THROW0(system_error,0,"file descriptor closed");
220 * gras_trp_file_chunk_recv:
222 * Receive data on a file pseudo-socket.
225 gras_trp_file_chunk_recv(gras_socket_t sock,
227 unsigned long int size) {
231 xbt_assert0(sock, "Cannot recv on an NULL socket");
232 xbt_assert0(sock->incoming, "Cannot recv on client file socket");
233 xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
236 data[0] = sock->recvd_val;
245 status = read(sock->sd, data+got, (long int)size);
246 DEBUG3("read(%d, %p, %ld);", sock->sd, data+got, size);
249 THROW4(system_error,0,"read(%d,%p,%d) failed: %s",
250 sock->sd, data+got, (int)size,
258 THROW1(system_error,errno,"file descriptor closed after %d bytes",got);