2015-01-31 20:10:14 -05:00
|
|
|
//
|
|
|
|
|
// AppAssetRetriever.m
|
2015-05-12 21:48:30 -04:00
|
|
|
// Moonlight
|
2015-01-31 20:10:14 -05:00
|
|
|
//
|
|
|
|
|
// Created by Diego Waxemberg on 1/31/15.
|
2015-05-12 21:48:30 -04:00
|
|
|
// Copyright (c) 2015 Moonlight Stream. All rights reserved.
|
2015-01-31 20:10:14 -05:00
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#import "AppAssetRetriever.h"
|
|
|
|
|
#import "HttpManager.h"
|
|
|
|
|
#import "CryptoManager.h"
|
2015-02-01 04:26:47 -05:00
|
|
|
#import "AppAssetResponse.h"
|
|
|
|
|
#import "HttpRequest.h"
|
2015-10-31 23:03:21 -07:00
|
|
|
#import "IdManager.h"
|
2015-01-31 20:10:14 -05:00
|
|
|
|
|
|
|
|
@implementation AppAssetRetriever
|
2015-02-10 16:13:06 -05:00
|
|
|
static const double RETRY_DELAY = 2; // seconds
|
|
|
|
|
static const int MAX_ATTEMPTS = 5;
|
2015-01-31 20:10:14 -05:00
|
|
|
|
|
|
|
|
- (void) main {
|
2015-02-10 16:13:06 -05:00
|
|
|
int attempts = 0;
|
2018-08-27 01:54:58 -07:00
|
|
|
while (![self isCancelled] && attempts++ < MAX_ATTEMPTS) {
|
2018-12-22 00:05:48 -08:00
|
|
|
HttpManager* hMan = [[HttpManager alloc] initWithHost:_host.activeAddress uniqueId:[IdManager getUniqueId] serverCert:_host.serverCert];
|
2015-07-10 21:22:57 -07:00
|
|
|
AppAssetResponse* appAssetResp = [[AppAssetResponse alloc] init];
|
|
|
|
|
[hMan executeRequestSynchronously:[HttpRequest requestForResponse:appAssetResp withUrlRequest:[hMan newAppAssetRequestWithAppId:self.app.id]]];
|
2018-03-27 08:50:40 +02:00
|
|
|
|
2018-08-27 01:54:58 -07:00
|
|
|
if (appAssetResp.data != nil) {
|
|
|
|
|
NSString* boxArtPath = [AppAssetManager boxArtPathForApp:self.app];
|
|
|
|
|
[[NSFileManager defaultManager] createDirectoryAtPath:[boxArtPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
|
|
|
|
|
[appAssetResp.data writeToFile:boxArtPath atomically:NO];
|
|
|
|
|
break;
|
|
|
|
|
}
|
2015-07-10 21:22:57 -07:00
|
|
|
|
2018-08-27 01:54:58 -07:00
|
|
|
if (![self isCancelled]) {
|
2015-07-12 00:43:20 -07:00
|
|
|
[NSThread sleepForTimeInterval:RETRY_DELAY];
|
|
|
|
|
}
|
2015-01-31 20:10:14 -05:00
|
|
|
}
|
|
|
|
|
[self performSelectorOnMainThread:@selector(sendCallbackForApp:) withObject:self.app waitUntilDone:NO];
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-01 20:31:24 -08:00
|
|
|
- (void) sendCallbackForApp:(TemporaryApp*)app {
|
2015-01-31 20:10:14 -05:00
|
|
|
[self.callback receivedAssetForApp:app];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|