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