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