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;
25 VersionString = ServerInstance->GetVersionString();
28 /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
29 * represents our own server. Therefore, it has no route, no parent, and
30 * no socket associated with it. Its version string is our own local version.
32 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc) : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
36 UserCount = ServerInstance->UserCount();
37 OperCount = ServerInstance->OperCount();
38 VersionString = ServerInstance->GetVersionString();
40 Socket = NULL; /* Fix by brain */
44 /** When we create a new server, we call this constructor to initialize it.
45 * This constructor initializes the server's Route and Parent, and sets up
46 * its ping counters so that it will be pinged one minute from now.
48 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, TreeServer* Above, TreeSocket* Sock)
49 : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util)
52 UserCount = OperCount = 0;
53 this->SetNextPingTime(time(NULL) + 60);
55 /* find the 'route' for this server (e.g. the one directly connected
56 * to the local server, which we can use to reach it)
58 * In the following example, consider we have just added a TreeServer
59 * class for server G on our network, of which we are server A.
60 * To route traffic to G (marked with a *) we must send the data to
61 * B (marked with a +) so this algorithm initializes the 'Route'
62 * value to point at whichever server traffic must be routed through
63 * to get here. If we were to try this algorithm with server B,
64 * the Route pointer would point at its own object ('this').
74 * We only run this algorithm when a server is created, as
75 * the routes remain constant while ever the server exists, and
76 * do not need to be re-calculated.
80 if (Route == Utils->TreeRoot)
86 while (this->Route->GetParent() != Utils->TreeRoot)
88 this->Route = Route->GetParent();
92 /* Because recursive code is slow and takes a lot of resources,
93 * we store two representations of the server tree. The first
94 * is a recursive structure where each server references its
95 * children and its parent, which is used for netbursts and
96 * netsplits to dump the whole dataset to the other server,
97 * and the second is used for very fast lookups when routing
98 * messages and is instead a hash_map, where each item can
99 * be referenced by its server name. The AddHashEntry()
100 * call below automatically inserts each TreeServer class
101 * into the hash_map as it is created. There is a similar
102 * maintainance call in the destructor to tidy up deleted
106 this->AddHashEntry();
109 int TreeServer::QuitUsers(const std::string &reason)
111 const char* reason_s = reason.c_str();
112 std::vector<userrec*> time_to_die;
113 for (user_hash::iterator n = ServerInstance->clientlist->begin(); n != ServerInstance->clientlist->end(); n++)
115 if (!strcmp(n->second->server, this->ServerName.c_str()))
117 time_to_die.push_back(n->second);
120 for (std::vector<userrec*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
122 userrec* a = (userrec*)*n;
124 userrec::QuitUser(ServerInstance, a, "*.net *.split", reason_s);
126 return time_to_die.size();
129 /** This method is used to add the structure to the
130 * hash_map for linear searches. It is only called
131 * by the constructors.
133 void TreeServer::AddHashEntry()
135 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
136 if (iter == Utils->serverlist.end())
137 Utils->serverlist[this->ServerName.c_str()] = this;
140 /** This method removes the reference to this object
141 * from the hash_map which is used for linear searches.
142 * It is only called by the default destructor.
144 void TreeServer::DelHashEntry()
146 server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
147 if (iter != Utils->serverlist.end())
148 Utils->serverlist.erase(iter);
151 /** These accessors etc should be pretty self-
154 TreeServer* TreeServer::GetRoute()
159 std::string TreeServer::GetName()
161 return ServerName.c_str();
164 std::string TreeServer::GetDesc()
169 std::string TreeServer::GetVersion()
171 return VersionString;
174 void TreeServer::SetNextPingTime(time_t t)
177 LastPingWasGood = false;
180 time_t TreeServer::NextPingTime()
185 bool TreeServer::AnsweredLastPing()
187 return LastPingWasGood;
190 void TreeServer::SetPingFlag()
192 LastPingWasGood = true;
195 int TreeServer::GetUserCount()
200 void TreeServer::AddUserCount()
205 void TreeServer::DelUserCount()
210 int TreeServer::GetOperCount()
215 TreeSocket* TreeServer::GetSocket()
220 TreeServer* TreeServer::GetParent()
225 void TreeServer::SetVersion(const std::string &Version)
227 VersionString = Version;
230 unsigned int TreeServer::ChildCount()
232 return Children.size();
235 TreeServer* TreeServer::GetChild(unsigned int n)
237 if (n < Children.size())
239 /* Make sure they cant request
240 * an out-of-range object. After
241 * all we know what these programmer
242 * types are like *grin*.
252 void TreeServer::AddChild(TreeServer* Child)
254 Children.push_back(Child);
257 bool TreeServer::DelChild(TreeServer* Child)
259 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
270 /** Removes child nodes of this node, and of that node, etc etc.
271 * This is used during netsplits to automatically tidy up the
272 * server tree. It is slow, we don't use it for much else.
274 bool TreeServer::Tidy()
276 bool stillchildren = true;
277 while (stillchildren)
279 stillchildren = false;
280 for (std::vector<TreeServer*>::iterator a = Children.begin(); a < Children.end(); a++)
282 TreeServer* s = (TreeServer*)*a;
286 stillchildren = true;
293 TreeServer::~TreeServer()
295 /* We'd better tidy up after ourselves, eh? */
296 this->DelHashEntry();