diff --git a/Limelight/Network/DiscoveryManager.h b/Limelight/Network/DiscoveryManager.h index 8d330a7..34ab10d 100644 --- a/Limelight/Network/DiscoveryManager.h +++ b/Limelight/Network/DiscoveryManager.h @@ -21,6 +21,7 @@ - (id) initWithHosts:(NSArray*)hosts andCallback:(id) callback; - (void) startDiscovery; - (void) stopDiscovery; +- (void) stopDiscoveryBlocking; - (BOOL) addHostToDiscovery:(Host*)host; - (void) removeHostFromDiscovery:(Host*)host; - (void) discoverHost:(NSString*)hostAddress withCallback:(void (^)(Host*))callback; diff --git a/Limelight/Network/DiscoveryManager.m b/Limelight/Network/DiscoveryManager.m index 9694b86..f0ea805 100644 --- a/Limelight/Network/DiscoveryManager.m +++ b/Limelight/Network/DiscoveryManager.m @@ -69,8 +69,14 @@ [_mdnsMan stopSearching]; [_opQueue cancelAllOperations]; } + +- (void) stopDiscoveryBlocking { + NSLog(@"Stopping discovery and waiting for workers to stop"); shouldDiscover = NO; [_mdnsMan stopSearching]; + [_opQueue cancelAllOperations]; + [_opQueue waitUntilAllOperationsAreFinished]; + NSLog(@"All discovery workers stopped"); } - (BOOL) addHostToDiscovery:(Host *)host { diff --git a/Limelight/ViewControllers/MainFrameViewController.m b/Limelight/ViewControllers/MainFrameViewController.m index 76d9e31..a78e924 100644 --- a/Limelight/ViewControllers/MainFrameViewController.m +++ b/Limelight/ViewControllers/MainFrameViewController.m @@ -51,6 +51,7 @@ static StreamConfiguration* streamConfig; [_pairAlert dismissWithClickedButtonIndex:0 animated:NO]; _pairAlert = [[UIAlertView alloc] initWithTitle:@"Pairing Failed" message:message delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [_pairAlert show]; + [_discMan startDiscovery]; }); } @@ -59,6 +60,7 @@ static StreamConfiguration* streamConfig; [_pairAlert dismissWithClickedButtonIndex:0 animated:NO]; _pairAlert = [[UIAlertView alloc] initWithTitle:@"Pairing Succesful" message:@"Successfully paired to host" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil]; [_pairAlert show]; + [_discMan startDiscovery]; }); } @@ -107,6 +109,8 @@ static StreamConfiguration* streamConfig; [self alreadyPaired]; } else { NSLog(@"Trying to pair"); + // Polling the server while pairing causes the server to screw up + [_discMan stopDiscoveryBlocking]; PairManager* pMan = [[PairManager alloc] initWithManager:hMan andCert:_cert callback:self]; [_opQueue addOperation:pMan]; } @@ -322,9 +326,7 @@ static StreamConfiguration* streamConfig; [super viewDidDisappear:animated]; // when discovery stops, we must create a new instance because you cannot restart an NSOperation when it is finished [_discMan stopDiscovery]; - @synchronized(hostList) { - _discMan = [[DiscoveryManager alloc] initWithHosts:[hostList allObjects] andCallback:self]; - } + // In case the host objects were updated in the background [[[DataManager alloc] init] saveHosts]; }