Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

removing the client from the network monitor does also work now

File size: 6.3 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/**
180 * removes a node from the network monitor
181 * @param pInfo the node to remove
182 */
183void NetworkMonitor::removeNode(PeerInfo* pInfo)
184{
185  this->removeNode(this->localNode, pInfo);
186}
187
188
189/**
190 * removes the network node
191 * @param node the network node where the PeerInfo node is connected to
192 * @param pInfo the PeerInfo to remove
193 */
194void NetworkMonitor::removeNode(NetworkNode* node, PeerInfo* pInfo)
195{
196  if( node == NULL || pInfo == NULL)
197    return;
198
199  if( pInfo->isClient())
200    node->removeClient(pInfo);
201  else if( pInfo->isProxyServerActive())
202    node->removeActiveProxyServer(pInfo);
203  else if( pInfo->isMasterServer())
204    node->removeMasterServer(pInfo);
205}
206
207
208/**
209 * @returns the proxy server of the first choice
210 */
211PeerInfo* NetworkMonitor::getFirstChoiceProxy() const
212{
213  // return the fist proxy in the list
214  return this->localNode->getActiveProxyServer(0);
215}
216
217
218/**
219 * @returns the proxy server of second choice
220 */
221PeerInfo* NetworkMonitor::getSecondChoiceProxy() const
222{
223  // return the second server in the list
224  return this->localNode->getActiveProxyServer(1);
225}
226
227
228/**
229 * this displays the network monitor gui
230 */
231void NetworkMonitor::toggleGUI()
232{
233  if (this->box == NULL)
234  {
235    this->box = new NetworkStatsWidget(this);
236    this->box->showAll();
237  }
238  else
239  {
240    delete this->box;
241    this->box = NULL;
242  }
243}
244
245/**
246 * processes the network monitor data
247 */
248void NetworkMonitor::process()
249{
250  this->playerNumber = 0;
251  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
252  for(; it != this->nodeList.end(); it++)
253  {
254    this->playerNumber += (*it)->getPlayerNumber();
255  }
256
257  // check if a proxy server has to activated
258
259//   this->debug();
260}
261
262/**
263 * prints out the debug informations
264 */
265void NetworkMonitor::debug() const
266{
267  PRINT(0)("================================= Network Monitor::debug() =====\n");
268  PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str());
269  PRINT(0)(" Total count of network connections: %i\n", this->connectionNumber);
270  PRINT(0)(" Total count of players: %i\n", this->playerNumber);
271  PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer());
272
273  std::list<NetworkNode*>::const_iterator it = this->nodeList.begin();
274  for(; it != this->nodeList.end(); it++)
275  {
276    (*it)->debug(0);
277  }
278
279  PRINT(0)("================================================================\n");
280}
281
Note: See TracBrowser for help on using the repository browser.