]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treeserver.cpp
Remove unneeded Extensible inheritance and remove "age" field from classbase
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treeserver.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "commands/cmd_whois.h"
16 #include "commands/cmd_stats.h"
17 #include "socket.h"
18 #include "xline.h"
19 #include "../transport.h"
20
21 #include "utils.h"
22 #include "treeserver.h"
23
24 /* $ModDep: m_spanningtree/utils.h m_spanningtree/treeserver.h */
25
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.
29  */
30 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id)
31                                                 : ServerInstance(Instance), ServerName(Name.c_str()), ServerDesc(Desc), Utils(Util)
32 {
33         age = Instance->Time();
34         bursting = false;
35         Parent = NULL;
36         VersionString.clear();
37         ServerUserCount = ServerOperCount = 0;
38         VersionString = ServerInstance->GetVersionString();
39         Route = NULL;
40         Socket = NULL; /* Fix by brain */
41         StartBurst = rtt = 0;
42         Warned = Hidden = false;
43         AddHashEntry();
44         SetID(id);
45 }
46
47 /** When we create a new server, we call this constructor to initialize it.
48  * This constructor initializes the server's Route and Parent, and sets up
49  * its ping counters so that it will be pinged one minute from now.
50  */
51 TreeServer::TreeServer(SpanningTreeUtilities* Util, InspIRCd* Instance, std::string Name, std::string Desc, const std::string &id, TreeServer* Above, TreeSocket* Sock, bool Hide)
52         : ServerInstance(Instance), Parent(Above), ServerName(Name.c_str()), ServerDesc(Desc), Socket(Sock), Utils(Util), Hidden(Hide)
53 {
54         age = Instance->Time();
55         bursting = true;
56         VersionString.clear();
57         ServerUserCount = ServerOperCount = 0;
58         SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
59         SetPingFlag();
60         Warned = false;
61         rtt = 0;
62
63         timeval t;
64         gettimeofday(&t, NULL);
65         long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
66         this->StartBurst = ts;
67         Instance->Logs->Log("m_spanningtree",DEBUG, "Started bursting at time %lu", ts);
68
69         /* find the 'route' for this server (e.g. the one directly connected
70          * to the local server, which we can use to reach it)
71          *
72          * In the following example, consider we have just added a TreeServer
73          * class for server G on our network, of which we are server A.
74          * To route traffic to G (marked with a *) we must send the data to
75          * B (marked with a +) so this algorithm initializes the 'Route'
76          * value to point at whichever server traffic must be routed through
77          * to get here. If we were to try this algorithm with server B,
78          * the Route pointer would point at its own object ('this').
79          *
80          *            A
81          *           / \
82          *        + B   C
83          *         / \   \
84          *        D   E   F
85          *       /         \
86          *    * G           H
87          *
88          * We only run this algorithm when a server is created, as
89          * the routes remain constant while ever the server exists, and
90          * do not need to be re-calculated.
91          */
92
93         Route = Above;
94         if (Route == Utils->TreeRoot)
95         {
96                 Route = this;
97         }
98         else
99         {
100                 while (this->Route->GetParent() != Utils->TreeRoot)
101                 {
102                         this->Route = Route->GetParent();
103                 }
104         }
105
106         /* Because recursive code is slow and takes a lot of resources,
107          * we store two representations of the server tree. The first
108          * is a recursive structure where each server references its
109          * children and its parent, which is used for netbursts and
110          * netsplits to dump the whole dataset to the other server,
111          * and the second is used for very fast lookups when routing
112          * messages and is instead a hash_map, where each item can
113          * be referenced by its server name. The AddHashEntry()
114          * call below automatically inserts each TreeServer class
115          * into the hash_map as it is created. There is a similar
116          * maintainance call in the destructor to tidy up deleted
117          * servers.
118          */
119
120         this->AddHashEntry();
121
122         SetID(id);
123 }
124
125 std::string& TreeServer::GetID()
126 {
127         return sid;
128 }
129
130 void TreeServer::FinishBurstInternal()
131 {
132         this->bursting = false;
133         SetNextPingTime(ServerInstance->Time() + Utils->PingFreq);
134         SetPingFlag();
135         for(unsigned int q=0; q < ChildCount(); q++)
136         {
137                 TreeServer* child = GetChild(q);
138                 child->FinishBurstInternal();
139         }
140 }
141
142 void TreeServer::FinishBurst()
143 {
144         FinishBurstInternal();
145         ServerInstance->XLines->ApplyLines();
146         timeval t;
147         gettimeofday(&t, NULL);
148         long ts = (t.tv_sec * 1000) + (t.tv_usec / 1000);
149         unsigned long bursttime = ts - this->StartBurst;
150         ServerInstance->SNO->WriteToSnoMask('l', "Received end of netburst from \2%s\2 (burst time: %lu %s)",
151                 ServerName.c_str(), (bursttime > 10000 ? bursttime / 1000 : bursttime), (bursttime > 10000 ? "secs" : "msecs"));
152         Event rmode((char*)ServerName.c_str(),  (Module*)Utils->Creator, "new_server");
153         rmode.Send(ServerInstance);
154 }
155
156 void TreeServer::SetID(const std::string &id)
157 {
158         ServerInstance->Logs->Log("m_spanningtree",DEBUG, "Setting SID to " + id);
159         sid = id;
160         Utils->sidlist[sid] = this;
161 }
162
163 int TreeServer::QuitUsers(const std::string &reason)
164 {
165         const char* reason_s = reason.c_str();
166         std::vector<User*> time_to_die;
167         for (user_hash::iterator n = ServerInstance->Users->clientlist->begin(); n != ServerInstance->Users->clientlist->end(); n++)
168         {
169                 if (!strcmp(n->second->server, this->ServerName.c_str()))
170                 {
171                         time_to_die.push_back(n->second);
172                 }
173         }
174         for (std::vector<User*>::iterator n = time_to_die.begin(); n != time_to_die.end(); n++)
175         {
176                 User* a = (User*)*n;
177                 if (!IS_LOCAL(a))
178                 {
179                         if (ServerInstance->Config->HideSplits)
180                                 ServerInstance->Users->QuitUser(a, "*.net *.split", reason_s);
181                         else
182                                 ServerInstance->Users->QuitUser(a, reason_s);
183
184                         if (this->Utils->quiet_bursts)
185                                 ServerInstance->GlobalCulls.MakeSilent(a);
186                 }
187         }
188         return time_to_die.size();
189 }
190
191 /** This method is used to add the structure to the
192  * hash_map for linear searches. It is only called
193  * by the constructors.
194  */
195 void TreeServer::AddHashEntry()
196 {
197         server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
198         if (iter == Utils->serverlist.end())
199                 Utils->serverlist[this->ServerName.c_str()] = this;
200 }
201
202 /** This method removes the reference to this object
203  * from the hash_map which is used for linear searches.
204  * It is only called by the default destructor.
205  */
206 void TreeServer::DelHashEntry()
207 {
208         server_hash::iterator iter = Utils->serverlist.find(this->ServerName.c_str());
209         if (iter != Utils->serverlist.end())
210                 Utils->serverlist.erase(iter);
211 }
212
213 /** These accessors etc should be pretty self-
214  * explanitory.
215  */
216 TreeServer* TreeServer::GetRoute()
217 {
218         return Route;
219 }
220
221 std::string TreeServer::GetName()
222 {
223         return ServerName.c_str();
224 }
225
226 std::string TreeServer::GetDesc()
227 {
228         return ServerDesc;
229 }
230
231 std::string TreeServer::GetVersion()
232 {
233         return VersionString;
234 }
235
236 void TreeServer::SetNextPingTime(time_t t)
237 {
238         this->NextPing = t;
239         LastPingWasGood = false;
240 }
241
242 time_t TreeServer::NextPingTime()
243 {
244         return NextPing;
245 }
246
247 bool TreeServer::AnsweredLastPing()
248 {
249         return LastPingWasGood;
250 }
251
252 void TreeServer::SetPingFlag()
253 {
254         LastPingWasGood = true;
255 }
256
257 unsigned int TreeServer::GetUserCount()
258 {
259         return ServerUserCount;
260 }
261
262 void TreeServer::SetUserCount(int diff)
263 {
264         ServerUserCount += diff;
265 }
266
267 void TreeServer::SetOperCount(int diff)
268 {
269         ServerOperCount += diff;
270 }
271
272 unsigned int TreeServer::GetOperCount()
273 {
274         return ServerOperCount;
275 }
276
277 TreeSocket* TreeServer::GetSocket()
278 {
279         return Socket;
280 }
281
282 TreeServer* TreeServer::GetParent()
283 {
284         return Parent;
285 }
286
287 void TreeServer::SetVersion(const std::string &Version)
288 {
289         VersionString = Version;
290 }
291
292 unsigned int TreeServer::ChildCount()
293 {
294         return Children.size();
295 }
296
297 TreeServer* TreeServer::GetChild(unsigned int n)
298 {
299         if (n < Children.size())
300         {
301                 /* Make sure they  cant request
302                  * an out-of-range object. After
303                  * all we know what these programmer
304                  * types are like *grin*.
305                  */
306                 return Children[n];
307         }
308         else
309         {
310                 return NULL;
311         }
312 }
313
314 void TreeServer::AddChild(TreeServer* Child)
315 {
316         Children.push_back(Child);
317 }
318
319 bool TreeServer::DelChild(TreeServer* Child)
320 {
321         for (std::vector<TreeServer*>::iterator a = Children.begin(); a != Children.end(); a++)
322         {
323                 if (*a == Child)
324                 {
325                         Children.erase(a);
326                         return true;
327                 }
328         }
329         return false;
330 }
331
332 /** Removes child nodes of this node, and of that node, etc etc.
333  * This is used during netsplits to automatically tidy up the
334  * server tree. It is slow, we don't use it for much else.
335  */
336 bool TreeServer::Tidy()
337 {
338         bool stillchildren = true;
339         while (stillchildren)
340         {
341                 stillchildren = false;
342                 for (std::vector<TreeServer*>::iterator a = Children.begin(); a != Children.end(); a++)
343                 {
344                         TreeServer* s = (TreeServer*)*a;
345                         s->Tidy();
346                         Children.erase(a);
347                         delete s;
348                         stillchildren = true;
349                         break;
350                 }
351         }
352         return true;
353 }
354
355 TreeServer::~TreeServer()
356 {
357         /* We'd better tidy up after ourselves, eh? */
358         this->DelHashEntry();
359
360         server_hash::iterator iter = Utils->sidlist.find(GetID());
361         if (iter != Utils->sidlist.end())
362                 Utils->sidlist.erase(iter);
363 }