-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use object to encapsule values from dict instead of accessing directl…
…y in desc callback
- Loading branch information
Showing
6 changed files
with
124 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#pragma once | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
@interface NCDataInfo : NSObject | ||
@property(nonatomic) NSNumber* tx; | ||
@property(nonatomic) NSNumber* txPackets; | ||
@property(nonatomic) NSNumber* txWiFi; | ||
@property(nonatomic) NSNumber* txCellular; | ||
@property(nonatomic) NSNumber* rx; | ||
@property(nonatomic) NSNumber* rxPackets; | ||
@property(nonatomic) NSNumber* rxWiFi; | ||
@property(nonatomic) NSNumber* rxCellular; | ||
- (instancetype)initWithDict:(NSDictionary*)dict; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#import "DataInfo.h" | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <NetworkStatistics/NetworkStatistics.h> | ||
|
||
@implementation NCDataInfo | ||
|
||
- (instancetype)initWithDict:(NSDictionary*)dict { | ||
self = [super init]; | ||
|
||
self.tx = dict[kNStatSrcKeyTxBytes]; | ||
self.txWiFi = dict[kNStatSrcKeyTxWiFiBytes]; | ||
self.txCellular = dict[kNStatSrcKeyTxCellularBytes]; | ||
|
||
self.rx = dict[kNStatSrcKeyRxBytes]; | ||
self.rxWiFi = dict[kNStatSrcKeyRxWiFiBytes]; | ||
self.rxCellular = dict[kNStatSrcKeyRxCellularBytes]; | ||
|
||
return self; | ||
} | ||
|
||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#pragma once | ||
|
||
#import <Foundation/Foundation.h> | ||
|
||
#include "DataInfo.h" | ||
|
||
@interface NCSourceInfo : NSObject | ||
@property(nonatomic) NSString* timeStamp; | ||
@property(nonatomic) NSString* processName; | ||
@property(nonatomic) NSString* protocol; | ||
@property(nonatomic) NSString* TCPState; | ||
@property(nonatomic) const struct sockaddr* localAddress; | ||
@property(nonatomic) const struct sockaddr* remoteAddress; | ||
@property(nonatomic) NSNumber* PID; | ||
@property(nonatomic) NCDataInfo* dataProcessed; | ||
- (instancetype)initWithDict:(NSDictionary*)dictionary; | ||
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "SourceInfo.h" | ||
|
||
#import <Foundation/Foundation.h> | ||
#import <NetworkStatistics/NetworkStatistics.h> | ||
|
||
#include "DataInfo.h" | ||
|
||
static BOOL isTCP(NSString*); | ||
static NSString* interfaceType(NSDictionary* sourceDict); | ||
|
||
@implementation NCSourceInfo | ||
- (instancetype)initWithDict:(NSDictionary*)dict { | ||
self = [super init]; | ||
|
||
NSDateFormatter* formatter = [[NSDateFormatter alloc] init]; | ||
[formatter setDateFormat:@"HH:mm:ss"]; | ||
|
||
self.timeStamp = [formatter stringFromDate:[NSDate date]]; | ||
self.protocol = dict[kNStatSrcKeyProvider]; | ||
self.TCPState = nil; | ||
self.localAddress = (const struct sockaddr*)[dict[kNStatSrcKeyLocal] bytes]; | ||
self.remoteAddress = | ||
(const struct sockaddr*)[dict[kNStatSrcKeyRemote] bytes]; | ||
|
||
NCDataInfo* dataInfo = [[NCDataInfo alloc] initWithDict:dict]; | ||
|
||
self.PID = dict[kNStatSrcKeyPID]; | ||
self.processName = dict[kNStatSrcKeyProcessName]; | ||
self.dataProcessed = dataInfo; | ||
|
||
if (isTCP(self.protocol)) { | ||
self.TCPState = dict[kNStatSrcKeyTCPState]; | ||
} | ||
|
||
return self; | ||
} | ||
@end | ||
|
||
static NSString* interfaceType(NSDictionary* sourceDict) { | ||
if (sourceDict[kNStatSrcKeyInterfaceTypeCellular]) { | ||
return @"Cellular"; | ||
} | ||
|
||
if (sourceDict[kNStatSrcKeyInterfaceTypeLoopback]) { | ||
return @"Loopback"; | ||
} | ||
|
||
if (sourceDict[kNStatSrcKeyInterfaceTypeUnknown]) { | ||
return @"Other"; | ||
} | ||
|
||
if (sourceDict[kNStatSrcKeyInterfaceTypeWiFi]) { | ||
return @"WiFi"; | ||
} | ||
|
||
if (sourceDict[kNStatSrcKeyInterfaceTypeWired]) { | ||
return @"Wired"; | ||
} | ||
|
||
return @"Other"; | ||
} | ||
|
||
static BOOL isTCP(NSString* provider) { | ||
return [provider isEqualToString:@"TCP"]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters