Enhance the connection callback protocol to include other connection listener callbacks exposed by common. Start handling some HTTP request errors.

This commit is contained in:
Cameron Gutman 2014-10-21 14:54:10 -04:00
parent 01a2853032
commit ea24b39fbe
7 changed files with 83 additions and 27 deletions

View File

@ -38,7 +38,8 @@ static const NSString* PORT = @"47984";
// Check root status_code // Check root status_code
if (![HttpManager verifyStatus: rootNode]) { if (![HttpManager verifyStatus: rootNode]) {
//TODO: handle error NSLog(@"ERROR: Request returned with failure status");
return NULL;
} }
// Skip the root node // Skip the root node
@ -229,6 +230,7 @@ static const NSString* PORT = @"47984";
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"connection error: %@", error); NSLog(@"connection error: %@", error);
dispatch_semaphore_signal(_requestLock);
} }
@end @end

View File

@ -10,15 +10,22 @@
#import "VideoDecoderRenderer.h" #import "VideoDecoderRenderer.h"
#import "StreamConfiguration.h" #import "StreamConfiguration.h"
@protocol ConTermCallback <NSObject> @protocol ConnectionCallbacks <NSObject>
- (void) connectionTerminated; - (void) connectionStarted;
- (void) connectionTerminated:(long)errorCode;
- (void) stageStarting:(char*)stageName;
- (void) stageComplete:(char*)stageName;
- (void) stageFailed:(char*)stageName withError:(long)errorCode;
- (void) launchFailed;
- (void) displayMessage:(char*)message;
- (void) displayTransientMessage:(char*)message;
@end @end
@interface Connection : NSOperation <NSStreamDelegate> @interface Connection : NSOperation <NSStreamDelegate>
-(id) initWithConfig:(StreamConfiguration*)config renderer:(VideoDecoderRenderer*)myRenderer connectionTerminatedCallback:(id<ConTermCallback>)callback; -(id) initWithConfig:(StreamConfiguration*)config renderer:(VideoDecoderRenderer*)myRenderer connectionCallbacks:(id<ConnectionCallbacks>)callbacks;
-(void) terminate; -(void) terminate;
-(void) main; -(void) main;

View File

