ircproxy The Ultimate Cyborg |
#include <Application.h>
This class represents the application itself. It is a singleton and needs to be accessed through Application::instance().
Definition at line 38 of file Application.h.
Public Member Functions | |
~Application () throw () | |
Destructor. | |
networks_type const & | networks (void) const |
A const reference to an STL container with all known networks. | |
networks_type & | networks (void) |
A reference to the STL container with all known networks. | |
identities_type const & | identities (void) const |
A const reference to a map with all known identities. | |
identities_type & | identities (void) |
A reference to the map with all known identities. | |
msg_block_ct & | dummy_msg_block (void) |
A dummy message block that can be used as place holder. | |
void | serialize (PersistXML &xml) |
Serialize application to XML. | |
void | last_server_session_destroyed (void) |
This function is called from the destructor of the last ServerSession. | |
Protected Member Functions | |
virtual void | start (void) |
Start application. | |
virtual void | finish_options (void) |
Process remaining command line arguments. | |
virtual void | print_version (std::ostream &) const |
Print version information. | |
virtual void | main_entered (int *argc_ptr, char *const **argv_ptr) |
Called immediately after we reached main. | |
void | command_line_option_listen_port (char const *argument) |
Process command line option --listen-port port. | |
Private Member Functions | |
Application (void) | |
Constructor. | |
void | handle_signal (SignalType const &) |
Called when a signal is received (SIGINT or SIGHUP). | |
Private Attributes | |
unsigned short | M_listen_port |
The listen port the IRC proxy listens on. | |
bool | M_suppress_ping |
True if PING/PONG debug messages should be suppressed. | |
bool | M_suppress_who |
True if WHO debug messages should be suppressed. | |
networks_type | M_networks |
List with all known networks. | |
identities_type | M_identities |
Map with all known identities. | |
memory_block_st * | M_memory_block_dummy |
Dummy variable used during construction. | |
msg_block_ct | M_msg_block_dummy |
Used for MessageIn objects not related to a real message block. | |
bool | M_terminating |
Set to true when the application should terminate after the last ServerSession is destructed. |
Application::Application | ( | void | ) | [private] |
Constructor.
Constructs an application object. The application will understand the commandline parameter --listen-port <port>, or -p <port>, next to all default commandline options (see documentation of libcw).
<port> is the port that the application will listen on for new client connections (on localhost).
Definition at line 44 of file Application.cc.
References command_line_option_listen_port().
00044 : 00045 M_listen_port(0), 00046 #ifdef CWDEBUG 00047 M_suppress_ping(false), 00048 M_suppress_who(false), 00049 #endif 00050 M_memory_block_dummy(memory_block_st::create(0)), 00051 M_msg_block_dummy(M_memory_block_dummy->block_start(), 0, M_memory_block_dummy), 00052 M_terminating(false) 00053 { 00054 add_option(&Application::command_line_option_listen_port, 00055 "listen-port", 'p', "<port>", "Port to listen on for new connections."); 00056 #ifdef CWDEBUG 00057 add_option(&Application::command_line_option_suppress_ping, 00058 "suppress-ping", 0, NULL, "Suppress debug output regarding PING/PONG messages."); 00059 add_option(&Application::command_line_option_suppress_who, 00060 "suppress-who", 0, NULL, "Suppress debug output regarding WHO messages."); 00061 #endif 00062 }
Application::~Application | ( | ) | throw () |
Destructor.
The destructor does not throw.
Definition at line 68 of file Application.cc.
References M_identities, and M_memory_block_dummy.
00069 { 00070 // Destroy all Identity's (and thus their target maps), before destroying the networks (and their targets map). 00071 M_identities.clear(); 00072 00073 // Release memory. 00074 M_memory_block_dummy->release(); 00075 }
void Application::handle_signal | ( | SignalType const & | signal_type | ) | [private] |
Called when a signal is received (SIGINT or SIGHUP).
Definition at line 213 of file Application.cc.
References DoutEntering, identities(), and M_terminating.
Referenced by start().
00214 { 00215 DoutEntering(dc::debug, "Application::handle_signal(" << signal_type << ")"); 00216 00217 switch(signal_type.get_signal()) 00218 { 00219 case SIGHUP: 00220 Dout( dc::notice, "Received SIGHUP " << signal_type.get_count() << " times." ); 00221 break; 00222 case SIGINT: 00223 { 00224 // QUIT all server connections. 00225 for (identities_type::iterator iter = identities().begin(); iter != identities().end(); ++iter) 00226 { 00227 if (iter->second.has_secondary_server_connection()) 00228 iter->second.secondary_server_connection()->disconnect(); 00229 if (iter->second.has_server_session()) 00230 iter->second.server_connection().quit("ircproxy received SIGINT"); // This set reconnect to false and QUIT's the client. 00231 } 00232 // Terminate the application when the last ServerSession is closed. 00233 M_terminating = true; 00234 break; 00235 } 00236 } 00237 }
networks_type const& Application::networks | ( | void | ) | const [inline] |
A const reference to an STL container with all known networks.
Definition at line 64 of file Application.h.
References M_networks.
00064 { return M_networks; }
networks_type& Application::networks | ( | void | ) | [inline] |
A reference to the STL container with all known networks.
Definition at line 67 of file Application.h.
References M_networks.
00067 { return M_networks; }
identities_type const& Application::identities | ( | void | ) | const [inline] |
A const reference to a map with all known identities.
Definition at line 70 of file Application.h.
References M_identities.
Referenced by handle_signal().
00070 { return M_identities; }
identities_type& Application::identities | ( | void | ) | [inline] |
A reference to the map with all known identities.
Definition at line 73 of file Application.h.
References M_identities.
00073 { return M_identities; }
msg_block_ct& Application::dummy_msg_block | ( | void | ) | [inline] |
A dummy message block that can be used as place holder.
Definition at line 76 of file Application.h.
References M_msg_block_dummy.
00076 { return M_msg_block_dummy; }
void Application::start | ( | void | ) | [protected, virtual] |
Start application.
This method is called at the start of the application. It initialized libcwd and creates a new listen socket that listens on Application::M_listen_port. Then the mainloop is started.
Definition at line 102 of file Application.cc.
References PersistXML::close(), handle_signal(), PersistXML::load, M_listen_port, PersistXML::open(), PersistXML::serialize(), and PersistXML::store.
00103 { 00104 #ifdef CWDEBUG 00105 // Redirect debug output to a file. 00106 static std::ofstream debug_file; 00107 debug_file.open("debug.out"); 00108 static debug::TeeStream debug_stream(std::cout, debug_file); 00109 libcwd::libcw_do.set_ostream(&debug_stream); 00110 #endif 00111 00112 Dout(dc::notice, "Called Application::start()"); 00113 00114 // Object used to load/store persistant data. 00115 PersistXML xml; 00116 00117 unsigned int commandline_listen_port = M_listen_port; 00118 00119 // If xml file exists, load it. 00120 struct stat statbuf; 00121 if (stat("ircproxy.xml", &statbuf) != -1) 00122 { 00123 xml.open("ircproxy.xml", PersistXML::load); 00124 xml.serialize("Application", *this); 00125 xml.close(); 00126 } 00127 00128 // Divert signal handling. 00129 signalRequest(SignalRequestData(SIGINT, sigOneShot|sigReplace), 00130 Application::instance(), &Application::handle_signal); 00131 00132 signalRequest(SignalRequestData(SIGHUP), 00133 Application::instance(), &Application::handle_signal); 00134 00135 // Determine the listen port of the application. 00136 if (commandline_listen_port) 00137 M_listen_port = commandline_listen_port; 00138 else if (M_listen_port == 0) 00139 M_listen_port = 6667; // Default listen port. 00140 00141 // Open a listen socket on M_listen_port. 00142 NEW(ProxyListenSocket(M_listen_port)); 00143 00144 mainloop(); 00145 00146 // Store persistent data. 00147 xml.open("ircproxy.xml", PersistXML::store); 00148 xml.serialize("Application", *this); 00149 xml.close(); 00150 00151 // Get rid of memory leaks of libxml2. 00152 xmlCleanupParser(); 00153 }
void Application::finish_options | ( | void | ) | [protected, virtual] |
Process remaining command line arguments.
This method is called when all command options have been processed. If errflg
is set, the usage message is printed to std::cerr and the application exists. If no port was specified, then the default port is set to 6667.
Definition at line 193 of file Application.cc.
00194 { 00195 if (errflg) 00196 { 00197 print_usage(std::cerr); 00198 libcw_exit(2); 00199 } 00200 }
void Application::print_version | ( | std::ostream & | os | ) | const [protected, virtual] |
Print version information.
This method is called when the application is called with commandline parameter --version or -v. See documentation of libcw.
Definition at line 208 of file Application.cc.
00209 { 00210 os << PACKAGE_NAME << " " << VERSION << " by Carlo Wood <carlo@alinoe.com>\n"; 00211 }
void Application::main_entered | ( | int * | argc_ptr, | |
char *const ** | argv_ptr | |||
) | [protected, virtual] |
Called immediately after we reached main.
Initialize libcwd here, allowing the command line handling of libcw to override any debug channels and/or set a debug output file.
Definition at line 89 of file Application.cc.
References debug::init().
00090 { 00091 Debug(debug::init()); 00092 }
void Application::command_line_option_listen_port | ( | char const * | argument | ) | [protected] |
Process command line option --listen-port port.
This method is called for every --listen-port or -p that is passed on the commandline. argument is set to either "--listen-port" or "-p" respectively. The global variable optarg
is the port that is being passed. When <port> is less than or equal 1024 then an error is printed to std:cerr and the application is terminated.
Definition at line 164 of file Application.cc.
References M_listen_port.
Referenced by Application().
00165 { 00166 M_listen_port = atoi(optarg); 00167 if (M_listen_port <= 1024) 00168 { 00169 std::cerr << application_name << ": " << argument << " : out of range" << std::endl; 00170 libcw_exit(-1); 00171 } 00172 }
void Application::serialize | ( | PersistXML & | xml | ) |
Serialize application to XML.
Definition at line 240 of file Application.cc.
References M_identities, M_listen_port, M_networks, and PersistXML::serialize().
00241 { 00242 xml.serialize("M_listen_port", M_listen_port); 00243 xml.serialize("M_networks", M_networks); 00244 xml.serialize("M_identities", M_identities); 00245 }
void Application::last_server_session_destroyed | ( | void | ) |
This function is called from the destructor of the last ServerSession.
Definition at line 77 of file Application.cc.
References M_terminating.
00078 { 00079 if (M_terminating) 00080 fd_dct::return_when_done(-1); 00081 }
unsigned short Application::M_listen_port [private] |
The listen port the IRC proxy listens on.
Definition at line 43 of file Application.h.
Referenced by command_line_option_listen_port(), serialize(), and start().
bool Application::M_suppress_ping [private] |
bool Application::M_suppress_who [private] |
networks_type Application::M_networks [private] |
List with all known networks.
Definition at line 48 of file Application.h.
Referenced by networks(), and serialize().
identities_type Application::M_identities [private] |
Map with all known identities.
Definition at line 49 of file Application.h.
Referenced by identities(), serialize(), and ~Application().
memory_block_st* Application::M_memory_block_dummy [private] |
Dummy variable used during construction.
Definition at line 50 of file Application.h.
Referenced by ~Application().
msg_block_ct Application::M_msg_block_dummy [private] |
Used for MessageIn objects not related to a real message block.
Definition at line 51 of file Application.h.
Referenced by dummy_msg_block().
bool Application::M_terminating [private] |
Set to true when the application should terminate after the last ServerSession is destructed.
Definition at line 52 of file Application.h.
Referenced by handle_signal(), and last_server_session_destroyed().
Copyright © 2005-2007 Carlo Wood. All rights reserved. |
---|