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

This reverts commit fd9ee45daccb3d119218ba3dfb0a6b7f59e4121c.
This commit is contained in:
Cameron Gutman 2015-04-06 00:22:51 -04:00
parent 58e41dfcf1
commit 7767ae497b
4 changed files with 10 additions and 98 deletions

View File

@ -1,22 +0,0 @@
//
// 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

@ -1,54 +0,0 @@
//
// 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,7 +9,6 @@
#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>
@ -17,7 +16,7 @@
@end @end
@interface DiscoveryManager : NSObject <MDNSCallback, CleanupCallback> @interface DiscoveryManager : NSObject <MDNSCallback>
- (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* _hostList; NSMutableArray* _hostQueue;
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];
_hostList = [NSMutableArray arrayWithArray:hosts]; _hostQueue = [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,11 +66,9 @@
Log(LOG_I, @"Starting discovery"); Log(LOG_I, @"Starting discovery");
shouldDiscover = YES; shouldDiscover = YES;
[_mdnsMan searchForHosts]; [_mdnsMan searchForHosts];
for (Host* host in _hostList) { for (Host* host in _hostQueue) {
[_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 {
@ -90,8 +88,8 @@
} }
- (BOOL) addHostToDiscovery:(Host *)host { - (BOOL) addHostToDiscovery:(Host *)host {
if (host.uuid.length > 0 && ![self isHostInDiscovery:host]) { if (![self isHostInDiscovery:host]) {
[_hostList addObject:host]; [_hostQueue addObject:host];
if (shouldDiscover) { if (shouldDiscover) {
[_opQueue addOperation:[self createWorkerForHost:host]]; [_opQueue addOperation:[self createWorkerForHost:host]];
} }
@ -106,16 +104,7 @@
[worker cancel]; [worker cancel];
} }
} }
[_hostList removeObject:host]; [_hostQueue 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
@ -130,7 +119,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:_hostList]; [_callback updateAllHosts:_hostQueue];
} 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];
@ -141,8 +130,8 @@
} }
- (BOOL) isHostInDiscovery:(Host*)host { - (BOOL) isHostInDiscovery:(Host*)host {
for (int i = 0; i < _hostList.count; i++) { for (int i = 0; i < _hostQueue.count; i++) {
Host* discoveredHost = [_hostList objectAtIndex:i]; Host* discoveredHost = [_hostQueue 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;
} }