Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some better debug and function call cleanup

File size: 6.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
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
33SHELL_COMMAND(showGUI, NetworkMonitor, showGUI);
34SHELL_COMMAND(hideGUI, NetworkMonitor, hideGUI);
35SHELL_COMMAND(debug, NetworkMonitor, debug);
36
37
38
39/**
40 * starts a network monitor
41 */
42NetworkMonitor::NetworkMonitor(NetworkStream* networkStream)
43  : Synchronizeable()
44{
45  this->setClassID(CL_NETWORK_MONITOR, "NetworkMonitor");
46
47  this->networkStream = networkStream;
48  this->playerNumber = 0;
49  // create the localnode, init it and add it to the nodes list
50  this->localNode = new NetworkNode( this->networkStream->getPeerInfo());
51  this->addNode(this->localNode);
52
53  this->setSynchronized(false);
54
55  // read in the proxy server list, this is only the case for the master server
56  if( SharedNetworkData::getInstance()->isMasterServer())
57  {
58    // assuming that the config files are already read we get the proxy servers
59    std::vector<IPaddress*>* proxyList = NetworkSettings::getInstance()->getProxyList();
60    std::vector<IPaddress*>::iterator it = proxyList->begin();
61    // create a peer info class and a network node class for each new proxy and add them to the passive list
62    for(; it < proxyList->end(); it++)
63    {
64      PeerInfo* peer = new PeerInfo();
65      peer->ip = *(*it);
66      peer->nodeType = NET_PROXY_SERVER_ACTIVE;
67      peer->userId = -1;
68
69      NetworkNode* node = new NetworkNode(peer);
70      this->addNode( node);
71      this->addActiveProxyServer( this->localNode, peer);
72    }
73  }
74}
75
76
77/**
78 * deconstructor
79 */
80NetworkMonitor::~NetworkMonitor()
81{
82  if( this->localNode)
83    delete this->localNode;
84}
85
86
87/**
88 * add the network node
89 * @param node to add
90 */
91void NetworkMonitor::addNetworkNode(NetworkNode* node)
92{
93  this->nodeList.push_back(node);
94}
95
96
97/**
98 * add the network node
99 * @param node to add
100 */
101void NetworkMonitor::removeNetworkNode(NetworkNode* node)
102{
103  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
104  for(; it != this->nodeList.end(); it++)
105  {
106    if( *it == node)
107    {
108      if( node->getNodeType() == NET_CLIENT)
109        this->playerNumber--;
110
111      this->nodeList.erase(it);
112      return;
113    }
114  }
115}
116
117/**
118 * tihs adds the new network node
119 * @param ip ip of the new node
120 */
121void NetworkMonitor::addNode(IP ip, int nodeType)
122{
123  PeerInfo* pInfo = new PeerInfo();
124  pInfo->nodeType = nodeType;
125  pInfo->ip = ip;
126
127  this->addNode( pInfo);
128}
129
130
131/**
132 * adds a network node to the local node
133 *  @param pInfo node information
134 */
135void NetworkMonitor::addNode(PeerInfo* pInfo)
136{
137  if( this->localNode == NULL)
138    return;
139
140  if( pInfo->isClient())
141    this->localNode->addClient(pInfo);
142  else if( pInfo->isProxyServer())
143  {
144    this->localNode->addActiveProxyServer(pInfo);
145    // create a new node, since a proxy can connect clients again
146    NetworkNode* node = new NetworkNode(pInfo);
147    this->nodeList.push_back(node);
148  }
149  else if( pInfo->isMasterServer())
150  {
151    this->localNode->addMasterServer(pInfo);
152  }
153}
154
155
156/**
157 * adds a network node to the local node
158 *  @param node node to add to
159 *  @param pInfo node information
160 */
161void NetworkMonitor::addNode(NetworkNode* node, PeerInfo* pInfo)
162{
163  if( node == NULL)
164    return;
165
166  if( pInfo->isClient())
167    node->addClient(pInfo);
168  else if( pInfo->isProxyServer())
169    node->addActiveProxyServer(pInfo);
170  else if( pInfo->isMasterServer())
171    node->addMasterServer(pInfo);
172}
173
174
175/**
176 * @returns the proxy server of the first choice
177 */
178PeerInfo* NetworkMonitor::getFirstChoiceProxy()
179{
180  // return the fist proxy in the list
181  return this->localNode->getActiveProxyServer(0);
182}
183
184
185/**
186 * @returns the proxy server of second choice
187 */
188PeerInfo* NetworkMonitor::getSecondChoiceProxy()
189{
190  // return the second server in the list
191  return this->localNode->getActiveProxyServer(1);
192}
193
194
195/**
196 * this displays the network monitor gui
197 */
198void NetworkMonitor::showGUI()
199{
200  if (this->box == NULL)
201  {
202    this->box = new OrxGui::GLGuiBox(OrxGui::Vertical);
203    {
204      OrxGui::GLGuiBox* waterColorBox = new OrxGui::GLGuiBox(OrxGui::Horizontal);
205      {
206        OrxGui::GLGuiText* waterColorText = new OrxGui::GLGuiText();
207        waterColorText->setText("NetworkMonitor");
208        waterColorBox->pack(waterColorText);
209      }
210      this->box->pack(waterColorBox);
211    }
212
213    this->box->showAll();
214    this->box->setAbsCoor2D(300, 40);
215    OrxGui::GLGuiHandler::getInstance()->activate();
216//     OrxGui::GLGuiHandler::getInstance()->activateCursor();
217  }
218}
219
220
221/**
222 * hides the network monitor gui again
223 */
224void NetworkMonitor::hideGUI()
225{
226  if( this->box == NULL)
227    return;
228
229  OrxGui::GLGuiHandler::getInstance()->deactivate();
230//   OrxGui::GLGuiHandler::getInstance()->deactivateCursor();
231
232  delete this->box;
233  this->box = NULL;
234}
235
236
237/**
238 * processes the network monitor data
239 */
240void NetworkMonitor::process()
241{
242  this->playerNumber = 0;
243  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
244  for(; it != this->nodeList.end(); it++)
245  {
246    this->playerNumber += (*it)->getPlayerNumber();
247  }
248
249  // check if a proxy server has to activated
250
251//   this->debug();
252}
253
254/**
255 * prints out the debug informations
256 */
257void NetworkMonitor::debug()
258{
259  PRINT(0)("================================= Network Monitor::debug() =====\n");
260  PRINT(0)(" I am: %s\n", this->localNode->getPeerInfo()->getNodeTypeString().c_str());
261  PRINT(0)(" Total count of network connections: %i\n", this->connectionNumber);
262  PRINT(0)(" Total count of players: %i\n", this->playerNumber);
263  PRINT(0)(" Max players on this server: %i\n", SharedNetworkData::getInstance()->getMaxPlayer());
264
265  std::list<NetworkNode*>::iterator it = this->nodeList.begin();
266  for(; it != this->nodeList.end(); it++)
267  {
268    (*it)->debug(0);
269  }
270
271  PRINT(0)("================================================================\n");
272}
273
Note: See TracBrowser for help on using the repository browser.