2 // This file is part of the aMule Project.
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 )
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
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.
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
32 * Interface to stat tree nodes.
34 * This file defines various classes representing
35 * statistics tree nodes.
39 #ifndef EC_REMOTE // i.e. not CLIENT_GUI
40 # define VIRTUAL virtual
46 #include <list> // Needed for std::list
47 #include <wx/string.h> // Needed for wxString
48 #include <wx/thread.h> // Needed for wxMutex
52 #include <wx/datetime.h> // Needed for wxDateTime
53 #include "GetTickCount.h" // Needed for GetTickCount64()
61 stNone
= 0, ///< Nothing. Really.
62 stSortChildren
= 1, ///< Childrens are sorted descending by their ID.
63 stShowPercent
= 2, /*!< Shows percentage compared to parent.
64 * Counters only, whose parent is also counter! (khmm...)
66 stHideIfZero
= 4, ///< Hides item (and children) if value is zero.
67 stSortByValue
= 8, /*!< Together with stSortChildren, sorts children by their value.
68 * WARNING! This assumes that value-sorted children are
70 * Sort-by-value works only on children with ID between 0x00000100-0x7fffffff.
71 * @note CStatTreeItemBase::ReSortChildren() must be called to get sort order right.
73 stCapChildren
= 16 ///< Caps children list.
74 /*!< Shows only top n children, where n is set by CStatisticsDlg::FillTree() to thePrefs::GetMaxClientVersions().
75 * The list itself is not changed, only visibility is ignored on items
76 * outside the top n visible.
78 * On an EC request, visibility range can be set with the EC_TAG_STATTREE_CAPPING tag.
91 * Display modes for Simple and Counter items
95 dmDefault
, ///< Default display mode.
96 dmTime
, ///< Treat integer value as time in seconds.
97 dmBytes
///< Treat integer value as bytes count.
100 #endif /* !EC_REMOTE */
103 class CStatTreeItemBase
;
104 typedef std::list
<CStatTreeItemBase
*>::iterator StatTreeItemIterator
;
109 uint32_t NewStatTreeItemId();
113 * Base tree item class
115 class CStatTreeItemBase
121 * Creates an item with a constant label.
123 * @param label Visible text for the item.
124 * @param flags Flags to use.
126 CStatTreeItemBase(const wxString
&label
, unsigned flags
= stNone
)
127 : m_label(label
), m_parent(NULL
), m_flags(flags
), m_id(0),
128 m_uniqueid(NewStatTreeItemId())
132 * Creates an item with a constant label.
134 * @param label Visible text for the item.
136 CStatTreeItemBase(const wxString
&label
, uint32_t uniqueid
)
137 : m_label(label
), m_uniqueid(uniqueid
)
141 * Creates an item (and the whole subtree) from an EC tag.
143 * @param tag EC tag containing a stat subtree.
145 CStatTreeItemBase(const CECTag
*tag
);
149 * Deletes all children.
151 VIRTUAL
~CStatTreeItemBase();
155 * Adds a new child node.
157 * @param child child to add.
158 * @param id an optional ID for the new item.
159 * @param skipOneLevel activates a trick to let the code work for aMule OSInfo and Version trees.
161 * @return the newly added item.
163 CStatTreeItemBase
*AddChild(
164 CStatTreeItemBase
* child
,
166 bool skipOneLevel
= false);
170 * Check for children.
172 * @return true if this node has children, false otherwise.
174 bool HasChildren() { wxMutexLocker
lock(m_lock
); return !m_children
.empty(); }
177 * Check for visible children.
179 * @return true if this node has children and at least one of them is visible.
181 bool HasVisibleChildren();
186 * Check for a given child.
188 * @return true if this node has a child with the given ID.
190 bool HasChildWithId(uint32_t id
);
193 * Access a specific child.
195 * @return the child with the given ID, or NULL if not found.
197 CStatTreeItemBase
*GetChildById(uint32_t id
);
200 * Get the first visible child.
202 * @param max_children The maximum number of children to show, when the stCapChildren flag is set. Otherwise it has no effect. (0 = unlimited)
204 * @return An iterator, that should be passed to GetNextVisibleChild() and IsAtEndOfList().
206 StatTreeItemIterator
GetFirstVisibleChild(uint32_t max_children
);
209 * Get the next visible child.
211 void GetNextVisibleChild(StatTreeItemIterator
& it
);
213 #else /* EC_REMOTE */
216 * Get the first visible child.
218 * @return An iterator, that should be passed to GetNextVisibleChild() and IsAtEndOfList().
220 * @note On a remote list every item is visible.
222 StatTreeItemIterator
GetFirstVisibleChild() { return m_children
.begin(); }
225 * Get the next visible child.
227 * @note On a remote list every item is visible.
229 void GetNextVisibleChild(StatTreeItemIterator
& it
) { ++it
; }
231 #endif /* !EC_REMOTE / EC_REMOTE */
234 * Check if we are past the end of child list.
236 bool IsAtEndOfList(StatTreeItemIterator
& it
) { return it
== m_children
.end(); }
240 * Resorts children for the stSortByValue flag.
242 void ReSortChildren() { wxMutexLocker
lock(m_lock
); m_children
.sort(ValueSort
); }
248 * Returns a string that will be displayed on the GUI tree.
250 virtual wxString
GetDisplayString() const;
253 * Returns the associated text (GUI item label).
255 const wxString
& GetDisplayString() const { return m_label
; }
256 #endif /* !EC_REMOTE / EC_REMOTE */
259 * Returns the mutex used to lock the child list of this node.
261 * This function is used by CStatisticsDlg to be able to lock the
262 * core tree while updating the GUI tree.
264 wxMutex
& GetLock() { return m_lock
; }
267 * Returns the unique ID of this node.
269 uint32_t GetUniqueId() const { return m_uniqueid
; }
270 #endif /* !AMULE_DAEMON */
273 * Check whether this node is visible.
275 VIRTUAL
bool IsVisible() const { return true; }
279 * Create an EC tag from this node (and children).
281 * @param max_children The maximum number of children to show, when the stCapChildren flag is set. Otherwise it has no effect. (0 = unlimited)
283 * @return A EC tag containing this node and all its children.
285 virtual CECTag
*CreateECTag(uint32_t max_children
);
292 * Add values to the EC tag being generated.
294 * Should have a real implementation in children which have some value.
295 * The given parameter is the tag to which values should be added.
297 virtual void AddECValues(CECTag
*) const {}
300 //! Unformatted and untranslated label of the node. Note: On remote gui it is already formatted and translated.
301 const wxString m_label
;
304 //! Parent of this node.
305 CStatTreeItemBase
*m_parent
;
307 //! Flags for the node.
314 //! Function used when sorting children by value.
315 static bool ValueSort(CStatTreeItemBase
* a
, CStatTreeItemBase
* b
);
320 //! Counter to keep track of displayed visible items
321 // (needed for the stCapChildren flag)
322 uint32_t m_visible_counter
;
325 //! Unique ID of this node.
328 //! Children of this node.
329 std::list
<CStatTreeItemBase
*> m_children
;
331 //! Lock to protect list from simultanous access.
337 // Anything below is only for core.
344 * This tree item has one value and nothing speciality. :)
345 * The value might be an arbitrary integer or floating point type,
346 * or a wxString string.
348 * The item is able to display value in different formats, see SetDisplayMode().
350 * @note that you have to specify the right format code on 'label', i.e.:
351 * %s for string and integers with displayMode dmTime or dmBytes,
352 * %u or similar for integers, and
353 * %f or similar for floating point types.
355 * @note You have to call SetValue() after creation for non-integer values, otherwise
356 * you'll get undesired results.
358 class CStatTreeItemSimple
: public CStatTreeItemBase
365 * @see CStatTreeItemBase::CStatTreeItemBase
368 const wxString
&label
,
369 unsigned flags
= stNone
,
370 enum EDisplayMode displaymode
= dmDefault
)
372 CStatTreeItemBase(label
, flags
),
373 m_valuetype(vtUnknown
),
374 m_displaymode(displaymode
)
376 SetValue((uint64_t)0);
380 * Sets the desired display mode of value.
382 void SetDisplayMode(enum EDisplayMode mode
)
384 m_displaymode
= mode
;
388 * Sets an integer type value.
390 * @param value the value to be set.
392 void SetValue(uint64_t value
)
394 m_valuetype
= vtInteger
;
399 * Sets a floating point type value.
401 * @param value the value to be set.
403 void SetValue(double value
)
405 m_valuetype
= vtFloat
;
406 m_floatvalue
= value
;
410 * Sets a string type value.
412 * @param value the value to be set.
414 void SetValue(const wxString
& value
)
416 m_valuetype
= vtString
;
417 m_stringvalue
= value
;
422 * @see CStatTreeItemBase::GetDisplayString()
424 virtual wxString
GetDisplayString() const;
428 * @see CStatTreeItemBase::IsVisible()
430 virtual bool IsVisible() const;
434 * Add values to EC tag being generated.
436 * @param tag The tag to which values should be added.
438 * @see CStatTreeItemBase::AddECValues
440 virtual void AddECValues(CECTag
*tag
) const;
442 //! Type of the value.
443 enum EValueType m_valuetype
;
444 //! Display mode of the value.
445 enum EDisplayMode m_displaymode
;
446 //! Union to save space.
449 uint64_t m_intvalue
; ///< Integer value.
450 double m_floatvalue
; ///< Floating point value.
452 wxString m_stringvalue
; ///< String value.
457 * Counter-type tree item template.
459 * Able to show percentage compared to parent, hide itself
460 * when value is zero, and nice functions for changing the value.
461 * stShowPercent and stHideIfZero flags take effect only on
464 template<typename _Tp
>
465 class CStatTreeItemCounterTmpl
: public CStatTreeItemBase
471 * @see CStatTreeItemBase::CStatTreeItemBase
473 CStatTreeItemCounterTmpl(
474 const wxString
&label
,
475 unsigned flags
= stNone
)
477 CStatTreeItemBase(label
, flags
),
479 m_displaymode(dmDefault
) {}
482 * Retrieve counter value.
484 _Tp
GetValue() const { return m_value
; }
487 * Retrieve counter value.
489 operator _Tp() const { return m_value
; }
492 * Set counter to given value.
494 void SetValue(_Tp value
) { m_value
= value
; }
497 * Set counter to given value.
499 void operator=(_Tp value
) { m_value
= value
; }
502 * Increase value by 1.
504 void operator++() { ++m_value
; }
507 * Decrease value by 1.
509 void operator--() { --m_value
; }
512 * Increase value by given amount.
514 void operator+=(_Tp value
) { m_value
+= value
; }
517 * Decrease value by given amount.
519 void operator-=(_Tp value
) { m_value
-= value
; }
522 * Sets the desired display mode of value.
524 void SetDisplayMode(enum EDisplayMode mode
) { m_displaymode
= mode
; }
528 * @see CStatTreeItemBase::GetDisplayString()
530 virtual wxString
GetDisplayString() const;
534 * @see CStatTreeItemBase::IsVisible()
536 virtual bool IsVisible() const
538 return (m_flags
& stHideIfZero
) ? (m_value
!= 0) : true;
543 * Add values to EC tag being generated.
545 * @param tag The tag to which values should be added.
547 * @see CStatTreeItemBase::AddECValues
549 virtual void AddECValues(CECTag
*tag
) const;
551 //! Actual value of the counter.
554 //! Display mode of the value.
555 enum EDisplayMode m_displaymode
;
558 typedef CStatTreeItemCounterTmpl
<uint64_t> CStatTreeItemCounter
;
559 typedef CStatTreeItemCounterTmpl
<uint32_t> CStatTreeItemNativeCounter
;
563 * A counter, which does not display its value :P
565 class CStatTreeItemHiddenCounter
: public CStatTreeItemCounter
571 * @see CStatTreeItemCounter::CStatTreeItemCounter
573 CStatTreeItemHiddenCounter(
574 const wxString
&label
,
575 unsigned flags
= stNone
)
577 CStatTreeItemCounter(label
, flags
) {}
581 * @see CStatTreeItemBase::GetDisplayString()
583 virtual wxString
GetDisplayString() const
585 return CStatTreeItemBase::GetDisplayString();
590 * @see CStatTreeItemBase::IsVisible()
592 virtual bool IsVisible() const { return true; }
596 virtual void AddECValues(CECTag
*) const {}
601 * Item for the session/total upload/download counter
603 class CStatTreeItemUlDlCounter
: public CStatTreeItemCounter
607 * @param label format text for item.
608 * @param totalfunc function that will return the totals.
610 CStatTreeItemUlDlCounter(
611 const wxString
&label
,
612 uint64_t (*totalfunc
)(),
613 unsigned flags
= stNone
)
615 CStatTreeItemCounter(label
, flags
),
616 m_totalfunc(totalfunc
) {}
620 * @see CStatTreeBase::GetDisplayString()
622 virtual wxString
GetDisplayString() const;
627 * Add values to EC tag being generated.
629 * @param tag The tag to which values should be added.
631 * @see CStatTreeItemBase::AddECValues
633 virtual void AddECValues(CECTag
*tag
) const;
635 //! A function whose return value is the total (without current) value.
636 uint64_t (*m_totalfunc
)();
641 * Counter-like tree item which remembers its max value.
643 * Used for active connections counter, to be able to get peak connections.
645 class CStatTreeItemCounterMax
: public CStatTreeItemBase
649 * @see CStatTreeItemBase::CStatTreeItemBase
651 CStatTreeItemCounterMax(const wxString
&label
)
653 CStatTreeItemBase(label
),
663 if (++m_value
> m_max_value
) {
664 m_max_value
= m_value
;
671 void operator--() { --m_value
; }
674 * Retrieve actual value
676 uint32_t GetValue() { return m_value
; }
681 uint32_t GetMaxValue() { return m_max_value
; }
685 * @see CStatTreeItemBase::GetDisplayString()
687 virtual wxString
GetDisplayString() const;
692 * Add values to EC tag being generated.
694 * @param tag The tag to which values should be added.
696 * @see CStatTreeItemBase::AddECValues
698 virtual void AddECValues(CECTag
*tag
) const;
700 //! Actual value of the counter.
703 //! Maximal value the counter has ever reached.
704 uint32_t m_max_value
;
709 * Tree item for counting packets
711 class CStatTreeItemPackets
: public CStatTreeItemBase
713 friend class CStatTreeItemPacketTotals
;
717 * @see CStatTreeItemBase::CStatTreeItemBase
719 CStatTreeItemPackets(const wxString
&label
)
721 CStatTreeItemBase(label
, stNone
),
726 * Add a packet of size 'size'.
728 void operator+=(long size
)
736 * @see CStatTreeItemBase::GetDisplayString()
738 virtual wxString
GetDisplayString() const;
743 * Add values to EC tag being generated.
745 * @param tag The tag to which values should be added.
747 * @see CStatTreeItemBase::AddECValues
749 virtual void AddECValues(CECTag
*tag
) const;
751 //! Total number of packets.
754 //! Total bytes in the packets.
760 * Tree item for counting totals on packet counters.
762 * This item sums up a number of packet counters, plus adds its own values.
764 class CStatTreeItemPacketTotals
: public CStatTreeItemPackets
768 * @see CStatTreeItemPackets::CStatTreeItemPackets
770 CStatTreeItemPacketTotals(const wxString
&label
)
772 CStatTreeItemPackets(label
) {}
775 * Adds a packet counter, whose values should be counted in the totals.
777 void AddPacketCounter(CStatTreeItemPackets
* counter
)
779 m_counters
.push_back(counter
);
784 * @see CStatTreeItemPackets::GetDisplayString()
786 virtual wxString
GetDisplayString() const;
791 * Add values to EC tag being generated.
793 * @param tag The tag to which values should be added.
795 * @see CStatTreeItemBase::AddECValues
797 virtual void AddECValues(CECTag
*tag
) const;
799 //! List of packet counters to sum.
800 std::vector
<CStatTreeItemPackets
*> m_counters
;
805 * Tree item for timer type nodes.
807 class CStatTreeItemTimer
: public CStatTreeItemBase
812 * @see CStatTreeItemBase::CStatTreeItemBase
815 const wxString
&label
,
816 unsigned flags
= stNone
)
818 CStatTreeItemBase(label
, flags
),
822 * Sets timer start time (and thus starts timer).
824 void SetStartTime(uint64_t value
) { m_value
= value
; }
827 * Starts the timer if it's not running.
829 void StartTimer() { if (!m_value
) m_value
= GetTickCount64(); }
834 void StopTimer() { m_value
= 0; }
837 * Check whether the timer is running.
839 bool IsRunning() const { return m_value
!= 0; }
842 * Reset timer unconditionally.
844 void ResetTimer() { m_value
= GetTickCount64(); }
849 uint64_t GetTimerValue() const
851 return m_value
? GetTickCount64() - m_value
: 0;
855 * Get timer value (in ticks).
857 operator uint64_t() const
859 return m_value
? GetTickCount64() - m_value
: 0;
863 * Get elapsed time in seconds.
865 uint64_t GetTimerSeconds() const
867 return m_value
? (GetTickCount64() - m_value
) / 1000 : 0;
871 * Get start time of the timer.
873 uint64_t GetTimerStart() const { return m_value
; }
877 * @see CStatTreeItemBase::GetDisplayString()
879 virtual wxString
GetDisplayString() const;
883 * @see CStatTreeItemBase::IsVisible()
885 virtual bool IsVisible() const
887 return (m_flags
& stHideIfZero
) ? m_value
!= 0 : true;
892 * Add values to EC tag being generated.
894 * @param tag The tag to which values should be added.
896 * @see CStatTreeItemBase::AddECValues
898 virtual void AddECValues(CECTag
*tag
) const;
900 //! Tick count value when timer was started.
906 * Tree item for shared files average size.
908 * Average is counted as dividend / divisor, if divisor is non-zero.
910 class CStatTreeItemAverage
: public CStatTreeItemBase
914 * @see CStatTreeItemBase::CStatTreeItemBase
916 * @param dividend What to divide.
917 * @param divisor Divide by what.
919 CStatTreeItemAverage(
920 const wxString
&label
,
921 const CStatTreeItemCounter
*dividend
,
922 const CStatTreeItemCounter
*divisor
,
923 enum EDisplayMode displaymode
)
925 CStatTreeItemBase(label
, stNone
),
926 m_dividend(dividend
),
928 m_displaymode(displaymode
) {}
932 * @see CStatTreeItemBase::GetDisplayString()
934 virtual wxString
GetDisplayString() const;
938 * @see CStatTreeItemBase::IsVisible()
940 virtual bool IsVisible() const { return (*m_divisor
) != 0; }
944 * Add values to EC tag being generated.
946 * @param tag The tag to which values should be added.
948 * @see CStatTreeItemBase::AddECValues
950 virtual void AddECValues(CECTag
*tag
) const;
953 const CStatTreeItemCounter
*m_dividend
;
956 const CStatTreeItemCounter
*m_divisor
;
959 enum EDisplayMode m_displaymode
;
964 * Tree item for average up/down speed.
966 class CStatTreeItemAverageSpeed
: public CStatTreeItemBase
971 * @see CStatTreeItemBase::CStatTreeItemBase
973 * @param counter Session up/down counter.
974 * @param timer Session uptime timer.
976 CStatTreeItemAverageSpeed(
977 const wxString
&label
,
978 const CStatTreeItemUlDlCounter
*counter
,
979 const CStatTreeItemTimer
*timer
)
981 CStatTreeItemBase(label
, stNone
),
987 * @see CStatTreeItemBase::GetDisplayString()
989 virtual wxString
GetDisplayString() const;
994 * Add values to EC tag being generated.
996 * @param tag The tag to which values should be added.
998 * @see CStatTreeItemBase::AddECValues
1000 virtual void AddECValues(CECTag
*tag
) const;
1002 //! Session sent/received bytes counter.
1003 const CStatTreeItemUlDlCounter
*m_counter
;
1006 const CStatTreeItemTimer
*m_timer
;
1011 * Tree item for displaying ratio between two counters.
1013 * Ratio is counted as counter1:counter2.
1015 class CStatTreeItemRatio
: public CStatTreeItemBase
1019 * @see CStatTreeItemBase::CStatTreeItemBase
1021 * @param cnt1 First counter to use.
1022 * @param cnt2 Second counter to use.
1025 const wxString
&label
,
1026 const CStatTreeItemCounter
*cnt1
,
1027 const CStatTreeItemCounter
* cnt2
)
1029 CStatTreeItemBase(label
, stNone
),
1033 #ifndef AMULE_DAEMON
1035 * @see CStatTreeItemBase::GetDisplayString()
1037 virtual wxString
GetDisplayString() const;
1042 * Add values to EC tag being generated.
1044 * @param tag The tag to which values should be added.
1046 * @see CStatTreeItemBase::AddECValues
1048 virtual void AddECValues(CECTag
*tag
) const;
1051 const CStatTreeItemCounter
*m_counter1
;
1054 const CStatTreeItemCounter
*m_counter2
;
1059 * Special counter for reconnects.
1061 class CStatTreeItemReconnects
: public CStatTreeItemNativeCounter
{
1064 * @see CStatTreeItemBase::CStatTreeItemBase
1066 CStatTreeItemReconnects(const wxString
&label
)
1068 CStatTreeItemNativeCounter(label
, stNone
) {}
1070 #ifndef AMULE_DAEMON
1072 * @see CStatTreeItemBase::GetDisplayString()
1074 virtual wxString
GetDisplayString() const;
1079 * Add values to EC tag being generated.
1081 * @param tag The tag to which values should be added.
1083 * @see CStatTreeItemBase::AddECValues
1085 virtual void AddECValues(CECTag
*tag
) const;
1089 * Special item for Max Connection Limit Reached
1091 class CStatTreeItemMaxConnLimitReached
: public CStatTreeItemBase
1095 * @see CStatTreeItemBase::CStatTreeItemBase
1097 CStatTreeItemMaxConnLimitReached(const wxString
&label
)
1099 CStatTreeItemBase(label
),
1103 * Increase counter and save time.
1108 m_time
.SetToCurrent();
1111 #ifndef AMULE_DAEMON
1113 * Returns a string to be displayed on GUI.
1115 * For m_count == 0 it will display "Never",
1116 * for other values it will display the counter value and the
1117 * date & time of the event.
1119 virtual wxString
GetDisplayString() const;
1124 * Add values to EC tag being generated.
1126 * @param tag The tag to which values should be added.
1128 * @see CStatTreeItemBase::AddECValues
1130 virtual void AddECValues(CECTag
*tag
) const;
1132 //! Number of times max conn limit reached.
1135 //! Last time when max conn limit reached.
1140 * Special item for total client count
1142 class CStatTreeItemTotalClients
: public CStatTreeItemBase
1146 * @see CStatTreeItemBase::CStatTreeItemBase
1148 * @param known Counter that counts known clients.
1149 * @param unknown Counter that counts unknown clients.
1151 CStatTreeItemTotalClients(
1152 const wxString
&label
,
1153 const CStatTreeItemCounter
*known
,
1154 const CStatTreeItemCounter
*unknown
)
1156 CStatTreeItemBase(label
),
1158 m_unknown(unknown
) {}
1160 #ifndef AMULE_DAEMON
1162 * @see CStatTreeItemBase::GetDisplayString()
1164 virtual wxString
GetDisplayString() const;
1169 * Add values to EC tag being generated.
1171 * @param tag The tag to which values should be added.
1173 * @see CStatTreeItemBase::AddECValues
1175 virtual void AddECValues(CECTag
*tag
) const;
1177 //! Counter counting known clients.
1178 const CStatTreeItemCounter
*m_known
;
1180 //! Counter counting unknown clients.
1181 const CStatTreeItemCounter
*m_unknown
;
1184 #endif /* !EC_REMOTE */
1186 #endif /* STATTREE_H */
1187 // File_checked_for_headers