improved app asset management and retrying to acquire

This commit is contained in:
Diego Waxemberg 2015-01-31 20:10:14 -05:00
parent 8e721a9c6a
commit 14f30349a3
7 changed files with 135 additions and 86 deletions

View File

@ -17,7 +17,7 @@
@end
@interface AppManager : NSObject
@interface AppAssetManager : NSObject
- (id) initWithCallback:(id<AppAssetCallback>)callback;
- (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host;

View File

@ -0,0 +1,57 @@
//
// AppManager.m
// Limelight
//
// Created by Diego Waxemberg on 10/25/14.
// Copyright (c) 2014 Limelight Stream. All rights reserved.
//
#import "AppAssetManager.h"
#import "CryptoManager.h"
#import "Utils.h"
#import "HttpResponse.h"
#import "AppAssetRetriever.h"
@implementation AppAssetManager {
NSOperationQueue* _opQueue;
id<AppAssetCallback> _callback;
Host* _host;
NSMutableDictionary* _imageCache;
}
- (id) initWithCallback:(id<AppAssetCallback>)callback {
self = [super init];
_callback = callback;
_opQueue = [[NSOperationQueue alloc] init];
_imageCache = [[NSMutableDictionary alloc] init];
return self;
}
- (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host {
Host* oldHost = _host;
_host = host;
BOOL useCache = [oldHost.uuid isEqualToString:_host.uuid];
NSLog(@"Using cached app images: %d", useCache);
if (!useCache) {
[_imageCache removeAllObjects];
}
for (App* app in appList) {
AppAssetRetriever* retriever = [[AppAssetRetriever alloc] init];
retriever.app = app;
retriever.host = _host;
retriever.callback = _callback;
retriever.cache = useCache ? _imageCache : nil;
[_opQueue addOperation:retriever];
}
}
- (void) stopRetrieving {
[_opQueue cancelAllOperations];
}
- (void) sendCallBackForApp:(App*)app {
[_callback receivedAssetForApp:app];
}
@end

View File

@ -0,0 +1,21 @@
//
// AppAssetRetriever.h
// Limelight
//
// Created by Diego Waxemberg on 1/31/15.
// Copyright (c) 2015 Limelight Stream. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Host.h"
#import "App.h"
#import "AppAssetManager.h"
@interface AppAssetRetriever : NSOperation
@property (nonatomic) Host* host;
@property (nonatomic) App* app;
@property (nonatomic) NSMutableDictionary* cache;
@property (nonatomic) id<AppAssetCallback> callback;
@end

View File

@ -0,0 +1,52 @@
//
// AppAssetRetriever.m
// Limelight
//
// Created by Diego Waxemberg on 1/31/15.
// Copyright (c) 2015 Limelight Stream. All rights reserved.
//
#import "AppAssetRetriever.h"
#import "HttpManager.h"
#import "CryptoManager.h"
#import "HttpResponse.h"
@implementation AppAssetRetriever
static const double RETRY_DELAY = 1; // seconds
- (void) main {
UIImage* appImage = nil;
while (![self isCancelled] && appImage == nil) {
if (self.cache) {
@synchronized(self.cache) {
UIImage* cachedImage = [self.cache objectForKey:self.app.appId];
if (cachedImage != nil) {
appImage = cachedImage;
self.app.appImage = appImage;
}
}
}
if (appImage == nil) {
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:[CryptoManager getUniqueID] deviceName:deviceName cert:[CryptoManager readCertFromFile]];
HttpResponse* appAssetResp = [hMan executeRequestSynchronously:[hMan newAppAssetRequestWithAppId:self.app.appId]];
appImage = [UIImage imageWithData:appAssetResp.responseData];
self.app.appImage = appImage;
if (appImage != nil) {
@synchronized(self.cache) {
[self.cache setObject:appImage forKey:self.app.appId];
}
}
}
[NSThread sleepForTimeInterval:RETRY_DELAY];
}
[self performSelectorOnMainThread:@selector(sendCallbackForApp:) withObject:self.app waitUntilDone:NO];
}
- (void) sendCallbackForApp:(App*)app {
[self.callback receivedAssetForApp:app];
}
@end

View File

@ -1,81 +0,0 @@
//
// AppManager.m
// Limelight
//
// Created by Diego Waxemberg on 10/25/14.
// Copyright (c) 2014 Limelight Stream. All rights reserved.
//
#import "AppManager.h"
#import "CryptoManager.h"
#import "Utils.h"
#import "HttpResponse.h"
@implementation AppManager {
NSOperationQueue* _opQueue;
id<AppAssetCallback> _callback;
Host* _host;
NSString* _uniqueId;
NSData* _cert;
NSMutableDictionary* _imageCache;
}
- (id) initWithCallback:(id<AppAssetCallback>)callback {
self = [super init];
_callback = callback;
_opQueue = [[NSOperationQueue alloc] init];
_imageCache = [[NSMutableDictionary alloc] init];
_uniqueId = [CryptoManager getUniqueID];
return self;
}
- (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host {
Host* oldHost = _host;
_host = host;
BOOL useCache = [oldHost.uuid isEqualToString:_host.uuid];
NSLog(@"Using cached app images: %d", useCache);
if (!useCache) {
[_imageCache removeAllObjects];
}
for (App* app in appList) {
[_opQueue addOperationWithBlock:^{
[self retrieveAssetForApp:app useCache:useCache];
}];
}
}
- (void) stopRetrieving {
[_opQueue cancelAllOperations];
}
- (void) retrieveAssetForApp:(App*)app useCache:(BOOL)useCache {
UIImage* appImage = nil;
if (useCache) {
@synchronized(_imageCache) {
UIImage* cachedImage = [_imageCache objectForKey:app.appId];
if (cachedImage != nil) {
appImage = cachedImage;
app.appImage = appImage;
}
}
}
if (appImage == nil) {
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.address uniqueId:_uniqueId deviceName:deviceName cert:_cert];
HttpResponse* appAssetResp = [hMan executeRequestSynchronously:[hMan newAppAssetRequestWithAppId:app.appId]];
appImage = [UIImage imageWithData:appAssetResp.responseData];
app.appImage = appImage;
if (appImage != nil) {
@synchronized(_imageCache) {
[_imageCache setObject:appImage forKey:app.appId];
}
}
}
[self performSelectorOnMainThread:@selector(sendCallBackForApp:) withObject:app waitUntilDone:NO];
}
- (void) sendCallBackForApp:(App*)app {
[_callback receivedAssetForApp:app];
}
@end

View File

@ -12,7 +12,7 @@
#import "StreamConfiguration.h"
#import "UIComputerView.h"
#import "UIAppView.h"
#import "AppManager.h"
#import "AppAssetManager.h"
#import "SWRevealViewController.h"
@interface MainFrameViewController : UICollectionViewController <DiscoveryCallback, PairCallback, HostCallback, AppCallback, AppAssetCallback, NSURLConnectionDelegate, SWRevealViewControllerDelegate>

View File

@ -27,7 +27,7 @@
NSData* _cert;
NSString* _currentGame;
DiscoveryManager* _discMan;
AppManager* _appManager;
AppAssetManager* _appManager;
UIAlertView* _pairAlert;
UIScrollView* hostScrollView;
int currentPosition;
@ -311,8 +311,8 @@ static StreamConfiguration* streamConfig;
[CryptoManager generateKeyPairUsingSSl];
_uniqueId = [CryptoManager getUniqueID];
_cert = [CryptoManager readCertFromFile];
_appManager = [[AppManager alloc] initWithCallback:self];
_appManager = [[AppAssetManager alloc] initWithCallback:self];
// Only initialize the host picker list once
if (hostList == nil) {