Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

proxy servers are synced in a better form

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