3 /* transport - low level communication */
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 "gras/Transport/transport_private.h"
14 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp,gras,"Conveying bytes over the network");
15 XBT_LOG_NEW_SUBCATEGORY(gras_trp_meas,gras_trp,"Conveying bytes over the network without formating for perf measurements");
16 static short int _gras_trp_started = 0;
18 static xbt_dict_t _gras_trp_plugins; /* All registered plugins */
19 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
22 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
25 gras_trp_plugin_t plug = xbt_new0(s_gras_trp_plugin_t, 1);
27 DEBUG1("Create plugin %s",name);
29 plug->name=xbt_strdup(name);
34 if (e.category == mismatch_error) {
35 /* SG plugin raise mismatch when in RL mode (and vice versa) */
46 xbt_dict_set(_gras_trp_plugins, name, plug, gras_trp_plugin_free);
49 void gras_trp_init(void){
50 if (!_gras_trp_started) {
51 /* make room for all plugins */
52 _gras_trp_plugins=xbt_dict_new();
54 #ifdef HAVE_WINSOCK2_H
55 /* initialize the windows mechanism */
57 WORD wVersionRequested;
60 wVersionRequested = MAKEWORD( 2, 0 );
61 xbt_assert0(WSAStartup( wVersionRequested, &wsaData ) == 0,
62 "Cannot find a usable WinSock DLL");
64 /* Confirm that the WinSock DLL supports 2.0.*/
65 /* Note that if the DLL supports versions greater */
66 /* than 2.0 in addition to 2.0, it will still return */
67 /* 2.0 in wVersion since that is the version we */
70 xbt_assert0(LOBYTE( wsaData.wVersion ) == 2 &&
71 HIBYTE( wsaData.wVersion ) == 0,
72 "Cannot find a usable WinSock DLL");
73 INFO0("Found and initialized winsock2");
74 } /* The WinSock DLL is acceptable. Proceed. */
77 xbt_assert0(WSAStartup( 0x0101, &wsaData ) == 0,
78 "Cannot find a usable WinSock DLL");
79 INFO0("Found and initialized winsock");
84 gras_trp_plugin_new("file",gras_trp_file_setup);
85 gras_trp_plugin_new("sg",gras_trp_sg_setup);
86 gras_trp_plugin_new("tcp", gras_trp_tcp_setup);
94 xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
95 gras_socket_t sock_iter;
98 if (_gras_trp_started == 0) {
102 if ( --_gras_trp_started == 0 ) {
103 #ifdef HAVE_WINSOCK_H
104 if ( WSACleanup() == SOCKET_ERROR ) {
105 if ( WSAGetLastError() == WSAEINPROGRESS ) {
106 WSACancelBlockingCall();
112 /* Close all the sockets */
113 xbt_dynar_foreach(sockets,cursor,sock_iter) {
114 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
116 gras_socket_close(sock_iter);
119 /* Delete the plugins */
120 xbt_dict_free(&_gras_trp_plugins);
125 void gras_trp_plugin_free(void *p) {
126 gras_trp_plugin_t plug = p;
131 } else if (plug->data) {
132 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
143 * gras_trp_socket_new:
145 * Malloc a new socket, and initialize it with defaults
147 void gras_trp_socket_new(int incoming,
148 gras_socket_t *dst) {
150 gras_socket_t sock=xbt_new0(s_gras_socket_t,1);
152 DEBUG1("Create a new socket (%p)", (void*)sock);
156 sock->incoming = incoming ? 1:0;
157 sock->outgoing = incoming ? 0:1;
158 sock->accepting = incoming ? 1:0;
163 sock->peer_port = -1;
164 sock->peer_name = NULL;
165 sock->peer_proc = NULL;
168 sock->bufdata = NULL;
172 xbt_dynar_push(((gras_trp_procdata_t)
173 gras_libdata_by_id(gras_trp_libdata_id))->sockets,dst);
179 * @brief Opens a server socket and makes it ready to be listened to.
180 * @param port: port on which you want to listen
181 * @param buf_size: size of the buffer (in byte) on the socket (for TCP sockets only). If 0, a sain default is used (32k, but may change)
182 * @param measurement: whether this socket is meant to convey measurement (if you don't know, use 0 to exchange regular messages)
184 * In real life, you'll get a TCP socket.
187 gras_socket_server_ext(unsigned short port,
189 unsigned long int buf_size,
193 gras_trp_plugin_t trp;
196 DEBUG2("Create a server socket from plugin %s on port %d",
197 gras_if_RL() ? "tcp" : "sg",
199 trp = gras_trp_plugin_get_by_name(gras_if_SG() ? "sg":"tcp");
201 /* defaults settings */
202 gras_trp_socket_new(1,&sock);
205 sock->buf_size = buf_size>0 ? buf_size : 32*1024;
206 sock->meas = measurement;
208 /* Call plugin socket creation function */
209 DEBUG1("Prepare socket with plugin (fct=%p)",trp->socket_server);
211 trp->socket_server(trp, sock);
212 DEBUG3("in=%c out=%c accept=%c",
213 sock->incoming?'y':'n',
214 sock->outgoing?'y':'n',
215 sock->accepting?'y':'n');
218 gras_socket_t sock_iter;
219 xbt_dynar_t socks = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
220 xbt_dynar_foreach(socks, cursor, sock_iter) {
222 xbt_dynar_cursor_rm(socks,&cursor);
232 * @brief Opens a client socket to a remote host.
233 * @param host: who you want to connect to
234 * @param port: where you want to connect to on this host
235 * @param buf_size: size of the buffer (in bytes) on the socket (for TCP sockets only). If 0, a sain default is used (32k, but may change)
236 * @param measurement: whether this socket is meant to convey measurement (if you don't know, use 0 to exchange regular messages)
238 * In real life, you'll get a TCP socket.
241 gras_socket_client_ext(const char *host,
244 unsigned long int buf_size,
248 gras_trp_plugin_t trp;
251 trp = gras_trp_plugin_get_by_name(gras_if_SG() ? "sg":"tcp");
253 DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
254 /* defaults settings */
255 gras_trp_socket_new(0,&sock);
257 sock->peer_port = port;
258 sock->peer_name = (char*)strdup(host?host:"localhost");
259 sock->buf_size = buf_size>0 ? buf_size: 32*1024;
260 sock->meas = measurement;
262 /* plugin-specific */
264 (*trp->socket_client)(trp, sock);
265 DEBUG3("in=%c out=%c accept=%c",
266 sock->incoming?'y':'n',
267 sock->outgoing?'y':'n',
268 sock->accepting?'y':'n');
278 * gras_socket_server:
280 * Opens a server socket and make it ready to be listened to.
281 * In real life, you'll get a TCP socket.
284 gras_socket_server(unsigned short port) {
285 return gras_socket_server_ext(port,32*1024,0);
289 * gras_socket_client:
291 * Opens a client socket to a remote host.
292 * In real life, you'll get a TCP socket.
295 gras_socket_client(const char *host,
296 unsigned short port) {
297 return gras_socket_client_ext(host,port,32*1024,0);
301 void gras_socket_close(gras_socket_t sock) {
302 xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
303 gras_socket_t sock_iter;
307 /* FIXME: Issue an event when the socket is closed */
309 xbt_dynar_foreach(sockets,cursor,sock_iter) {
310 if (sock == sock_iter) {
311 xbt_dynar_cursor_rm(sockets,&cursor);
312 if (sock->plugin->socket_close)
313 (* sock->plugin->socket_close)(sock);
315 /* free the memory */
317 free(sock->peer_name);
323 WARN1("Ignoring request to free an unknown socket (%p)",sock);
331 * Send a bunch of bytes from on socket
332 * (stable if we know the storage will keep as is until the next trp_flush)
335 gras_trp_send(gras_socket_t sd, char *data, long int size, int stable) {
336 xbt_assert0(sd->outgoing,"Socket not suited for data send");
337 (*sd->plugin->send)(sd,data,size,stable);
340 * gras_trp_chunk_recv:
342 * Receive a bunch of bytes from a socket
345 gras_trp_recv(gras_socket_t sd, char *data, long int size) {
346 xbt_assert0(sd->incoming,"Socket not suited for data receive");
347 (sd->plugin->recv)(sd,data,size);
353 * Make sure all pending communications are done
356 gras_trp_flush(gras_socket_t sd) {
357 if (sd->plugin->flush)
358 (sd->plugin->flush)(sd);
362 gras_trp_plugin_get_by_name(const char *name){
363 return xbt_dict_get(_gras_trp_plugins,name);
366 int gras_socket_my_port (gras_socket_t sock) {
369 int gras_socket_peer_port(gras_socket_t sock) {
370 return sock->peer_port;
372 char *gras_socket_peer_name(gras_socket_t sock) {
373 return sock->peer_name;
375 char *gras_socket_peer_proc(gras_socket_t sock) {
376 return sock->peer_proc;
379 void gras_socket_peer_proc_set(gras_socket_t sock,char*peer_proc) {
380 sock->peer_proc = peer_proc;
383 /** \brief Check if the provided socket is a measurement one (or a regular one) */
384 int gras_socket_is_meas(gras_socket_t sock) {
388 /** \brief Send a chunk of (random) data over a measurement socket
390 * @param peer measurement socket to use for the experiment
391 * @param timeout timeout (in seconds)
392 * @param exp_size total amount of data to send (in bytes).
393 * @param msg_size size of each chunk sent over the socket (in bytes).
395 * Calls to gras_socket_meas_send() and gras_socket_meas_recv() on
396 * each side of the socket should be paired.
398 * The exchanged data is zeroed to make sure it's initialized, but
399 * there is no way to control what is sent (ie, you cannot use these
400 * functions to exchange data out of band).
402 void gras_socket_meas_send(gras_socket_t peer,
403 unsigned int timeout,
404 unsigned long int exp_size,
405 unsigned long int msg_size) {
407 unsigned long int exp_sofar;
412 chunk=xbt_malloc0(msg_size);
414 xbt_assert0(peer->meas,"Asked to send measurement data on a regular socket");
415 xbt_assert0(peer->outgoing,"Socket not suited for data send (was created with gras_socket_server(), not gras_socket_client())");
417 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
418 CDEBUG5(gras_trp_meas,"Sent %lu of %lu (msg_size=%ld) to %s:%d",
419 exp_sofar,exp_size,msg_size,
420 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
421 (*peer->plugin->raw_send)(peer,chunk,msg_size);
423 CDEBUG5(gras_trp_meas,"Sent %lu of %lu (msg_size=%ld) to %s:%d",
424 exp_sofar,exp_size,msg_size,
425 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
433 /** \brief Receive a chunk of data over a measurement socket
435 * Calls to gras_socket_meas_send() and gras_socket_meas_recv() on
436 * each side of the socket should be paired.
438 void gras_socket_meas_recv(gras_socket_t peer,
439 unsigned int timeout,
440 unsigned long int exp_size,
441 unsigned long int msg_size){
444 unsigned long int exp_sofar;
449 chunk = xbt_malloc(msg_size);
451 xbt_assert0(peer->meas,
452 "Asked to receive measurement data on a regular socket");
453 xbt_assert0(peer->incoming,"Socket not suited for data receive");
455 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
456 CDEBUG5(gras_trp_meas,"Recvd %ld of %lu (msg_size=%ld) from %s:%d",
457 exp_sofar,exp_size,msg_size,
458 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
459 (peer->plugin->raw_recv)(peer,chunk,msg_size);
461 CDEBUG5(gras_trp_meas,"Recvd %ld of %lu (msg_size=%ld) from %s:%d",
462 exp_sofar,exp_size,msg_size,
463 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
471 * \brief Something similar to the good old accept system call.
473 * Make sure that there is someone speaking to the provided server socket.
474 * In RL, it does an accept(2) and return the result as last argument.
475 * In SG, as accepts are useless, it returns the provided argument as result.
476 * You should thus test whether (peer != accepted) before closing both of them.
478 * You should only call this on measurement sockets. It is automatically
479 * done for regular sockets, but you usually want more control about
480 * what's going on with measurement sockets.
482 gras_socket_t gras_socket_meas_accept(gras_socket_t peer){
485 xbt_assert0(peer->meas,
486 "No need to accept on non-measurement sockets (it's automatic)");
488 if (!peer->accepting) {
489 /* nothing to accept here (must be in SG) */
490 /* BUG: FIXME: this is BAD! it makes tricky to free the accepted socket*/
494 res = (peer->plugin->socket_accept)(peer);
495 res->meas = peer->meas;
496 CDEBUG1(gras_trp_meas,"meas_accepted onto %d",res->sd);
503 * Creating procdata for this module
505 static void *gras_trp_procdata_new() {
506 gras_trp_procdata_t res = xbt_new(s_gras_trp_procdata_t,1);
508 res->name = xbt_strdup("gras_trp");
510 res->sockets = xbt_dynar_new(sizeof(gras_socket_t*), NULL);
516 * Freeing procdata for this module
518 static void gras_trp_procdata_free(void *data) {
519 gras_trp_procdata_t res = (gras_trp_procdata_t)data;
521 xbt_dynar_free(&( res->sockets ));
527 * Module registration
529 int gras_trp_libdata_id;
530 void gras_trp_register() {
531 gras_trp_libdata_id = gras_procdata_add("gras_trp",gras_trp_procdata_new, gras_trp_procdata_free);