]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treeserver.h
Textual improvements and fixes such as typos, casing, etc. (#1612)
[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 #include "pingtimer.h"
26
27 /** Each server in the tree is represented by one class of
28  * type TreeServer. A locally connected TreeServer can
29  * have a class of type TreeSocket associated with it, for
30  * remote servers, the TreeSocket entry will be NULL.
31  * Each server also maintains a pointer to its parent
32  * (NULL if this server is ours, at the top of the tree)
33  * and a pointer to its "Route" (see the comments in the
34  * constructors below), and also a dynamic list of pointers
35  * to its children which can be iterated recursively
36  * if required. Creating or deleting objects of type
37  i* TreeServer automatically maintains the hash_map of
38  * TreeServer items, deleting and inserting them as they
39  * are created and destroyed.
40  */
41 class TreeServer : public Server
42 {
43         TreeServer* Parent;                     /* Parent entry */
44         TreeServer* Route;                      /* Route entry */
45         std::vector<TreeServer*> Children;      /* List of child objects */
46         std::string VersionString;              /* Version string or empty string */
47
48         /** Full version string including patch version and other info
49          */
50         std::string fullversion;
51         std::string rawversion;
52
53         TreeSocket* Socket;                     /* Socket used to communicate with this server */
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         /** Timer handling PINGing the server and killing it on timeout
68          */
69         PingTimer pingtimer;
70
71         /** This method is used to add this TreeServer to the
72          * hash maps. It is only called by the constructors.
73          */
74         void AddHashEntry();
75
76         /** Used by SQuit logic to recursively remove servers
77          */
78         void SQuitInternal(unsigned int& num_lost_servers);
79
80         /** Remove the reference to this server from the hash maps
81          */
82         void RemoveHash();
83
84  public:
85         typedef std::vector<TreeServer*> ChildServers;
86         FakeUser* const ServerUser;             /* User representing this server */
87         const time_t age;
88
89         unsigned int UserCount;                 /* How many users are on this server? [note: doesn't care about +i] */
90         unsigned int OperCount;                 /* How many opers are on this server? */
91
92         /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
93          * represents our own server. Therefore, it has no route, no parent, and
94          * no socket associated with it. Its version string is our own local version.
95          */
96         TreeServer();
97
98         /** When we create a new server, we call this constructor to initialize it.
99          * This constructor initializes the server's Route and Parent, and sets up
100          * its ping counters so that it will be pinged one minute from now.
101          */
102         TreeServer(const std::string& Name, const std::string& Desc, const std::string& id, TreeServer* Above, TreeSocket* Sock, bool Hide);
103
104         /** SQuit a server connected to this server, removing the given server and all servers behind it
105          * @param server Server to squit, must be directly below this server
106          * @param reason Reason for quitting the server, sent to opers and other servers
107          */
108         void SQuitChild(TreeServer* server, const std::string& reason);
109
110         /** SQuit this server, removing this server and all servers behind it
111          * @param reason Reason for quitting the server, sent to opers and other servers
112          */
113         void SQuit(const std::string& reason)
114         {
115                 GetParent()->SQuitChild(this, reason);
116         }
117
118         static unsigned int QuitUsers(const std::string& reason);
119
120         /** Get route.
121          * The 'route' is defined as the locally-
122          * connected server which can be used to reach this server.
123          */
124         TreeServer* GetRoute() const { return Route; }
125
126         /** Returns true if this server is the tree root (i.e.: us)
127          */
128         bool IsRoot() const { return (this->Parent == NULL); }
129
130         /** Returns true if this server is locally connected
131          */
132         bool IsLocal() const { return (this->Route == this); }
133
134         /** Returns true if the server is awaiting destruction
135          * @return True if the server is waiting to be culled and deleted, false otherwise
136          */
137         bool IsDead() const { return isdead; }
138
139         /** Get server version string
140          */
141         const std::string& GetVersion() const { return VersionString; }
142
143         /** Get the full version string of this server
144          * @return The full version string of this server, including patch version and other info
145          */
146         const std::string& GetFullVersion() const { return fullversion; }
147
148         /** Get the raw version string of this server
149          */
150         const std::string& GetRawVersion() const { return rawversion; }
151
152         /** Round trip time of last ping
153          */
154         unsigned long rtt;
155
156         /** When we received BURST from this server, used to calculate total burst time at ENDBURST.
157          */
158         uint64_t StartBurst;
159
160         /** True if this server is hidden
161          */
162         bool Hidden;
163
164         /** Get the TreeSocket pointer for local servers.
165          * For remote servers, this returns NULL.
166          */
167         TreeSocket* GetSocket() const { return Socket; }
168
169         /** Get the parent server.
170          * For the root node, this returns NULL.
171          */
172         TreeServer* GetParent() const { return Parent; }
173
174         /** Set the server version string
175          */
176         void SetVersion(const std::string& verstr) { VersionString = verstr; }
177
178         /** Set the full version string
179          * @param verstr The version string to set
180          */
181         void SetFullVersion(const std::string& verstr) { fullversion = verstr; }
182
183         /** Set the raw version string
184          */
185         void SetRawVersion(const std::string& verstr) { rawversion = verstr; }
186
187         /** Sets the description of this server. Called when the description of a remote server changes
188          * and we are notified about it.
189          * @param descstr The description to set
190          */
191         void SetDesc(const std::string& descstr) { description = descstr; }
192
193         /** Return all child servers
194          */
195         const ChildServers& GetChildren() const { return Children; }
196
197         /** Get server ID
198          */
199         const std::string& GetID() const { return sid; }
200
201         /** Marks a server as having finished bursting and performs appropriate actions.
202          */
203         void FinishBurst();
204         /** Recursive call for child servers */
205         void FinishBurstInternal();
206
207         /** (Re)check the uline state of this server
208          */
209         void CheckULine();
210
211         /** Get the bursting state of this server
212          * @return True if this server is bursting, false if it isn't
213          */
214         bool IsBursting() const { return (StartBurst != 0); }
215
216         /** Check whether this server is behind a bursting server or is itself bursting.
217          * This can tell whether a user is on a part of the network that is still bursting.
218          * @return True if this server is bursting or is behind a server that is bursting, false if it isn't
219          */
220         bool IsBehindBursting() const { return (behind_bursting != 0); }
221
222         /** Set the bursting state of the server
223          * @param startms Time the server started bursting, if 0 or omitted, use current time
224          */
225         void BeginBurst(uint64_t startms = 0);
226
227         /** Register a PONG from the server
228          */
229         void OnPong() { pingtimer.OnPong(); }
230
231         CullResult cull() CXX11_OVERRIDE;
232
233         /** Destructor, deletes ServerUser unless IsRoot()
234          */
235         ~TreeServer();
236
237         /** Returns the TreeServer the given user is connected to
238          * @param user The user whose server to return
239          * @return The TreeServer this user is connected to.
240          */
241         static TreeServer* Get(User* user)
242         {
243                 return static_cast<TreeServer*>(user->server);
244         }
245 };