SockIt
Public Member Functions | Private Member Functions | Private Attributes

NetworkThread Class Reference

#include <NetworkThread.h>

List of all members.

Public Member Functions

 NetworkThread ()
virtual ~NetworkThread ()
boost::shared_ptr< TcpServercreate_tcp_server (int port, boost::optional< map< string, string > > options)
boost::shared_ptr< TcpClientcreate_tcp_client (const string &host, int port, boost::optional< map< string, string > > options)
boost::shared_ptr< UdpServercreate_udp_server (int port, boost::optional< map< string, string > > options)
boost::shared_ptr< UdpClientcreate_udp_client (const string &host, int port, boost::optional< map< string, string > > options)

Private Member Functions

void run ()

Private Attributes

string logger_category
boost::asio::io_service io_service
boost::thread background_thread
set< boost::shared_ptr
< UdpClient > > 
udp_clients
set< boost::shared_ptr
< UdpServer > > 
udp_servers
set< boost::shared_ptr
< TcpClient > > 
tcp_clients
set< boost::shared_ptr
< TcpServer > > 
tcp_servers

Detailed Description

Class representing a high-level thread of computation for the plugin. This class a single background thread associated with it to perform asynchronous I/O for all clients and servers created from this (high-level) thread.

This class also exposes four methods to Javascript to allow creating servers and clients whose I/O will be performed on this NetworkThread.

Definition at line 37 of file NetworkThread.h.


Constructor & Destructor Documentation

NetworkThread::NetworkThread ( )

Creates a new network thread, registers its functions to the Javascript, and starts up the background thread.

Definition at line 10 of file NetworkThread.cpp.

References background_thread, create_tcp_client(), create_tcp_server(), create_udp_client(), create_udp_server(), Logger::info(), logger_category, Logger::NO_PORT, and run().

                             :
    logger_category("NETWORK THREAD")
{
    // No initialization required for the logger
    Logger::info("Network thread initialized", Logger::NO_PORT, logger_category);

    // Register root methods for creating servers & clients
    registerMethod("createUdpClient", make_method(this, &NetworkThread::create_udp_client));
    registerMethod("createUdpServer", make_method(this, &NetworkThread::create_udp_server));
    registerMethod("createTcpClient", make_method(this, &NetworkThread::create_tcp_client));
    registerMethod("createTcpServer", make_method(this, &NetworkThread::create_tcp_server));

    // Start the I/O service running in the background
    background_thread = boost::thread(boost::bind(&NetworkThread::run, this));
}
NetworkThread::~NetworkThread ( ) [virtual]

Destroys this network thread by stopping the background I/O service and joining on the background thread.

Definition at line 26 of file NetworkThread.cpp.

References background_thread, io_service, logger_category, Logger::NO_PORT, tcp_clients, tcp_servers, udp_clients, udp_servers, and Logger::warn().

{
    // Stop the IO service, and wait for the background thread to exit, log (but don't do anything) if there's an error
    try
    {
        io_service.stop();
        background_thread.join();
    }
    catch (std::exception & error)
    {
        Logger::warn("Warning, improperly shutdown network thread IO service: '" + string(error.what()) + "'",
                Logger::NO_PORT, logger_category);
    }
    
    tcp_clients.clear();
    tcp_servers.clear();
    udp_clients.clear();
    udp_servers.clear();
}

Member Function Documentation

boost::shared_ptr< TcpClient > NetworkThread::create_tcp_client ( const string &  host,
int  port,
boost::optional< map< string, string > >  options 
)

Creates a new TCP client on this NetworkThread.

Parameters:
hostThe hostname to which this TCP client will connect
portThe port on the remote host to which this client should connect
optionsA map of options to specify the behavior of this object.
Returns:
A shared pointer to a newly created TCP client

Definition at line 63 of file NetworkThread.cpp.

References Logger::info(), io_service, logger_category, Logger::NO_PORT, and tcp_clients.

Referenced by SockItAPI::create_tcp_client(), and NetworkThread().

{
    Logger::info(
            "Spawning TCP client to '" + boost::lexical_cast<string>(host) + ":" + boost::lexical_cast<string>(port)
                    + "'", Logger::NO_PORT, logger_category);

    if (options)
    {
        boost::shared_ptr<TcpClient> new_client(new TcpClient(host, port, io_service, *options));
        tcp_clients.insert(new_client);
        return new_client;
    }

    boost::shared_ptr<TcpClient> new_client(new TcpClient(host, port, io_service));
    tcp_clients.insert(new_client);
    return new_client;
}
boost::shared_ptr< TcpServer > NetworkThread::create_tcp_server ( int  port,
boost::optional< map< string, string > >  options 
)

Creates a new TCP server on this NetworkThread.

Parameters:
portThe port on which this new TCP server should listen
optionsA map of options to specify the behavior of this object.
Returns:
A shared pointer to a newly created TCP server

Definition at line 46 of file NetworkThread.cpp.

References Logger::info(), io_service, logger_category, Logger::NO_PORT, and tcp_servers.

Referenced by SockItAPI::create_tcp_server(), and NetworkThread().

