Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/monitor/network_monitor.cc @ 9643

Last change on this file since 9643 was 9643, checked in by patrick, 18 years ago

localhost now gets its own ip, hoover is added to the correct team

File size: 7.2 KB
Line 
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
35SHELL_COMMAND(gui, NetworkMonitor, toggleGUI)
36 ->setAlias("ProxyGui");
37SHELL_COMMAND(debug, NetworkMonitor, debug);
38
39
40
41/**
42 * starts a network monitor
43 */
44NetworkMonitor::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_PASSIVE;
70      peer->userId = -1;
71
72      this->addActiveProxyServer( this->localNode, peer);
73    }
74  }
75  this->box = NULL;
76}
77
78
79/**
80 * deconstructor
81 */
82NetworkMonitor::~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 */
93void 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 */
103void 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 */
123void 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 */
137void 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 */
170void 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 */
189void 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 */
200void 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 */
217PeerInfo* 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 */
227PeerInfo* 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 */
238PeerInfo* NetworkMonitor::getPeerByUserId( int userId)
239{
240  NetworkNode* node = this->getNodeByUserId(userId);
241  if( node != NULL)
242    return node->getPeerInfo();
243
244  return NULL;
245}
246
247/**
248 * searches for a given NetworkNode
249 * @param userId of the searched node
250 * @returns the PeerInfo of the userId peer
251 */
252NetworkNode* NetworkMonitor::getNodeByUserId( int userId)
253{
254  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
255  NetworkNode* node = NULL;
256  for(; it != this->nodeList.end(); it++)
257  {
258    node = (*it)->getNodeByUserId(userId);
259    if( node != NULL)
260      return node;
261  }
262
263  return NULL;
264}
265
266
267/**
268 * this displays the network monitor gui
269 */
270void NetworkMonitor::toggleGUI()
271{
272  if (this->box == NULL)
273  {
274    this->box = new NetworkStatsWidget(this);
275    this->box->showAll();
276  }
277  else
278  {
279    delete this->box;
280    this->box = NULL;
281  }
282}
283
284/**
285 * processes the network monitor data
286 */
287void NetworkMonitor::process()
288{
289  this->playerNumber = 0;
290  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
291  for(; it != this->nodeList.end(); it++)
292  {
293    this->playerNumber += (*it)->getPlayerNumber();
294  }
295
296  // check if a proxy server has to activated
297
298//   this->debug();
299}
300
301/**
302 * prints out the debug informations
303 */
304void NetworkMonitor::debug() const
305{
306  PRINT(0)("================================= Network Monitor::debug() =====\n");
307  PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str());
308  PRINT(0)(" Total count of network connections: %i\n", this->connectionNumber);
309  PRINT(0)(" Total count of players: %i\n", this->playerNumber);
310  PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer());
311
312  std::list<NetworkNode*>::const_iterator it = this->nodeList.begin();
313  for(; it != this->nodeList.end(); it++)
314  {
315    (*it)->debug(1);
316  }
317
318  PRINT(0)("================================================================\n");
319}
320
Note: See TracBrowser for help on using the repository browser.