Skip to content

Commit

Permalink
Sort result list of devices on ip addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Jelle Alten authored and Jelle Alten committed May 29, 2017
1 parent 136952f commit d3ef500
Showing 1 changed file with 28 additions and 8 deletions.
36 changes: 28 additions & 8 deletions MMLanScanDemo/MMLanScanDemo/MainPresenter.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "LANProperties.h"
#import "MMLANScanner.h"
#import "MMDevice.h"
#include <arpa/inet.h>

@interface MainPresenter()<MMLANScannerDelegate>

Expand Down Expand Up @@ -60,7 +61,7 @@ -(void)startNetworkScan {

self.isScanRunning=YES;

connectedDevicesMutable = [[NSMutableArray alloc] init];
connectedDevicesMutable = [[NSMutableDictionary alloc] init];

[self.lanScanner start];
};
Expand All @@ -82,15 +83,34 @@ -(NSString*)ssidName {
#pragma mark - MMLANScannerDelegate methods
//The delegate methods of MMLANScanner
-(void)lanScanDidFindNewDevice:(MMDevice*)device{

//Check if the MMDevice is already added
if (![connectedDevicesMutable containsObject:device]) {

[connectedDevicesMutable addObject:device];
}

connectedDevicesMutable[device.ipAddress] = device;

//Updating the array that holds the data. MainVC will be notified by KVO
self.connectedDevices = [NSArray arrayWithArray:connectedDevicesMutable];
self.connectedDevices = [[NSArray arrayWithArray:connectedDevicesMutable.allValues] sortedArrayUsingComparator:^NSComparisonResult(MMDevice* _Nonnull obj1, MMDevice* _Nonnull obj2) {

return [self compareIP:obj1.ipAddress withIP:obj2.ipAddress];
}];
}

- (NSComparisonResult) compareIP:(NSString*) ip1 withIP:(NSString*) ip2 {

struct in_addr sin_addr1;
struct in_addr sin_addr2;

const char *ipCStr = [ip1 UTF8String];
inet_pton(AF_INET, ipCStr, &sin_addr1);

ipCStr = [ip2 UTF8String];
inet_pton(AF_INET, ipCStr, &sin_addr2);

if (sin_addr1.s_addr < sin_addr2.s_addr) {
return NSOrderedAscending;
} else if (sin_addr1.s_addr > sin_addr2.s_addr) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}

-(void)lanScanDidFinishScanningWithStatus:(MMLanScannerStatus)status{
Expand Down

0 comments on commit d3ef500

Please sign in to comment.