diff --git a/Limelight/Network/DiscoveryManager.m b/Limelight/Network/DiscoveryManager.m index 39616db..6f667e5 100644 --- a/Limelight/Network/DiscoveryManager.m +++ b/Limelight/Network/DiscoveryManager.m @@ -105,12 +105,15 @@ DataManager* dataMan = [[DataManager alloc] init]; // Discover the hosts before adding to eliminate duplicates 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 - NSOperation* worker = [self createWorkerForHost:host]; - [worker main]; + DiscoveryWorker* worker = (DiscoveryWorker*)[self createWorkerForHost:host]; + [worker discoverHost]; if ([self addHostToDiscovery:host]) { + NSLog(@"Adding host to discovery: %@", host.name); [_callback updateAllHosts:_hostQueue]; } else { + NSLog(@"Not adding host to discovery: %@", host.name); [dataMan removeHost:host]; } } diff --git a/Limelight/Network/DiscoveryWorker.h b/Limelight/Network/DiscoveryWorker.h index 5c4ea82..7f5b6e1 100644 --- a/Limelight/Network/DiscoveryWorker.h +++ b/Limelight/Network/DiscoveryWorker.h @@ -12,6 +12,7 @@ @interface DiscoveryWorker : NSOperation - (id) initWithHost:(Host*)host uniqueId:(NSString*)uniqueId cert:(NSData*)cert; +- (void) discoverHost; - (Host*) getHost; + (void) updateHost:(Host*)host withServerInfo:(NSData*)serverInfoData; diff --git a/Limelight/Network/DiscoveryWorker.m b/Limelight/Network/DiscoveryWorker.m index cd4b2f1..01113fc 100644 --- a/Limelight/Network/DiscoveryWorker.m +++ b/Limelight/Network/DiscoveryWorker.m @@ -32,47 +32,51 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds - (void)main { while (!self.cancelled) { - BOOL receivedResponse = NO; - if (!self.cancelled && _host.localAddress != nil) { - HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.localAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert]; - NSData* serverInfoData = [hMan executeRequestSynchronously:[hMan newServerInfoRequest]]; - if ([[HttpManager getStatusStringFromXML:serverInfoData] isEqualToString:@"OK"]) { - [DiscoveryWorker updateHost:_host withServerInfo:serverInfoData]; - _host.address = _host.localAddress; - receivedResponse = YES; - } - } - if (!self.cancelled && !receivedResponse && _host.externalAddress != nil) { - HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.externalAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert]; - NSData* serverInfoData = [hMan executeRequestSynchronously:[hMan newServerInfoRequest]]; - if ([[HttpManager getStatusStringFromXML:serverInfoData] isEqualToString:@"OK"]) { - [DiscoveryWorker updateHost:_host withServerInfo:serverInfoData]; - _host.address = _host.externalAddress; - receivedResponse = YES; - } - } - - if (!self.cancelled && !receivedResponse && _host.address != nil) { - HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert]; - NSData* serverInfoData = [hMan executeRequestSynchronously:[hMan newServerInfoRequest]]; - if ([[HttpManager getStatusStringFromXML:serverInfoData] isEqualToString:@"OK"]) { - [DiscoveryWorker updateHost:_host withServerInfo:serverInfoData]; - receivedResponse = YES; - } - } - _host.online = receivedResponse; - if (receivedResponse) { - NSLog(@"Received response from: %@\n{\n\t address:%@ \n\t localAddress:%@ \n\t externalAddress:%@ \n\t uuid:%@ \n\t mac:%@ \n\t pairState:%d \n\t online:%d \n}", _host.name, _host.address, _host.localAddress, _host.externalAddress, _host.uuid, _host.mac, _host.pairState, _host.online); - } else { - // If the host is not online, we do not know the pairstate - _host.pairState = PairStateUnknown; - } + [self discoverHost]; if (!self.cancelled) { [NSThread sleepForTimeInterval:POLL_RATE]; } } } +- (void) discoverHost { + BOOL receivedResponse = NO; + if (!self.cancelled && _host.localAddress != nil) { + HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.localAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert]; + NSData* serverInfoData = [hMan executeRequestSynchronously:[hMan newServerInfoRequest]]; + if ([[HttpManager getStatusStringFromXML:serverInfoData] isEqualToString:@"OK"]) { + [DiscoveryWorker updateHost:_host withServerInfo:serverInfoData]; + _host.address = _host.localAddress; + receivedResponse = YES; + } + } + if (!self.cancelled && !receivedResponse && _host.externalAddress != nil) { + HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.externalAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert]; + NSData* serverInfoData = [hMan executeRequestSynchronously:[hMan newServerInfoRequest]]; + if ([[HttpManager getStatusStringFromXML:serverInfoData] isEqualToString:@"OK"]) { + [DiscoveryWorker updateHost:_host withServerInfo:serverInfoData]; + _host.address = _host.externalAddress; + receivedResponse = YES; + } + } + + if (!self.cancelled && !receivedResponse && _host.address != nil) { + HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert]; + NSData* serverInfoData = [hMan executeRequestSynchronously:[hMan newServerInfoRequest]]; + if ([[HttpManager getStatusStringFromXML:serverInfoData] isEqualToString:@"OK"]) { + [DiscoveryWorker updateHost:_host withServerInfo:serverInfoData]; + receivedResponse = YES; + } + } + _host.online = receivedResponse; + if (receivedResponse) { + NSLog(@"Received response from: %@\n{\n\t address:%@ \n\t localAddress:%@ \n\t externalAddress:%@ \n\t uuid:%@ \n\t mac:%@ \n\t pairState:%d \n\t online:%d \n}", _host.name, _host.address, _host.localAddress, _host.externalAddress, _host.uuid, _host.mac, _host.pairState, _host.online); + } else { + // If the host is not online, we do not know the pairstate + _host.pairState = PairStateUnknown; + } +} + + (void) updateHost:(Host*)host withServerInfo:(NSData*)serverInfoData { host.name = [HttpManager getStringFromXML:serverInfoData tag:@"hostname"]; host.externalAddress = [HttpManager getStringFromXML:serverInfoData tag:@"ExternalIP"];