mirror of
https://github.com/moonlight-stream/moonlight-ios.git
synced 2026-06-15 21:21:45 +00:00
Host discovery is much better now
This commit is contained in:
@@ -24,6 +24,6 @@
|
|||||||
- (void) stopDiscoveryBlocking;
|
- (void) stopDiscoveryBlocking;
|
||||||
- (BOOL) addHostToDiscovery:(Host*)host;
|
- (BOOL) addHostToDiscovery:(Host*)host;
|
||||||
- (void) removeHostFromDiscovery:(Host*)host;
|
- (void) removeHostFromDiscovery:(Host*)host;
|
||||||
- (void) discoverHost:(NSString*)hostAddress withCallback:(void (^)(Host*))callback;
|
- (void) discoverHost:(NSString*)hostAddress withCallback:(void (^)(Host*, NSString*))callback;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -38,7 +38,7 @@
|
|||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) discoverHost:(NSString *)hostAddress withCallback:(void (^)(Host *))callback {
|
- (void) discoverHost:(NSString *)hostAddress withCallback:(void (^)(Host *, NSString*))callback {
|
||||||
HttpManager* hMan = [[HttpManager alloc] initWithHost:hostAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert];
|
HttpManager* hMan = [[HttpManager alloc] initWithHost:hostAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert];
|
||||||
ServerInfoResponse* serverInfoResponse = [[ServerInfoResponse alloc] init];
|
ServerInfoResponse* serverInfoResponse = [[ServerInfoResponse alloc] init];
|
||||||
[hMan executeRequestSynchronously:[HttpRequest requestForResponse:serverInfoResponse withUrlRequest:[hMan newServerInfoRequest]]];
|
[hMan executeRequestSynchronously:[HttpRequest requestForResponse:serverInfoResponse withUrlRequest:[hMan newServerInfoRequest]]];
|
||||||
@@ -52,9 +52,14 @@
|
|||||||
[serverInfoResponse populateHost:host];
|
[serverInfoResponse populateHost:host];
|
||||||
if (![self addHostToDiscovery:host]) {
|
if (![self addHostToDiscovery:host]) {
|
||||||
[dataMan removeHost:host];
|
[dataMan removeHost:host];
|
||||||
|
callback(nil, @"Host already added");
|
||||||
|
} else {
|
||||||
|
callback(host, nil);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
callback(nil, @"Could not connect to host");
|
||||||
}
|
}
|
||||||
callback(host);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void) startDiscovery {
|
- (void) startDiscovery {
|
||||||
|
|||||||
@@ -44,35 +44,16 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds
|
|||||||
- (void) discoverHost {
|
- (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];
|
ServerInfoResponse* serverInfoResp = [self requestInfoAtAddress:_host.localAddress];
|
||||||
|
receivedResponse = [self checkResponse:serverInfoResp];
|
||||||
ServerInfoResponse* serverInfoResp = [[ServerInfoResponse alloc] init];
|
|
||||||
[hMan executeRequestSynchronously:[HttpRequest requestForResponse:serverInfoResp withUrlRequest:[hMan newServerInfoRequest]]];
|
|
||||||
if ([serverInfoResp isStatusOk]) {
|
|
||||||
[serverInfoResp populateHost:_host];
|
|
||||||
_host.address = _host.localAddress;
|
|
||||||
receivedResponse = YES;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (!self.cancelled && !receivedResponse && _host.externalAddress != nil) {
|
if (!self.cancelled && !receivedResponse && _host.externalAddress != nil) {
|
||||||
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.externalAddress uniqueId:_uniqueId deviceName:deviceName cert:_cert];
|
ServerInfoResponse* serverInfoResp = [self requestInfoAtAddress:_host.externalAddress];
|
||||||
ServerInfoResponse* serverInfoResp = [[ServerInfoResponse alloc]init];
|
receivedResponse = [self checkResponse:serverInfoResp];
|
||||||
[hMan executeRequestSynchronously:[HttpRequest requestForResponse:serverInfoResp withUrlRequest:[hMan newServerInfoRequest]]];
|
|
||||||
if ([serverInfoResp isStatusOk]) {
|
|
||||||
[serverInfoResp populateHost:_host];
|
|
||||||
_host.address = _host.externalAddress;
|
|
||||||
receivedResponse = YES;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!self.cancelled && !receivedResponse && _host.address != nil) {
|
if (!self.cancelled && !receivedResponse && _host.address != nil) {
|
||||||
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert];
|
ServerInfoResponse* serverInfoResp = [self requestInfoAtAddress:_host.address];
|
||||||
ServerInfoResponse* serverInfoResp = [[ServerInfoResponse alloc] init];
|
receivedResponse = [self checkResponse:serverInfoResp];
|
||||||
[hMan executeRequestSynchronously:[HttpRequest requestForResponse:serverInfoResp withUrlRequest:[hMan newServerInfoRequest]]];
|
|
||||||
if ([serverInfoResp isStatusOk]) {
|
|
||||||
[serverInfoResp populateHost:_host];
|
|
||||||
receivedResponse = YES;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_host.online = receivedResponse;
|
_host.online = receivedResponse;
|
||||||
if (receivedResponse) {
|
if (receivedResponse) {
|
||||||
@@ -80,4 +61,28 @@ static const float POLL_RATE = 2.0f; // Poll every 2 seconds
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (ServerInfoResponse*) requestInfoAtAddress:(NSString*)address {
|
||||||
|
HttpManager* hMan = [[HttpManager alloc] initWithHost:address
|
||||||
|
uniqueId:_uniqueId
|
||||||
|
deviceName:deviceName
|
||||||
|
cert:_cert];
|
||||||
|
ServerInfoResponse* response = [[ServerInfoResponse alloc] init];
|
||||||
|
[hMan executeRequestSynchronously:[HttpRequest requestForResponse:response
|
||||||
|
withUrlRequest:[hMan newServerInfoRequest]]];
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL) checkResponse:(ServerInfoResponse*)response {
|
||||||
|
if ([response isStatusOk]) {
|
||||||
|
// If the response is from a different host then do not update this host
|
||||||
|
if ((_host.uuid == nil || [[response getStringTag:TAG_UNIQUE_ID] isEqualToString:_host.uuid])) {
|
||||||
|
[response populateHost:_host];
|
||||||
|
return YES;
|
||||||
|
} else {
|
||||||
|
NSLog(@"Received response from incorrect host: %@ expected: %@", [response getStringTag:TAG_UNIQUE_ID], _host.uuid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@@ -8,9 +8,17 @@
|
|||||||
|
|
||||||
#import "HttpResponse.h"
|
#import "HttpResponse.h"
|
||||||
|
|
||||||
|
#define TAG_HOSTNAME @"hostname"
|
||||||
|
#define TAG_EXTERNAL_IP @"ExternalIP"
|
||||||
|
#define TAG_LOCAL_IP @"LocalIP"
|
||||||
|
#define TAG_UNIQUE_ID @"uniqueid"
|
||||||
|
#define TAG_MAC_ADDRESS @"mac"
|
||||||
|
#define TAG_PAIR_STATUS @"PairStatus"
|
||||||
|
|
||||||
@interface ServerInfoResponse : HttpResponse <Response>
|
@interface ServerInfoResponse : HttpResponse <Response>
|
||||||
|
|
||||||
- (void) populateWithData:(NSData *)data;
|
- (void) populateWithData:(NSData *)data;
|
||||||
- (void) populateHost:(Host*)host;
|
- (void) populateHost:(Host*)host;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
|||||||
@@ -12,13 +12,6 @@
|
|||||||
@implementation ServerInfoResponse
|
@implementation ServerInfoResponse
|
||||||
@synthesize data, statusCode, statusMessage;
|
@synthesize data, statusCode, statusMessage;
|
||||||
|
|
||||||
static NSString* TAG_HOSTNAME = @"hostname";
|
|
||||||
static NSString* TAG_EXTERNAL_IP = @"ExternalIP";
|
|
||||||
static NSString* TAG_LOCAL_IP = @"LocalIP";
|
|
||||||
static NSString* TAG_UNIQUE_ID = @"uniqueid";
|
|
||||||
static NSString* TAG_MAC_ADDRESS = @"mac";
|
|
||||||
static NSString* TAG_PAIR_STATUS = @"PairStatus";
|
|
||||||
|
|
||||||
- (void) populateWithData:(NSData *)xml {
|
- (void) populateWithData:(NSData *)xml {
|
||||||
self.data = xml;
|
self.data = xml;
|
||||||
[super parseData];
|
[super parseData];
|
||||||
|
|||||||
@@ -190,7 +190,7 @@ static NSArray* appList;
|
|||||||
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
|
[alertController addAction:[UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction* action){
|
||||||
NSString* hostAddress = ((UITextField*)[[alertController textFields] objectAtIndex:0]).text;
|
NSString* hostAddress = ((UITextField*)[[alertController textFields] objectAtIndex:0]).text;
|
||||||
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
|
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
|
||||||
[_discMan discoverHost:hostAddress withCallback:^(Host* host){
|
[_discMan discoverHost:hostAddress withCallback:^(Host* host, NSString* error){
|
||||||
if (host != nil) {
|
if (host != nil) {
|
||||||
DataManager* dataMan = [[DataManager alloc] init];
|
DataManager* dataMan = [[DataManager alloc] init];
|
||||||
[dataMan saveHosts];
|
[dataMan saveHosts];
|
||||||
@@ -201,7 +201,7 @@ static NSArray* appList;
|
|||||||
[self updateHosts];
|
[self updateHosts];
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
UIAlertController* hostNotFoundAlert = [UIAlertController alertControllerWithTitle:@"Host Not Found" message:[NSString stringWithFormat:@"Unable to connect to host: \n%@", hostAddress] preferredStyle:UIAlertControllerStyleAlert];
|
UIAlertController* hostNotFoundAlert = [UIAlertController alertControllerWithTitle:@"Add Host" message:error preferredStyle:UIAlertControllerStyleAlert];
|
||||||
[hostNotFoundAlert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDestructive handler:nil]];
|
[hostNotFoundAlert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDestructive handler:nil]];
|
||||||
dispatch_async(dispatch_get_main_queue(), ^{
|
dispatch_async(dispatch_get_main_queue(), ^{
|
||||||
[self presentViewController:hostNotFoundAlert animated:YES completion:nil];
|
[self presentViewController:hostNotFoundAlert animated:YES completion:nil];
|
||||||
|
|||||||
Reference in New Issue
Block a user