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