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 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance) : ServerInstance(Instance), Utils(Util)
22 UserCount = OperCount = 0;
23 VersionString = ServerInstance->GetVersionString();
26 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
27 * represents our own server. Therefore, it has no route, no parent, and
28 * no socket associated with it. Its version string is our own local version.
30 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc) : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
34 UserCount = ServerInstance->UserCount();
35 OperCount = ServerInstance->OperCount();
36 VersionString = ServerInstance->GetVersionString();
38 Socket = NULL; /* Fix by brain */
42 /** When we create a new server, we call this constructor to initialize it.
43 * This constructor initializes the server's Route and Parent, and sets up
44 * its ping counters so that it will be pinged one minute from now.
46 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock)
47 : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util)
50 UserCount = OperCount = 0;
51 this->SetNextPingTime(time(NULL) + 60);
54 /* find the 'route' for this server (e.g. the one directly connected
55 * to the local server, which we can use to reach it)
57 * In the following example, consider we have just added a TreeServer
58 * class for server G on our network, of which we are server A.
59 * To route traffic to G (marked with a *) we must send the data to
60 * B (marked with a +) so this algorithm initializes the 'Route'
61 * value to point at whichever server traffic must be routed through
62 * to get here. If we were to try this algorithm with server B,
63 * the Route pointer would point at its own object ('this').
73 * We only run this algorithm when a server is created, as
74 * the routes remain constant while ever the server exists, and
75 * do not need to be re-calculated.
79 if (Route == Utils->TreeRoot)
85 while (this->Route->GetParent() != Utils->TreeRoot)
87 this->Route = Route->GetParent();
91 /* Because recursive code is slow and takes a lot of resources,
92 * we store two representations of the server tree. The first
93 * is a recursive structure where each server references its
94 * children and its parent, which is used for netbursts and
95 * netsplits to dump the whole dataset to the other server,
96 * and the second is used for very fast lookups when routing
97 * messages and is instead a hash_map, where each item can
98 * be referenced by its server name. The AddHashEntry()
99 * call below automatically inserts each TreeServer class
100 * into the hash_map as it is created. There is a similar
101 * maintainance call in the destructor to tidy up deleted
105 this->AddHashEntry();
108 int TreeServer::QuitUsers(const std::string &reason)
110 const char* reason_s = reason.c_str();
111 std::vector<userrec*> time_to_die;
112 for (user_hash::iterator n = ServerInstance->clientlist->begin(); n != ServerInstance->clientlist->end(); n++)
114 if (!strcmp(n->second->server, this->ServerName.c_str()))
116 time_to_die.push_back(n->second);
119 for (std::vector<userrec*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
121 userrec* a = (userrec*)*n;
123 userrec::QuitUser(ServerInstance,a,reason_s);
125 return time_to_die.size();
128 /** This method is used to add the structure to the
129 * hash_map for linear searches. It is only called
130 * by the constructors.
132 void TreeServer::AddHashEntry()
134 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
135 if (iter == Utils->serverlist.end())
136 Utils->serverlist[this->ServerName.c_str()] = this;
139 /** This method removes the reference to this object
140 * from the hash_map which is used for linear searches.
141 * It is only called by the default destructor.
143 void TreeServer::DelHashEntry()
145 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
146 if (iter != Utils->serverlist.end())
147 Utils->serverlist.erase(iter);
150 /** These accessors etc should be pretty self-
153 TreeServer* TreeServer::GetRoute()
158 std::string TreeServer::GetName()
160 return ServerName.c_str();
163 std::string TreeServer::GetDesc()
168 std::string TreeServer::GetVersion()
170 return VersionString;
173 void TreeServer::SetNextPingTime(time_t t)
176 LastPingWasGood = false;
179 time_t TreeServer::NextPingTime()
184 bool TreeServer::AnsweredLastPing()
186 return LastPingWasGood;
189 void TreeServer::SetPingFlag()
191 LastPingWasGood = true;
194 int TreeServer::GetUserCount()
199 void TreeServer::AddUserCount()
204 void TreeServer::DelUserCount()
209 int TreeServer::GetOperCount()
214 TreeSocket* TreeServer::GetSocket()
219 TreeServer* TreeServer::GetParent()
224 void TreeServer::SetVersion(const std::string &Version)
226 VersionString = Version;
229 unsigned int TreeServer::ChildCount()
231 return Children.size();
234 TreeServer* TreeServer::GetChild(unsigned int n)
236 if (n < Children.size())
238 /* Make sure they cant request
239 * an out-of-range object. After
240 * all we know what these programmer
241 * types are like *grin*.
251 void TreeServer::AddChild(TreeServer* Child)
253 Children.push_back(Child);
256 bool TreeServer::DelChild(TreeServer* Child)
258 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
269 /** Removes child nodes of this node, and of that node, etc etc.
270 * This is used during netsplits to automatically tidy up the
271 * server tree. It is slow, we don't use it for much else.
273 bool TreeServer::Tidy()
275 bool stillchildren = true;
276 while (stillchildren)
278 stillchildren = false;
279 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
281 TreeServer* s = (TreeServer*)*a;
285 stillchildren = true;
292 TreeServer::~TreeServer()
294 /* We'd better tidy up after ourselves, eh? */
295 this->DelHashEntry();