Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

more gui

File size: 5.7 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);
36SHELL_COMMAND(debug, NetworkMonitor, debug);
37
38
39
40/**
41 * starts a network monitor
42 */
43NetworkMonitor::NetworkMonitor(NetworkStream* networkStream)
44  : Synchronizeable()
45{
46  this->setClassID(CL_NETWORK_MONITOR, "NetworkMonitor");
47
48  this->networkStream = networkStream;
49  this->playerNumber = 0;
50  // create the localnode, init it and add it to the nodes list
51  this->localNode = new NetworkNode( this->networkStream->getPeerInfo());
52  this->addNode(this->localNode);
53
54  this->setSynchronized(false);
55
56  // read in the proxy server list, this is only the case for the master server
57  if( SharedNetworkData::getInstance()->isMasterServer())
58  {
59    // assuming that the config files are already read we get the proxy servers
60    std::vector<IP>* proxyList = NetworkSettings::getInstance()->getProxyList();
61    std::vector<IP>::iterator it = proxyList->begin();
62    // create a peer info class and a network node class for each new proxy and add them to the passive list
63    for(; it < proxyList->end(); it++)
64    {
65      PeerInfo* peer = new PeerInfo();
66      peer->ip = (*it);
67      peer->nodeType = NET_PROXY_SERVER_ACTIVE;
68      peer->userId = -1;
69
70      NetworkNode* node = new NetworkNode(peer);
71      this->addNode( node);
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  if( pInfo->isClient())
143    this->localNode->addClient(pInfo);
144  else if( pInfo->isProxyServer())
145  {
146    this->localNode->addActiveProxyServer(pInfo);
147    // create a new node, since a proxy can connect clients again
148    NetworkNode* node = new NetworkNode(pInfo);
149    this->nodeList.push_back(node);
150  }
151  else if( pInfo->isMasterServer())
152  {
153    this->localNode->addMasterServer(pInfo);
154  }
155}
156
157
158/**
159 * adds a network node to the local node
160 *  @param node node to add to
161 *  @param pInfo node information
162 */
163void NetworkMonitor::addNode(NetworkNode* node, PeerInfo* pInfo)
164{
165  if( node == NULL)
166    return;
167
168  if( pInfo->isClient())
169    node->addClient(pInfo);
170  else if( pInfo->isProxyServer())
171    node->addActiveProxyServer(pInfo);
172  else if( pInfo->isMasterServer())
173    node->addMasterServer(pInfo);
174}
175
176
177/**
178 * @returns the proxy server of the first choice
179 */
180PeerInfo* NetworkMonitor::getFirstChoiceProxy()
181{
182  // return the fist proxy in the list
183  return this->localNode->getActiveProxyServer(0);
184}
185
186
187/**
188 * @returns the proxy server of second choice
189 */
190PeerInfo* NetworkMonitor::getSecondChoiceProxy()
191{
192  // return the second server in the list
193  return this->localNode->getActiveProxyServer(1);
194}
195
196
197/**
198 * this displays the network monitor gui
199 */
200void NetworkMonitor::toggleGUI()
201{
202  if (this->box == NULL)
203  {
204    this->box = new OrxGui::GLGuiBox(OrxGui::Vertical);
205    {
206      NetworkStatsWidget* netStats = new NetworkStatsWidget();
207      this->box->pack(netStats);
208
209    }
210
211    this->box->showAll();
212    this->box->setAbsCoor2D(300, 40);
213  }
214  else
215  {
216    delete this->box;
217    this->box = NULL;
218  }
219}
220
221/**
222 * processes the network monitor data
223 */
224void NetworkMonitor::process()
225{
226  this->playerNumber = 0;
227  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
228  for(; it != this->nodeList.end(); it++)
229  {
230    this->playerNumber += (*it)->getPlayerNumber();
231  }
232
233  // check if a proxy server has to activated
234
235//   this->debug();
236}
237
238/**
239 * prints out the debug informations
240 */
241void NetworkMonitor::debug()
242{
243  PRINT(0)("================================= Network Monitor::debug() =====\n");
244  PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str());
245  PRINT(0)(" Total count of network connections: %i\n", this->connectionNumber);
246  PRINT(0)(" Total count of players: %i\n", this->playerNumber);
247  PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer());
248
249  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
250  for(; it != this->nodeList.end(); it++)
251  {
252    (*it)->debug(0);
253  }
254
255  PRINT(0)("================================================================\n");
256}
257
Note: See TracBrowser for help on using the repository browser.