1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Patrick Boenzli (patrick@orxonox.ethz.ch) |
---|
13 | */ |
---|
14 | |
---|
15 | #include "glgui.h" |
---|
16 | #include "shell_command.h" |
---|
17 | |
---|
18 | #include "network_stream.h" |
---|
19 | #include "debug.h" |
---|
20 | |
---|
21 | #include "proxy/network_settings.h" |
---|
22 | #include "shared_network_data.h" |
---|
23 | |
---|
24 | #include "network_monitor.h" |
---|
25 | #include "network_node.h" |
---|
26 | #include "peer_info.h" |
---|
27 | #include "network_stream.h" |
---|
28 | #include "netdefs.h" |
---|
29 | |
---|
30 | #include <vector> |
---|
31 | |
---|
32 | |
---|
33 | #include "network_stats_widget.h" |
---|
34 | |
---|
35 | SHELL_COMMAND(gui, NetworkMonitor, toggleGUI) |
---|
36 | ->setAlias("ProxyGui"); |
---|
37 | SHELL_COMMAND(debug, NetworkMonitor, debug); |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | /** |
---|
42 | * starts a network monitor |
---|
43 | */ |
---|
44 | NetworkMonitor::NetworkMonitor(NetworkStream* networkStream) |
---|
45 | : Synchronizeable() |
---|
46 | { |
---|
47 | this->setClassID(CL_NETWORK_MONITOR, "NetworkMonitor"); |
---|
48 | |
---|
49 | this->networkStream = networkStream; |
---|
50 | this->playerNumber = 0; |
---|
51 | this->connectionNumber = 0; |
---|
52 | // create the localnode, init it and add it to the nodes list |
---|
53 | this->localNode = new NetworkNode( this->networkStream->getPeerInfo()); |
---|
54 | this->addNode(this->localNode); |
---|
55 | |
---|
56 | this->setSynchronized(false); |
---|
57 | |
---|
58 | // read in the proxy server list, this is only the case for the master server |
---|
59 | if( SharedNetworkData::getInstance()->isMasterServer()) |
---|
60 | { |
---|
61 | // assuming that the config files are already read we get the proxy servers |
---|
62 | std::vector<IP>* proxyList = NetworkSettings::getInstance()->getProxyList(); |
---|
63 | std::vector<IP>::iterator it = proxyList->begin(); |
---|
64 | // create a peer info class and a network node class for each new proxy and add them to the passive list |
---|
65 | for(; it < proxyList->end(); it++) |
---|
66 | { |
---|
67 | PeerInfo* peer = new PeerInfo(); |
---|
68 | peer->ip = (*it); |
---|
69 | peer->nodeType = NET_PROXY_SERVER_ACTIVE; |
---|
70 | peer->userId = -1; |
---|
71 | |
---|
72 | this->addActiveProxyServer( this->localNode, peer); |
---|
73 | } |
---|
74 | } |
---|
75 | this->box = NULL; |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | /** |
---|
80 | * deconstructor |
---|
81 | */ |
---|
82 | NetworkMonitor::~NetworkMonitor() |
---|
83 | { |
---|
84 | if( this->localNode) |
---|
85 | delete this->localNode; |
---|
86 | } |
---|
87 | |
---|
88 | |
---|
89 | /** |
---|
90 | * add the network node |
---|
91 | * @param node to add |
---|
92 | */ |
---|
93 | void NetworkMonitor::addNetworkNode(NetworkNode* node) |
---|
94 | { |
---|
95 | this->nodeList.push_back(node); |
---|
96 | } |
---|
97 | |
---|
98 | |
---|
99 | /** |
---|
100 | * add the network node |
---|
101 | * @param node to add |
---|
102 | */ |
---|
103 | void NetworkMonitor::removeNetworkNode(NetworkNode* node) |
---|
104 | { |
---|
105 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
106 | for(; it != this->nodeList.end(); it++) |
---|
107 | { |
---|
108 | if( *it == node) |
---|
109 | { |
---|
110 | if( node->getNodeType() == NET_CLIENT) |
---|
111 | this->playerNumber--; |
---|
112 | |
---|
113 | this->nodeList.erase(it); |
---|
114 | return; |
---|
115 | } |
---|
116 | } |
---|
117 | } |
---|
118 | |
---|
119 | /** |
---|
120 | * tihs adds the new network node |
---|
121 | * @param ip ip of the new node |
---|
122 | */ |
---|
123 | void NetworkMonitor::addNode(const IP& ip, int nodeType) |
---|
124 | { |
---|
125 | PeerInfo* pInfo = new PeerInfo(); |
---|
126 | pInfo->nodeType = nodeType; |
---|
127 | pInfo->ip = ip; |
---|
128 | |
---|
129 | this->addNode( pInfo); |
---|
130 | } |
---|
131 | |
---|
132 | |
---|
133 | /** |
---|
134 | * adds a network node to the local node |
---|
135 | * @param pInfo node information |
---|
136 | */ |
---|
137 | void NetworkMonitor::addNode(PeerInfo* pInfo) |
---|
138 | { |
---|
139 | if( this->localNode == NULL) |
---|
140 | return; |
---|
141 | |
---|
142 | PRINTF(0)("^^^^^^^^^^^^^^^^^^^^^^^^^^ adding node: %i with type: %s\n\n", pInfo->userId, pInfo->getNodeTypeString().c_str()); |
---|
143 | |
---|
144 | if( pInfo->isClient()) |
---|
145 | { |
---|
146 | this->localNode->addClient(new NetworkNode(pInfo)); |
---|
147 | } |
---|
148 | else if( pInfo->isProxyServerActive()) |
---|
149 | { |
---|
150 | this->localNode->addActiveProxyServer(new NetworkNode(pInfo)); |
---|
151 | } |
---|
152 | else if( pInfo->isProxyServerActivePassive()) |
---|
153 | { |
---|
154 | this->localNode->addPassiveProxyServer(new NetworkNode(pInfo)); |
---|
155 | } |
---|
156 | else if( pInfo->isMasterServer()) |
---|
157 | { |
---|
158 | this->localNode->addMasterServer(new NetworkNode(pInfo)); |
---|
159 | } |
---|
160 | else |
---|
161 | assert(false); |
---|
162 | } |
---|
163 | |
---|
164 | |
---|
165 | /** |
---|
166 | * adds a network node to the local node |
---|
167 | * @param node node to add to |
---|
168 | * @param pInfo node information |
---|
169 | */ |
---|
170 | void NetworkMonitor::addNode(NetworkNode* node, PeerInfo* pInfo) |
---|
171 | { |
---|
172 | if( node == NULL) |
---|
173 | return; |
---|
174 | |
---|
175 | if( pInfo->isClient()) |
---|
176 | node->addClient(new NetworkNode(pInfo)); |
---|
177 | else if( pInfo->isProxyServerActive()) |
---|
178 | node->addActiveProxyServer(new NetworkNode(pInfo)); |
---|
179 | else if( pInfo->isMasterServer()) |
---|
180 | node->addMasterServer(new NetworkNode(pInfo)); |
---|
181 | } |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | /** |
---|
186 | * removes a node from the network monitor |
---|
187 | * @param pInfo the node to remove |
---|
188 | */ |
---|
189 | void NetworkMonitor::removeNode(PeerInfo* pInfo) |
---|
190 | { |
---|
191 | this->removeNode(this->localNode, pInfo); |
---|
192 | } |
---|
193 | |
---|
194 | |
---|
195 | /** |
---|
196 | * removes the network node |
---|
197 | * @param node the network node where the PeerInfo node is connected to |
---|
198 | * @param pInfo the PeerInfo to remove |
---|
199 | */ |
---|
200 | void NetworkMonitor::removeNode(NetworkNode* node, PeerInfo* pInfo) |
---|
201 | { |
---|
202 | if( node == NULL || pInfo == NULL) |
---|
203 | return; |
---|
204 | |
---|
205 | if( pInfo->isClient()) |
---|
206 | node->removeClient(pInfo->userId); |
---|
207 | else if( pInfo->isProxyServerActive()) |
---|
208 | node->removeActiveProxyServer(pInfo->userId); |
---|
209 | else if( pInfo->isMasterServer()) |
---|
210 | node->removeMasterServer(pInfo->userId); |
---|
211 | } |
---|
212 | |
---|
213 | |
---|
214 | /** |
---|
215 | * @returns the proxy server of the first choice |
---|
216 | */ |
---|
217 | PeerInfo* NetworkMonitor::getFirstChoiceProxy() const |
---|
218 | { |
---|
219 | // return the fist proxy in the list |
---|
220 | return this->localNode->getActiveProxyServer(0); |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | /** |
---|
225 | * @returns the proxy server of second choice |
---|
226 | */ |
---|
227 | PeerInfo* NetworkMonitor::getSecondChoiceProxy() const |
---|
228 | { |
---|
229 | // return the second server in the list |
---|
230 | return this->localNode->getActiveProxyServer(1); |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | /** |
---|
235 | * @param userId of the searched node |
---|
236 | * @returns the PeerInfo of the userId peer |
---|
237 | */ |
---|
238 | PeerInfo* NetworkMonitor::getPeerByUserId( int userId) |
---|
239 | { |
---|
240 | PeerInfo* pInfo; |
---|
241 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
242 | for(; it != this->nodeList.end(); it++) |
---|
243 | { |
---|
244 | pInfo = (*it)->getPeerByUserId(userId); |
---|
245 | if( pInfo != NULL) |
---|
246 | return pInfo; |
---|
247 | } |
---|
248 | return NULL; |
---|
249 | } |
---|
250 | |
---|
251 | /** |
---|
252 | * searches for a given NetworkNode |
---|
253 | * @param userId of the searched node |
---|
254 | * @returns the PeerInfo of the userId peer |
---|
255 | */ |
---|
256 | NetworkNode* NetworkMonitor::getNodeByUserId( int userId) |
---|
257 | { |
---|
258 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
259 | for(; it != this->nodeList.end(); it++) |
---|
260 | { |
---|
261 | PRINTF(0)("comparing %i to %i\n", (*it)->getPeerInfo()->userId, userId); |
---|
262 | if( (*it)->getPeerInfo()->userId == userId) |
---|
263 | return (*it); |
---|
264 | } |
---|
265 | return NULL; |
---|
266 | } |
---|
267 | |
---|
268 | |
---|
269 | /** |
---|
270 | * this displays the network monitor gui |
---|
271 | */ |
---|
272 | void NetworkMonitor::toggleGUI() |
---|
273 | { |
---|
274 | if (this->box == NULL) |
---|
275 | { |
---|
276 | this->box = new NetworkStatsWidget(this); |
---|
277 | this->box->showAll(); |
---|
278 | } |
---|
279 | else |
---|
280 | { |
---|
281 | delete this->box; |
---|
282 | this->box = NULL; |
---|
283 | } |
---|
284 | } |
---|
285 | |
---|
286 | /** |
---|
287 | * processes the network monitor data |
---|
288 | */ |
---|
289 | void NetworkMonitor::process() |
---|
290 | { |
---|
291 | this->playerNumber = 0; |
---|
292 | std::list<NetworkNode*>::iterator it = this->nodeList.begin(); |
---|
293 | for(; it != this->nodeList.end(); it++) |
---|
294 | { |
---|
295 | this->playerNumber += (*it)->getPlayerNumber(); |
---|
296 | } |
---|
297 | |
---|
298 | // check if a proxy server has to activated |
---|
299 | |
---|
300 | // this->debug(); |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * prints out the debug informations |
---|
305 | */ |
---|
306 | void NetworkMonitor::debug() const |
---|
307 | { |
---|
308 | PRINT(0)("================================= Network Monitor::debug() =====\n"); |
---|
309 | PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str()); |
---|
310 | PRINT(0)(" Total count of network connections: %i\n", this->connectionNumber); |
---|
311 | PRINT(0)(" Total count of players: %i\n", this->playerNumber); |
---|
312 | PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer()); |
---|
313 | |
---|
314 | std::list<NetworkNode*>::const_iterator it = this->nodeList.begin(); |
---|
315 | for(; it != this->nodeList.end(); it++) |
---|
316 | { |
---|
317 | (*it)->debug(1); |
---|
318 | } |
---|
319 | |
---|
320 | PRINT(0)("================================================================\n"); |
---|
321 | } |
---|
322 | |
---|