]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treeserver.h
Use Modes::ChangeList in ModeHandler::RemoveMode()
[user/henk/code/inspircd.git] / src / modules / m_spanningtree / treeserver.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
7  *
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.
11  *
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
15  * details.
16  *
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/>.
19  */
20
21
22 #pragma once
23
24 #include "treesocket.h"
25
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.
39  */
40 class TreeServer : public Server
41 {
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 */
46
47         /** Full version string including patch version and other info
48          */
49         std::string fullversion;
50
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 */
55
56         /** Counter counting how many servers are bursting in front of this server, including
57          * this server. Set to parents' value on construction then it is increased if the
58          * server itself starts bursting. Decreased when a server on the path to this server
59          * finishes burst.
60          */
61         unsigned int behind_bursting;
62
63         /** True if this server has been lost in a split and is awaiting destruction
64          */
65         bool isdead;
66
67         /** This method is used to add this TreeServer to the
68          * hash maps. It is only called by the constructors.
69          */
70         void AddHashEntry();
71
72         /** Used by SQuit logic to recursively remove servers
73          */
74         void SQuitInternal(unsigned int& num_lost_servers);
75
76         /** Remove the reference to this server from the hash maps
77          */
78         void RemoveHash();
79
80  public:
81         typedef std::vector<TreeServer*> ChildServers;
82         FakeUser* const ServerUser;             /* User representing this server */
83         const time_t age;
84
85         bool Warned;                            /* True if we've warned opers about high latency on this server */
86
87         unsigned int UserCount;                 /* How many users are on this server? [note: doesn't care about +i] */
88         unsigned int OperCount;                 /* How many opers are on this server? */
89
90         /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
91          * represents our own server. Therefore, it has no route, no parent, and
92          * no socket associated with it. Its version string is our own local version.
93          */
94         TreeServer();
95
96         /** When we create a new server, we call this constructor to initialize it.
97          * This constructor initializes the server's Route and Parent, and sets up
98          * its ping counters so that it will be pinged one minute from now.
99          */
100         TreeServer(const std::string& Name, const std::string& Desc, const std::string& id, TreeServer* Above, TreeSocket* Sock, bool Hide);
101
102         /** SQuit a server connected to this server, removing the given server and all servers behind it
103          * @param server Server to squit, must be directly below this server
104          * @param reason Reason for quitting the server, sent to opers and other servers
105          */
106         void SQuitChild(TreeServer* server, const std::string& reason);
107
108         /** SQuit this server, removing this server and all servers behind it
109          * @param reason Reason for quitting the server, sent to opers and other servers
110          */
111         void SQuit(const std::string& reason)
112         {
113                 GetParent()->SQuitChild(this, reason);
114         }
115
116         static unsigned int QuitUsers(const std::string& reason);
117
118         /** Get route.
119          * The 'route' is defined as the locally-
120          * connected server which can be used to reach this server.
121          */
122         TreeServer* GetRoute();
123
124         /** Returns true if this server is the tree root (i.e.: us)
125          */
126         bool IsRoot() const { return (this->Parent == NULL); }
127
128         /** Returns true if this server is locally connected
129          */
130         bool IsLocal() const { return (this->Route == this); }
131
132         /** Returns true if the server is awaiting destruction
133          * @return True if the server is waiting to be culled and deleted, false otherwise
134          */
135         bool IsDead() const { return isdead; }
136
137         /** Get server version string
138          */
139         const std::string& GetVersion();
140
141         /** Get the full version string of this server
142          * @return The full version string of this server, including patch version and other info
143          */
144         const std::string& GetFullVersion() const { return fullversion; }
145
146         /** Set time we are next due to ping this server
147          */
148         void SetNextPingTime(time_t t);
149
150         /** Get the time we are next due to ping this server
151          */
152         time_t NextPingTime();
153
154         /** Last ping time in milliseconds, used to calculate round trip time
155          */
156         unsigned long LastPingMsec;
157
158         /** Round trip time of last ping
159          */
160         unsigned long rtt;
161
162         /** When we recieved BURST from this server, used to calculate total burst time at ENDBURST.
163          */
164         unsigned long StartBurst;
165
166         /** True if this server is hidden
167          */
168         bool Hidden;
169
170         /** True if the server answered their last ping
171          */
172         bool AnsweredLastPing();
173
174         /** Set the server as responding to its last ping
175          */
176         void SetPingFlag();
177
178         /** Get the TreeSocket pointer for local servers.
179          * For remote servers, this returns NULL.
180          */
181         TreeSocket* GetSocket();
182
183         /** Get the parent server.
184          * For the root node, this returns NULL.
185          */
186         TreeServer* GetParent();
187
188         /** Set the server version string
189          */
190         void SetVersion(const std::string &Version);
191
192         /** Set the full version string
193          * @param verstr The version string to set
194          */
195         void SetFullVersion(const std::string& verstr) { fullversion = verstr; }
196
197         /** Sets the description of this server. Called when the description of a remote server changes
198          * and we are notified about it.
199          * @param descstr The description to set
200          */
201         void SetDesc(const std::string& descstr) { description = descstr; }
202
203         /** Return all child servers
204          */
205         const ChildServers& GetChildren() const { return Children; }
206
207         /** Add a child server
208          */
209         void AddChild(TreeServer* Child);
210
211         /** Delete a child server, return false if it didn't exist.
212          */
213         bool DelChild(TreeServer* Child);
214
215         /** Get server ID
216          */
217         const std::string& GetID();
218
219         /** Marks a server as having finished bursting and performs appropriate actions.
220          */
221         void FinishBurst();
222         /** Recursive call for child servers */
223         void FinishBurstInternal();
224
225         /** (Re)check the uline state of this server
226          */
227         void CheckULine();
228
229         /** Get the bursting state of this server
230          * @return True if this server is bursting, false if it isn't
231          */
232         bool IsBursting() const { return (StartBurst != 0); }
233
234         /** Check whether this server is behind a bursting server or is itself bursting.
235          * This can tell whether a user is on a part of the network that is still bursting.
236          * @return True if this server is bursting or is behind a server that is bursting, false if it isn't
237          */
238         bool IsBehindBursting() const { return (behind_bursting != 0); }
239
240         /** Set the bursting state of the server
241          * @param startms Time the server started bursting, if 0 or omitted, use current time
242          */
243         void BeginBurst(unsigned long startms = 0);
244
245         CullResult cull();
246
247         /** Destructor, deletes ServerUser unless IsRoot()
248          */
249         ~TreeServer();
250
251         /** Returns the TreeServer the given user is connected to
252          * @param user The user whose server to return
253          * @return The TreeServer this user is connected to.
254          */
255         static TreeServer* Get(User* user)
256         {
257                 return static_cast<TreeServer*>(user->server);
258         }
259 };