XMPPBandwidthMonitor.m
4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#import "XMPPBandwidthMonitor.h"
#import "XMPPLogging.h"
#import "NSXMLElement+XMPP.h"
#if ! __has_feature(objc_arc)
#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
#endif
// Log levels: off, error, warn, info, verbose
#if DEBUG
static const int xmppLogLevel = XMPP_LOG_LEVEL_VERBOSE;
#else
static const int xmppLogLevel = XMPP_LOG_LEVEL_WARN;
#endif
@implementation XMPPBandwidthMonitor
{
dispatch_source_t timer;
uint64_t lastNumberOfBytesSent;
uint64_t lastNumberOfBytesReceived;
double smoothedAverageOutgoingBandwidth;
double smoothedAverageIncomingBandwidth;
}
- (double)outgoingBandwidth
{
__block double result = 0.0;
dispatch_block_t block = ^{
result = smoothedAverageOutgoingBandwidth;
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);
return result;
}
- (double)incomingBandwidth
{
__block double result = 0.0;
dispatch_block_t block = ^{
result = smoothedAverageIncomingBandwidth;
};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);
return result;
}
- (void)updateBandwidth
{
uint64_t currentNumberOfBytesSent = 0;
uint64_t currentNumberOfBytesReceived = 0;
[xmppStream getNumberOfBytesSent:¤tNumberOfBytesSent numberOfBytesReceived:¤tNumberOfBytesReceived];
double currentOutgoingBandwidth = currentNumberOfBytesSent - lastNumberOfBytesSent; // Bytes per second
double currentIncomingBandwidth = currentNumberOfBytesReceived - lastNumberOfBytesReceived; // Bytes per second
if (smoothedAverageOutgoingBandwidth || smoothedAverageIncomingBandwidth)
{
// Apply Brown's Simple Exponential Smoothing algorithm
double smoothingFactor = 0.3;
smoothedAverageOutgoingBandwidth = (smoothingFactor * currentOutgoingBandwidth)
+ ((1 - smoothingFactor) * smoothedAverageOutgoingBandwidth);
smoothedAverageIncomingBandwidth = (smoothingFactor * currentIncomingBandwidth)
+ ((1 - smoothingFactor) * smoothedAverageIncomingBandwidth);
}
else
{
// No previous data
smoothedAverageOutgoingBandwidth = currentOutgoingBandwidth;
smoothedAverageIncomingBandwidth = currentIncomingBandwidth;
}
lastNumberOfBytesSent = currentNumberOfBytesSent;
lastNumberOfBytesReceived = currentNumberOfBytesReceived;
XMPPLogVerbose(@"Bandwidth = Out(%.0f, %.0f) In(%.0f, %.0f)",
currentOutgoingBandwidth, smoothedAverageOutgoingBandwidth,
currentIncomingBandwidth, smoothedAverageIncomingBandwidth);
}
- (void)startTimer
{
if (timer == NULL)
{
uint64_t numberOfBytesSent = 0;
uint64_t numberOfBytesReceived = 0;
[xmppStream getNumberOfBytesSent:&numberOfBytesSent numberOfBytesReceived:&numberOfBytesReceived];
lastNumberOfBytesSent = numberOfBytesSent;
lastNumberOfBytesReceived = numberOfBytesReceived;
smoothedAverageOutgoingBandwidth = 0.0;
smoothedAverageIncomingBandwidth = 0.0;
timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, moduleQueue);
dispatch_source_set_event_handler(timer, ^{ @autoreleasepool {
[self updateBandwidth];
}});
NSTimeInterval interval = 1.0; // Update every 1 second(s)
uint64_t intervalTime = interval * NSEC_PER_SEC;
dispatch_time_t startTime = dispatch_time(DISPATCH_TIME_NOW, intervalTime);
dispatch_source_set_timer(timer, startTime, intervalTime, 0.25);
dispatch_resume(timer);
}
}
- (void)stopTimer
{
if (timer)
{
lastNumberOfBytesSent = 0;
lastNumberOfBytesReceived = 0;
smoothedAverageOutgoingBandwidth = 0.0;
smoothedAverageIncomingBandwidth = 0.0;
dispatch_source_cancel(timer);
#if !OS_OBJECT_USE_OBJC
dispatch_release(timer);
#endif
timer = NULL;
}
}
- (BOOL)activate:(XMPPStream *)aXmppStream
{
if ([super activate:aXmppStream])
{
if ([xmppStream isConnected])
{
[self startTimer];
}
return YES;
}
return NO;
}
- (void)deactivate
{
dispatch_block_t block = ^{ @autoreleasepool {
[self stopTimer];
[super deactivate];
}};
if (dispatch_get_specific(moduleQueueTag))
block();
else
dispatch_sync(moduleQueue, block);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#pragma mark XMPPStream Delegate
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)xmppStreamDidStartNegotiation:(XMPPStream *)sender;
{
[self startTimer];
}
- (void)xmppStreamDidDisconnect:(XMPPStream *)sender withError:(NSError *)error
{
[self stopTimer];
}
@end