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
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
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  // create the localnode, init it and add it to the nodes list
52  this->localNode = new NetworkNode( this->networkStream->getPeerInfo());
53  this->addNode(this->localNode);
54
55  this->setSynchronized(false);
56
57  // read in the proxy server list, this is only the case for the master server
58  if( SharedNetworkData::getInstance()->isMasterServer())
59  {
60    // assuming that the config files are already read we get the proxy servers
61    std::vector<IP>* proxyList = NetworkSettings::getInstance()->getProxyList();
62    std::vector<IP>::iterator it = proxyList->begin();
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();
67      peer->ip = (*it);
68      peer->nodeType = NET_PROXY_SERVER_ACTIVE;
69      peer->userId = -1;
70
71      NetworkNode* node = new NetworkNode(peer);
72      this->addNode( node);
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  if( pInfo->isClient())
144    this->localNode->addClient(pInfo);
145  else if( pInfo->isProxyServerActive())
146  {
147    this->localNode->addActiveProxyServer(pInfo);
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  }
152  else if( pInfo->isMasterServer())
153  {
154    this->localNode->addMasterServer(pInfo);
155  }
156}
157
158
159/**
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);
171  else if( pInfo->isProxyServerActive())
172    node->addActiveProxyServer(pInfo);
173  else if( pInfo->isMasterServer())
174    node->addMasterServer(pInfo);
175}
176
177
178/**
179 * @returns the proxy server of the first choice
180 */
181PeerInfo* NetworkMonitor::getFirstChoiceProxy() const
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 */
191PeerInfo* NetworkMonitor::getSecondChoiceProxy() const
192{
193  // return the second server in the list
194  return this->localNode->getActiveProxyServer(1);
195}
196
197
198/**
199 * this displays the network monitor gui
200 */
201void NetworkMonitor::toggleGUI()
202{
203  if (this->box == NULL)
204  {
205    this->box = new NetworkStatsWidget(this);
206    this->box->showAll();
207  }
208  else
209  {
210    delete this->box;
211    this->box = NULL;
212  }
213}
214
215/**
216 * processes the network monitor data
217 */
218void NetworkMonitor::process()
219{
220  this->playerNumber = 0;
221  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
222  for(; it != this->nodeList.end(); it++)
223  {
224    this->playerNumber += (*it)->getPlayerNumber();
225  }
226
227  // check if a proxy server has to activated
228
229//   this->debug();
230}
231
232/**
233 * prints out the debug informations
234 */
235void NetworkMonitor::debug() const
236{
237  PRINT(0)("================================= Network Monitor::debug() =====\n");
238  PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str());
239  PRINT(0)(" Total count of network connections: %i\n", this->connectionNumber);
240  PRINT(0)(" Total count of players: %i\n", this->playerNumber);
241  PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer());
242
243  std::list<NetworkNode*>::const_iterator it = this->nodeList.begin();
244  for(; it != this->nodeList.end(); it++)
245  {
246    (*it)->debug(0);
247  }
248
249  PRINT(0)("================================================================\n");
250}
251
Note: See TracBrowser for help on using the repository browser.