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 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(trp_file,transport,
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(gras_socket_t sd,
24 unsigned long int size);
26 void gras_trp_file_chunk_recv(gras_socket_t sd,
28 unsigned long int size);
32 *** Specific plugin part
36 fd_set incoming_socks;
37 } gras_trp_file_plug_data_t;
40 *** Specific socket part
49 gras_trp_file_setup(gras_trp_plugin_t plug) {
51 gras_trp_file_plug_data_t *file = xbt_new(gras_trp_file_plug_data_t,1);
53 FD_ZERO(&(file->incoming_socks));
55 plug->socket_close = gras_trp_file_close;
56 plug->chunk_send = gras_trp_file_chunk_send;
57 plug->chunk_recv = gras_trp_file_chunk_recv;
58 plug->data = (void*)file;
62 * gras_socket_client_from_file:
64 * Create a client socket from a file path.
66 * This only possible in RL, and is mainly for debugging.
69 gras_socket_client_from_file(const char*path) {
72 xbt_assert0(gras_if_RL(),
73 "Cannot use file as socket in the simulator");
75 gras_trp_socket_new(0,&res);
77 res->plugin=gras_trp_plugin_get_by_name("file");
79 if (strcmp("-", path)) {
80 res->sd = open(path, O_WRONLY|O_CREAT | O_BINARY, S_IRUSR|S_IWUSR|S_IRGRP );
83 THROW2(system_error,0,
84 "Cannot create a client socket from file %s: %s",
85 path, strerror(errno));
88 res->sd = 1; /* stdout */
91 DEBUG5("sock_client_from_file(%s): sd=%d in=%c out=%c accept=%c",
94 res->incoming?'y':'n',
95 res->outgoing?'y':'n',
96 res->accepting?'y':'n');
102 * gras_socket_server_from_file:
104 * Create a server socket from a file path.
106 * This only possible in RL, and is mainly for debugging.
108 gras_socket_t gras_socket_server_from_file(const char*path) {
111 xbt_assert0(gras_if_RL(),
112 "Cannot use file as socket in the simulator");
114 gras_trp_socket_new(1,&res);
116 res->plugin=gras_trp_plugin_get_by_name("file");
119 if (strcmp("-", path)) {
120 res->sd = open(path, O_RDONLY | O_BINARY);
123 THROW2(system_error,0,
124 "Cannot create a server socket from file %s: %s",
125 path, strerror(errno));
128 res->sd = 0; /* stdin */
131 DEBUG4("sd=%d in=%c out=%c accept=%c",
133 res->incoming?'y':'n',
134 res->outgoing?'y':'n',
135 res->accepting?'y':'n');
140 void gras_trp_file_close(gras_socket_t sock){
141 gras_trp_file_plug_data_t *data;
143 if (!sock) return; /* close only once */
144 data=sock->plugin->data;
147 DEBUG0("Do not close stdin");
148 } else if (sock->sd == 1) {
149 DEBUG0("Do not close stdout");
151 DEBUG1("close file connection %d", sock->sd);
153 /* forget about the socket */
154 FD_CLR(sock->sd, &(data->incoming_socks));
156 /* close the socket */
157 if(close(sock->sd) < 0) {
158 WARN2("error while closing file %d: %s",
159 sock->sd, strerror(errno));
165 * gras_trp_file_chunk_send:
167 * Send data on a file pseudo-socket
170 gras_trp_file_chunk_send(gras_socket_t sock,
172 unsigned long int size) {
174 xbt_assert0(sock->outgoing, "Cannot write on client file socket");
175 xbt_assert0(size >= 0, "Cannot send a negative amount of data");
180 DEBUG3("write(%d, %p, %ld);", sock->sd, data, (long int)size);
181 status = write(sock->sd, data, (long int)size);
184 THROW4(system_error,0,"write(%d,%p,%d) failed: %s",
185 sock->sd, data, (int)size,
193 THROW0(system_error,0,"file descriptor closed");
198 * gras_trp_file_chunk_recv:
200 * Receive data on a file pseudo-socket.
203 gras_trp_file_chunk_recv(gras_socket_t sock,
205 unsigned long int size) {
207 xbt_assert0(sock, "Cannot recv on an NULL socket");
208 xbt_assert0(sock->incoming, "Cannot recv on client file socket");
209 xbt_assert0(size >= 0, "Cannot receive a negative amount of data");
214 status = read(sock->sd, data, (long int)size);
215 DEBUG3("read(%d, %p, %ld);", sock->sd, data, size);
218 THROW4(system_error,0,"read(%d,%p,%d) failed: %s",
219 sock->sd, data, (int)size,
227 THROW0(system_error,0,"file descriptor closed");