Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/new_class_id/src/lib/network/monitor/network_stats_widget.cc @ 9690

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

some network-stuff

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