Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/proxy: way faster update, so update will be made automatically

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