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. */
13 #include "gras/Transport/transport_private.h"
15 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(gras_trp,gras,"Conveying bytes over the network");
16 XBT_LOG_NEW_SUBCATEGORY(gras_trp_meas,gras_trp,"Conveying bytes over the network without formating for perf measurements");
17 static short int _gras_trp_started = 0;
19 static xbt_dict_t _gras_trp_plugins; /* All registered plugins */
20 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
23 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
26 gras_trp_plugin_t plug = xbt_new0(s_gras_trp_plugin_t, 1);
28 DEBUG1("Create plugin %s",name);
30 plug->name=xbt_strdup(name);
35 if (e.category == mismatch_error) {
36 /* SG plugin raise mismatch when in RL mode (and vice versa) */
47 xbt_dict_set(_gras_trp_plugins, name, plug, gras_trp_plugin_free);
50 void gras_trp_init(void){
51 if (!_gras_trp_started) {
52 /* make room for all plugins */
53 _gras_trp_plugins=xbt_dict_new();
55 #ifdef HAVE_WINSOCK2_H
56 /* initialize the windows mechanism */
58 WORD wVersionRequested;
61 wVersionRequested = MAKEWORD( 2, 0 );
62 xbt_assert0(WSAStartup( wVersionRequested, &wsaData ) == 0,
63 "Cannot find a usable WinSock DLL");
65 /* Confirm that the WinSock DLL supports 2.0.*/
66 /* Note that if the DLL supports versions greater */
67 /* than 2.0 in addition to 2.0, it will still return */
68 /* 2.0 in wVersion since that is the version we */
71 xbt_assert0(LOBYTE( wsaData.wVersion ) == 2 &&
72 HIBYTE( wsaData.wVersion ) == 0,
73 "Cannot find a usable WinSock DLL");
74 INFO0("Found and initialized winsock2");
75 } /* The WinSock DLL is acceptable. Proceed. */
78 xbt_assert0(WSAStartup( 0x0101, &wsaData ) == 0,
79 "Cannot find a usable WinSock DLL");
80 INFO0("Found and initialized winsock");
85 gras_trp_plugin_new("file",gras_trp_file_setup);
86 gras_trp_plugin_new("sg",gras_trp_sg_setup);
87 gras_trp_plugin_new("tcp", gras_trp_tcp_setup);
95 xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
96 gras_socket_t sock_iter;
99 if (_gras_trp_started == 0) {
103 if ( --_gras_trp_started == 0 ) {
104 #ifdef HAVE_WINSOCK_H
105 if ( WSACleanup() == SOCKET_ERROR ) {
106 if ( WSAGetLastError() == WSAEINPROGRESS ) {
107 WSACancelBlockingCall();
113 /* Close all the sockets */
114 xbt_dynar_foreach(sockets,cursor,sock_iter) {
115 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
117 gras_socket_close(sock_iter);
120 /* Delete the plugins */
121 xbt_dict_free(&_gras_trp_plugins);
126 void gras_trp_plugin_free(void *p) {
127 gras_trp_plugin_t plug = p;
132 } else if (plug->data) {
133 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
144 * gras_trp_socket_new:
146 * Malloc a new socket, and initialize it with defaults
148 void gras_trp_socket_new(int incoming,
149 gras_socket_t *dst) {
151 gras_socket_t sock=xbt_new0(s_gras_socket_t,1);
153 VERB1("Create a new socket (%p)", (void*)sock);
157 sock->incoming = incoming ? 1:0;
158 sock->outgoing = incoming ? 0:1;
159 sock->accepting = incoming ? 1:0;
166 sock->peer_port = -1;
167 sock->peer_name = NULL;
168 sock->peer_proc = NULL;
171 sock->bufdata = NULL;
175 xbt_dynar_push(((gras_trp_procdata_t)
176 gras_libdata_by_id(gras_trp_libdata_id))->sockets,dst);
181 * @brief Opens a server socket and makes it ready to be listened to.
182 * @param port: port on which you want to listen
183 * @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)
184 * @param measurement: whether this socket is meant to convey measurement (if you don't know, use 0 to exchange regular messages)
186 * In real life, you'll get a TCP socket.
189 gras_socket_server_ext(unsigned short port,
191 unsigned long int buf_size,
195 gras_trp_plugin_t trp;
198 DEBUG2("Create a server socket from plugin %s on port %d",
199 gras_if_RL() ? "tcp" : "sg",
201 trp = gras_trp_plugin_get_by_name(gras_if_SG() ? "sg":"tcp");
203 /* defaults settings */
204 gras_trp_socket_new(1,&sock);
207 sock->buf_size = buf_size>0 ? buf_size : 32*1024;
208 sock->meas = measurement;
210 /* Call plugin socket creation function */
211 DEBUG1("Prepare socket with plugin (fct=%p)",trp->socket_server);
213 trp->socket_server(trp, sock);
214 DEBUG3("in=%c out=%c accept=%c",
215 sock->incoming?'y':'n',
216 sock->outgoing?'y':'n',
217 sock->accepting?'y':'n');
220 gras_socket_t sock_iter;
221 xbt_dynar_t socks = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
222 xbt_dynar_foreach(socks, cursor, sock_iter) {
224 xbt_dynar_cursor_rm(socks,&cursor);
231 ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport = port;
235 * @brief Opens a server socket on any port in the given range
237 * @param minport: first port we will try
238 * @param maxport: last port we will try
239 * @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)
240 * @param measurement: whether this socket is meant to convey measurement (if you don't know, use 0 to exchange regular messages)
242 * If none of the provided ports works, raises the exception got when trying the last possibility
245 gras_socket_server_range(unsigned short minport, unsigned short maxport,
246 unsigned long int buf_size, int measurement) {
249 gras_socket_t res=NULL;
252 for (port=minport; port<maxport;port ++) {
254 res=gras_socket_server_ext(port,buf_size,measurement);
267 * @brief Opens a client socket to a remote host.
268 * @param host: who you want to connect to
269 * @param port: where you want to connect to on this host
270 * @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)
271 * @param measurement: whether this socket is meant to convey measurement (if you don't know, use 0 to exchange regular messages)
273 * In real life, you'll get a TCP socket.
276 gras_socket_client_ext(const char *host,
279 unsigned long int buf_size,
283 gras_trp_plugin_t trp;
286 trp = gras_trp_plugin_get_by_name(gras_if_SG() ? "sg":"tcp");
288 DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
289 /* defaults settings */
290 gras_trp_socket_new(0,&sock);
292 sock->peer_port = port;
293 sock->peer_name = (char*)strdup(host?host:"localhost");
294 sock->buf_size = buf_size>0 ? buf_size: 32*1024;
295 sock->meas = measurement;
297 /* plugin-specific */
299 (*trp->socket_client)(trp, sock);
300 DEBUG3("in=%c out=%c accept=%c",
301 sock->incoming?'y':'n',
302 sock->outgoing?'y':'n',
303 sock->accepting?'y':'n');
313 * gras_socket_server:
315 * Opens a server socket and make it ready to be listened to.
316 * In real life, you'll get a TCP socket.
319 gras_socket_server(unsigned short port) {
320 return gras_socket_server_ext(port,32*1024,0);
323 /** @brief Opens a client socket to a remote host */
325 gras_socket_client(const char *host,
326 unsigned short port) {
327 return gras_socket_client_ext(host,port,0,0);
330 /** @brief Opens a client socket to a remote host specified as '\a host:\a port' */
332 gras_socket_client_from_string(const char *host) {
333 xbt_peer_t p = xbt_peer_from_string(host);
334 gras_socket_t res = gras_socket_client_ext(p->name,p->port,0,0);
339 /** \brief Close socket */
340 void gras_socket_close(gras_socket_t sock) {
341 xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
342 gras_socket_t sock_iter;
346 VERB1("Close %p",sock);
347 /* FIXME: Issue an event when the socket is closed */
349 xbt_dynar_foreach(sockets,cursor,sock_iter) {
350 if (sock == sock_iter) {
351 xbt_dynar_cursor_rm(sockets,&cursor);
352 if (sock->plugin->socket_close)
353 (* sock->plugin->socket_close)(sock);
355 /* free the memory */
357 free(sock->peer_name);
363 WARN1("Ignoring request to free an unknown socket (%p). Execution stack:",sock);
364 xbt_backtrace_display();
372 * Send a bunch of bytes from on socket
373 * (stable if we know the storage will keep as is until the next trp_flush)
376 gras_trp_send(gras_socket_t sd, char *data, long int size, int stable) {
377 xbt_assert0(sd->outgoing,"Socket not suited for data send");
378 (*sd->plugin->send)(sd,data,size,stable);
381 * gras_trp_chunk_recv:
383 * Receive a bunch of bytes from a socket
386 gras_trp_recv(gras_socket_t sd, char *data, long int size) {
387 xbt_assert0(sd->incoming,"Socket not suited for data receive");
388 (sd->plugin->recv)(sd,data,size);
394 * Make sure all pending communications are done
397 gras_trp_flush(gras_socket_t sd) {
398 if (sd->plugin->flush)
399 (sd->plugin->flush)(sd);
403 gras_trp_plugin_get_by_name(const char *name){
404 return xbt_dict_get(_gras_trp_plugins,name);
407 int gras_socket_my_port (gras_socket_t sock) {
410 int gras_socket_peer_port(gras_socket_t sock) {
411 return sock->peer_port;
413 char *gras_socket_peer_name(gras_socket_t sock) {
414 return sock->peer_name;
416 char *gras_socket_peer_proc(gras_socket_t sock) {
417 return sock->peer_proc;
420 void gras_socket_peer_proc_set(gras_socket_t sock,char*peer_proc) {
421 sock->peer_proc = peer_proc;
424 /** \brief Check if the provided socket is a measurement one (or a regular one) */
425 int gras_socket_is_meas(gras_socket_t sock) {
429 /** \brief Send a chunk of (random) data over a measurement socket
431 * @param peer measurement socket to use for the experiment
432 * @param timeout timeout (in seconds)
433 * @param exp_size total amount of data to send (in bytes).
434 * @param msg_size size of each chunk sent over the socket (in bytes).
436 * Calls to gras_socket_meas_send() and gras_socket_meas_recv() on
437 * each side of the socket should be paired.
439 * The exchanged data is zeroed to make sure it's initialized, but
440 * there is no way to control what is sent (ie, you cannot use these
441 * functions to exchange data out of band).
443 void gras_socket_meas_send(gras_socket_t peer,
444 unsigned int timeout,
445 unsigned long int exp_size,
446 unsigned long int msg_size) {
448 unsigned long int exp_sofar;
453 chunk=xbt_malloc0(msg_size);
455 xbt_assert0(peer->meas,"Asked to send measurement data on a regular socket");
456 xbt_assert0(peer->outgoing,"Socket not suited for data send (was created with gras_socket_server(), not gras_socket_client())");
458 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
459 CDEBUG5(gras_trp_meas,"Sent %lu of %lu (msg_size=%ld) to %s:%d",
460 exp_sofar,exp_size,msg_size,
461 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
462 (*peer->plugin->raw_send)(peer,chunk,msg_size);
464 CDEBUG5(gras_trp_meas,"Sent %lu of %lu (msg_size=%ld) to %s:%d",
465 exp_sofar,exp_size,msg_size,
466 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
474 /** \brief Receive a chunk of data over a measurement socket
476 * Calls to gras_socket_meas_send() and gras_socket_meas_recv() on
477 * each side of the socket should be paired.
479 void gras_socket_meas_recv(gras_socket_t peer,
480 unsigned int timeout,
481 unsigned long int exp_size,
482 unsigned long int msg_size){
485 unsigned long int exp_sofar;
490 chunk = xbt_malloc(msg_size);
492 xbt_assert0(peer->meas,
493 "Asked to receive measurement data on a regular socket");
494 xbt_assert0(peer->incoming,"Socket not suited for data receive");
496 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
497 CDEBUG5(gras_trp_meas,"Recvd %ld of %lu (msg_size=%ld) from %s:%d",
498 exp_sofar,exp_size,msg_size,
499 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
500 (peer->plugin->raw_recv)(peer,chunk,msg_size);
502 CDEBUG5(gras_trp_meas,"Recvd %ld of %lu (msg_size=%ld) from %s:%d",
503 exp_sofar,exp_size,msg_size,
504 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
512 * \brief Something similar to the good old accept system call.
514 * Make sure that there is someone speaking to the provided server socket.
515 * In RL, it does an accept(2) and return the result as last argument.
516 * In SG, as accepts are useless, it returns the provided argument as result.
517 * You should thus test whether (peer != accepted) before closing both of them.
519 * You should only call this on measurement sockets. It is automatically
520 * done for regular sockets, but you usually want more control about
521 * what's going on with measurement sockets.
523 gras_socket_t gras_socket_meas_accept(gras_socket_t peer){
526 xbt_assert0(peer->meas,
527 "No need to accept on non-measurement sockets (it's automatic)");
529 if (!peer->accepting) {
530 /* nothing to accept here (must be in SG) */
531 /* BUG: FIXME: this is BAD! it makes tricky to free the accepted socket*/
535 res = (peer->plugin->socket_accept)(peer);
536 res->meas = peer->meas;
537 CDEBUG1(gras_trp_meas,"meas_accepted onto %d",res->sd);
544 * Creating procdata for this module
546 static void *gras_trp_procdata_new() {
547 gras_trp_procdata_t res = xbt_new(s_gras_trp_procdata_t,1);
549 res->name = xbt_strdup("gras_trp");
551 res->sockets = xbt_dynar_new(sizeof(gras_socket_t*), NULL);
558 * Freeing procdata for this module
560 static void gras_trp_procdata_free(void *data) {
561 gras_trp_procdata_t res = (gras_trp_procdata_t)data;
563 xbt_dynar_free(&( res->sockets ));
568 void gras_trp_socketset_dump(const char *name) {
569 gras_trp_procdata_t procdata =
570 (gras_trp_procdata_t)gras_libdata_by_id(gras_trp_libdata_id);
575 INFO1("** Dump the socket set %s",name);
576 xbt_dynar_foreach(procdata->sockets, it, s) {
577 INFO4(" %p -> %s:%d %s",
578 s,gras_socket_peer_name(s),gras_socket_peer_port(s),
579 s->valid?"(valid)":"(peer dead)");
581 INFO1("** End of socket set %s",name);
585 * Module registration
587 int gras_trp_libdata_id;
588 void gras_trp_register() {
589 gras_trp_libdata_id = gras_procdata_add("gras_trp",gras_trp_procdata_new, gras_trp_procdata_free);
592 int gras_os_myport(void) {
593 return ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->myport;