@ -20,11 +20,10 @@
CONNECTION_LISTENER_CALLBACKS _clCallbacks; CONNECTION_LISTENER_CALLBACKS _clCallbacks;
DECODER_RENDERER_CALLBACKS _drCallbacks; DECODER_RENDERER_CALLBACKS _drCallbacks;
AUDIO_RENDERER_CALLBACKS _arCallbacks; AUDIO_RENDERER_CALLBACKS _arCallbacks;
} }
static OpusDecoder *opusDecoder; static OpusDecoder *opusDecoder;
static id<ConTermCallback> _callback; static id<ConnectionCallbacks> _callbacks;
#define PCM_BUFFER_SIZE 1024 #define PCM_BUFFER_SIZE 1024
#define OUTPUT_BUS 0 #define OUTPUT_BUS 0
@ -243,37 +242,37 @@ void ArDecodeAndPlaySample(char* sampleData, int sampleLength)
void ClStageStarting(int stage) void ClStageStarting(int stage)
{ {
[_callbacks stageStarting:(char*)LiGetStageName(stage)];
} }
void ClStageComplete(int stage) void ClStageComplete(int stage)
{ {
[_callbacks stageComplete:(char*)LiGetStageName(stage)];
} }
void ClStageFailed(int stage, long errorCode) void ClStageFailed(int stage, long errorCode)
{ {
printf("Stage %d failed: %ld\n", stage, errorCode); [_callbacks stageFailed:(char*)LiGetStageName(stage) withError:errorCode];
} }
void ClConnectionStarted(void) void ClConnectionStarted(void)
{ {
printf("Connection started\n"); [_callbacks connectionStarted];
} }
void ClConnectionTerminated(long errorCode) void ClConnectionTerminated(long errorCode)
{ {
printf("ConnectionTerminated: %ld\n", errorCode); [_callbacks connectionTerminated: errorCode];
[_callback connectionTerminated];
} }
void ClDisplayMessage(char* message) void ClDisplayMessage(char* message)
{ {
printf("DisplayMessage: %s\n", message); [_callbacks displayMessage: message];
} }
void ClDisplayTransientMessage(char* message) void ClDisplayTransientMessage(char* message)
{ {
printf("DisplayTransientMessage: %s\n", message); [_callbacks displayTransientMessage: message];
} }
-(void) terminate -(void) terminate
@ -286,12 +285,12 @@ void ClDisplayTransientMessage(char* message)
}); });
} }
-(id) initWithConfig:(StreamConfiguration*)config renderer:(VideoDecoderRenderer*)myRenderer connectionTerminatedCallback:(id<ConTermCallback>)callback -(id) initWithConfig:(StreamConfiguration*)config renderer:(VideoDecoderRenderer*)myRenderer connectionCallbacks:(id<ConnectionCallbacks>)callbacks
{ {
self = [super init]; self = [super init];
_host = config.hostAddr; _host = config.hostAddr;
renderer = myRenderer; renderer = myRenderer;
_callback = callback; _callbacks = callbacks;
_streamConfig.width = config.width; _streamConfig.width = config.width;
_streamConfig.height = config.height; _streamConfig.height = config.height;
_streamConfig.fps = config.frameRate; _streamConfig.fps = config.frameRate;

View File

@ -12,7 +12,7 @@
@interface StreamManager : NSOperation @interface StreamManager : NSOperation
- (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionTerminatedCallback:(id<ConTermCallback>)callback; - (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionCallbacks:(id<ConnectionCallbacks>)callback;
- (void) stopStream; - (void) stopStream;
@end @end

View File

@ -14,15 +14,15 @@
@implementation StreamManager { @implementation StreamManager {
StreamConfiguration* _config; StreamConfiguration* _config;
UIView* _renderView; UIView* _renderView;
id<ConTermCallback> _callback; id<ConnectionCallbacks> _callbacks;
Connection* _connection; Connection* _connection;
} }
- (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionTerminatedCallback:(id<ConTermCallback>)callback { - (id) initWithConfig:(StreamConfiguration*)config renderView:(UIView*)view connectionCallbacks:(id<ConnectionCallbacks>)callbacks {
self = [super init]; self = [super init];
_config = config; _config = config;
_renderView = view; _renderView = view;
_callback = callback; _callbacks = callbacks;
_config.riKey = [Utils randomBytes:16]; _config.riKey = [Utils randomBytes:16];
_config.riKeyId = arc4random(); _config.riKeyId = arc4random();
_config.bitRate = 10000; _config.bitRate = 10000;
@ -50,7 +50,7 @@
} }
VideoDecoderRenderer* renderer = [[VideoDecoderRenderer alloc]initWithView:_renderView]; VideoDecoderRenderer* renderer = [[VideoDecoderRenderer alloc]initWithView:_renderView];
_connection = [[Connection alloc] initWithConfig:_config renderer:renderer connectionTerminatedCallback:_callback]; _connection = [[Connection alloc] initWithConfig:_config renderer:renderer connectionCallbacks:_callbacks];
NSOperationQueue* opQueue = [[NSOperationQueue alloc] init]; NSOperationQueue* opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperation:_connection]; [opQueue addOperation:_connection];
} }
@ -60,7 +60,7 @@
[_connection terminate]; [_connection terminate];
} }
- (void) launchApp:(HttpManager*)hMan { - (BOOL) launchApp:(HttpManager*)hMan {
NSData* launchResp = [hMan executeRequestSynchronously: NSData* launchResp = [hMan executeRequestSynchronously:
[hMan newLaunchRequest:@"67339056" [hMan newLaunchRequest:@"67339056"
width:_config.width width:_config.width
@ -68,14 +68,24 @@
refreshRate:_config.frameRate refreshRate:_config.frameRate
rikey:[Utils bytesToHex:_config.riKey] rikey:[Utils bytesToHex:_config.riKey]
rikeyid:_config.riKeyId]]; rikeyid:_config.riKeyId]];
[HttpManager getStringFromXML:launchResp tag:@"gamesession"]; NSString *gameSession = [HttpManager getStringFromXML:launchResp tag:@"gamesession"];
if (gameSession == NULL || [gameSession isEqualToString:@"0"]) {
return FALSE;
} }
- (void) resumeApp:(HttpManager*)hMan { return TRUE;
}
- (BOOL) resumeApp:(HttpManager*)hMan {
NSData* resumeResp = [hMan executeRequestSynchronously: NSData* resumeResp = [hMan executeRequestSynchronously:
[hMan newResumeRequestWithRiKey:[Utils bytesToHex:_config.riKey] [hMan newResumeRequestWithRiKey:[Utils bytesToHex:_config.riKey]
riKeyId:_config.riKeyId]]; riKeyId:_config.riKeyId]];
[HttpManager getStringFromXML:resumeResp tag:@"gamesession"]; NSString *resume = [HttpManager getStringFromXML:resumeResp tag:@"resume"];
if (resume == NULL || [resume isEqualToString:@"0"]) {
return FALSE;
}
return TRUE;
} }
@end @end

