Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/proxy/src/lib/network/monitor/network_stats_widget.cc @ 9649

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

gui should be hirachical now

File size: 8.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: Benjamin Grauer
13   co-programmer: ...
14*/
15
16//#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_
17
18#include "network_stats_widget.h"
19#include "network_monitor.h"
20#include "peer_info.h"
21
22#include "multi_type.h"
23
24#include "shell_command.h"
25
26#include "loading/resource_manager.h"
27
28// this fcuk does not work!
29// SHELL_COMMAND(gui, NetworkStatsWidget, toggleGUI)
30// ->setAlias("ProxyGui");
31
32
33HostWidget::HostWidget(const std::string& name, const IP& ip)
34    : GLGuiBox(OrxGui::Horizontal)
35{
36  this->init();
37
38  this->setName(name);
39  this->setIP(ip);
40
41}
42
43HostWidget::HostWidget(const PeerInfo* peerInfo)
44    : GLGuiBox(OrxGui::Horizontal)
45{
46  this->init();
47  if (peerInfo == NULL)
48  {
49    this->setName("INVALID");
50    return;
51  }
52  this->setName(peerInfo->getNodeTypeString() + "ID: " + MultiType(peerInfo->userId).getString());
53  this->setIP(peerInfo->ip);
54}
55
56void HostWidget::init()
57{
58  if(_font == NULL)
59    _font = new Font(ResourceManager::getInstance()->getDataDir() + "/fonts/arial.ttf", 20);
60
61  //this->_name.setFont(*_font);
62  this->_name.setTextSize(15);
63  //this->_ip.setFont(*_font);
64  this->_ip.setTextSize(15);
65
66  this->pack(&this->_name);
67  this->pack(&this->_ip);
68}
69
70void HostWidget::showing()
71{
72  this->_name.show();
73  this->_ip.show();
74}
75
76void HostWidget::hiding()
77{
78  this->_name.hide();
79  this->_ip.hide();
80}
81
82Font* HostWidget::_font = NULL;
83
84
85
86
87//======================================================//
88
89
90NodeWidget::NodeWidget(const std::string& proxyName, const IP& ip)
91    : _proxyWidget(proxyName, ip)
92{
93  this->_nodeNameWidth = 100.0f;
94  this->pack(&_proxyWidget);
95}
96
97NodeWidget::NodeWidget(const NetworkNode* node)
98    : _proxyWidget(node->getPeerInfo())// "node", node->getPeerInfo()->ip)
99{
100  this->_nodeNameWidth = 100.0f;
101  this->pack(&_proxyWidget);
102
103  std::list<NetworkNode*> list = node->getMasterServers();
104  std::list<NetworkNode*>::const_iterator it;
105
106  for(it = list.begin(); it != list.end(); it++)
107    this->addNode(*it);
108
109  list = node->getActiveProxyServers();
110  for(it = list.begin(); it != list.end(); it++)
111    this->addNode(*it);
112
113  list = node->getPassiveProxyServers();
114  for(it = list.begin(); it != list.end(); it++)
115    this->addNode(*it);
116
117  list = node->getClients();
118  for(it = list.begin(); it != list.end(); it++)
119    this->addNode(*it);
120}
121
122
123void NodeWidget::addNode(const NetworkNode* node)
124{
125  this->_nodes.push_back(new NodeWidget(node));
126  this->pack(this->_nodes.back());
127  this->_nodes.back()->show();
128}
129
130
131void NodeWidget::addNode(const std::string& name, const IP& ip)
132{
133  HostWidget* newClient = new HostWidget(name, ip);
134  newClient->setNameWidth(this->_nodeNameWidth);
135
136  this->pack(newClient);
137
138  if (this->isVisible())
139    newClient->show();
140}
141
142bool NodeWidget::removeNode(const IP& ip)
143{
144//   std::vector<HostWidget*>::iterator rmIt;
145//   for(rmIt = this->_nodes.begin(); rmIt != this->_nodes.end(); ++rmIt)
146//   {
147//     if (*(*rmIt) == ip)
148//     {
149//       delete (*rmIt);
150//       this->_nodes.erase(rmIt);
151//       return true;
152//     }
153//   }
154//   return false;
155}
156
157bool NodeWidget::removeNode(const std::string& name)
158{
159//   std::vector<HostWidget*>::iterator rmIt;
160//   for(rmIt = this->_nodes.begin(); rmIt != this->_nodes.end(); ++rmIt)
161//   {
162//     if (*(*rmIt) == name)
163//     {
164//       delete (*rmIt);
165//       this->_nodes.erase(rmIt);
166//       return true;
167//     }
168//   }
169//   return false;
170
171}
172
173bool NodeWidget::removeNode(const std::string& name, const IP& ip)
174{
175//   std::vector<HostWidget*>::iterator rmIt;
176//   for(rmIt = this->_nodes.begin(); rmIt != this->_nodes.end(); ++rmIt)
177//   {
178//     if (*(*rmIt) == ip && *(*rmIt) == name)
179//     {
180//       delete (*rmIt);
181//       this->_nodes.erase(rmIt);
182//       return true;
183//     }
184//   }
185//   return false;
186}
187
188
189void NodeWidget::setNodeNameWidths(float width)
190{
191/*  this->_nodeNameWidth = width;
192  for (unsigned int i = 0; i < this->_nodes.size(); ++i)
193    this->_nodes[i]->setNameWidth(width);*/
194}
195
196void NodeWidget::hiding()
197{
198  this->_proxyWidget.hide();
199  for (unsigned int i = 0; i < this->_nodes.size(); ++i)
200    this->_nodes[i]->hide();
201}
202
203void NodeWidget::showing()
204{
205  this->_proxyWidget.show();
206  for (unsigned int i = 0; i < this->_nodes.size(); ++i)
207    this->_nodes[i]->show();
208}
209
210
211//======================================================//
212
213/**
214 * @brief standard constructor
215 */
216NetworkStatsWidget::NetworkStatsWidget(const NetworkMonitor* monitor)
217    : OrxGui::GLGuiFixedpositionBox(OrxGui::Center, OrxGui::Vertical), _thisHost("myName", IP(127, 0, 0 , 1))
218{
219  this->_monitor = monitor;
220  this->_passedTime = 0.0f;
221
222  /*
223  this->_bar.setSize2D(100, 30);
224  this->pack(&this->_valueText);
225  this->_bar.setParent2D(&this->_valueText);
226
227  this->_valueText.setChangedTextColor(Color::white);
228
229  //this->setBackgroundTexture("maps/gui_element_background_2.png");
230  this->setBackgroundColor(Color(.5,.5,.5,1));
231
232  //this->_name.setBackgroundTexture(Texture());
233  //this->_valueText.setBackgroundTexture("maps/gui_element_background_2.png");
234  this->_bar.setBackgroundTexture(Texture());
235  this->_bar.setBackgroundColor(Color(0,0,0,0));
236  this->_bar.setForegroundTexture("maps/gui_element_background_faded.png");
237  this->_bar.setForegroundColor(Color(.5, .5, .5, 1));
238  this->_bar.setChangedValueColor(Color::black);
239  */
240  this->_thisHostIs.setText(std::string("I am ") + _monitor->getLocalNode()->getPeerInfo()->getNodeTypeString());
241
242  this->pack(&this->_thisHostIs);
243
244  this->pack(&this->_thisHost);
245
246  this->pack(&this->_serverBox);
247
248  this->pack(&this->_upstreamText);
249  this->pack(&this->_downstreamText);
250
251
252  this->rebuild();
253}
254
255
256/**
257 * @brief standard deconstructor
258 */
259NetworkStatsWidget::~NetworkStatsWidget ()
260{}
261
262
263void NetworkStatsWidget::addNode(const NetworkNode* node)
264{
265  this->_proxies.push_back(new NodeWidget(node));
266  this->_serverBox.pack(this->_proxies.back());
267  this->_proxies.back()->show();
268}
269
270
271
272
273NetworkStatsWidget* NetworkStatsWidget::_statsWidget = NULL;
274
275#include "class_list.h"
276
277void NetworkStatsWidget::toggleGUI()
278{
279  BaseObject* bo = NULL;
280  const std::list<BaseObject*>* ls = ClassList::getList(CL_NETWORK_MONITOR);
281  if (ls != NULL && !ls->empty())
282    bo = ls->front();
283
284  if (bo != NULL && NetworkStatsWidget::_statsWidget == NULL)
285  {
286    NetworkStatsWidget::_statsWidget = new NetworkStatsWidget(dynamic_cast<NetworkMonitor*> (bo));
287    NetworkStatsWidget::_statsWidget->showAll();
288  }
289  else
290  {
291    delete NetworkStatsWidget::_statsWidget;
292    NetworkStatsWidget::_statsWidget = NULL;
293  }
294}
295
296
297void NetworkStatsWidget::setMaximum(float max)
298{
299  // this->_bar.setMaximum(max);
300}
301
302void NetworkStatsWidget::setValue(float value)
303{
304  MultiType val(value);
305  val.setType(MT_INT);
306  /*
307
308  this->_bar.setValue(value);
309  this->_bar.setForegroundColor(Color::slerpHSVColor(Color::red, Color::green, value/this->_bar.maximum()));
310  this->_bar.setFrontColor(Color(1,1,1,1), true);
311  this->_valueText.setText(val.getString());
312  */
313}
314
315
316void NetworkStatsWidget::addProxy(const std::string& name, const IP& proxy)
317{}
318
319void NetworkStatsWidget::clearProxies()
320{}
321
322
323void NetworkStatsWidget::rebuild()
324{
325  while (!this->_proxies.empty())
326  {
327    delete this->_proxies.back();
328    this->_proxies.pop_back();
329  }
330
331  const NetworkNode* node = this->_monitor->getLocalNode();
332  if (node == NULL)
333  {
334    printf("NO NODE\n");
335    return;
336  }
337
338  this->addNode(node);
339}
340
341
342
343void NetworkStatsWidget::tick(float dt)
344{
345
346  if ((_passedTime+= dt) > 1.0f)
347  {
348    this->_passedTime = 0.0f;
349    this->rebuild();
350  }
351
352  assert (this->_monitor->getLocalNode() != NULL);
353  assert(this->_monitor->getLocalNode()->getPeerInfo() != NULL);
354
355  if(this->_monitor->getLocalNode()->getPeerInfo()->connectionMonitor != NULL)
356  {
357    ConnectionMonitor* mon = this->_monitor->getLocalNode()->getPeerInfo()->connectionMonitor;
358
359    this->_upstreamText.setText(MultiType(mon->getOutgoingZippedBandWidth()).getString());
360    this->_downstreamText.setText(MultiType(mon->getIncomingZippedBandWidth()).getString());
361  }
362}
363
364void NetworkStatsWidget::resize()
365{
366  GLGuiFixedpositionBox::resize();
367}
368
369
370void NetworkStatsWidget::showing()
371{}
372
373void NetworkStatsWidget::hiding()
374{}
Note: See TracBrowser for help on using the repository browser.