00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef SERVER_H
00017 #define SERVER_H
00018
00019 #ifndef USE_PCH
00020 #include <vector>
00021 #include <netinet/in.h>
00022 #include "debug.h"
00023 #include <libcw/timing.h>
00024 #endif
00025
00026 #include "ServerVersion.h"
00027
00028 class PersistXML;
00029
00030
00031
00032
00033
00034 class Server {
00035 public:
00036 typedef std::vector<unsigned short> ports_type;
00037
00038 private:
00039
00040 std::string M_hostname;
00041
00042
00043 std::string M_irc_name;
00044
00045 ports_type M_ports;
00046 unsigned short M_default_port;
00047 unsigned short M_next_port;
00048 unsigned short M_port;
00049
00050 struct in_addr M_vhost;
00051
00052 Network* M_network;
00053 timeval M_last_connection_event;
00054
00055 static timeval S_last_connection_event_init;
00056
00057 ServerVersion M_version;
00058
00059 public:
00060
00061 Server(Network& network, std::string const& hostname, unsigned short port, struct in_addr vhost) :
00062 M_hostname(hostname), M_default_port(port), M_next_port(port), M_port(0), M_vhost(vhost),
00063 M_network(&network), M_last_connection_event(S_last_connection_event_init)
00064 {
00065 Dout(dc::objects, "Constructing Server(" << network << ", \"" << hostname << "\", " << port << ", " << vhost << ")");
00066 }
00067
00068
00069 Server(void) : M_next_port(0), M_network(NULL)
00070 {
00071 Dout(dc::objects, "Constructing Server()");
00072 std::memset(&M_vhost, 0, sizeof(struct in_addr));
00073 }
00074
00075
00076 Server(Server const& server) :
00077 M_hostname(server.M_hostname),
00078 M_irc_name(server.M_irc_name),
00079 M_ports(server.M_ports),
00080 M_default_port(server.M_default_port),
00081 M_next_port(server.M_next_port),
00082 M_port(server.M_port),
00083 M_vhost(server.M_vhost),
00084 M_network(server.M_network),
00085 M_last_connection_event(server.M_last_connection_event)
00086 { Dout(dc::objects, "Copy-constructing Server from " << server); }
00087
00088
00089 std::string const& hostname(void) const { return M_hostname; }
00090
00091
00092 unsigned short next_port(void) const { return M_next_port; }
00093
00094
00095 void set_next_port(unsigned short next_port) { M_next_port = next_port; }
00096
00097
00098 unsigned short random_port(void) const;
00099
00100
00101 unsigned short port(void) const { return M_port; }
00102
00103
00104 struct in_addr& vhost(void) { return M_vhost; }
00105
00106
00107 struct in_addr vhost(void) const { return M_vhost; }
00108
00109
00110 ports_type const& ports(void) const { return M_ports; }
00111
00112
00113 Network const& network(void) const { return *M_network; }
00114
00115
00116 Network& network(void) { return *M_network; }
00117
00118
00119 void set_network(Network* network) { M_network = network; }
00120
00121
00122 ServerVersion const& version(void) const { return M_version; }
00123
00124
00125 ServerVersion& version(void) { return M_version; }
00126
00127
00128 bool find_port(unsigned short port) const
00129 {
00130 ports_type::const_iterator iter = std::find(M_ports.begin(), M_ports.end(), port);
00131 return iter != M_ports.end();
00132 }
00133
00134
00135 bool add_port(unsigned short port)
00136 {
00137 bool new_port = !this->find_port(port);
00138 if (new_port)
00139 M_ports.push_back(port);
00140 return new_port;
00141 }
00142
00143
00144 bool delete_port(unsigned short port)
00145 {
00146 ports_type::iterator iter = std::find(M_ports.begin(), M_ports.end(), port);
00147 bool found = iter != M_ports.end();
00148 if (found)
00149 M_ports.erase(iter);
00150 return found;
00151 }
00152
00153
00154 void set_port(unsigned short port)
00155 {
00156 delete_port(port);
00157 M_ports.insert(M_ports.begin(), port);
00158 }
00159
00160
00161 bool set_irc_name(std::string const& irc_name)
00162 {
00163 bool empty = M_irc_name.empty();
00164 M_irc_name = irc_name;
00165 return empty;
00166 }
00167
00168
00169 std::string const& get_irc_name(void) const { return M_irc_name; }
00170
00171
00172 timeval const& last_connection_event(void) const { return M_last_connection_event; }
00173
00174
00175 void attempting_to_connect(void)
00176 {
00177 M_port = M_next_port;
00178 M_next_port = random_port();
00179 M_last_connection_event = timerRequest.get_now();
00180 }
00181
00182
00183 void connection_lost(void) { M_last_connection_event = timerRequest.get_now(); }
00184
00185 void serialize(PersistXML& xml);
00186 };
00187
00188 #endif // SERVER_H