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. */
11 #include "gras/Transport/transport_private.h"
13 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(transport,gras,"Conveying bytes over the network");
14 XBT_LOG_NEW_SUBCATEGORY(trp_meas,transport,"Conveying bytes over the network without formating for perf measurements");
15 static short int _gras_trp_started = 0;
17 static xbt_dict_t _gras_trp_plugins; /* All registered plugins */
18 static void gras_trp_plugin_free(void *p); /* free one of the plugins */
21 gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
24 gras_trp_plugin_t *plug = xbt_new0(gras_trp_plugin_t, 1);
26 DEBUG1("Create plugin %s",name);
28 plug->name=xbt_strdup(name);
30 errcode = setup(plug);
33 /* SG plugin return mismatch when in RL mode (and vice versa) */
39 xbt_dict_set(_gras_trp_plugins,
40 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("tcp", gras_trp_tcp_setup);
85 gras_trp_plugin_new("file",gras_trp_file_setup);
86 gras_trp_plugin_new("sg",gras_trp_sg_setup);
88 /* buf is composed, so it must come after the others */
89 gras_trp_plugin_new("buf", gras_trp_buf_setup);
97 xbt_dynar_t sockets = gras_socketset_get();
98 gras_socket_t sock_iter;
101 if (_gras_trp_started == 0) {
105 if ( --_gras_trp_started == 0 ) {
106 #ifdef HAVE_WINSOCK_H
107 if ( WSACleanup() == SOCKET_ERROR ) {
108 if ( WSAGetLastError() == WSAEINPROGRESS ) {
109 WSACancelBlockingCall();
115 /* Close all the sockets */
116 xbt_dynar_foreach(sockets,cursor,sock_iter) {
117 VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
119 gras_socket_close(sock_iter);
122 /* Delete the plugins */
123 xbt_dict_free(&_gras_trp_plugins);
128 void gras_trp_plugin_free(void *p) {
129 gras_trp_plugin_t *plug = p;
134 } else if (plug->data) {
135 DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
146 * gras_trp_socket_new:
148 * Malloc a new socket, and initialize it with defaults
150 void gras_trp_socket_new(int incoming,
151 gras_socket_t *dst) {
153 gras_socket_t sock=xbt_new0(s_gras_socket_t,1);
155 DEBUG1("Create a new socket (%p)", (void*)sock);
161 sock->incoming = incoming ? 1:0;
162 sock->outgoing = incoming ? 0:1;
163 sock->accepting = incoming ? 1:0;
166 sock->peer_port = -1;
167 sock->peer_name = NULL;
172 xbt_dynar_push(gras_socketset_get(),dst);
178 * gras_socket_server_ext:
180 * Opens a server socket and make it ready to be listened to.
181 * In real life, you'll get a TCP socket.
184 gras_socket_server_ext(unsigned short port,
186 unsigned long int bufSize,
189 /* OUT */ gras_socket_t *dst) {
192 gras_trp_plugin_t *trp;
197 DEBUG2("Create a server socket from plugin %s on port %d",
198 gras_if_RL() ? "tcp" : "sg",
200 TRY(gras_trp_plugin_get_by_name((measurement? (gras_if_RL() ? "tcp" : "sg")
204 /* defaults settings */
205 gras_trp_socket_new(1,&sock);
208 sock->bufSize = bufSize;
209 sock->meas = measurement;
211 /* Call plugin socket creation function */
212 DEBUG1("Prepare socket with plugin (fct=%p)",trp->socket_server);
213 errcode = 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');
219 if (errcode != no_error) {
230 * gras_socket_client_ext:
232 * Opens a client socket to a remote host.
233 * In real life, you'll get a TCP socket.
236 gras_socket_client_ext(const char *host,
239 unsigned long int bufSize,
242 /* OUT */ gras_socket_t *dst) {
245 gras_trp_plugin_t *trp;
250 TRY(gras_trp_plugin_get_by_name((measurement? (gras_if_RL() ? "tcp" : "sg")
254 DEBUG1("Create a client socket from plugin %s",gras_if_RL() ? "tcp" : "sg");
255 /* defaults settings */
256 gras_trp_socket_new(0,&sock);
258 sock->peer_port = port;
259 sock->peer_name = (char*)strdup(host?host:"localhost");
260 sock->bufSize = bufSize;
261 sock->meas = measurement;
263 /* plugin-specific */
264 errcode= (*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');
270 if (errcode != no_error) {
281 * gras_socket_server:
283 * Opens a server socket and make it ready to be listened to.
284 * In real life, you'll get a TCP socket.
287 gras_socket_server(unsigned short port,
288 /* OUT */ gras_socket_t *dst) {
289 return gras_socket_server_ext(port,32,0,dst);
293 * gras_socket_client:
295 * Opens a client socket to a remote host.
296 * In real life, you'll get a TCP socket.
299 gras_socket_client(const char *host,
301 /* OUT */ gras_socket_t *dst) {
302 return gras_socket_client_ext(host,port,32,0,dst);
306 void gras_socket_close(gras_socket_t sock) {
307 xbt_dynar_t sockets = gras_socketset_get();
308 gras_socket_t sock_iter;
312 /* FIXME: Issue an event when the socket is closed */
314 xbt_dynar_foreach(sockets,cursor,sock_iter) {
315 if (sock == sock_iter) {
316 xbt_dynar_cursor_rm(sockets,&cursor);
317 if (sock->plugin->socket_close)
318 (* sock->plugin->socket_close)(sock);
320 /* free the memory */
322 free(sock->peer_name);
328 WARN1("Ignoring request to free an unknown socket (%p)",sock);
334 * gras_trp_chunk_send:
336 * Send a bunch of bytes from on socket
339 gras_trp_chunk_send(gras_socket_t sd,
342 xbt_assert1(sd->outgoing,
343 "Socket not suited for data send (outgoing=%c)",
344 sd->outgoing?'y':'n');
345 xbt_assert1(sd->plugin->chunk_send,
346 "No function chunk_send on transport plugin %s",
348 return (*sd->plugin->chunk_send)(sd,data,size);
351 * gras_trp_chunk_recv:
353 * Receive a bunch of bytes from a socket
356 gras_trp_chunk_recv(gras_socket_t sd,
359 xbt_assert0(sd->incoming,
360 "Socket not suited for data receive");
361 xbt_assert1(sd->plugin->chunk_recv,
362 "No function chunk_recv on transport plugin %s",
364 return (sd->plugin->chunk_recv)(sd,data,size);
370 * Make sure all pending communications are done
373 gras_trp_flush(gras_socket_t sd) {
374 return (sd->plugin->flush)(sd);
378 gras_trp_plugin_get_by_name(const char *name,
379 gras_trp_plugin_t **dst){
381 return xbt_dict_get(_gras_trp_plugins,name,(void**)dst);
384 int gras_socket_my_port (gras_socket_t sock) {
387 int gras_socket_peer_port(gras_socket_t sock) {
388 return sock->peer_port;
390 char *gras_socket_peer_name(gras_socket_t sock) {
391 return sock->peer_name;
394 xbt_error_t gras_socket_meas_send(gras_socket_t peer,
395 unsigned int timeout,
396 unsigned long int exp_size,
397 unsigned long int msg_size) {
399 char *chunk = xbt_malloc0(msg_size);
400 unsigned long int exp_sofar;
404 xbt_assert0(peer->meas,"Asked to send measurement data on a regular socket");
406 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
407 CDEBUG5(trp_meas,"Sent %lu of %lu (msg_size=%ld) to %s:%d",
408 exp_sofar,exp_size,msg_size,
409 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
410 TRY(gras_trp_chunk_send(peer,chunk,msg_size));
412 CDEBUG5(trp_meas,"Sent %lu of %lu (msg_size=%ld) to %s:%d",
413 exp_sofar,exp_size,msg_size,
414 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
422 xbt_error_t gras_socket_meas_recv(gras_socket_t peer,
423 unsigned int timeout,
424 unsigned long int exp_size,
425 unsigned long int msg_size){
428 char *chunk = xbt_malloc(msg_size);
429 unsigned long int exp_sofar;
433 xbt_assert0(peer->meas,"Asked to receive measurement data on a regular socket\n");
435 for (exp_sofar=0; exp_sofar < exp_size; exp_sofar += msg_size) {
436 CDEBUG5(trp_meas,"Recvd %ld of %lu (msg_size=%ld) from %s:%d",
437 exp_sofar,exp_size,msg_size,
438 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
439 TRY(gras_trp_chunk_recv(peer,chunk,msg_size));
441 CDEBUG5(trp_meas,"Recvd %ld of %lu (msg_size=%ld) from %s:%d",
442 exp_sofar,exp_size,msg_size,
443 gras_socket_peer_name(peer), gras_socket_peer_port(peer));
452 * \brief Something similar to the good old accept system call.
454 * Make sure that there is someone speaking to the provided server socket.
455 * In RL, it does an accept(2), close the master socket, and put the accepted
456 * socket in place of the provided one. In SG, it does not do anything for
458 * You should only call this on measurement sockets. It is automatically
459 * done for regular sockets, but you usually want more control about
460 * what's going on with measurement sockets.
464 xbt_error_t gras_socket_meas_accept(gras_socket_t peer, gras_socket_t *accepted){
468 xbt_assert0(peer->meas,
469 "No need to accept on non-measurement sockets (it's automatic)");
471 if (!peer->accepting) {
472 /* nothing to accept here */
477 TRY((peer->plugin->socket_accept)(peer,accepted));
478 (*accepted)->meas = peer->meas;
479 CDEBUG1(trp_meas,"meas_accepted onto %d",(*accepted)->sd);
486 * Creating procdata for this module
488 static void *gras_trp_procdata_new() {
489 gras_trp_procdata_t res = xbt_new(s_gras_trp_procdata_t,1);
491 res->sockets = xbt_dynar_new(sizeof(gras_socket_t*), NULL);
497 * Freeing procdata for this module
499 static void gras_trp_procdata_free(void *data) {
500 gras_trp_procdata_t res = (gras_trp_procdata_t)data;
502 xbt_dynar_free(&( res->sockets ));
507 * Module registration
509 void gras_trp_register() {
510 gras_procdata_add("gras_trp",gras_trp_procdata_new, gras_trp_procdata_free);
515 gras_socketset_get(void) {
517 return ((gras_trp_procdata_t) gras_libdata_get("gras_trp"))->sockets;