2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
5 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
6 * Copyright (C) 2007 Craig Edwards <craigedwards@brainbox.cc>
8 * This file is part of InspIRCd. InspIRCd is free software: you can
9 * redistribute it and/or modify it under the terms of the GNU General Public
10 * License as published by the Free Software Foundation, version 2.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "treesocket.h"
26 /** Each server in the tree is represented by one class of
27 * type TreeServer. A locally connected TreeServer can
28 * have a class of type TreeSocket associated with it, for
29 * remote servers, the TreeSocket entry will be NULL.
30 * Each server also maintains a pointer to its parent
31 * (NULL if this server is ours, at the top of the tree)
32 * and a pointer to its "Route" (see the comments in the
33 * constructors below), and also a dynamic list of pointers
34 * to its children which can be iterated recursively
35 * if required. Creating or deleting objects of type
36 i* TreeServer automatically maintains the hash_map of
37 * TreeServer items, deleting and inserting them as they
38 * are created and destroyed.
40 class TreeServer : public Server
42 TreeServer* Parent; /* Parent entry */
43 TreeServer* Route; /* Route entry */
44 std::vector<TreeServer*> Children; /* List of child objects */
45 std::string VersionString; /* Version string or empty string */
47 /** Full version string including patch version and other info
49 std::string fullversion;
51 TreeSocket* Socket; /* Socket used to communicate with this server */
52 time_t NextPing; /* After this time, the server should be PINGed*/
53 bool LastPingWasGood; /* True if the server responded to the last PING with a PONG */
54 std::string sid; /* Server ID */
56 /** This method is used to add this TreeServer to the
57 * hash maps. It is only called by the constructors.
62 typedef std::vector<TreeServer*> ChildServers;
63 FakeUser* const ServerUser; /* User representing this server */
66 bool Warned; /* True if we've warned opers about high latency on this server */
67 bool bursting; /* whether or not this server is bursting */
69 unsigned int UserCount; /* How many users are on this server? [note: doesn't care about +i] */
70 unsigned int OperCount; /* How many opers are on this server? */
72 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
73 * represents our own server. Therefore, it has no route, no parent, and
74 * no socket associated with it. Its version string is our own local version.
78 /** When we create a new server, we call this constructor to initialize it.
79 * This constructor initializes the server's Route and Parent, and sets up
80 * its ping counters so that it will be pinged one minute from now.
82 TreeServer(const std::string& Name, const std::string& Desc, const std::string& id, TreeServer* Above, TreeSocket* Sock, bool Hide);
84 int QuitUsers(const std::string &reason);
87 * The 'route' is defined as the locally-
88 * connected server which can be used to reach this server.
90 TreeServer* GetRoute();
92 /** Returns true if this server is the tree root (i.e.: us)
94 bool IsRoot() const { return (this->Parent == NULL); }
96 /** Returns true if this server is locally connected
98 bool IsLocal() const { return (this->Route == this); }
100 /** Get server version string
102 const std::string& GetVersion();
104 /** Get the full version string of this server
105 * @return The full version string of this server, including patch version and other info
107 const std::string& GetFullVersion() const { return fullversion; }
109 /** Set time we are next due to ping this server
111 void SetNextPingTime(time_t t);
113 /** Get the time we are next due to ping this server
115 time_t NextPingTime();
117 /** Last ping time in milliseconds, used to calculate round trip time
119 unsigned long LastPingMsec;
121 /** Round trip time of last ping
125 /** When we recieved BURST from this server, used to calculate total burst time at ENDBURST.
127 unsigned long StartBurst;
129 /** True if this server is hidden
133 /** True if the server answered their last ping
135 bool AnsweredLastPing();
137 /** Set the server as responding to its last ping
141 /** Get the TreeSocket pointer for local servers.
142 * For remote servers, this returns NULL.
144 TreeSocket* GetSocket();
146 /** Get the parent server.
147 * For the root node, this returns NULL.
149 TreeServer* GetParent();
151 /** Set the server version string
153 void SetVersion(const std::string &Version);
155 /** Set the full version string
156 * @param verstr The version string to set
158 void SetFullVersion(const std::string& verstr) { fullversion = verstr; }
160 /** Return all child servers
162 const ChildServers& GetChildren() const { return Children; }
164 /** Add a child server
166 void AddChild(TreeServer* Child);
168 /** Delete a child server, return false if it didn't exist.
170 bool DelChild(TreeServer* Child);
172 /** Removes child nodes of this node, and of that node, etc etc.
173 * This is used during netsplits to automatically tidy up the
174 * server tree. It is slow, we don't use it for much else.
180 const std::string& GetID();
182 /** Marks a server as having finished bursting and performs appropriate actions.
185 /** Recursive call for child servers */
186 void FinishBurstInternal();
188 /** (Re)check the uline state of this server
195 * Removes the reference to this object from the
200 /** Returns the TreeServer the given user is connected to
201 * @param user The user whose server to return
202 * @return The TreeServer this user is connected to.
204 static TreeServer* Get(User* user)
206 return static_cast<TreeServer*>(user->server);