SockIt

NetworkThread.cpp

Go to the documentation of this file.
00001 /*
00002  * NetworkThread.cpp
00003  *
00004  *  Created on: Jun 3, 2011
00005  *      Author: jtedesco
00006  */
00007 
00008 #include "NetworkThread.h"
00009 
00010 NetworkThread::NetworkThread() :
00011     logger_category("NETWORK THREAD")
00012 {
00013     // No initialization required for the logger
00014     Logger::info("Network thread initialized", Logger::NO_PORT, logger_category);
00015 
00016     // Register root methods for creating servers & clients
00017     registerMethod("createUdpClient", make_method(this, &NetworkThread::create_udp_client));
00018     registerMethod("createUdpServer", make_method(this, &NetworkThread::create_udp_server));
00019     registerMethod("createTcpClient", make_method(this, &NetworkThread::create_tcp_client));
00020     registerMethod("createTcpServer", make_method(this, &NetworkThread::create_tcp_server));
00021 
00022     // Start the I/O service running in the background
00023     background_thread = boost::thread(boost::bind(&NetworkThread::run, this));
00024 }
00025 
00026 NetworkThread::~NetworkThread()
00027 {
00028     // Stop the IO service, and wait for the background thread to exit, log (but don't do anything) if there's an error
00029     try
00030     {
00031         io_service.stop();
00032         background_thread.join();
00033     }
00034     catch (std::exception & error)
00035     {
00036         Logger::warn("Warning, improperly shutdown network thread IO service: '" + string(error.what()) + "'",
00037                 Logger::NO_PORT, logger_category);
00038     }
00039     
00040     tcp_clients.clear();
00041     tcp_servers.clear();
00042     udp_clients.clear();
00043     udp_servers.clear();
00044 }
00045 
00046 boost::shared_ptr<TcpServer> NetworkThread::create_tcp_server(int port, boost::optional<map<string, string> > options)
00047 {
00048     Logger::info("Spawning TCP server on port = " + boost::lexical_cast<string>(port), Logger::NO_PORT,
00049             logger_category);
00050 
00051     if (options)
00052     {
00053         boost::shared_ptr<TcpServer> new_server(new TcpServer(port, io_service, *options));
00054         tcp_servers.insert(new_server);
00055         return new_server;
00056     }
00057 
00058     boost::shared_ptr<TcpServer> new_server(new TcpServer(port, io_service));
00059     tcp_servers.insert(new_server);
00060     return new_server;
00061 }
00062 
00063 boost::shared_ptr<TcpClient> NetworkThread::create_tcp_client(const string & host, int port,
00064         boost::optional<map<string, string> > options)
00065 {
00066     Logger::info(
00067             "Spawning TCP client to '" + boost::lexical_cast<string>(host) + ":" + boost::lexical_cast<string>(port)
00068                     + "'", Logger::NO_PORT, logger_category);
00069 
00070     if (options)
00071     {
00072         boost::shared_ptr<TcpClient> new_client(new TcpClient(host, port, io_service, *options));
00073         tcp_clients.insert(new_client);
00074         return new_client;
00075     }
00076 
00077     boost::shared_ptr<TcpClient> new_client(new TcpClient(host, port, io_service));
00078     tcp_clients.insert(new_client);
00079     return new_client;
00080 }
00081 
00082 boost::shared_ptr<UdpServer> NetworkThread::create_udp_server(int port, boost::optional<map<string, string> > options)
00083 {
00084     Logger::info("Spawning UDP server on port = " + boost::lexical_cast<string>(port), Logger::NO_PORT,
00085             logger_category);
00086 
00087     if (options)
00088     {
00089         boost::shared_ptr<UdpServer> new_server(new UdpServer(port, io_service, *options));
00090         udp_servers.insert(new_server);
00091         return new_server;
00092     }
00093 
00094     boost::shared_ptr<UdpServer> new_server(new UdpServer(port, io_service));
00095     udp_servers.insert(new_server);
00096     return new_server;
00097 }
00098 
00099 boost::shared_ptr<UdpClient> NetworkThread::create_udp_client(const string & host, int port,
00100         boost::optional<map<string, string> > options)
00101 {
00102     Logger::info(
00103             "Spawning TCP client to '" + boost::lexical_cast<string>(host) + ":" + boost::lexical_cast<
00104                     string>(port) + "'", Logger::NO_PORT, logger_category);
00105 
00106     if (options)
00107     {
00108         boost::shared_ptr<UdpClient> new_client(new UdpClient(host, port, io_service, *options));
00109         udp_clients.insert(new_client);
00110         return new_client;
00111     }
00112 
00113     boost::shared_ptr<UdpClient> new_client(new UdpClient(host, port, io_service));
00114     udp_clients.insert(new_client);
00115     return new_client;
00116 }
00117 
00118 void NetworkThread::run()
00119 {
00120     boost::asio::io_service::work work(io_service);
00121 
00122     try
00123     {
00124         io_service.run();
00125     }
00126     catch (std::exception & error)
00127     {
00128         // Catch any exception and dump it to the log
00129         Logger::error("Error running network thread: '" + string(error.what()) + "'", Logger::NO_PORT, logger_category);
00130     }
00131 }
 All Classes Files Functions Variables Typedefs Friends Defines