network.c 3.37 KB
Newer Older
1 2 3 4 5
#include "common.h"
#include "threads.h"
#include "connpool.h"
#include "connection.h"
#include "msgchannel.h"
6

7
#include <assert.h>
8 9
#include <sys/types.h>

10
//
11 12 13 14 15 16 17 18 19 20 21

#ifdef MINGW

#include <winsock2.h>
#include <ws2tcpip.h>

#else /* !MINGW */

#include <sys/socket.h>
#include <netdb.h>

22 23 24 25 26 27 28 29
#endif

//
// This appears to be missing on MingW and as well on DarwinX.
//
#ifndef AI_NUMERICSERV
#define AI_NUMERICSERV 0x00000008
#endif
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158


/**
 * Address info for the DiSL server.
 */
static struct addrinfo * disl_addrinfo;

/**
 * Pool of connections to DiSL server.
 */
static struct connection_pool disl_connections;

/**
 * Mutex to protect the manipulation with the connection pool.
 */
static mutex_t disl_connections_mutex;


static void
__connection_close_hook (struct connection * conn) {
	//
	// Send an empty message to the server before closing the connection
	// to indicate end of processing for that connection.
	//
	struct message shutdown = {
		.message_flags = 0,
		.control_size = 0, .classcode_size = 0,
		.control = NULL, .classcode = NULL
	};

	message_send (conn, &shutdown);
}


/**
 * Initializes the address info, the pool of connections to the remote
 * instrumentation server, and a mutex guarding the pool.
 */
void
network_init (const char * host_name, const char * port_number) {
	assert (host_name != NULL);
	assert (port_number != NULL);

	//

	mutex_init (&disl_connections_mutex);

	struct addrinfo hints;
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_flags = AI_NUMERICSERV | AI_CANONNAME;
	hints.ai_protocol = 0;

	int gai_result = getaddrinfo (host_name, port_number, &hints, &disl_addrinfo);
	check_error (gai_result != 0, gai_strerror (gai_result));

	connection_pool_init (&disl_connections, disl_addrinfo);
	connection_pool_set_before_close_hook (&disl_connections, __connection_close_hook);
}


/**
 * Closes all connections in the connection pool, releases the address
 * info, and destroys the mutex guarding the pool.
 */
void
network_fini () {
	//
	// Shut down and close all connections to the server. This has to run
	// under lock, because the connection pool will manipulate the list of
	// connections.
	//
	mutex_lock (&disl_connections_mutex);
	{
		connection_pool_close (&disl_connections);
	}
	mutex_unlock (&disl_connections_mutex);

	freeaddrinfo (disl_addrinfo);
	disl_addrinfo = NULL;

	mutex_destroy (&disl_connections_mutex);
}


struct connection *
network_acquire_connection () {
	dlprintf ("acquiring connection ... ");

	//
	// The connection pool must be protected by a lock so that multiple threads
	// acquiring a connection do not corrupt the internal state of the pool.
	//
	struct connection * connection;
	mutex_lock (&disl_connections_mutex);
	{
		connection = connection_pool_get_connection (&disl_connections);
	}
	mutex_unlock (&disl_connections_mutex);

	//

	dprintf ("done\n");
	return connection;
}


/**
 * Returns the connection to the pool of available connections.
 */
void
network_release_connection (struct connection * connection) {
	assert (connection != NULL);
	dlprintf ("releasing connection ... ");

	//
	// The connection pool must be protected by a lock so that multiple threads
	// releasing a connection do not corrupt the internal state of the pool.
	//
	mutex_lock (&disl_connections_mutex);
	{
		connection_pool_put_connection (&disl_connections, connection);
	}
	mutex_unlock (&disl_connections_mutex);

	//

	dprintf ("done\n");
}