diff --git a/Limelight/Network/MDNSManager.m b/Limelight/Network/MDNSManager.m index ce28a799..0feb314c 100644 --- a/Limelight/Network/MDNSManager.m +++ b/Limelight/Network/MDNSManager.m @@ -9,10 +9,12 @@ #import "MDNSManager.h" #import "Computer.h" -@implementation MDNSManager : NSObject -NSNetServiceBrowser* mDNSBrowser; -NSMutableArray* domains; -NSMutableArray* services; +@implementation MDNSManager { + NSNetServiceBrowser* mDNSBrowser; + NSMutableArray* domains; + NSMutableArray* services; + BOOL scanActive; +} static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp"; @@ -32,11 +34,13 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp"; - (void) searchForHosts { NSLog(@"Starting mDNS discovery"); + scanActive = TRUE; [mDNSBrowser searchForServicesOfType:NV_SERVICE_TYPE inDomain:@""]; } - (void) stopSearching { NSLog(@"Stopping mDNS discovery"); + scanActive = FALSE; [mDNSBrowser stop]; } @@ -47,7 +51,7 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp"; [hosts addObject:[[Computer alloc] initWithHost:service]]; } } - return [[NSArray alloc] initWithArray:hosts]; + return hosts; } - (void)netServiceDidResolveAddress:(NSNetService *)service { @@ -74,6 +78,24 @@ static NSString* NV_SERVICE_TYPE = @"_nvstream._tcp"; - (void)netServiceBrowser:(NSNetServiceBrowser *)aNetServiceBrowser didNotSearch:(NSDictionary *)errorDict { NSLog(@"Did not perform search"); NSLog(@"%@", [errorDict description]); + + // Schedule a retry in 2 seconds + [NSTimer scheduledTimerWithTimeInterval:2.0 + target:self + selector:@selector(retryTimerCallback:) + userInfo:nil + repeats:NO]; +} + +- (void)retryTimerCallback:(NSTimer *)timer { + // Check if we've been stopped since this was queued + if (!scanActive) { + return; + } + + NSLog(@"Retrying mDNS search"); + [mDNSBrowser stop]; + [mDNSBrowser searchForServicesOfType:NV_SERVICE_TYPE inDomain:@""]; } @end \ No newline at end of file diff --git a/Limelight/Utility/Computer.h b/Limelight/Utility/Computer.h index 854a3dd4..dc0058cc 100644 --- a/Limelight/Utility/Computer.h +++ b/Limelight/Utility/Computer.h @@ -15,5 +15,6 @@ - (id) initWithHost:(NSNetService*)host; - (id) initWithIp:(NSString*)host; +- (id) initPlaceholder; @end diff --git a/Limelight/Utility/Computer.m b/Limelight/Utility/Computer.m index 6a7cb22b..fa8deb9b 100644 --- a/Limelight/Utility/Computer.m +++ b/Limelight/Utility/Computer.m @@ -28,4 +28,13 @@ return self; } +- (id) initPlaceholder { + self = [super init]; + + self.hostName = NULL; + self.displayName = @"No computers found"; + + return self; +} + @end diff --git a/Limelight/ViewControllers/MainFrameViewController.m b/Limelight/ViewControllers/MainFrameViewController.m index 07db5a6c..14a323b3 100644 --- a/Limelight/ViewControllers/MainFrameViewController.m +++ b/Limelight/ViewControllers/MainFrameViewController.m @@ -122,10 +122,19 @@ static StreamConfiguration* streamConfig; } } +- (void)setSelectedHost:(NSInteger)selectedIndex +{ + _selectedHost = (Computer*)([self.hostPickerVals objectAtIndex:selectedIndex]); + if (_selectedHost.hostName == NULL) { + // This must be the placeholder computer + _selectedHost = NULL; + } +} + - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component { if (pickerView == self.HostPicker) { - _selectedHost = (Computer*)([self.hostPickerVals objectAtIndex:[self.HostPicker selectedRowInComponent:0]]); + [self setSelectedHost:[self.HostPicker selectedRowInComponent:0]]; } //TODO: figure out how to save this info!! @@ -154,10 +163,12 @@ static StreamConfiguration* streamConfig; [super viewDidLoad]; self.streamConfigVals = [[NSArray alloc] initWithObjects:@"1280x720 (30Hz)", @"1280x720 (60Hz)", @"1920x1080 (30Hz)", @"1920x1080 (60Hz)",nil]; - self.hostPickerVals = [[NSArray alloc] init]; [self.StreamConfigs selectRow:1 inComponent:0 animated:NO]; _opQueue = [[NSOperationQueue alloc] init]; + + // Initialize the host picker list + [self updateHosts:[[NSArray alloc] init]]; } - (void)viewDidAppear:(BOOL)animated @@ -172,9 +183,16 @@ static StreamConfiguration* streamConfig; } - (void)updateHosts:(NSArray *)hosts { - self.hostPickerVals = hosts; + NSMutableArray *hostPickerValues = [[NSMutableArray alloc] initWithArray:hosts]; + + if ([hostPickerValues count] == 0) { + [hostPickerValues addObject:[[Computer alloc] initPlaceholder]]; + } + + self.hostPickerVals = hostPickerValues; [self.HostPicker reloadAllComponents]; - _selectedHost = (Computer*)([self.hostPickerVals objectAtIndex:[self.HostPicker selectedRowInComponent:0]]); + + [self setSelectedHost:[self.HostPicker selectedRowInComponent:0]]; } - (void)didReceiveMemoryWarning