Upstream tarball 20080304
[amule.git] / src / StatTree.cpp
blob86aab16c45c7d75329f928409c7bb3e7b221479c
1 //
2 // This file is part of the aMule Project.
3 //
4 // Copyright (c) 2003-2008 aMule Team ( admin@amule.org / http://www.amule.org )
5 // Copyright (C) 2005-2008 Dévai Tamás ( gonosztopi@amule.org )
6 //
7 // Any parts of this program derived from the xMule, lMule or eMule project,
8 // or contributed by third-party developers are copyrighted by their
9 // respective authors.
11 // This program is free software; you can redistribute it and/or modify
12 // it under the terms of the GNU General Public License as published by
13 // the Free Software Foundation; either version 2 of the License, or
14 // (at your option) any later version.
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software
23 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
26 #include "StatTree.h"
28 #include <wx/intl.h>
29 #include "OtherFunctions.h"
31 #ifndef EC_REMOTE
33 #ifndef AMULE_DAEMON
34 #include <common/Format.h> // Needed for CFormat
36 #define a_brackets_b(a,b) (a + wxT(" (") + b + wxT(")"))
38 #endif /* !AMULE_DAEMON */
40 #endif /* !EC_REMOTE */
42 #include <ec/cpp/ECTag.h> // Needed for CECTag
44 #ifdef EC_REMOTE
45 #include <ec/cpp/ECSpecialTags.h> // Needed for CEC_StatTree_Node_Tag
46 #endif
49 #ifndef EC_REMOTE
50 uint32_t NewStatTreeItemId()
52 static uint32_t lastid = 0;
54 return ++lastid;
56 #endif
58 /* CStatTreeItemBase */
60 #ifdef EC_REMOTE
61 CStatTreeItemBase::CStatTreeItemBase(const CECTag *tag)
62 : m_label(((CEC_StatTree_Node_Tag*)tag)->GetDisplayString())
63 , m_uniqueid(tag->GetTagByNameSafe(EC_TAG_STATTREE_NODEID)->GetInt())
65 wxASSERT(tag->GetTagName() == EC_TAG_STATTREE_NODE);
67 for (int i = 0; i < tag->GetTagCount(); ++i) {
68 const CECTag *tmp = tag->GetTagByIndex(i);
69 if (tmp->GetTagName() == EC_TAG_STATTREE_NODE) {
70 m_children.push_back(new CStatTreeItemBase(tmp));
74 #endif /* EC_REMOTE */
76 CStatTreeItemBase::~CStatTreeItemBase()
78 for (std::list<CStatTreeItemBase*>::iterator it = m_children.begin();
79 it != m_children.end(); ++it) {
80 delete *it;
82 m_children.clear();
85 #ifndef EC_REMOTE
86 CStatTreeItemBase* CStatTreeItemBase::AddChild(
87 CStatTreeItemBase* child,
88 uint32_t id,
89 bool skipOneLevel)
91 wxMutexLocker lock(m_lock);
93 if (skipOneLevel) {
94 child->m_parent = m_parent;
95 } else {
96 child->m_parent = this;
98 child->m_id = id;
100 if (m_flags & stSortChildren) {
101 std::list<CStatTreeItemBase*>::iterator it = m_children.begin();
102 while (it != m_children.end() && id < (*it)->m_id) {
103 ++it;
105 m_children.insert(it, child);
106 } else {
107 m_children.push_back(child);
109 return child;
111 #endif /* !EC_REMOTE */
113 bool CStatTreeItemBase::HasVisibleChildren()
115 wxMutexLocker lock(m_lock);
117 for (std::list<CStatTreeItemBase*>::const_iterator it = m_children.begin();
118 it != m_children.end(); ++it) {
119 if ((*it)->IsVisible()) {
120 return true;
123 return false;
126 #ifndef EC_REMOTE
127 bool CStatTreeItemBase::HasChildWithId(uint32_t id)
129 wxMutexLocker lock(m_lock);
131 for (std::list<CStatTreeItemBase*>::const_iterator it = m_children.begin();
132 it != m_children.end(); ++it) {
133 if ((*it)->m_id == id) {
134 return true;
137 return false;
140 CStatTreeItemBase* CStatTreeItemBase::GetChildById(uint32_t id)
142 wxMutexLocker lock(m_lock);
144 for (std::list<CStatTreeItemBase*>::const_iterator it = m_children.begin();
145 it != m_children.end(); ++it) {
146 if ((*it)->m_id == id) {
147 return *it;
150 return NULL;
152 #endif /* !EC_REMOTE */
154 // Note: these functions do not lock the list, because it is already locked at the time they're called
155 #ifndef EC_REMOTE
156 StatTreeItemIterator CStatTreeItemBase::GetFirstVisibleChild(uint32_t max_children)
158 StatTreeItemIterator it = m_children.begin();
159 m_visible_counter = --max_children;
160 while (it != m_children.end() && !(*it)->IsVisible()) ++it;
161 return it;
164 void CStatTreeItemBase::GetNextVisibleChild(StatTreeItemIterator& it)
166 if (m_flags & stCapChildren) {
167 if (m_visible_counter == 0) {
168 it = m_children.end();
169 return;
170 } else {
171 --m_visible_counter;
174 if (it != m_children.end()) ++it;
175 while (it != m_children.end() && !(*it)->IsVisible()) ++it;
177 #endif
180 // Anything below is only for core.
182 #ifndef EC_REMOTE
184 bool CStatTreeItemBase::ValueSort(CStatTreeItemBase* a, CStatTreeItemBase* b)
186 if (a->m_id < 0x00000100 || a->m_id > 0x7fffffff || b->m_id < 0x00000100 || b->m_id > 0x7fffffff) {
187 return a->m_id > b->m_id;
188 } else {
189 return ((CStatTreeItemCounter*)a)->GetValue() > ((CStatTreeItemCounter*)b)->GetValue();
193 #ifndef AMULE_DAEMON
194 wxString CStatTreeItemBase::GetDisplayString() const
196 return wxGetTranslation(m_label);
198 #endif
200 CECTag *CStatTreeItemBase::CreateECTag(uint32_t max_children)
202 if (IsVisible()) {
203 wxMutexLocker lock(m_lock);
204 CECTag *tag = new CECTag(EC_TAG_STATTREE_NODE, m_label);
205 tag->AddTag(CECTag(EC_TAG_STATTREE_NODEID, m_uniqueid));
206 AddECValues(tag);
207 m_visible_counter = max_children - 1;
208 for (std::list<CStatTreeItemBase*>::const_iterator it = m_children.begin();
209 it != m_children.end(); ++it) {
210 CECTag *tmp = (*it)->CreateECTag(max_children);
211 if (tmp) {
212 tag->AddTag(*tmp);
213 delete tmp;
214 if (m_flags & stCapChildren) {
215 if (m_visible_counter == 0) {
216 break;
217 } else {
218 --m_visible_counter;
223 return tag;
224 } else {
225 return NULL;
230 /* CStatTreeItemSimple */
232 #ifndef AMULE_DAEMON
233 wxString CStatTreeItemSimple::GetDisplayString() const
235 switch (m_valuetype) {
236 case vtInteger:
237 switch (m_displaymode) {
238 case dmTime: return CFormat(wxGetTranslation(m_label)) % CastSecondsToHM(m_intvalue);
239 case dmBytes: return CFormat(wxGetTranslation(m_label)) % CastItoXBytes(m_intvalue);
240 default: return CFormat(wxGetTranslation(m_label)) % m_intvalue;
242 case vtFloat: return wxString::Format(wxGetTranslation(m_label), m_floatvalue);
243 case vtString: return CFormat(wxGetTranslation(m_label)) % m_stringvalue;
244 default: return wxGetTranslation(m_label);
247 #endif
249 bool CStatTreeItemSimple::IsVisible() const
251 if (m_flags & stHideIfZero) {
252 switch (m_valuetype) {
253 case vtInteger: return m_intvalue != 0;
254 case vtFloat: return m_floatvalue != 0.0;
255 case vtString: return !m_stringvalue.IsEmpty();
256 default: return false;
259 return true;
262 void CStatTreeItemSimple::AddECValues(CECTag *tag) const
264 switch (m_valuetype) {
265 case vtInteger: {
266 if (m_displaymode == dmTime) {
267 CECTag value(EC_TAG_STAT_NODE_VALUE, (uint32)m_intvalue);
268 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_TIME));
269 tag->AddTag(value);
270 } else {
271 CECTag value(EC_TAG_STAT_NODE_VALUE, (uint64)m_intvalue);
272 if (m_displaymode == dmBytes) {
273 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_BYTES));
275 tag->AddTag(value);
277 break;
279 case vtFloat: {
280 CECTag value(EC_TAG_STAT_NODE_VALUE, m_floatvalue);
281 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_DOUBLE));
282 tag->AddTag(value);
283 break;
285 case vtString: {
286 CECTag value(EC_TAG_STAT_NODE_VALUE, m_stringvalue);
287 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_STRING));
288 tag->AddTag(value);
289 break;
291 default:
292 break;
297 /* CStatTreeItemCounterTmpl */
299 #ifndef AMULE_DAEMON
300 template<typename _Tp>
301 wxString CStatTreeItemCounterTmpl<_Tp>::GetDisplayString() const
303 wxString my_label = wxGetTranslation(m_label);
304 // This is needed for client names, for example
305 if (my_label == m_label) {
306 if (m_label.Right(4) == wxT(": %s")) {
307 my_label = wxGetTranslation(
308 m_label.Mid(0, m_label.Length() - 4)) +
309 wxString(wxT(": %s"));
312 CFormat label(my_label);
313 if (m_displaymode == dmBytes) {
314 return label % CastItoXBytes(m_value);
315 } else {
316 wxString result = CFormat(wxT("%u")) % m_value;
317 if ((m_flags & stShowPercent) && m_parent) {
318 result.append(wxString::Format(wxT(" (%.2f%%)"),
319 ((double)m_value / ((CStatTreeItemCounterTmpl<_Tp>*)m_parent)->m_value) * 100.0));
321 return label % result;
324 #endif
326 template<typename _Tp>
327 void CStatTreeItemCounterTmpl<_Tp>::AddECValues(CECTag *tag) const
329 CECTag value(EC_TAG_STAT_NODE_VALUE, (uint64)m_value);
330 if (m_displaymode == dmBytes) {
331 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_BYTES));
332 } else {
333 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_ISTRING));
334 if ((m_flags & stShowPercent) && m_parent) {
335 CECTag tmp(EC_TAG_STAT_NODE_VALUE,
336 ((double)m_value / ((CStatTreeItemCounterTmpl<_Tp>*)m_parent)->m_value) * 100.0);
337 tmp.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_DOUBLE));
338 value.AddTag(tmp);
341 tag->AddTag(value);
344 /* CStatTreeItemUlDlCounter */
346 #ifndef AMULE_DAEMON
347 wxString CStatTreeItemUlDlCounter::GetDisplayString() const
349 return CFormat(wxGetTranslation(m_label)) %
350 a_brackets_b(CastItoXBytes(m_value), CastItoXBytes(m_value + m_totalfunc()));
352 #endif
354 void CStatTreeItemUlDlCounter::AddECValues(CECTag *tag) const
356 CECTag value(EC_TAG_STAT_NODE_VALUE, m_value);
357 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_BYTES));
358 CECTag tmp(EC_TAG_STAT_NODE_VALUE, m_value + m_totalfunc());
359 tmp.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_BYTES));
360 value.AddTag(tmp);
361 tag->AddTag(value);
365 /* CStatTreeItemCounterMax */
367 #ifndef AMULE_DAEMON
368 wxString CStatTreeItemCounterMax::GetDisplayString() const
370 return wxString::Format(wxGetTranslation(m_label), m_value);
372 #endif
374 void CStatTreeItemCounterMax::AddECValues(CECTag *tag) const
376 tag->AddTag(CECTag(EC_TAG_STAT_NODE_VALUE, (uint64)m_value));
380 /* CStatTreeItemPackets */
382 #ifndef AMULE_DAEMON
383 wxString CStatTreeItemPackets::GetDisplayString() const
385 return CFormat(wxGetTranslation(m_label)) %
386 a_brackets_b(CastItoXBytes(m_bytes), CastItoIShort(m_packets));
388 #endif
390 void CStatTreeItemPackets::AddECValues(CECTag *tag) const
392 CECTag value(EC_TAG_STAT_NODE_VALUE, m_bytes);
393 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_BYTES));
394 CECTag tmp(EC_TAG_STAT_NODE_VALUE, (uint64)m_packets);
395 tmp.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_ISHORT));
396 value.AddTag(tmp);
397 tag->AddTag(value);
401 /* CStatTreeItemPacketTotals */
403 #ifndef AMULE_DAEMON
404 wxString CStatTreeItemPacketTotals::GetDisplayString() const
406 uint32_t tmp_packets = m_packets;
407 uint64_t tmp_bytes = m_bytes;
408 for (std::vector<CStatTreeItemPackets*>::const_iterator it = m_counters.begin();
409 it != m_counters.end(); ++it) {
410 tmp_packets += (*it)->m_packets;
411 tmp_bytes += (*it)->m_bytes;
414 return CFormat(wxGetTranslation(m_label)) %
415 a_brackets_b(CastItoXBytes(tmp_bytes), CastItoIShort(tmp_packets));
417 #endif
419 void CStatTreeItemPacketTotals::AddECValues(CECTag *tag) const
421 uint32_t tmp_packets = m_packets;
422 uint64_t tmp_bytes = m_bytes;
423 for (std::vector<CStatTreeItemPackets*>::const_iterator it = m_counters.begin();
424 it != m_counters.end(); ++it) {
425 tmp_packets += (*it)->m_packets;
426 tmp_bytes += (*it)->m_bytes;
429 CECTag value(EC_TAG_STAT_NODE_VALUE, tmp_bytes);
430 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_BYTES));
431 CECTag tmp(EC_TAG_STAT_NODE_VALUE, (uint64)tmp_packets);
432 tmp.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_ISHORT));
433 value.AddTag(tmp);
434 tag->AddTag(value);
438 /* CStatTreeItemTimer */
440 #ifndef AMULE_DAEMON
441 wxString CStatTreeItemTimer::GetDisplayString() const
443 return CFormat(wxGetTranslation(m_label)) %
444 CastSecondsToHM(m_value ? GetTimerSeconds() : 0);
446 #endif
448 void CStatTreeItemTimer::AddECValues(CECTag *tag) const
450 CECTag value(EC_TAG_STAT_NODE_VALUE,
451 m_value ? (uint32)GetTimerSeconds() : (uint32)0);
452 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_TIME));
453 tag->AddTag(value);
457 /* CStatTreeItemAverage */
459 #ifndef AMULE_DAEMON
460 wxString CStatTreeItemAverage::GetDisplayString() const
462 if ((*m_divisor) != 0) {
463 switch (m_displaymode) {
464 case dmBytes:
465 return CFormat(wxGetTranslation(m_label)) %
466 CastItoXBytes((*m_dividend)/(*m_divisor));
467 case dmTime:
468 return CFormat(wxGetTranslation(m_label)) %
469 CastSecondsToHM((*m_dividend)/(*m_divisor));
470 default:
471 return CFormat(wxGetTranslation(m_label)) %
472 (CFormat(wxT("%u")) % ((uint64)(*m_dividend)/(*m_divisor))).GetString();
474 } else {
475 return CFormat(wxGetTranslation(m_label)) % wxT("-");
478 #endif
480 void CStatTreeItemAverage::AddECValues(CECTag *tag) const
482 if ((*m_divisor) != 0) {
483 uint64 data = (*m_dividend)/(*m_divisor);
484 if (m_displaymode == dmTime) {
485 CECTag value(EC_TAG_STAT_NODE_VALUE, (uint32)data);
486 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_TIME));
487 tag->AddTag(value);
488 } else {
489 CECTag value(EC_TAG_STAT_NODE_VALUE, data);
490 if (m_displaymode == dmBytes) {
491 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_BYTES));
493 tag->AddTag(value);
495 } else {
496 CECTag value(EC_TAG_STAT_NODE_VALUE, wxString(wxT("-")));
497 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_STRING));
498 tag->AddTag(value);
503 /* CStatTreeItemAverageSpeed */
505 #ifndef AMULE_DAEMON
506 wxString CStatTreeItemAverageSpeed::GetDisplayString() const
508 uint64 time = m_timer->GetTimerSeconds();
509 if (time) {
510 return CFormat(wxGetTranslation(m_label)) % CastItoSpeed((*m_counter)/time);
511 } else {
512 return CFormat(wxGetTranslation(m_label)) % CastItoSpeed(0);
515 #endif
517 void CStatTreeItemAverageSpeed::AddECValues(CECTag *tag) const
519 uint64 time = m_timer->GetTimerSeconds();
520 if (time) {
521 CECTag value(EC_TAG_STAT_NODE_VALUE, (uint32)((*m_counter)/time));
522 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_SPEED));
523 tag->AddTag(value);
524 } else {
525 CECTag value(EC_TAG_STAT_NODE_VALUE, (uint32)0);
526 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_SPEED));
527 tag->AddTag(value);
532 /* CStatTreeItemRatio */
534 #ifndef AMULE_DAEMON
535 wxString CStatTreeItemRatio::GetDisplayString() const
537 if (m_counter1->GetValue() && m_counter2->GetValue()) {
538 if ((*m_counter2) < (*m_counter1)) {
539 return CFormat(wxGetTranslation(m_label)) %
540 wxString::Format(wxT("%.2f : 1"),
541 (float)(*m_counter1)/(*m_counter2));
542 } else {
543 return CFormat(wxGetTranslation(m_label)) %
544 wxString::Format(wxT("1 : %.2f"),
545 (float)(*m_counter2)/(*m_counter1));
547 } else {
548 return CFormat(wxGetTranslation(m_label)) % _("Not available");
551 #endif
553 void CStatTreeItemRatio::AddECValues(CECTag *tag) const
555 wxString result;
556 if (m_counter1->GetValue() && m_counter2->GetValue()) {
557 if ((*m_counter2) < (*m_counter1)) {
558 result = wxString::Format(wxT("%.2f : 1"), (float)(*m_counter1)/(*m_counter2));
559 } else {
560 result = wxString::Format(wxT("1 : %.2f"), (float)(*m_counter2)/(*m_counter1));
562 } else {
563 result = wxTRANSLATE("Not available");
566 CECTag value(EC_TAG_STAT_NODE_VALUE, result);
567 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_STRING));
568 tag->AddTag(value);
572 /* CStatTreeItemReconnects */
574 #ifndef AMULE_DAEMON
575 wxString CStatTreeItemReconnects::GetDisplayString() const
577 return CFormat(wxGetTranslation(m_label)) % (m_value ? m_value - 1 : 0);
579 #endif
581 void CStatTreeItemReconnects::AddECValues(CECTag *tag) const
583 tag->AddTag(CECTag(EC_TAG_STAT_NODE_VALUE,
584 m_value ? (uint64)(m_value - 1) : (uint64)0));
587 /* CStatTreeItemMaxConnLimitReached */
589 #ifndef AMULE_DAEMON
590 wxString CStatTreeItemMaxConnLimitReached::GetDisplayString() const
592 if (m_count) {
593 return CFormat(wxGetTranslation(m_label)) %
594 (wxString::Format(wxT("%i : "), m_count) +
595 m_time.FormatISODate() +
596 wxT(" ") +
597 m_time.FormatISOTime());
598 } else {
599 return CFormat(wxGetTranslation(m_label)) % _("Never");
602 #endif
604 void CStatTreeItemMaxConnLimitReached::AddECValues(CECTag *tag) const
606 wxString result;
607 if (m_count) {
608 result = wxString::Format(wxT("%i : "), m_count) +
609 m_time.FormatISODate() +
610 wxT(" ") +
611 m_time.FormatISOTime();
612 } else {
613 result = wxTRANSLATE("Never");
615 CECTag value(EC_TAG_STAT_NODE_VALUE, result);
616 value.AddTag(CECTag(EC_TAG_STAT_VALUE_TYPE, (uint8)EC_VALUE_STRING));
617 tag->AddTag(value);
620 /* CStatTreeItemTotalClients */
622 #ifndef AMULE_DAEMON
623 wxString CStatTreeItemTotalClients::GetDisplayString() const
625 return CFormat(wxGetTranslation(m_label)) %
626 (m_known->GetValue() + m_unknown->GetValue()) %
627 m_known->GetValue();
629 #endif
631 void CStatTreeItemTotalClients::AddECValues(CECTag *tag) const
633 CECTag value1(EC_TAG_STAT_NODE_VALUE,
634 (uint64)(m_known->GetValue() + m_unknown->GetValue()));
635 tag->AddTag(value1);
636 CECTag value2(EC_TAG_STAT_NODE_VALUE,
637 (uint64)m_known->GetValue());
638 tag->AddTag(value2);
641 #endif /* !EC_REMOTE */
642 // File_checked_for_headers