Added a hack to de-duplicate hosts and some more checks when adding a host to minimize the chances for dupes

This commit is contained in:
Diego Waxemberg 2015-04-05 20:08:21 -07:00
parent 40a04eb08b
commit fd9ee45dac
4 changed files with 98 additions and 10 deletions

View File

@ -0,0 +1,22 @@
//
// CleanupCrew.h
// Limelight
//
// Created by Diego Waxemberg on 4/5/15.
// Copyright (c) 2015 Limelight Stream. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Host.h"
@protocol CleanupCallback <NSObject>
- (void) cleanedUpHosts:(NSSet*) host;
@end
@interface CleanupCrew : NSOperation
- (id) initWithHostList:(NSArray*) hostList andCallback:(id<CleanupCallback>)callback;
@end

View File

@ -0,0 +1,54 @@
//
// CleanupCrew.m
// Limelight
//
// Created by Diego Waxemberg on 4/5/15.
// Copyright (c) 2015 Limelight Stream. All rights reserved.
//
#import "CleanupCrew.h"
@implementation CleanupCrew {
NSArray* _hostList;
id<CleanupCallback> _callback;
}
static const int CLEANUP_RATE = 2;
- (id) initWithHostList:(NSArray *)hostList andCallback:(id<CleanupCallback>)callback {
self = [super init];
_hostList = hostList;
_callback = callback;
return self;
}
- (void) main {
while (!self.isCancelled) {
NSMutableSet* dirtyHosts = [[NSMutableSet alloc] init];
for (Host* host in _hostList) {
for (Host* other in _hostList) {
if (self.isCancelled) {
break;
}
// We check the name as well as a hack to remove duplicates
if (host != other && ([host.uuid isEqualToString:other.uuid]
|| [host.name isEqualToString:other.name])) {
[dirtyHosts addObject:other];
}
}
}
if (dirtyHosts.count > 0) {
[_callback cleanedUpHosts:dirtyHosts];
}
if (!self.cancelled) {
[NSThread sleepForTimeInterval:CLEANUP_RATE];
}
}
}
@end

View File

@ -9,6 +9,7 @@
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
#import "MDNSManager.h" #import "MDNSManager.h"
#import "Host.h" #import "Host.h"
#import "CleanupCrew.h"
@protocol DiscoveryCallback <NSObject> @protocol DiscoveryCallback <NSObject>
@ -16,7 +17,7 @@
@end @end
@interface DiscoveryManager : NSObject <MDNSCallback> @interface DiscoveryManager : NSObject <MDNSCallback, CleanupCallback>
- (id) initWithHosts:(NSArray*)hosts andCallback:(id<DiscoveryCallback>) callback; - (id) initWithHosts:(NSArray*)hosts andCallback:(id<DiscoveryCallback>) callback;
- (void) startDiscovery; - (void) startDiscovery;

View File

@ -15,7 +15,7 @@
#import "ServerInfoResponse.h" #import "ServerInfoResponse.h"
@implementation DiscoveryManager { @implementation DiscoveryManager {
NSMutableArray* _hostQueue; NSMutableArray* _hostList;
NSMutableArray* _discoveredHosts; NSMutableArray* _discoveredHosts;
id<DiscoveryCallback> _callback; id<DiscoveryCallback> _callback;
MDNSManager* _mdnsMan; MDNSManager* _mdnsMan;
@ -27,7 +27,7 @@
- (id)initWithHosts:(NSArray *)hosts andCallback:(id<DiscoveryCallback>)callback { - (id)initWithHosts:(NSArray *)hosts andCallback:(id<DiscoveryCallback>)callback {
self = [super init]; self = [super init];
_hostQueue = [NSMutableArray arrayWithArray:hosts]; _hostList = [NSMutableArray arrayWithArray:hosts];
_callback = callback; _callback = callback;
_opQueue = [[NSOperationQueue alloc] init]; _opQueue = [[NSOperationQueue alloc] init];
_mdnsMan = [[MDNSManager alloc] initWithCallback:self]; _mdnsMan = [[MDNSManager alloc] initWithCallback:self];
@ -66,9 +66,11 @@
Log(LOG_I, @"Starting discovery"); Log(LOG_I, @"Starting discovery");
shouldDiscover = YES; shouldDiscover = YES;
[_mdnsMan searchForHosts]; [_mdnsMan searchForHosts];
for (Host* host in _hostQueue) { for (Host* host in _hostList) {
[_opQueue addOperation:[self createWorkerForHost:host]]; [_opQueue addOperation:[self createWorkerForHost:host]];
} }
CleanupCrew* cleanup = [[CleanupCrew alloc] initWithHostList:_hostList andCallback:self];
[_opQueue addOperation:cleanup];
} }
- (void) stopDiscovery { - (void) stopDiscovery {
@ -88,8 +90,8 @@
} }
- (BOOL) addHostToDiscovery:(Host *)host { - (BOOL) addHostToDiscovery:(Host *)host {
if (![self isHostInDiscovery:host]) { if (host.uuid.length > 0 && ![self isHostInDiscovery:host]) {
[_hostQueue addObject:host]; [_hostList addObject:host];
if (shouldDiscover) { if (shouldDiscover) {
[_opQueue addOperation:[self createWorkerForHost:host]]; [_opQueue addOperation:[self createWorkerForHost:host]];
} }
@ -104,7 +106,16 @@
[worker cancel]; [worker cancel];
} }
} }
[_hostQueue removeObject:host]; [_hostList removeObject:host];
}
// Override from CleanupCallback
- (void) cleanedUpHosts:(NSSet*)dirtyHosts {
for (Host* host in dirtyHosts) {
Log(LOG_I, @"Cleaning up duplicate host: %@", host.name);
[self removeHostFromDiscovery:host];
}
[_callback updateAllHosts:_hostList];
} }
// Override from MDNSCallback // Override from MDNSCallback
@ -119,7 +130,7 @@
[worker discoverHost]; [worker discoverHost];
if ([self addHostToDiscovery:host]) { if ([self addHostToDiscovery:host]) {
Log(LOG_I, @"Adding host to discovery: %@", host.name); Log(LOG_I, @"Adding host to discovery: %@", host.name);
[_callback updateAllHosts:_hostQueue]; [_callback updateAllHosts:_hostList];
} else { } else {
Log(LOG_I, @"Not adding host to discovery: %@", host.name); Log(LOG_I, @"Not adding host to discovery: %@", host.name);
[dataMan removeHost:host]; [dataMan removeHost:host];
@ -129,8 +140,8 @@
} }
- (BOOL) isHostInDiscovery:(Host*)host { - (BOOL) isHostInDiscovery:(Host*)host {
for (int i = 0; i < _hostQueue.count; i++) { for (int i = 0; i < _hostList.count; i++) {
Host* discoveredHost = [_hostQueue objectAtIndex:i]; Host* discoveredHost = [_hostList objectAtIndex:i];
if (discoveredHost.uuid.length > 0 && [discoveredHost.uuid isEqualToString:host.uuid]) { if (discoveredHost.uuid.length > 0 && [discoveredHost.uuid isEqualToString:host.uuid]) {
return YES; return YES;
} }