Distinguish between offline and unknown hosts

This commit is contained in:
Cameron Gutman
2019-08-30 19:19:23 -07:00
parent a065c78b5f
commit 6d94897c21
7 changed files with 50 additions and 17 deletions
+1 -1
View File
@@ -11,7 +11,7 @@
@interface TemporaryHost : NSObject @interface TemporaryHost : NSObject
@property (nonatomic) BOOL online; @property (nonatomic) State state;
@property (nonatomic) PairState pairState; @property (nonatomic) PairState pairState;
@property (nonatomic, nullable) NSString * activeAddress; @property (nonatomic, nullable) NSString * activeAddress;
@property (nonatomic, nullable) NSString * currentGame; @property (nonatomic, nullable) NSString * currentGame;
+1
View File
@@ -16,6 +16,7 @@
self = [super init]; self = [super init];
self.appList = [[NSMutableSet alloc] init]; self.appList = [[NSMutableSet alloc] init];
self.currentGame = @"0"; self.currentGame = @"0";
self.state = StateUnknown;
return self; return self;
} }
+2 -2
View File
@@ -56,7 +56,7 @@
if ([serverInfoResponse isStatusOk]) { if ([serverInfoResponse isStatusOk]) {
host = [[TemporaryHost alloc] init]; host = [[TemporaryHost alloc] init];
host.activeAddress = host.address = hostAddress; host.activeAddress = host.address = hostAddress;
host.online = YES; host.state = StateOnline;
[serverInfoResponse populateHost:host]; [serverInfoResponse populateHost:host];
if (![self addHostToDiscovery:host]) { if (![self addHostToDiscovery:host]) {
callback(nil, @"Host information updated"); callback(nil, @"Host information updated");
@@ -137,7 +137,7 @@
existingHost.externalAddress = host.externalAddress; existingHost.externalAddress = host.externalAddress;
} }
existingHost.activeAddress = host.activeAddress; existingHost.activeAddress = host.activeAddress;
existingHost.online = host.online; existingHost.state = host.state;
return NO; return NO;
} }
else { else {
+2 -2
View File
@@ -118,9 +118,9 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds
} }
} }
_host.online = receivedResponse; _host.state = receivedResponse ? StateOnline : StateOffline;
if (receivedResponse) { if (receivedResponse) {
Log(LOG_D, @"Received response from: %@\n{\n\t address:%@ \n\t localAddress:%@ \n\t externalAddress:%@ \n\t ipv6Address:%@ \n\t uuid:%@ \n\t mac:%@ \n\t pairState:%d \n\t online:%d \n\t activeAddress:%@ \n}", _host.name, _host.address, _host.localAddress, _host.externalAddress, _host.ipv6Address, _host.uuid, _host.mac, _host.pairState, _host.online, _host.activeAddress); Log(LOG_D, @"Received response from: %@\n{\n\t address:%@ \n\t localAddress:%@ \n\t externalAddress:%@ \n\t ipv6Address:%@ \n\t uuid:%@ \n\t mac:%@ \n\t pairState:%d \n\t online:%d \n\t activeAddress:%@ \n}", _host.name, _host.address, _host.localAddress, _host.externalAddress, _host.ipv6Address, _host.uuid, _host.mac, _host.pairState, _host.state, _host.activeAddress);
} }
} }
+7 -5
View File
@@ -33,14 +33,14 @@ static const int LABEL_DY = 20;
_hostButton.layer.shadowColor = [[UIColor blackColor] CGColor]; _hostButton.layer.shadowColor = [[UIColor blackColor] CGColor];
_hostButton.layer.shadowOffset = CGSizeMake(5,8); _hostButton.layer.shadowOffset = CGSizeMake(5,8);
_hostButton.layer.shadowOpacity = 0.7; _hostButton.layer.shadowOpacity = 0.3;
_hostLabel = [[UILabel alloc] init]; _hostLabel = [[UILabel alloc] init];
#if !TARGET_OS_TV #if !TARGET_OS_TV
_hostLabel.textColor = [UIColor whiteColor]; _hostLabel.textColor = [UIColor whiteColor];
#endif #endif
_hostOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(_hostButton.frame.size.width / 4, _hostButton.frame.size.height / 6, _hostButton.frame.size.width / 2, _hostButton.frame.size.height / 2)]; _hostOverlay = [[UIImageView alloc] initWithFrame:CGRectMake(_hostButton.frame.size.width / 3, _hostButton.frame.size.height / 4, _hostButton.frame.size.width / 3, _hostButton.frame.size.height / 3)];
[self addSubview:_hostButton]; [self addSubview:_hostButton];
[self addSubview:_hostLabel]; [self addSubview:_hostLabel];
@@ -122,7 +122,7 @@ static const int LABEL_DY = 20;
_hostLabel.text = _host.name; _hostLabel.text = _host.name;
[_hostLabel sizeToFit]; [_hostLabel sizeToFit];
if (host.online) { if (host.state == StateOnline) {
if (host.pairState == PairStateUnpaired) { if (host.pairState == PairStateUnpaired) {
[_hostOverlay setImage:[UIImage imageNamed:@"LockedOverlayIcon"]]; [_hostOverlay setImage:[UIImage imageNamed:@"LockedOverlayIcon"]];
} }
@@ -130,10 +130,12 @@ static const int LABEL_DY = 20;
[_hostOverlay setImage:nil]; [_hostOverlay setImage:nil];
} }
} }
else { else if (host.state == StateOffline) {
// TODO: Use updating icon if we've not determined online state yet
[_hostOverlay setImage:[UIImage imageNamed:@"ErrorOverlayIcon"]]; [_hostOverlay setImage:[UIImage imageNamed:@"ErrorOverlayIcon"]];
} }
else {
[_hostOverlay setImage:[UIImage imageNamed:@"UpdatingOverlayIcon"]];
}
float x = _hostButton.frame.origin.x + _hostButton.frame.size.width / 2; float x = _hostButton.frame.origin.x + _hostButton.frame.size.width / 2;
_hostLabel.center = CGPointMake(x, _hostButton.frame.origin.y + _hostButton.frame.size.height + LABEL_DY); _hostLabel.center = CGPointMake(x, _hostButton.frame.origin.y + _hostButton.frame.size.height + LABEL_DY);
+6
View File
@@ -14,6 +14,12 @@ typedef NS_ENUM(int, PairState) {
PairStatePaired PairStatePaired
}; };
typedef NS_ENUM(int, State) {
StateUnknown,
StateOffline,
StateOnline
};
FOUNDATION_EXPORT NSString *const deviceName; FOUNDATION_EXPORT NSString *const deviceName;
+ (NSData*) randomBytes:(NSInteger)length; + (NSData*) randomBytes:(NSInteger)length;
@@ -162,7 +162,7 @@ static NSMutableSet* hostList;
[self hideLoadingFrame: ^{ [self hideLoadingFrame: ^{
[[self activeViewController] presentViewController:applistAlert animated:YES completion:nil]; [[self activeViewController] presentViewController:applistAlert animated:YES completion:nil];
}]; }];
host.online = NO; host.state = StateOffline;
[self showHostSelectionView]; [self showHostSelectionView];
}); });
} else { } else {
@@ -284,7 +284,7 @@ static NSMutableSet* hostList;
// This shows the context menu with wake, delete, etc. rather // This shows the context menu with wake, delete, etc. rather
// than just hanging for a while and failing as we would in this // than just hanging for a while and failing as we would in this
// code path. // code path.
if (!host.online && view != nil) { if (host.state != StateOnline && view != nil) {
[self hostLongClicked:host view:view]; [self hostLongClicked:host view:view];
return; return;
} }
@@ -303,7 +303,7 @@ static NSMutableSet* hostList;
// should hit most. Check for a valid view because we don't want to hit the fast // should hit most. Check for a valid view because we don't want to hit the fast
// path after coming back from streaming, since we need to fetch serverinfo too // path after coming back from streaming, since we need to fetch serverinfo too
// so that our active game data is correct. // so that our active game data is correct.
if (host.online && host.pairState == PairStatePaired && host.appList.count > 0 && view != nil) { if (host.state == StateOnline && host.pairState == PairStatePaired && host.appList.count > 0 && view != nil) {
[self alreadyPaired]; [self alreadyPaired];
return; return;
} }
@@ -341,7 +341,7 @@ static NSMutableSet* hostList;
} }
}]; }];
host.online = NO; host.state = StateOffline;
[self showHostSelectionView]; [self showHostSelectionView];
}); });
} else { } else {
@@ -384,8 +384,32 @@ static NSMutableSet* hostList;
- (void)hostLongClicked:(TemporaryHost *)host view:(UIView *)view { - (void)hostLongClicked:(TemporaryHost *)host view:(UIView *)view {
Log(LOG_D, @"Long clicked host: %@", host.name); Log(LOG_D, @"Long clicked host: %@", host.name);
UIAlertController* longClickAlert = [UIAlertController alertControllerWithTitle:host.name message:@"" preferredStyle:UIAlertControllerStyleActionSheet]; NSString* message;
if (!host.online) {
switch (host.state) {
case StateOffline:
message = @"Offline";
break;
case StateOnline:
if (host.pairState == PairStatePaired) {
message = @"Online - Paired";
}
else {
message = @"Online - Not Paired";
}
break;
case StateUnknown:
message = @"Connecting";
break;
default:
break;
}
UIAlertController* longClickAlert = [UIAlertController alertControllerWithTitle:host.name message:message preferredStyle:UIAlertControllerStyleActionSheet];
if (host.state != StateOnline) {
[longClickAlert addAction:[UIAlertAction actionWithTitle:@"Wake" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){ [longClickAlert addAction:[UIAlertAction actionWithTitle:@"Wake" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
UIAlertController* wolAlert = [UIAlertController alertControllerWithTitle:@"Wake On LAN" message:@"" preferredStyle:UIAlertControllerStyleAlert]; UIAlertController* wolAlert = [UIAlertController alertControllerWithTitle:@"Wake On LAN" message:@"" preferredStyle:UIAlertControllerStyleAlert];
[wolAlert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]]; [wolAlert addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil]];
@@ -879,7 +903,7 @@ static NSMutableSet* hostList;
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
Log(LOG_D, @"New host list:"); Log(LOG_D, @"New host list:");
for (TemporaryHost* host in hosts) { for (TemporaryHost* host in hosts) {
Log(LOG_D, @"Host: \n{\n\t name:%@ \n\t address:%@ \n\t localAddress:%@ \n\t externalAddress:%@ \n\t ipv6Address:%@ \n\t uuid:%@ \n\t mac:%@ \n\t pairState:%d \n\t online:%d \n\t activeAddress:%@ \n}", host.name, host.address, host.localAddress, host.externalAddress, host.ipv6Address, host.uuid, host.mac, host.pairState, host.online, host.activeAddress); Log(LOG_D, @"Host: \n{\n\t name:%@ \n\t address:%@ \n\t localAddress:%@ \n\t externalAddress:%@ \n\t ipv6Address:%@ \n\t uuid:%@ \n\t mac:%@ \n\t pairState:%d \n\t online:%d \n\t activeAddress:%@ \n}", host.name, host.address, host.localAddress, host.externalAddress, host.ipv6Address, host.uuid, host.mac, host.pairState, host.state, host.activeAddress);
} }
@synchronized(hostList) { @synchronized(hostList) {
[hostList removeAllObjects]; [hostList removeAllObjects];