diff --git a/Limelight/Network/AppManager.h b/Limelight/Network/AppAssetManager.h similarity index 92% rename from Limelight/Network/AppManager.h rename to Limelight/Network/AppAssetManager.h index e0d4fbaa..736d444d 100644 --- a/Limelight/Network/AppManager.h +++ b/Limelight/Network/AppAssetManager.h @@ -17,7 +17,7 @@ @end -@interface AppManager : NSObject +@interface AppAssetManager : NSObject - (id) initWithCallback:(id)callback; - (void) retrieveAssets:(NSArray*)appList fromHost:(Host*)host; diff --git a/Limelight/Network/AppAssetManager.m b/Limelight/Network/AppAssetManager.m new file mode 100644 index 00000000..d7510a0b --- /dev/null +++ b/Limelight/Network/AppAssetManager.m @@ -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 _callback; + Host* _host; + NSMutableDictionary* _imageCache; +} + +- (id) initWithCallback:(id)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 diff --git a/Limelight/Network/AppAssetRetriever.h b/Limelight/Network/AppAssetRetriever.h new file mode 100644 index 00000000..25301e1a --- /dev/null +++ b/Limelight/Network/AppAssetRetriever.h @@ -0,0 +1,21 @@ +// +// AppAssetRetriever.h +// Limelight +// +// Created by Diego Waxemberg on 1/31/15. +// Copyright (c) 2015 Limelight Stream. All rights reserved. +// + +#import +#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 callback; + +@end diff --git a/Limelight/Network/AppAssetRetriever.m b/Limelight/Network/AppAssetRetriever.m new file mode 100644 index 00000000..384772fa --- /dev/null +++ b/Limelight/Network/AppAssetRetriever.m @@ -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 diff --git a/Limelight/Network/AppManager.m b/Limelight/Network/AppManager.m deleted file mode 100644 index 6e24cf89..00000000 --- a/Limelight/Network/AppManager.m +++ /dev/null @@ -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 _callback; - Host* _host; - NSString* _uniqueId; - NSData* _cert; - NSMutableDictionary* _imageCache; -} - -- (id) initWithCallback:(id)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 diff --git a/Limelight/ViewControllers/MainFrameViewController.h b/Limelight/ViewControllers/MainFrameViewController.h index 48ab399a..cb40f214 100644 --- a/Limelight/ViewControllers/MainFrameViewController.h +++ b/Limelight/ViewControllers/MainFrameViewController.h @@ -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 diff --git a/Limelight/ViewControllers/MainFrameViewController.m b/Limelight/ViewControllers/MainFrameViewController.m index e21612de..4952c5b5 100644 --- a/Limelight/ViewControllers/MainFrameViewController.m +++ b/Limelight/ViewControllers/MainFrameViewController.m @@ -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) {