Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

framework integration

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