View File

@ -10,6 +10,6 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
@interface StreamFrameViewController : UIViewController <ConTermCallback> @interface StreamFrameViewController : UIViewController <ConnectionCallbacks>
@end @end

View File

@ -29,7 +29,9 @@
_controllerSupport = [[ControllerSupport alloc] init]; _controllerSupport = [[ControllerSupport alloc] init];
_streamMan = [[StreamManager alloc] initWithConfig:[MainFrameViewController getStreamConfiguration] renderView:self.view connectionTerminatedCallback:self]; _streamMan = [[StreamManager alloc] initWithConfig:[MainFrameViewController getStreamConfiguration]
renderView:self.view
connectionCallbacks:self];
NSOperationQueue* opQueue = [[NSOperationQueue alloc] init]; NSOperationQueue* opQueue = [[NSOperationQueue alloc] init];
[opQueue addOperation:_streamMan]; [opQueue addOperation:_streamMan];
@ -44,7 +46,13 @@
[self performSegueWithIdentifier:@"returnToMainFrame" sender:self]; [self performSegueWithIdentifier:@"returnToMainFrame" sender:self];
} }
- (void)connectionTerminated { - (void) connectionStarted {
printf("Connection started\n");
}
- (void)connectionTerminated:(long)errorCode {
printf("Connection terminated: %ld\n", errorCode);
UIAlertController* conTermAlert = [UIAlertController alertControllerWithTitle:@"Connection Terminated" message:@"The connection terminated unexpectedly" preferredStyle:UIAlertControllerStyleAlert]; UIAlertController* conTermAlert = [UIAlertController alertControllerWithTitle:@"Connection Terminated" message:@"The connection terminated unexpectedly" preferredStyle:UIAlertControllerStyleAlert];
[conTermAlert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action){ [conTermAlert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action){
[self performSegueWithIdentifier:@"returnToMainFrame" sender:self]; [self performSegueWithIdentifier:@"returnToMainFrame" sender:self];
@ -54,6 +62,36 @@
[_streamMan stopStream]; [_streamMan stopStream];
} }
- (void) stageStarting:(char*)stageName {
printf("Starting %s\n", stageName);
}
- (void) stageComplete:(char*)stageName {
}
- (void) stageFailed:(char*)stageName withError:(long)errorCode {
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Connection Failed"
message:[NSString stringWithFormat:@"%s failed with error %ld",
stageName, errorCode]
preferredStyle:UIAlertControllerStyleAlert];
[alert addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action){
[self performSegueWithIdentifier:@"returnToMainFrame" sender:self];
}]];
[self presentViewController:alert animated:YES completion:nil];
}
- (void) launchFailed {
}
- (void) displayMessage:(char*)message {
printf("Display message: %s\n", message);
}
- (void) displayTransientMessage:(char*)message {
printf("Display transient message: %s\n", message);
}
- (void)didReceiveMemoryWarning - (void)didReceiveMemoryWarning
{ {
[super didReceiveMemoryWarning]; [super didReceiveMemoryWarning];