/
XRGNetView.m
397 lines (327 loc) · 14.5 KB
/
XRGNetView.m
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
* XRG (X Resource Graph): A system resource grapher for Mac OS X.
* Copyright (C) 2002-2016 Gaucho Software, LLC.
* You can view the complete license in the LICENSE file in the root
* of the source tree.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
*/
//
// XRGNetView.m
//
#import "XRGGraphWindow.h"
#import "XRGNetView.h"
#import "XRGCommon.h"
@implementation XRGNetView
- (void)awakeFromNib {
graphSize = NSMakeSize(90, 112);
self.miner = [[XRGNetMiner alloc] init];
self.fastMiner = [[XRGNetMiner alloc] init];
parentWindow = (XRGGraphWindow *)[self window];
[parentWindow setNetView:self];
[parentWindow initTimers];
appSettings = [parentWindow appSettings];
moduleManager = [parentWindow moduleManager];
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
m = [[XRGModule alloc] initWithName:@"Network" andReference:self];
m.doesFastUpdate = YES;
m.doesGraphUpdate = YES;
m.doesMin5Update = NO;
m.doesMin30Update = NO;
m.displayOrder = 5;
[self updateMinSize];
[m setIsDisplayed: (bool)[defs boolForKey:XRG_showNetworkGraph]];
[[parentWindow moduleManager] addModule:m];
[self setGraphSize:[m currentSize]];
}
- (void)graphUpdate:(NSTimer *)aTimer {
self.miner.monitorNetworkInterface = [appSettings networkInterface];
[self.miner getLatestNetInfo];
[self setNeedsDisplay: YES];
}
- (void)fastUpdate:(NSTimer *)aTimer {
if (self.bounds.size.height < XRG_MINI_HEIGHT * 2) {
self.fastMiner.monitorNetworkInterface = [appSettings networkInterface];
[self.fastMiner getLatestNetInfo];
fastTXValue = [XRGCommon dampedValueUsingPreviousValue:fastTXValue currentValue:self.fastMiner.currentTX];
fastRXValue = [XRGCommon dampedValueUsingPreviousValue:fastRXValue currentValue:self.fastMiner.currentRX];
[self setNeedsDisplay: YES];
}
}
- (void)setGraphSize:(NSSize)newSize {
NSSize tmpSize;
tmpSize.width = newSize.width;
tmpSize.height = newSize.height;
if (tmpSize.width < 1) tmpSize.width = 1;
if (tmpSize.width > 2000) tmpSize.width = 2000;
[self setWidth:tmpSize.width];
graphSize = tmpSize;
}
- (void)setWidth:(int)newWidth {
int newNumSamples = newWidth;
[self.miner setDataSize:newWidth];
[self.fastMiner setDataSize:newWidth];
numSamples = newNumSamples;
}
- (void)updateMinSize {
[m setMinWidth: [@"N1023K Rx" sizeWithAttributes:[appSettings alignRightAttributes]].width];
[m setMinHeight: XRG_MINI_HEIGHT];
}
- (void)drawRect:(NSRect)rect {
if ([self isHidden]) return;
#ifdef XRG_DEBUG
NSLog(@"In Network DrawRect.");
#endif
if (self.bounds.size.height < XRG_MINI_HEIGHT * 2) {
[self drawMiniGraph:self.bounds];
return;
}
NSGraphicsContext *gc = [NSGraphicsContext currentContext];
NSInteger max, tx, rx;
NSInteger textRectHeight = [appSettings textRectHeight];
max = MAX([self.miner maxBandwidth], [appSettings netMinGraphScale]);
[[appSettings graphBGColor] set];
NSRectFill([self bounds]);
NSRect tmpRect = NSMakeRect(0, 0, graphSize.width, textRectHeight * 2);
tmpRect.origin.x += 3;
tmpRect.size.width -= 6;
tmpRect.size.height = textRectHeight;
[gc setShouldAntialias:[appSettings antiAliasing]];
NSInteger netGraphMode = [appSettings netGraphMode];
/* received data */
if (netGraphMode == 0) {
[self drawGraphWithDataFromDataSet:self.miner.totalValues maxValue:[self.miner maxBandwidth] inRect:rect flipped:(netGraphMode == 2) filled:YES color:[appSettings graphFG2Color]];
}
else {
[self drawGraphWithDataFromDataSet:self.miner.rxValues maxValue:[self.miner maxBandwidth] inRect:rect flipped:(netGraphMode == 2) filled:YES color:[appSettings graphFG2Color]];
}
/* sent data */
[self drawGraphWithDataFromDataSet:self.miner.txValues maxValue:[self.miner maxBandwidth] inRect:rect flipped:(netGraphMode == 1) filled:YES color:[appSettings graphFG1Color]];
[gc setShouldAntialias:YES];
// draw the text
[gc setShouldAntialias:[appSettings antialiasText]];
NSMutableString *s = [[NSMutableString alloc] init];
if ([@"Net1023K Rx" sizeWithAttributes:[appSettings alignRightAttributes]].width + 6 > [self frame].size.width) {
[s appendFormat:@"N"];
}
else {
[s appendFormat:@"Net"];
}
//[s appendFormat:@"Net - %@", [appSettings networkInterface]];
tmpRect.origin.y = graphSize.height - textRectHeight;
// draw the scale if there is room
if (tmpRect.origin.y - textRectHeight > 0) {
tmpRect.origin.y -= textRectHeight;
tmpRect.size.height += textRectHeight;
if (max >= 1048576)
[s appendFormat:@"\n%3.2fM/s", ((CGFloat)max / 1048576.)];
else if (max >= 1024)
[s appendFormat:@"\n%4.1fK/s", ((CGFloat)max / 1024.)];
else
[s appendFormat:@"\n%ldB/s", (long)max];
}
// draw the total bandwidth used if there is room
if ([appSettings showTotalBandwidthSinceBoot]) {
if (tmpRect.origin.y - textRectHeight > 0) {
tmpRect.origin.y -= textRectHeight;
tmpRect.size.height += textRectHeight;
UInt64 totalBytesSinceBoot = self.miner.totalBytesSinceBoot;
if (totalBytesSinceBoot >= 1073741824)
[s appendFormat:@"\n%3.2fG", ((CGFloat)totalBytesSinceBoot / 1073741824.)];
else if (totalBytesSinceBoot >= 1048576)
[s appendFormat:@"\n%3.2fM", ((CGFloat)totalBytesSinceBoot / 1048576.)];
else if (totalBytesSinceBoot >= 1024)
[s appendFormat:@"\n%4.1fK", ((CGFloat)totalBytesSinceBoot / 1024.)];
else
[s appendFormat:@"\n%quB", totalBytesSinceBoot];
}
}
if ([appSettings showTotalBandwidthSinceLoad]) {
if (tmpRect.origin.y - textRectHeight > 0) {
tmpRect.origin.y -= textRectHeight;
tmpRect.size.height += textRectHeight;
UInt64 totalBytesSinceLoad = self.miner.totalBytesSinceLoad;
if (totalBytesSinceLoad >= 1073741824)
[s appendFormat:@"\n%3.2fG", ((CGFloat)totalBytesSinceLoad / 1073741824.)];
else if (totalBytesSinceLoad >= 1048576)
[s appendFormat:@"\n%3.2fM", ((CGFloat)totalBytesSinceLoad / 1048576.)];
else if (totalBytesSinceLoad >= 1024)
[s appendFormat:@"\n%4.1fK", ((CGFloat)totalBytesSinceLoad / 1024.)];
else
[s appendFormat:@"\n%quB", totalBytesSinceLoad];
}
}
[s drawInRect:tmpRect withAttributes:[appSettings alignLeftAttributes]];
if (netGraphMode == 0) {
tmpRect.origin.y = 0;
tmpRect.size.height = textRectHeight * 2;
[s setString:@""];
rx = [self.miner currentRX];
if (rx >= 104857600)
[s appendFormat:@"%3.1fM Rx", ((CGFloat)rx / 1048576.)];
else if (rx >= 1048576)
[s appendFormat:@"%3.2fM Rx", ((CGFloat)rx / 1048576.)];
else if (rx >= 102400)
[s appendFormat:@"%4.0fK Rx", ((CGFloat)rx / 1024.)];
else if (rx >= 1024)
[s appendFormat:@"%4.1fK Rx", ((CGFloat)rx / 1024.)];
else
[s appendFormat:@"%ldB Rx", (long)rx];
tx = [self.miner currentTX];
if (tx >= 104857600)
[s appendFormat:@"\n%3.1fM Tx", ((CGFloat)tx / 1048576.)];
else if (tx >= 1048576)
[s appendFormat:@"\n%3.2fM Tx", ((CGFloat)tx / 1048576.)];
else if (rx >= 102400)
[s appendFormat:@"\n%4.0fK Tx", ((CGFloat)tx / 1024.)];
else if (tx >= 1024)
[s appendFormat:@"\n%4.1fK Tx", ((CGFloat)tx / 1024.)];
else
[s appendFormat:@"\n%ldB Tx", (long)tx];
[s drawInRect:tmpRect withAttributes:[appSettings alignRightAttributes]];
}
else if (netGraphMode == 1) {
tmpRect.origin.y = 0;
tmpRect.size.height = textRectHeight;
[s setString:@""];
rx = [self.miner currentRX];
if (rx >= 104857600)
[s appendFormat:@"%3.1fM Rx", ((CGFloat)rx / 1048576.)];
else if (rx >= 1048576)
[s appendFormat:@"%3.2fM Rx", ((CGFloat)rx / 1048576.)];
else if (rx >= 102400)
[s appendFormat:@"%4.0fK Rx", ((CGFloat)rx / 1024.)];
else if (rx >= 1024)
[s appendFormat:@"%4.1fK Rx", ((CGFloat)rx / 1024.)];
else
[s appendFormat:@"%ldB Rx", (long)rx];
[s drawInRect:tmpRect withAttributes:[appSettings alignRightAttributes]];
tmpRect.origin.y = graphSize.height - textRectHeight;
[s setString:@""];
tx = [self.miner currentTX];
if (tx >= 104857600)
[s appendFormat:@"%3.1fM Tx", ((CGFloat)tx / 1048576.)];
else if (tx >= 1048576)
[s appendFormat:@"%3.2fM Tx", ((CGFloat)tx / 1048576.)];
else if (tx >= 102400)
[s appendFormat:@"%4.0fK Tx", ((CGFloat)tx / 1024.)];
else if (tx >= 1024)
[s appendFormat:@"%4.1fK Tx", ((CGFloat)tx / 1024.)];
else
[s appendFormat:@"%ldB Tx", (long)tx];
[s drawInRect:tmpRect withAttributes:[appSettings alignRightAttributes]];
}
else { // netGraphMode == 2
tmpRect.origin.y = 0;
tmpRect.size.height = textRectHeight;
[s setString:@""];
tx = [self.miner currentTX];
if (tx >= 104857600)
[s appendFormat:@"%3.1fM Tx", ((CGFloat)tx / 1048576.)];
else if (tx >= 1048576)
[s appendFormat:@"%3.2fM Tx", ((CGFloat)tx / 1048576.)];
else if (tx >= 102400)
[s appendFormat:@"%4.0fK Tx", ((CGFloat)tx / 1024.)];
else if (tx >= 1024)
[s appendFormat:@"%4.1fK Tx", ((CGFloat)tx / 1024.)];
else
[s appendFormat:@"%ldB Tx", (long)tx];
[s drawInRect:tmpRect withAttributes:[appSettings alignRightAttributes]];
tmpRect.origin.y = graphSize.height - textRectHeight;
[s setString:@""];
rx = [self.miner currentRX];
if (rx >= 104857600)
[s appendFormat:@"%3.1fM Rx", ((CGFloat)rx / 1048576.)];
else if (rx >= 1048576)
[s appendFormat:@"%3.2fM Rx", ((CGFloat)rx / 1048576.)];
else if (rx >= 102400)
[s appendFormat:@"%4.0fK Rx", ((CGFloat)rx / 1024.)];
else if (rx >= 1024)
[s appendFormat:@"%4.1fK Rx", ((CGFloat)rx / 1024.)];
else
[s appendFormat:@"%ldB Rx", (long)rx];
[s drawInRect:tmpRect withAttributes:[appSettings alignRightAttributes]];
}
[gc setShouldAntialias:YES];
}
- (void)drawMiniGraph:(NSRect)inRect {
NSString *leftLabel = nil;
if ([@"Net1023K Rx" sizeWithAttributes:[appSettings alignRightAttributes]].width + 6 > [self frame].size.width) {
leftLabel = @"N";
}
else {
leftLabel = @"Net";
}
NSInteger max = MAX([self.fastMiner maxBandwidth], [appSettings netMinGraphScale]);
if ([appSettings netGraphMode] == 1) { // Tx on top of Rx.
[self drawMiniGraphWithValues:@[@(fastTXValue), @(fastRXValue)] upperBound:max lowerBound:0 leftLabel:leftLabel printValueBytes:self.miner.totalValues.currentValue printValueIsRate:YES];
}
else { // Rx on top of Tx.
[self drawMiniGraphWithValues:@[@(fastRXValue), @(fastTXValue)] upperBound:max lowerBound:0 leftLabel:leftLabel printValueBytes:self.miner.totalValues.currentValue printValueIsRate:YES];
}
}
- (NSInteger)convertHeight:(NSInteger) yComponent {
return (yComponent >= 0 ? yComponent : 0) * (graphSize.height - ([appSettings textRectHeight] * 2)) / 100;
}
- (NSMenu *)menuForEvent:(NSEvent *)theEvent {
NSMenu *myMenu = [[NSMenu allocWithZone:[NSMenu menuZone]] initWithTitle:@"Network View"];
NSMenuItem *tMI;
tMI = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:@"Network Interface Traffic" action:@selector(emptyEvent:) keyEquivalent:@""];
[myMenu addItem:tMI];
for (NSInteger i = 0; i < self.miner.numInterfaces; i++) {
tMI = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:[NSString stringWithFormat:@"%s: RX(%1.1fM) TX(%1.1fM)", self.miner.interfaceStats[i].if_name, self.miner.interfaceStats[i].if_in.bytes / 1024. / 1024., self.miner.interfaceStats[i].if_out.bytes / 1024. / 1024.] action:@selector(emptyEvent:) keyEquivalent:@""];
[myMenu addItem:tMI];
}
[myMenu addItem:[NSMenuItem separatorItem]];
tMI = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:@"Open Network System Preferences..." action:@selector(openNetworkSystemPreferences:) keyEquivalent:@""];
[myMenu addItem:tMI];
tMI = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:@"Open Network Utility..." action:@selector(openNetworkUtility:) keyEquivalent:@""];
[myMenu addItem:tMI];
[myMenu addItem:[NSMenuItem separatorItem]];
tMI = [[NSMenuItem allocWithZone:[NSMenu menuZone]] initWithTitle:@"Open XRG Network Preferences..." action:@selector(openNetworkPreferences:) keyEquivalent:@""];
[myMenu addItem:tMI];
return myMenu;
}
- (void)emptyEvent:(NSEvent *)theEvent {
}
- (void)openNetworkSystemPreferences:(NSEvent *)theEvent {
[NSTask
launchedTaskWithLaunchPath:@"/usr/bin/open"
arguments:@[@"/System/Library/PreferencePanes/Network.prefPane"]
];
}
- (void)openNetworkUtility:(NSEvent *)theEvent {
[NSTask
launchedTaskWithLaunchPath:@"/usr/bin/open"
arguments:@[@"/Applications/Utilities/Network Utility.app"]
];
}
- (void)openNetworkPreferences:(NSEvent *)theEvent {
[[parentWindow controller] showPrefsWithPanel:@"Network"];
}
- (BOOL)acceptsFirstMouse:(NSEvent *)theEvent {
return YES;
}
- (void)mouseDown:(NSEvent *)theEvent {
[parentWindow mouseDown:theEvent];
}
- (void)mouseDragged:(NSEvent *)theEvent {
[parentWindow mouseDragged:theEvent];
}
- (void)mouseUp:(NSEvent *)theEvent {
[parentWindow mouseUp:theEvent];
}
@end