fixed MDNS discovered hosts getting stuck in infinite loop

This commit is contained in:
Diego Waxemberg 2015-01-08 17:03:51 -05:00
parent 94d399e275
commit fe64d70577
3 changed files with 45 additions and 37 deletions

View File

@ -105,12 +105,15 @@
DataManager* dataMan = [[DataManager alloc] init]; DataManager* dataMan = [[DataManager alloc] init];
// Discover the hosts before adding to eliminate duplicates // Discover the hosts before adding to eliminate duplicates
for (Host* host in hosts) { for (Host* host in hosts) {
NSLog(@"Found host through MDNS: %@:", host.name);
// Since this is on a background thread, we do not need to use the opQueue // Since this is on a background thread, we do not need to use the opQueue
NSOperation* worker = [self createWorkerForHost:host]; DiscoveryWorker* worker = (DiscoveryWorker*)[self createWorkerForHost:host];
[worker main]; [worker discoverHost];
if ([self addHostToDiscovery:host]) { if ([self addHostToDiscovery:host]) {
NSLog(@"Adding host to discovery: %@", host.name);
[_callback updateAllHosts:_hostQueue]; [_callback updateAllHosts:_hostQueue];
} else { } else {
NSLog(@"Not adding host to discovery: %@", host.name);
[dataMan removeHost:host]; [dataMan removeHost:host];
} }
} }

View File

@ -12,6 +12,7 @@
@interface DiscoveryWorker : NSOperation @interface DiscoveryWorker : NSOperation
- (id) initWithHost:(Host*)host uniqueId:(NSString*)uniqueId cert:(NSData*)cert; - (id) initWithHost:(Host*)host uniqueId:(NSString*)uniqueId cert:(NSData*)cert;
- (void) discoverHost;
- (Host*) getHost; - (Host*) getHost;
+ (void) updateHost:(Host*)host withServerInfo:(NSData*)serverInfoData; + (void) updateHost:(Host*)host withServerInfo:(NSData*)serverInfoData;

View File

@ -32,6 +32,14 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds
- (void)main { - (void)main {
while (!self.cancelled) { while (!self.cancelled) {
[self discoverHost];
if (!self.cancelled) {
[NSThread sleepForTimeInterval:POLL_RATE];
}
}
}
- (void) discoverHost {
BOOL receivedResponse = NO; BOOL receivedResponse = NO;
if (!self.cancelled && _host.localAddress != nil) { if (!self.cancelled && _host.localAddress != nil) {
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.localAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert]; HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.localAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert];
@ -67,10 +75,6 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds
// If the host is not online, we do not know the pairstate // If the host is not online, we do not know the pairstate
_host.pairState = PairStateUnknown; _host.pairState = PairStateUnknown;
} }
if (!self.cancelled) {
[NSThread sleepForTimeInterval:POLL_RATE];
}
}
} }
+ (void) updateHost:(Host*)host withServerInfo:(NSData*)serverInfoData { + (void) updateHost:(Host*)host withServerInfo:(NSData*)serverInfoData {