{
    Logger::info("Spawning TCP server on port = " + boost::lexical_cast<string>(port), Logger::NO_PORT,
            logger_category);

    if (options)
    {
        boost::shared_ptr<TcpServer> new_server(new TcpServer(port, io_service, *options));
        tcp_servers.insert(new_server);
        return new_server;
    }

    boost::shared_ptr<TcpServer> new_server(new TcpServer(port, io_service));
    tcp_servers.insert(new_server);
    return new_server;
}
boost::shared_ptr< UdpClient > NetworkThread::create_udp_client ( const string &  host,
int  port,
boost::optional< map< string, string > >  options 
)

Creates a new UDP client on this NetworkThread.

Parameters:
hostThe hostname to which this UDP client will connect
portThe port on the remote host to which this client should connect
optionsA map of options to specify the behavior of this object.
Returns:
A shared pointer to a newly created UDP client

Definition at line 99 of file NetworkThread.cpp.

References Logger::info(), io_service, logger_category, Logger::NO_PORT, and udp_clients.

Referenced by SockItAPI::create_udp_client(), and NetworkThread().

{
    Logger::info(
            "Spawning TCP client to '" + boost::lexical_cast<string>(host) + ":" + boost::lexical_cast<
                    string>(port) + "'", Logger::NO_PORT, logger_category);

    if (options)
    {
        boost::shared_ptr<UdpClient> new_client(new UdpClient(host, port, io_service, *options));
        udp_clients.insert(new_client);
        return new_client;
    }

    boost::shared_ptr<UdpClient> new_client(new UdpClient(host, port, io_service));
    udp_clients.insert(new_client);
    return new_client;
}
boost::shared_ptr< UdpServer > NetworkThread::create_udp_server ( int  port,
boost::optional< map< string, string > >  options 
)

Creates a new UDP server on this NetworkThread.

Parameters:
portThe port on which this new UDP server should listen
optionsA map of options to specify the behavior of this object.
Returns:
A shared pointer to a newly created UDP server

Definition at line 82 of file NetworkThread.cpp.

References Logger::info(), io_service, logger_category, Logger::NO_PORT, and udp_servers.

Referenced by SockItAPI::create_udp_server(), and NetworkThread().

{
    Logger::info("Spawning UDP server on port = " + boost::lexical_cast<string>(port), Logger::NO_PORT,
            logger_category);

    if (options)
    {
        boost::shared_ptr<UdpServer> new_server(new UdpServer(port, io_service, *options));
        udp_servers.insert(new_server);
        return new_server;
    }

    boost::shared_ptr<UdpServer> new_server(new UdpServer(port, io_service));
    udp_servers.insert(new_server);
    return new_server;
}
void NetworkThread::run ( ) [private]

A helper method in which the background thread will run. This method simply starts the I/O service, and returns once the I/O service is stopped.

Definition at line 118 of file NetworkThread.cpp.

References Logger::error(), io_service, logger_category, and Logger::NO_PORT.

Referenced by NetworkThread().

{
    boost::asio::io_service::work work(io_service);

    try
    {
        io_service.run();
    }
    catch (std::exception & error)
    {
        // Catch any exception and dump it to the log
        Logger::error("Error running network thread: '" + string(error.what()) + "'", Logger::NO_PORT, logger_category);
    }
}

Member Data Documentation

boost::thread NetworkThread::background_thread [private]

The background thread which launches the I/O service, and exits when the I/O service is stopped.

Definition at line 113 of file NetworkThread.h.

Referenced by NetworkThread(), and ~NetworkThread().

boost::asio::io_service NetworkThread::io_service [private]

The boost I/O service to be shared between all clients and servers created on this NetworkThread, which will be used to perform asynchronous I/O.

Definition at line 108 of file NetworkThread.h.

Referenced by create_tcp_client(), create_tcp_server(), create_udp_client(), create_udp_server(), run(), and ~NetworkThread().

The category string for a network thread

Definition at line 96 of file NetworkThread.h.

Referenced by create_tcp_client(), create_tcp_server(), create_udp_client(), create_udp_server(), NetworkThread(), run(), and ~NetworkThread().

set<boost::shared_ptr<TcpClient> > NetworkThread::tcp_clients [private]

Set of all tcp clients 'on' this thread

Definition at line 122 of file NetworkThread.h.

Referenced by create_tcp_client(), and ~NetworkThread().

set<boost::shared_ptr<TcpServer> > NetworkThread::tcp_servers [private]

Set of all tcp servers 'on' this thread

Definition at line 125 of file NetworkThread.h.

Referenced by create_tcp_server(), and ~NetworkThread().

set<boost::shared_ptr<UdpClient> > NetworkThread::udp_clients [private]

Set of all udp clients 'on' this thread

Definition at line 116 of file NetworkThread.h.

Referenced by create_udp_client(), and ~NetworkThread().

set<boost::shared_ptr<UdpServer> > NetworkThread::udp_servers [private]

Set of all udp servers 'on' this thread

Definition at line 119 of file NetworkThread.h.

Referenced by create_udp_server(), and ~NetworkThread().


The documentation for this class was generated from the following files:
 All Classes Files Functions Variables Typedefs Friends Defines