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