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