-XBT_LOG_NEW_DEFAULT_SUBCATEGORY(transport,gras,"Conveying bytes over the network");
-XBT_LOG_NEW_SUBCATEGORY(trp_meas,transport,"Conveying bytes over the network without formating for perf measurements");
-static short int _gras_trp_started = 0;
-
-static xbt_dict_t _gras_trp_plugins; /* All registered plugins */
-static void gras_trp_plugin_free(void *p); /* free one of the plugins */
-
-static void
-gras_trp_plugin_new(const char *name, gras_trp_setup_t setup) {
- xbt_ex_t e;
-
- gras_trp_plugin_t plug = xbt_new0(s_gras_trp_plugin_t, 1);
-
- DEBUG1("Create plugin %s",name);
-
- plug->name=xbt_strdup(name);
-
- TRY {
- setup(plug);
- } CATCH(e) {
- if (e.category == mismatch_error) {
- /* SG plugin raise mismatch when in RL mode (and vice versa) */
- free(plug->name);
- free(plug);
- plug=NULL;
- xbt_ex_free(&e);
- } else {
- RETHROW;
- }
- }
-
- if (plug)
- xbt_dict_set(_gras_trp_plugins, name, plug, gras_trp_plugin_free);
-}
-
-void gras_trp_init(void){
- if (!_gras_trp_started) {
- /* make room for all plugins */
- _gras_trp_plugins=xbt_dict_new();
-
-#ifdef HAVE_WINSOCK2_H
- /* initialize the windows mechanism */
- {
- WORD wVersionRequested;
- WSADATA wsaData;
-
- wVersionRequested = MAKEWORD( 2, 0 );
- xbt_assert0(WSAStartup( wVersionRequested, &wsaData ) == 0,
- "Cannot find a usable WinSock DLL");
-
- /* Confirm that the WinSock DLL supports 2.0.*/
- /* Note that if the DLL supports versions greater */
- /* than 2.0 in addition to 2.0, it will still return */
- /* 2.0 in wVersion since that is the version we */
- /* requested. */
-
- xbt_assert0(LOBYTE( wsaData.wVersion ) == 2 &&
- HIBYTE( wsaData.wVersion ) == 0,
- "Cannot find a usable WinSock DLL");
- INFO0("Found and initialized winsock2");
- } /* The WinSock DLL is acceptable. Proceed. */
-#elif HAVE_WINSOCK_H
- { WSADATA wsaData;
- xbt_assert0(WSAStartup( 0x0101, &wsaData ) == 0,
- "Cannot find a usable WinSock DLL");
- INFO0("Found and initialized winsock");
- }
-#endif
-
- /* Add plugins */
- gras_trp_plugin_new("file",gras_trp_file_setup);
- gras_trp_plugin_new("sg",gras_trp_sg_setup);
- gras_trp_plugin_new("tcp", gras_trp_tcp_setup);
- }
-
- _gras_trp_started++;
-}
-
-void
-gras_trp_exit(void){
- xbt_dynar_t sockets = ((gras_trp_procdata_t) gras_libdata_by_id(gras_trp_libdata_id))->sockets;
- gras_socket_t sock_iter;
- int cursor;
-
- if (_gras_trp_started == 0) {
- return;
- }
-
- if ( --_gras_trp_started == 0 ) {
-#ifdef HAVE_WINSOCK_H
- if ( WSACleanup() == SOCKET_ERROR ) {
- if ( WSAGetLastError() == WSAEINPROGRESS ) {
- WSACancelBlockingCall();
- WSACleanup();
- }
- }
-#endif
-
- /* Close all the sockets */
- xbt_dynar_foreach(sockets,cursor,sock_iter) {
- VERB1("Closing the socket %p left open on exit. Maybe a socket leak?",
- sock_iter);
- gras_socket_close(sock_iter);
- }
-
- /* Delete the plugins */
- xbt_dict_free(&_gras_trp_plugins);
- }
-}
-
-
-void gras_trp_plugin_free(void *p) {
- gras_trp_plugin_t plug = p;
-
- if (plug) {
- if (plug->exit) {
- plug->exit(plug);
- } else if (plug->data) {
- DEBUG1("Plugin %s lacks exit(). Free data anyway.",plug->name);
- free(plug->data);
- }
-
- free(plug->name);
- free(plug);
- }
-}
-
-
-/**
- * gras_trp_socket_new:
- *
- * Malloc a new socket, and initialize it with defaults
- */
-void gras_trp_socket_new(int incoming,
- gras_socket_t *dst) {
-
- gras_socket_t sock=xbt_new0(s_gras_socket_t,1);
-
- DEBUG1("Create a new socket (%p)", (void*)sock);
-
- sock->plugin = NULL;
-
- sock->incoming = incoming ? 1:0;
- sock->outgoing = incoming ? 0:1;
- sock->accepting = incoming ? 1:0;
- sock->meas = 0;
-
- sock->sd = -1;
- sock->port = -1;
- sock->peer_port = -1;
- sock->peer_name = NULL;
-
- sock->data = NULL;
- sock->bufdata = NULL;
-
- *dst = sock;
-
- xbt_dynar_push(((gras_trp_procdata_t)
- gras_libdata_by_id(gras_trp_libdata_id))->sockets,dst);
- XBT_OUT;
-}
-