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"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp_file,gras_trp,
15 "Pseudo-transport to write to/read from a file");
20 void gras_trp_file_close(gras_socket_t sd);
22 void gras_trp_file_chunk_send_raw(gras_socket_t sd,
24 unsigned long int size);
25 void gras_trp_file_chunk_send(gras_socket_t sd,
27 unsigned long int size,
30 int gras_trp_file_chunk_recv(gras_socket_t sd,
32 unsigned long int size);
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;
60 plug->raw_send = gras_trp_file_chunk_send_raw;
61 plug->send = gras_trp_file_chunk_send;
63 plug->raw_recv = plug->recv = gras_trp_file_chunk_recv;
65 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) {
79 xbt_assert0(gras_if_RL(),
80 "Cannot use file as socket in the simulator");
82 gras_trp_socket_new(0,&res);
84 res->plugin=gras_trp_plugin_get_by_name("file");
86 if (strcmp("-", path)) {
87 res->sd = open(path, O_WRONLY|O_CREAT | O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP );
90 THROW2(system_error,0,
91 "Cannot create a client socket from file %s: %s",
92 path, strerror(errno));
95 res->sd = 1; /* stdout */
99 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
102 res->incoming?'y':'n',
103 res->outgoing?'y':'n',
104 res->accepting?'y':'n');
106 xbt_dynar_push(((gras_trp_procdata_t)
107 gras_libdata_by_id(gras_trp_libdata_id))->sockets,&res);
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.
118 gras_socket_t gras_socket_server_from_file(const char*path) {
121 xbt_assert0(gras_if_RL(),
122 "Cannot use file as socket in the simulator");
124 gras_trp_socket_new(1,&res);
126 res->plugin=gras_trp_plugin_get_by_name("file");
129 if (strcmp("-", path)) {
130 res->sd = open(path, O_RDONLY | O_BINARY);
133 THROW2(system_error,0,
134 "Cannot create a server socket from file %s: %s",
135 path, strerror(errno));
138 res->sd = 0; /* stdin */
141 DEBUG4("sd=%d in=%c out=%c accept=%c",
143 res->incoming?'y':'n',
144 res->outgoing?'y':'n',
145 res->accepting?'y':'n');
148 xbt_dynar_push(((gras_trp_procdata_t)
149 gras_libdata_by_id(gras_trp_libdata_id))->sockets,&res);
153 void gras_trp_file_close(gras_socket_t sock){
154 gras_trp_file_plug_data_t *data;
156 if (!sock) return; /* close only once */
157 data=sock->plugin->data;
160 DEBUG0("Do not close stdin");
161 } else if (sock->sd == 1) {
162 DEBUG0("Do not close stdout");
164 DEBUG1("close file connection %d", sock->sd);
166 /* forget about the socket */
167 FD_CLR(sock->sd, &(data->incoming_socks));
169 /* close the socket */
170 if(close(sock->sd) < 0) {
171 WARN2("error while closing file %d: %s",
172 sock->sd, strerror(errno));
178 * gras_trp_file_chunk_send:
180 * Send data on a file pseudo-socket
183 gras_trp_file_chunk_send(gras_socket_t sock,
185 unsigned long int size,
186 int stable_ignored) {
187 gras_trp_file_chunk_send_raw(sock,data,size);
190 gras_trp_file_chunk_send_raw(gras_socket_t sock,
192 unsigned long int size) {
194 xbt_assert0(sock->outgoing, "Cannot write on client file socket");
195 xbt_assert0(size >= 0, "Cannot send a negative amount of data");
200 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
201 status = write(sock->sd, data, (long int)size);
204 THROW4(system_error,0,"write(%d,%p,%d) failed: %s",
205 sock->sd, data, (int)size,
213 THROW0(system_error,0,"file descriptor closed");
218 * gras_trp_file_chunk_recv:
220 * Receive data on a file pseudo-socket.
223 gras_trp_file_chunk_recv(gras_socket_t sock,
225 unsigned long int size) {
229 xbt_assert0(sock, "Cannot recv on an NULL socket");
230 xbt_assert0(sock->incoming, "Cannot recv on client file socket");
231 xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
236 status = read(sock->sd, data+got, (long int)size);
237 DEBUG3("read(%d, %p, %ld);", sock->sd, data+got, size);
240 THROW4(system_error,0,"read(%d,%p,%d) failed: %s",
241 sock->sd, data+got, (int)size,
249 THROW0(system_error,0,"file descriptor closed");