Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

resizing works perfectly :)… man there were little bugs…

File size: 5.6 KB
RevLine 
[9263]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
13*/
14
15#include "glgui.h"
16#include "shell_command.h"
17
18#include "network_stream.h"
[9266]19#include "debug.h"
[9263]20
[9396]21#include "proxy/network_settings.h"
[9308]22#include "shared_network_data.h"
[9300]23
[9263]24#include "network_monitor.h"
[9276]25#include "network_node.h"
[9300]26#include "peer_info.h"
[9279]27#include "network_stream.h"
[9300]28#include "netdefs.h"
[9263]29
[9300]30#include <vector>
[9276]31
[9300]32
[9494]33#include "network_stats_widget.h"
34
35SHELL_COMMAND(gui, NetworkMonitor, toggleGUI)
36 ->setAlias("ProxyGui");
[9404]37SHELL_COMMAND(debug, NetworkMonitor, debug);
[9263]38
39
40
41/**
42 * starts a network monitor
43 */
44NetworkMonitor::NetworkMonitor(NetworkStream* networkStream)
[9281]45  : Synchronizeable()
[9263]46{
[9281]47  this->setClassID(CL_NETWORK_MONITOR, "NetworkMonitor");
48
[9279]49  this->networkStream = networkStream;
[9272]50  this->playerNumber = 0;
[9287]51  // create the localnode, init it and add it to the nodes list
[9284]52  this->localNode = new NetworkNode( this->networkStream->getPeerInfo());
[9287]53  this->addNode(this->localNode);
[9283]54
[9281]55  this->setSynchronized(false);
[9300]56
[9308]57  // read in the proxy server list, this is only the case for the master server
58  if( SharedNetworkData::getInstance()->isMasterServer())
[9300]59  {
[9308]60    // assuming that the config files are already read we get the proxy servers
[9494]61    std::vector<IP>* proxyList = NetworkSettings::getInstance()->getProxyList();
62    std::vector<IP>::iterator it = proxyList->begin();
[9308]63    // create a peer info class and a network node class for each new proxy and add them to the passive list
64    for(; it < proxyList->end(); it++)
65    {
66      PeerInfo* peer = new PeerInfo();
[9494]67      peer->ip = (*it);
[9308]68      peer->nodeType = NET_PROXY_SERVER_ACTIVE;
69      peer->userId = -1;
[9300]70
[9308]71      NetworkNode* node = new NetworkNode(peer);
72      this->addNode( node);
[9338]73      this->addActiveProxyServer( this->localNode, peer);
[9308]74    }
[9300]75  }
[9494]76  this->box = NULL;
[9263]77}
78
79
[9264]80/**
81 * deconstructor
82 */
83NetworkMonitor::~NetworkMonitor()
[9283]84{
85  if( this->localNode)
86    delete this->localNode;
87}
[9263]88
89
[9266]90/**
[9277]91 * add the network node
92 * @param node to add
[9266]93 */
[9277]94void NetworkMonitor::addNetworkNode(NetworkNode* node)
[9266]95{
[9277]96  this->nodeList.push_back(node);
[9266]97}
[9263]98
[9266]99
100/**
[9277]101 * add the network node
102 * @param node to add
[9266]103 */
[9277]104void NetworkMonitor::removeNetworkNode(NetworkNode* node)
[9266]105{
[9277]106  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
107  for(; it != this->nodeList.end(); it++)
[9266]108  {
109    if( *it == node)
110    {
[9404]111      if( node->getNodeType() == NET_CLIENT)
112        this->playerNumber--;
113
[9277]114      this->nodeList.erase(it);
[9266]115      return;
116    }
117  }
118}
119
[9300]120/**
121 * tihs adds the new network node
122 * @param ip ip of the new node
123 */
[9494]124void NetworkMonitor::addNode(const IP& ip, int nodeType)
[9308]125{
126  PeerInfo* pInfo = new PeerInfo();
127  pInfo->nodeType = nodeType;
128  pInfo->ip = ip;
[9266]129
[9308]130  this->addNode( pInfo);
131}
[9300]132
[9308]133
[9283]134/**
135 * adds a network node to the local node
136 *  @param pInfo node information
137 */
[9282]138void NetworkMonitor::addNode(PeerInfo* pInfo)
139{
[9283]140  if( this->localNode == NULL)
141    return;
[9282]142
143  if( pInfo->isClient())
144    this->localNode->addClient(pInfo);
[9494]145  else if( pInfo->isProxyServerActive())
[9290]146  {
[9292]147    this->localNode->addActiveProxyServer(pInfo);
[9290]148    // create a new node, since a proxy can connect clients again
149    NetworkNode* node = new NetworkNode(pInfo);
150    this->nodeList.push_back(node);
151  }
[9282]152  else if( pInfo->isMasterServer())
[9290]153  {
[9282]154    this->localNode->addMasterServer(pInfo);
[9290]155  }
[9282]156}
157
[9287]158
[9266]159/**
[9287]160 * adds a network node to the local node
161 *  @param node node to add to
162 *  @param pInfo node information
163 */
164void NetworkMonitor::addNode(NetworkNode* node, PeerInfo* pInfo)
165{
166  if( node == NULL)
167    return;
168
169  if( pInfo->isClient())
170    node->addClient(pInfo);
[9494]171  else if( pInfo->isProxyServerActive())
[9292]172    node->addActiveProxyServer(pInfo);
[9287]173  else if( pInfo->isMasterServer())
174    node->addMasterServer(pInfo);
175}
176
177
178/**
[9300]179 * @returns the proxy server of the first choice
180 */
[9494]181PeerInfo* NetworkMonitor::getFirstChoiceProxy() const
[9300]182{
183  // return the fist proxy in the list
184  return this->localNode->getActiveProxyServer(0);
185}
186
187
188/**
189 * @returns the proxy server of second choice
190 */
[9494]191PeerInfo* NetworkMonitor::getSecondChoiceProxy() const
[9300]192{
193  // return the second server in the list
194  return this->localNode->getActiveProxyServer(1);
195}
196
197
198/**
[9265]199 * this displays the network monitor gui
200 */
[9494]201void NetworkMonitor::toggleGUI()
[9265]202{
[9263]203  if (this->box == NULL)
204  {
[9560]205    this->box = new NetworkStatsWidget(this);
[9265]206    this->box->showAll();
207  }
[9494]208  else
209  {
210    delete this->box;
211    this->box = NULL;
212  }
[9265]213}
[9263]214
[9265]215/**
216 * processes the network monitor data
217 */
218void NetworkMonitor::process()
219{
[9280]220  this->playerNumber = 0;
[9279]221  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
222  for(; it != this->nodeList.end(); it++)
223  {
[9280]224    this->playerNumber += (*it)->getPlayerNumber();
[9279]225  }
[9286]226
[9300]227  // check if a proxy server has to activated
228
[9400]229//   this->debug();
[9265]230}
[9263]231
[9286]232/**
233 * prints out the debug informations
234 */
[9494]235void NetworkMonitor::debug() const
[9285]236{
[9289]237  PRINT(0)("================================= Network Monitor::debug() =====\n");
238  PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str());
[9494]239  PRINT(0)(" Total count of network connections: %i\n", this->connectionNumber);
240  PRINT(0)(" Total count of players: %i\n", this->playerNumber);
[9373]241  PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer());
[9287]242
[9494]243  std::list<NetworkNode*>::const_iterator it = this->nodeList.begin();
[9287]244  for(; it != this->nodeList.end(); it++)
245  {
[9288]246    (*it)->debug(0);
[9287]247  }
248
[9289]249  PRINT(0)("================================================================\n");
[9285]250}
251
Note: See TracBrowser for help on using the repository browser.