]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_spanningtree/treeserver.h
m_spanningtree Fix routing of unicast messages
[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         TreeSocket* Socket;                     /* Socket used to communicate with this server */
47         time_t NextPing;                        /* After this time, the server should be PINGed*/
48         bool LastPingWasGood;                   /* True if the server responded to the last PING with a PONG */
49         std::string sid;                        /* Server ID */
50
51         /** This method is used to add this TreeServer to the
52          * hash maps. It is only called by the constructors.
53          */
54         void AddHashEntry();
55
56  public:
57         typedef std::vector<TreeServer*> ChildServers;
58         FakeUser* const ServerUser;             /* User representing this server */
59         const time_t age;
60
61         bool Warned;                            /* True if we've warned opers about high latency on this server */
62         bool bursting;                          /* whether or not this server is bursting */
63
64         unsigned int UserCount;                 /* How many users are on this server? [note: doesn't care about +i] */
65         unsigned int OperCount;                 /* How many opers are on this server? */
66
67         /** We use this constructor only to create the 'root' item, Utils->TreeRoot, which
68          * represents our own server. Therefore, it has no route, no parent, and
69          * no socket associated with it. Its version string is our own local version.
70          */
71         TreeServer();
72
73         /** When we create a new server, we call this constructor to initialize it.
74          * This constructor initializes the server's Route and Parent, and sets up
75          * its ping counters so that it will be pinged one minute from now.
76          */
77         TreeServer(const std::string& Name, const std::string& Desc, const std::string& id, TreeServer* Above, TreeSocket* Sock, bool Hide);
78
79         int QuitUsers(const std::string &reason);
80
81         /** Get route.
82          * The 'route' is defined as the locally-
83          * connected server which can be used to reach this server.
84          */
85         TreeServer* GetRoute();
86
87         /** Returns true if this server is the tree root (i.e.: us)
88          */
89         bool IsRoot() const { return (this->Parent == NULL); }
90
91         /** Returns true if this server is locally connected
92          */
93         bool IsLocal() const { return (this->Route == this); }
94
95         /** Get server version string
96          */
97         const std::string& GetVersion();
98
99         /** Set time we are next due to ping this server
100          */
101         void SetNextPingTime(time_t t);
102
103         /** Get the time we are next due to ping this server
104          */
105         time_t NextPingTime();
106
107         /** Last ping time in milliseconds, used to calculate round trip time
108          */
109         unsigned long LastPingMsec;
110
111         /** Round trip time of last ping
112          */
113         unsigned long rtt;
114
115         /** When we recieved BURST from this server, used to calculate total burst time at ENDBURST.
116          */
117         unsigned long StartBurst;
118
119         /** True if this server is hidden
120          */
121         bool Hidden;
122
123         /** True if the server answered their last ping
124          */
125         bool AnsweredLastPing();
126
127         /** Set the server as responding to its last ping
128          */
129         void SetPingFlag();
130
131         /** Get the TreeSocket pointer for local servers.
132          * For remote servers, this returns NULL.
133          */
134         TreeSocket* GetSocket();
135
136         /** Get the parent server.
137          * For the root node, this returns NULL.
138          */
139         TreeServer* GetParent();
140
141         /** Set the server version string
142          */
143         void SetVersion(const std::string &Version);
144
145         /** Return all child servers
146          */
147         const ChildServers& GetChildren() const { return Children; }
148
149         /** Add a child server
150          */
151         void AddChild(TreeServer* Child);
152
153         /** Delete a child server, return false if it didn't exist.
154          */
155         bool DelChild(TreeServer* Child);
156
157         /** Removes child nodes of this node, and of that node, etc etc.
158          * This is used during netsplits to automatically tidy up the
159          * server tree. It is slow, we don't use it for much else.
160          */
161         void Tidy();
162
163         /** Get server ID
164          */
165         const std::string& GetID();
166
167         /** Marks a server as having finished bursting and performs appropriate actions.
168          */
169         void FinishBurst();
170         /** Recursive call for child servers */
171         void FinishBurstInternal();
172
173         /** (Re)check the uline state of this server
174          */
175         void CheckULine();
176
177         CullResult cull();
178
179         /** Destructor
180          * Removes the reference to this object from the
181          * hash maps.
182          */
183         ~TreeServer();
184
185         /** Returns the TreeServer the given user is connected to
186          * @param user The user whose server to return
187          * @return The TreeServer this user is connected to.
188          */
189         static TreeServer* Get(User* user)
190         {
191                 return static_cast<TreeServer*>(user->server);
192         }
193 };