1 #include "configreader.h"
5 #include "commands/cmd_whois.h"
6 #include "commands/cmd_stats.h"
11 #include "transport.h"
13 #include "m_spanningtree/utils.h"
14 #include "m_spanningtree/treeserver.h"
16 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h */
18 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance) : ServerInstance(Instance), Utils(Util)
24 UserCount = OperCount = 0;
26 VersionString = ServerInstance->GetVersionString();
29 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
30 * represents our own server. Therefore, it has no route, no parent, and
31 * no socket associated with it. Its version string is our own local version.
33 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc) : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
37 UserCount = ServerInstance->UserCount();
38 OperCount = ServerInstance->OperCount();
39 VersionString = ServerInstance->GetVersionString();
41 Socket = NULL; /* Fix by brain */
46 /** When we create a new server, we call this constructor to initialize it.
47 * This constructor initializes the server's Route and Parent, and sets up
48 * its ping counters so that it will be pinged one minute from now.
50 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock)
51 : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util)
54 UserCount = OperCount = 0;
55 this->SetNextPingTime(time(NULL) + 60);
58 /* find the 'route' for this server (e.g. the one directly connected
59 * to the local server, which we can use to reach it)
61 * In the following example, consider we have just added a TreeServer
62 * class for server G on our network, of which we are server A.
63 * To route traffic to G (marked with a *) we must send the data to
64 * B (marked with a +) so this algorithm initializes the 'Route'
65 * value to point at whichever server traffic must be routed through
66 * to get here. If we were to try this algorithm with server B,
67 * the Route pointer would point at its own object ('this').
77 * We only run this algorithm when a server is created, as
78 * the routes remain constant while ever the server exists, and
79 * do not need to be re-calculated.
83 if (Route == Utils->TreeRoot)
89 while (this->Route->GetParent() != Utils->TreeRoot)
91 this->Route = Route->GetParent();
95 /* Because recursive code is slow and takes a lot of resources,
96 * we store two representations of the server tree. The first
97 * is a recursive structure where each server references its
98 * children and its parent, which is used for netbursts and
99 * netsplits to dump the whole dataset to the other server,
100 * and the second is used for very fast lookups when routing
101 * messages and is instead a hash_map, where each item can
102 * be referenced by its server name. The AddHashEntry()
103 * call below automatically inserts each TreeServer class
104 * into the hash_map as it is created. There is a similar
105 * maintainance call in the destructor to tidy up deleted
109 this->AddHashEntry();
112 int TreeServer::QuitUsers(const std::string &reason)
114 const char* reason_s = reason.c_str();
115 std::vector<userrec*> time_to_die;
116 for (user_hash::iterator n = ServerInstance->clientlist->begin(); n != ServerInstance->clientlist->end(); n++)
118 if (!strcmp(n->second->server, this->ServerName.c_str()))
120 time_to_die.push_back(n->second);
123 for (std::vector<userrec*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
125 userrec* a = (userrec*)*n;
128 if (ServerInstance->Config->HideSplits)
129 userrec::QuitUser(ServerInstance, a, "*.net *.split", reason_s);
131 userrec::QuitUser(ServerInstance, a, reason_s);
134 return time_to_die.size();
137 /** This method is used to add the structure to the
138 * hash_map for linear searches. It is only called
139 * by the constructors.
141 void TreeServer::AddHashEntry()
143 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
144 if (iter == Utils->serverlist.end())
145 Utils->serverlist[this->ServerName.c_str()] = this;
148 /** This method removes the reference to this object
149 * from the hash_map which is used for linear searches.
150 * It is only called by the default destructor.
152 void TreeServer::DelHashEntry()
154 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
155 if (iter != Utils->serverlist.end())
156 Utils->serverlist.erase(iter);
159 /** These accessors etc should be pretty self-
162 TreeServer* TreeServer::GetRoute()
167 std::string TreeServer::GetName()
169 return ServerName.c_str();
172 std::string TreeServer::GetDesc()
177 std::string TreeServer::GetVersion()
179 return VersionString;
182 void TreeServer::SetNextPingTime(time_t t)
185 LastPingWasGood = false;
188 time_t TreeServer::NextPingTime()
193 bool TreeServer::AnsweredLastPing()
195 return LastPingWasGood;
198 void TreeServer::SetPingFlag()
200 LastPingWasGood = true;
203 int TreeServer::GetUserCount()
208 void TreeServer::AddUserCount()
213 void TreeServer::DelUserCount()
218 int TreeServer::GetOperCount()
223 TreeSocket* TreeServer::GetSocket()
228 TreeServer* TreeServer::GetParent()
233 void TreeServer::SetVersion(const std::string &Version)
235 VersionString = Version;
238 unsigned int TreeServer::ChildCount()
240 return Children.size();
243 TreeServer* TreeServer::GetChild(unsigned int n)
245 if (n < Children.size())
247 /* Make sure they cant request
248 * an out-of-range object. After
249 * all we know what these programmer
250 * types are like *grin*.
260 void TreeServer::AddChild(TreeServer* Child)
262 Children.push_back(Child);
265 bool TreeServer::DelChild(TreeServer* Child)
267 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
278 /** Removes child nodes of this node, and of that node, etc etc.
279 * This is used during netsplits to automatically tidy up the
280 * server tree. It is slow, we don't use it for much else.
282 bool TreeServer::Tidy()
284 bool stillchildren = true;
285 while (stillchildren)
287 stillchildren = false;
288 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
290 TreeServer* s = (TreeServer*)*a;
294 stillchildren = true;
301 TreeServer::~TreeServer()
303 /* We'd better tidy up after ourselves, eh? */
304 this->DelHashEntry();