Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/lib/network/monitor/network_monitor.cc

Last change on this file was 9869, checked in by bensch, 18 years ago

orxonox/trunk: merged the new_class_id branche back to the trunk.
merged with command:
svn merge https://svn.orxonox.net/orxonox/branches/new_class_id trunk -r9683:HEAD
no conflicts… puh..

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