SLikeNet 0.1.3

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StatisticsHistory.h
Go to the documentation of this file.
1 /*
2 * Original work: Copyright (c) 2014, Oculus VR, Inc.
3 * All rights reserved.
4 *
5 * This source code is licensed under the BSD-style license found in the
6 * RakNet License.txt file in the licenses directory of this source tree. An additional grant
7 * of patent rights can be found in the RakNet Patents.txt file in the same directory.
8 *
9 *
10 * Modified work: Copyright (c) 2017, SLikeSoft UG (haftungsbeschränkt)
11 *
12 * This source code was modified by SLikeSoft. Modifications are licensed under the MIT-style
13 * license found in the license.txt file in the root directory of this source tree.
14 */
15
18
19
20 #include "NativeFeatureIncludes.h"
21 #if _RAKNET_SUPPORT_StatisticsHistory==1
22
23 #ifndef __STATISTICS_HISTORY_H
24 #define __STATISTICS_HISTORY_H
25
26 #include "PluginInterface2.h"
27 #include "memoryoverride.h"
28 #include "NativeTypes.h"
29 #include "DS_List.h"
30 #include "types.h"
31 #include "DS_OrderedList.h"
32 #include "string.h"
33 #include "DS_Queue.h"
34 #include "DS_Hash.h"
35 #include <float.h>
36
37 namespace SLNet
38 {
40 class RakPeerInterface;
41
42 // Type used to track values. If needed, change to double and recompile
43 typedef double SHValueType;
44 #define SH_TYPE_MAX DBL_MAX
45
47 class RAK_DLL_EXPORT StatisticsHistory
48 {
49 public:
50 // GetInstance() and DestroyInstance(instance*)
51 STATIC_FACTORY_DECLARATIONS(StatisticsHistory)
52
53 enum SHErrorCode
54 {
55 SH_OK,
56 SH_UKNOWN_OBJECT,
57 SH_UKNOWN_KEY,
58 SH_INVALID_PARAMETER,
59 };
60
61 enum SHSortOperation
62 {
63 SH_DO_NOT_SORT,
64
65 SH_SORT_BY_RECENT_SUM_ASCENDING,
66 SH_SORT_BY_RECENT_SUM_DESCENDING,
67 SH_SORT_BY_LONG_TERM_SUM_ASCENDING,
68 SH_SORT_BY_LONG_TERM_SUM_DESCENDING,
69 SH_SORT_BY_RECENT_SUM_OF_SQUARES_ASCENDING,
70 SH_SORT_BY_RECENT_SUM_OF_SQUARES_DESCENDING,
71 SH_SORT_BY_RECENT_AVERAGE_ASCENDING,
72 SH_SORT_BY_RECENT_AVERAGE_DESCENDING,
73 SH_SORT_BY_LONG_TERM_AVERAGE_ASCENDING,
74 SH_SORT_BY_LONG_TERM_AVERAGE_DESCENDING,
75 SH_SORT_BY_RECENT_HIGHEST_ASCENDING,
76 SH_SORT_BY_RECENT_HIGHEST_DESCENDING,
77 SH_SORT_BY_RECENT_LOWEST_ASCENDING,
78 SH_SORT_BY_RECENT_LOWEST_DESCENDING,
79 SH_SORT_BY_LONG_TERM_HIGHEST_ASCENDING,
80 SH_SORT_BY_LONG_TERM_HIGHEST_DESCENDING,
81 SH_SORT_BY_LONG_TERM_LOWEST_ASCENDING,
82 SH_SORT_BY_LONG_TERM_LOWEST_DESCENDING,
83 };
84
85 enum SHDataCategory
86 {
90 DC_DISCRETE,
91
96 DC_CONTINUOUS
97 };
98
99 struct TimeAndValue;
100 struct TimeAndValueQueue;
101
102 struct TrackedObjectData
103 {
104 TrackedObjectData();
105 TrackedObjectData(uint64_t _objectId, int _objectType, void *_userData);
106 uint64_t objectId;
107 int objectType;
108 void *userData;
109 };
110
111 StatisticsHistory();
112 virtual ~StatisticsHistory();
113 void SetDefaultTimeToTrack(Time defaultTimeToTrack);
114 Time GetDefaultTimeToTrack(void) const;
115 bool AddObject(TrackedObjectData tod);
116 bool RemoveObject(uint64_t objectId, void **userData);
117 void RemoveObjectAtIndex(unsigned int index);
118 void Clear(void);
119 unsigned int GetObjectCount(void) const;
120 StatisticsHistory::TrackedObjectData * GetObjectAtIndex(unsigned int index) const;
121 unsigned int GetObjectIndex(uint64_t objectId) const;
122 bool AddValueByObjectID(uint64_t objectId, RakString key, SHValueType val, Time curTime, bool combineEqualTimes);
123 void AddValueByIndex(unsigned int index, RakString key, SHValueType val, Time curTime, bool combineEqualTimes);
124 SHErrorCode GetHistoryForKey(uint64_t objectId, RakString key, TimeAndValueQueue **values, Time curTime) const;
125 bool GetHistorySorted(uint64_t objectId, SHSortOperation sortType, DataStructures::List<TimeAndValueQueue *> &values) const;
126 void MergeAllObjectsOnKey(RakString key, TimeAndValueQueue *tavqOutput, SHDataCategory dataCategory) const;
127 void GetUniqueKeyList(DataStructures::List<RakString> &keys);
128
129 struct TimeAndValue
130 {
131 Time time;
132 SHValueType val;
133 };
134
135 struct TimeAndValueQueue
136 {
137 TimeAndValueQueue();
138 ~TimeAndValueQueue();
139
140 DataStructures::Queue<TimeAndValue> values;
141
142 Time timeToTrackValues;
143 RakString key;
144
145 SHValueType recentSum;
146 SHValueType recentSumOfSquares;
147 SHValueType longTermSum;
148 SHValueType longTermCount;
149 SHValueType longTermLowest;
150 SHValueType longTermHighest;
151
152 void SetTimeToTrackValues(Time t);
153 Time GetTimeToTrackValues(void) const;
154 SHValueType GetRecentSum(void) const;
155 SHValueType GetRecentSumOfSquares(void) const;
156 SHValueType GetLongTermSum(void) const;
157 SHValueType GetRecentAverage(void) const;
158 SHValueType GetRecentLowest(void) const;
159 SHValueType GetRecentHighest(void) const;
160 SHValueType GetRecentStandardDeviation(void) const;
161 SHValueType GetLongTermAverage(void) const;
162 SHValueType GetLongTermLowest(void) const;
163 SHValueType GetLongTermHighest(void) const;
164 SHValueType GetSumSinceTime(Time t) const;
165 Time GetTimeRange(void) const;
166
167 // Merge two sets to output
168 static void MergeSets( const TimeAndValueQueue *lhs, SHDataCategory lhsDataCategory, const TimeAndValueQueue *rhs, SHDataCategory rhsDataCategory, TimeAndValueQueue *output );
169
170 // Shrink or expand a sample set to the approximate number given
171 // DC_DISCRETE will produce a histogram (sum) while DC_CONTINUOUS will produce an average
172 void ResizeSampleSet( int approximateSamples, DataStructures::Queue<StatisticsHistory::TimeAndValue> &blendedSamples, SHDataCategory dataCategory, Time timeClipStart=0, Time timeClipEnd=0 );
173
174 // Clear out all values
175 void Clear(void);
176
177 TimeAndValueQueue& operator = ( const TimeAndValueQueue& input );
178
180 void CullExpiredValues(Time curTime);
182 static SHValueType Interpolate(TimeAndValue t1, TimeAndValue t2, Time time);
184 SHValueType sortValue;
185 };
186
187 protected:
188 struct TrackedObject;
189 public:
190 static int TrackedObjectComp( const uint64_t &key, TrackedObject* const &data );
191 protected:
192
193 struct TrackedObject
194 {
195 TrackedObject();
196 ~TrackedObject();
197 TrackedObjectData trackedObjectData;
198 DataStructures::Hash<SLNet::RakString, TimeAndValueQueue*, 32, SLNet::RakString::ToInteger> dataQueues;
199 };
200
201 DataStructures::OrderedList<uint64_t, TrackedObject*,TrackedObjectComp> objects;
202
203 Time timeToTrack;
204 };
205
208 class RAK_DLL_EXPORT StatisticsHistoryPlugin : public PluginInterface2
209 {
210 public:
211 // GetInstance() and DestroyInstance(instance*)
212 STATIC_FACTORY_DECLARATIONS(StatisticsHistoryPlugin)
213
214 StatisticsHistory statistics;
215
216 StatisticsHistoryPlugin();
217 virtual ~StatisticsHistoryPlugin();
218 void SetTrackConnections(bool _addNewConnections, int newConnectionsObjectType, bool _removeLostConnections);
219
220 protected:
221 virtual void Update(void);
222 virtual void OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason );
223 virtual void OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming);
224
225 // Too slow
226 // virtual bool UsesReliabilityLayer(void) const {return true;}
227 // virtual void OnDirectSocketSend(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress);
228 // virtual void OnDirectSocketReceive(const char *data, const BitSize_t bitsUsed, SystemAddress remoteSystemAddress);
229
230
231 bool addNewConnections;
232 bool removeLostConnections;
233 int newConnectionsObjectType;
234 };
235
236 } // namespace SLNet
237
238 #endif // __STATISTICS_HISTORY_H
239
240 #endif // _RAKNET_SUPPORT_*