Commit b28b9b4e authored by Felix Paul Kühne's avatar Felix Paul Kühne

local network: refactor shared library code to never block the main thread

parent 9135e2e8
...@@ -74,13 +74,7 @@ ...@@ -74,13 +74,7 @@
- (void)dealloc - (void)dealloc
{ {
[[NSNotificationCenter defaultCenter] removeObserver:self [[NSNotificationCenter defaultCenter] removeObserver:self];
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
[_reachability stopNotifier]; [_reachability stopNotifier];
[_ftpNetServiceBrowser stop]; [_ftpNetServiceBrowser stop];
...@@ -124,15 +118,22 @@ ...@@ -124,15 +118,22 @@
{ {
[super viewDidLoad]; [super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification [defaultCenter addObserver:self
object:[UIApplication sharedApplication]]; selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:[UIApplication sharedApplication]];
[defaultCenter addObserver:self
selector:@selector(applicationDidBecomeActive:)
name:UIApplicationDidBecomeActiveNotification
object:[UIApplication sharedApplication]];
[[NSNotificationCenter defaultCenter] addObserver:self [defaultCenter addObserver:self
selector:@selector(applicationDidBecomeActive:) selector:@selector(sharedLibraryFound:)
name:UIApplicationDidBecomeActiveNotification name:VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance
object:[UIApplication sharedApplication]]; object:nil];
/* if (SYSTEM_RUNS_IOS7_OR_LATER) /* if (SYSTEM_RUNS_IOS7_OR_LATER)
_sectionHeaderTexts = @[@"Universal Plug'n'Play (UPNP)", @"Plex Media Server (via Bonjour)", @"File Transfer Protocol (FTP)", NSLocalizedString(@"SHARED_VLC_IOS_LIBRARY", nil), @"libDSM", NSLocalizedString(@"SAP_STREAMS", nil)]; _sectionHeaderTexts = @[@"Universal Plug'n'Play (UPNP)", @"Plex Media Server (via Bonjour)", @"File Transfer Protocol (FTP)", NSLocalizedString(@"SHARED_VLC_IOS_LIBRARY", nil), @"libDSM", NSLocalizedString(@"SAP_STREAMS", nil)];
...@@ -426,7 +427,10 @@ ...@@ -426,7 +427,10 @@
NSString *name = [_httpServicesInfo[row] objectForKey:@"name"]; NSString *name = [_httpServicesInfo[row] objectForKey:@"name"];
NSString *hostName = [_httpServicesInfo[row] objectForKey:@"hostName"]; NSString *hostName = [_httpServicesInfo[row] objectForKey:@"hostName"];
NSString *portNum = [_httpServicesInfo[row] objectForKey:@"port"]; NSString *portNum = [_httpServicesInfo[row] objectForKey:@"port"];
VLCSharedLibraryListViewController *targetViewController = [[VLCSharedLibraryListViewController alloc] initWithHttpServer:name serverAddress:hostName portNumber:portNum]; VLCSharedLibraryListViewController *targetViewController = [[VLCSharedLibraryListViewController alloc]
initWithHttpServer:name
serverAddress:hostName
portNumber:portNum];
[[self navigationController] pushViewController:targetViewController animated:YES]; [[self navigationController] pushViewController:targetViewController animated:YES];
} else if (section == 4) { } else if (section == 4) {
NSLog(@"DSM entry selected"); NSLog(@"DSM entry selected");
...@@ -571,17 +575,7 @@ ...@@ -571,17 +575,7 @@
} }
} else if ([aNetService.type isEqualToString:@"_http._tcp."]) { } else if ([aNetService.type isEqualToString:@"_http._tcp."]) {
if ([[aNetService hostName] rangeOfString:_myHostName].location == NSNotFound) { if ([[aNetService hostName] rangeOfString:_myHostName].location == NSNotFound) {
if ([_httpParser isVLCMediaServer:[aNetService hostName] port:[NSString stringWithFormat:@":%ld", (long)[aNetService port]]]) { [_httpParser checkNetserviceForVLCService:aNetService];
if (![_httpServices containsObject:aNetService]) {
[_httpServices addObject:aNetService];
NSMutableDictionary *_dictService = [[NSMutableDictionary alloc] init];
[_dictService setObject:[aNetService name] forKey:@"name"];
[_dictService setObject:[aNetService hostName] forKey:@"hostName"];
NSString *portStr = [[NSString alloc] initWithFormat:@":%ld", (long)[aNetService port]];
[_dictService setObject:portStr forKey:@"port"];
[_httpServicesInfo addObject:_dictService];
}
}
} }
} }
[_rawNetServices removeObject:aNetService]; [_rawNetServices removeObject:aNetService];
...@@ -594,6 +588,23 @@ ...@@ -594,6 +588,23 @@
[_rawNetServices removeObject:aNetService]; [_rawNetServices removeObject:aNetService];
} }
#pragma mark - shared library stuff
- (void)sharedLibraryFound:(NSNotification *)aNotification
{
NSNetService *aNetService = [aNotification.userInfo objectForKey:@"aNetService"];
if (![_httpServices containsObject:aNetService]) {
[_httpServices addObject:aNetService];
NSMutableDictionary *_dictService = [[NSMutableDictionary alloc] init];
[_dictService setObject:[aNetService name] forKey:@"name"];
[_dictService setObject:[aNetService hostName] forKey:@"hostName"];
NSString *portStr = [[NSString alloc] initWithFormat:@"%ld", (long)[aNetService port]];
[_dictService setObject:portStr forKey:@"port"];
[_httpServicesInfo addObject:_dictService];
}
}
#pragma mark - UPNP details #pragma mark - UPNP details
//protocol UPnPDBObserver //protocol UPnPDBObserver
- (void)UPnPDBWillUpdate:(UPnPDB*)sender - (void)UPnPDBWillUpdate:(UPnPDB*)sender
......
...@@ -21,9 +21,9 @@ ...@@ -21,9 +21,9 @@
#import "VLCAlertView.h" #import "VLCAlertView.h"
#import "UIDevice+VLC.h" #import "UIDevice+VLC.h"
@interface VLCSharedLibraryListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalNetworkListCell, UISearchBarDelegate, UISearchDisplayDelegate> @interface VLCSharedLibraryListViewController () <UITableViewDataSource, UITableViewDelegate, VLCLocalNetworkListCell, UISearchBarDelegate, UISearchDisplayDelegate, VLCSharedLibraryParserDelegate>
{ {
NSMutableArray *_mutableObjectList; NSArray *_serverDataArray;
NSCache *_imageCache; NSCache *_imageCache;
NSString *_httpServerName; NSString *_httpServerName;
...@@ -61,32 +61,30 @@ ...@@ -61,32 +61,30 @@
_httpServerAddress = serverAddress; _httpServerAddress = serverAddress;
_httpServerPort = portNumber; _httpServerPort = portNumber;
_mutableObjectList = [[NSMutableArray alloc] init];
_imageCache = [[NSCache alloc] init]; _imageCache = [[NSCache alloc] init];
[_imageCache setCountLimit:50]; [_imageCache setCountLimit:50];
_httpParser = [[VLCSharedLibraryParser alloc] init]; _httpParser = [[VLCSharedLibraryParser alloc] init];
_httpParser.delegate = self;
} }
return self; return self;
} }
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[_httpParser fetchDataFromServer:_httpServerAddress port:_httpServerPort.longLongValue];
}
- (void)viewDidLoad - (void)viewDidLoad
{ {
[super viewDidLoad]; [super viewDidLoad];
[_mutableObjectList removeAllObjects];
_mutableObjectList = [_httpParser VLCLibraryServerParser:_httpServerAddress port:_httpServerPort];
self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor]; self.tableView.separatorColor = [UIColor VLCDarkBackgroundColor];
self.view.backgroundColor = [UIColor VLCDarkBackgroundColor]; self.view.backgroundColor = [UIColor VLCDarkBackgroundColor];
NSString *titleValue; self.title = _httpServerAddress;
if (_mutableObjectList.count == 0)
titleValue = _httpServerAddress;
else
titleValue = [_mutableObjectList.firstObject objectForKey:@"libTitle"];
self.title = titleValue;
_searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; _searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationBar *navBar = self.navigationController.navigationBar; UINavigationBar *navBar = self.navigationController.navigationBar;
...@@ -139,10 +137,12 @@ ...@@ -139,10 +137,12 @@
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{ {
if (tableView == self.searchDisplayController.searchResultsTableView) @synchronized(self) {
return _searchData.count; if (tableView == self.searchDisplayController.searchResultsTableView)
else return _searchData.count;
return _mutableObjectList.count; else
return _serverDataArray.count;
}
} }
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
...@@ -154,18 +154,18 @@ ...@@ -154,18 +154,18 @@
if (cell == nil) if (cell == nil)
cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier]; cell = [VLCLocalNetworkListCell cellWithReuseIdentifier:CellIdentifier];
NSMutableArray *ObjList = [[NSMutableArray alloc] init]; NSDictionary *cellObject;
[ObjList removeAllObjects]; @synchronized(self) {
if (tableView == self.searchDisplayController.searchResultsTableView)
if (tableView == self.searchDisplayController.searchResultsTableView) cellObject = _searchData[indexPath.row];
[ObjList addObjectsFromArray:_searchData]; else
else cellObject = _serverDataArray[indexPath.row];
[ObjList addObjectsFromArray:_mutableObjectList]; }
[cell setTitle:[[ObjList objectAtIndex:indexPath.row] objectForKey:@"title"]]; [cell setTitle:[cellObject objectForKey:@"title"]];
[cell setIcon:[UIImage imageNamed:@"blank"]]; [cell setIcon:[UIImage imageNamed:@"blank"]];
NSString *thumbPath = [[ObjList objectAtIndex:indexPath.row] objectForKey:@"thumb"]; NSString *thumbPath = [cellObject objectForKey:@"thumb"];
if (thumbPath) { if (thumbPath) {
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_async(queue, ^{ dispatch_async(queue, ^{
...@@ -179,9 +179,9 @@ ...@@ -179,9 +179,9 @@
}); });
} }
NSInteger size = [[[ObjList objectAtIndex:indexPath.row] objectForKey:@"size"] integerValue]; NSInteger size = [[cellObject objectForKey:@"size"] integerValue];
NSString *mediaSize = [NSByteCountFormatter stringFromByteCount:size countStyle:NSByteCountFormatterCountStyleFile]; NSString *mediaSize = [NSByteCountFormatter stringFromByteCount:size countStyle:NSByteCountFormatterCountStyleFile];
NSString *duration = [[ObjList objectAtIndex:indexPath.row] objectForKey:@"duration"]; NSString *duration = [cellObject objectForKey:@"duration"];
[cell setIsDirectory:NO]; [cell setIsDirectory:NO];
[cell setSubtitle:[NSString stringWithFormat:@"%@ (%@)", mediaSize, duration]]; [cell setSubtitle:[NSString stringWithFormat:@"%@ (%@)", mediaSize, duration]];
[cell setIsDownloadable:YES]; [cell setIsDownloadable:YES];
...@@ -214,21 +214,22 @@ ...@@ -214,21 +214,22 @@
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{ {
NSMutableArray *ObjList = [[NSMutableArray alloc] init]; NSDictionary *selectedObject;
[ObjList removeAllObjects];
if (tableView == self.searchDisplayController.searchResultsTableView) @synchronized(self) {
[ObjList addObjectsFromArray:_searchData]; if (tableView == self.searchDisplayController.searchResultsTableView)
else selectedObject = _searchData[indexPath.row];
[ObjList addObjectsFromArray:_mutableObjectList]; else
selectedObject = _serverDataArray[indexPath.row];
}
NSString *URLofSubtitle = nil; NSString *URLofSubtitle = nil;
if (![[[ObjList objectAtIndex:indexPath.row] objectForKey:@"pathSubtitle"] isEqualToString:@""]) { if (![[selectedObject objectForKey:@"pathSubtitle"] isEqualToString:@""]) {
NSURL *url = [NSURL URLWithString:[[[ObjList objectAtIndex:indexPath.row] objectForKey:@"pathSubtitle"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSURL *url = [NSURL URLWithString:[[selectedObject objectForKey:@"pathSubtitle"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
URLofSubtitle = [self _getFileSubtitleFromServer:url modeStream:YES]; URLofSubtitle = [self _getFileSubtitleFromServer:url modeStream:YES];
} }
NSURL *itemURL = [NSURL URLWithString:[[ObjList objectAtIndex:indexPath.row] objectForKey:@"pathfile"]]; NSURL *itemURL = [NSURL URLWithString:[selectedObject objectForKey:@"pathfile"]];
if (itemURL) { if (itemURL) {
VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate; VLCAppDelegate *appDelegate = [UIApplication sharedApplication].delegate;
[appDelegate openMovieWithExternalSubtitleFromURL:itemURL externalSubURL:URLofSubtitle]; [appDelegate openMovieWithExternalSubtitleFromURL:itemURL externalSubURL:URLofSubtitle];
...@@ -239,8 +240,19 @@ ...@@ -239,8 +240,19 @@
#pragma mark - Specifics #pragma mark - Specifics
- (void)sharedLibraryDataProcessings:(NSArray *)result
{
@synchronized(self) {
_serverDataArray = result;
self.title = [_serverDataArray.firstObject objectForKey:@"libTitle"];
}
[self.tableView reloadData];
}
- (void)_downloadFileFromMediaItem:(NSURL *)itemURL - (void)_downloadFileFromMediaItem:(NSURL *)itemURL
{ {
NSLog(@"trying to download %@", [itemURL absoluteString]);
if (![[itemURL absoluteString] isSupportedFormat]) { if (![[itemURL absoluteString] isSupportedFormat]) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil) message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), [itemURL absoluteString]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:nil]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"FILE_NOT_SUPPORTED", nil) message:[NSString stringWithFormat:NSLocalizedString(@"FILE_NOT_SUPPORTED_LONG", nil), [itemURL absoluteString]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_CANCEL", nil) otherButtonTitles:nil];
[alert show]; [alert show];
...@@ -249,13 +261,6 @@ ...@@ -249,13 +261,6 @@
} }
} }
- (void)reloadSharedLibrary
{
[_mutableObjectList removeAllObjects];
_mutableObjectList = [_httpParser VLCLibraryServerParser:_httpServerAddress port:_httpServerPort];
[self.tableView reloadData];
}
- (NSString *)_getFileSubtitleFromServer:(NSURL *)url modeStream:(BOOL)modeStream - (NSString *)_getFileSubtitleFromServer:(NSURL *)url modeStream:(BOOL)modeStream
{ {
NSString *FileSubtitlePath = nil; NSString *FileSubtitlePath = nil;
...@@ -290,28 +295,29 @@ ...@@ -290,28 +295,29 @@
- (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell - (void)triggerDownloadForCell:(VLCLocalNetworkListCell *)cell
{ {
NSMutableArray *ObjList = [[NSMutableArray alloc] init]; NSDictionary *dataItem;
[ObjList removeAllObjects];
if ([self.searchDisplayController isActive]) @synchronized(self) {
[ObjList addObject:_searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row]]; if ([self.searchDisplayController isActive])
else dataItem = _searchData[[self.searchDisplayController.searchResultsTableView indexPathForCell:cell].row];
[ObjList addObject:_mutableObjectList[[self.tableView indexPathForCell:cell].row]]; else
dataItem = _serverDataArray[[self.tableView indexPathForCell:cell].row];
}
NSURL *itemURL = [NSURL URLWithString:[ObjList[0] objectForKey:@"pathfile"]]; NSURL *itemURL = [NSURL URLWithString:[dataItem objectForKey:@"pathfile"]];
NSInteger size = [[[ObjList objectAtIndex:0] objectForKey:@"size"] integerValue]; NSInteger size = [[dataItem objectForKey:@"size"] integerValue];
if (size < [[UIDevice currentDevice] freeDiskspace].longLongValue) { if (size < [[UIDevice currentDevice] freeDiskspace].longLongValue) {
NSString *URLofSubtitle = nil; NSString *URLofSubtitle = nil;
if (![[[ObjList objectAtIndex:0] objectForKey:@"pathSubtitle"] isEqualToString:@""]) { if (![[dataItem objectForKey:@"pathSubtitle"] isEqualToString:@""]) {
NSURL *url = [NSURL URLWithString:[[[ObjList objectAtIndex:0] objectForKey:@"pathSubtitle"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSURL *url = [NSURL URLWithString:[[dataItem objectForKey:@"pathSubtitle"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
URLofSubtitle = [self _getFileSubtitleFromServer:url modeStream:NO]; URLofSubtitle = [self _getFileSubtitleFromServer:url modeStream:NO];
} }
[self _downloadFileFromMediaItem:itemURL]; [self _downloadFileFromMediaItem:itemURL];
[cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)]; [cell.statusLabel showStatusMessage:NSLocalizedString(@"DOWNLOADING", nil)];
} else { } else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [[ObjList objectAtIndex:0] objectForKey:@"title"], [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil]; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"DISK_FULL", nil) message:[NSString stringWithFormat:NSLocalizedString(@"DISK_FULL_FORMAT", nil), [dataItem objectForKey:@"title"], [[UIDevice currentDevice] model]] delegate:self cancelButtonTitle:NSLocalizedString(@"BUTTON_OK", nil) otherButtonTitles:nil];
[alert show]; [alert show];
} }
} }
...@@ -320,13 +326,15 @@ ...@@ -320,13 +326,15 @@
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString - (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{ {
[_searchData removeAllObjects]; @synchronized (self) {
NSUInteger count = _mutableObjectList.count; [_searchData removeAllObjects];
for (NSUInteger i = 0; i < count; i++) { NSUInteger count = _serverDataArray.count;
NSRange nameRange; for (NSUInteger i = 0; i < count; i++) {
nameRange = [[[_mutableObjectList objectAtIndex:i] objectForKey:@"title"] rangeOfString:searchString options:NSCaseInsensitiveSearch]; NSRange nameRange;
if (nameRange.location != NSNotFound) nameRange = [[_serverDataArray[i] objectForKey:@"title"] rangeOfString:searchString options:NSCaseInsensitiveSearch];
[_searchData addObject:_mutableObjectList[i]]; if (nameRange.location != NSNotFound)
[_searchData addObject:_serverDataArray[i]];
}
} }
return YES; return YES;
} }
...@@ -355,7 +363,11 @@ ...@@ -355,7 +363,11 @@
_refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary]; _refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:lastupdated attributes:attrsDictionary];
//end the refreshing //end the refreshing
[_refreshControl endRefreshing]; [_refreshControl endRefreshing];
[self performSelector:@selector(reloadSharedLibrary) withObject:nil];
@synchronized(self) {
_serverDataArray = nil;
}
[_httpParser fetchDataFromServer:_httpServerAddress port:_httpServerPort.longLongValue];
} }
#pragma mark - Gesture Action #pragma mark - Gesture Action
......
...@@ -10,9 +10,22 @@ ...@@ -10,9 +10,22 @@
*****************************************************************************/ *****************************************************************************/
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
extern NSString *const VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance;
@protocol VLCSharedLibraryParserDelegate <NSObject>
@required
- (void)sharedLibraryDataProcessings:(NSArray *)result;
@end
@interface VLCSharedLibraryParser : NSObject @interface VLCSharedLibraryParser : NSObject
- (NSMutableArray *)VLCLibraryServerParser:(NSString *)adress port:(NSString *)port; @property (readwrite, weak) id<VLCSharedLibraryParserDelegate> delegate;
- (BOOL)isVLCMediaServer:(NSString *)adress port:(NSString *)port;
- (void)checkNetserviceForVLCService:(NSNetService *)netservice;
- (void)fetchDataFromServer:(NSString *)hostname port:(long)port;
@end @end
...@@ -13,74 +13,97 @@ ...@@ -13,74 +13,97 @@
#define kLibraryXmlFile @"libMediaVLC.xml" #define kLibraryXmlFile @"libMediaVLC.xml"
NSString *const VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance = @"VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance";
@interface VLCSharedLibraryParser () <NSXMLParserDelegate> @interface VLCSharedLibraryParser () <NSXMLParserDelegate>
{ {
NSMutableArray *_containerInfo; NSMutableArray *_containerInfo;
NSMutableDictionary *_dicoInfo; NSMutableDictionary *_dicoInfo;
NSString *_libraryServerUrl;
} }
@end @end
@implementation VLCSharedLibraryParser @implementation VLCSharedLibraryParser
- (NSMutableArray *)VLCLibraryServerParser:(NSString *)adress port:(NSString *)port - (void)checkNetserviceForVLCService:(NSNetService *)aNetService
{
[self performSelectorInBackground:@selector(parseNetServiceOnBackgroundThread:) withObject:aNetService];
}
- (void)parseNetServiceOnBackgroundThread:(NSNetService *)aNetService
{
NSString *hostnamePort = [NSString stringWithFormat:@"%@:%ld", [aNetService hostName], [aNetService port]];
NSArray *parsedContents = [self downloadAndProcessDataFromServer:hostnamePort];
if (parsedContents.count > 0) {
if ([[parsedContents.firstObject objectForKey:@"identifier"] isEqualToString:@"org.videolan.vlc-ios"]) {
[[NSNotificationCenter defaultCenter] postNotificationName:VLCSharedLibraryParserDeterminedNetserviceAsVLCInstance
object:self
userInfo:@{@"aNetService" : aNetService}];
}
}
}
- (void)fetchDataFromServer:(NSString *)hostname port:(long)port
{
NSString *hostnamePort = [NSString stringWithFormat:@"%@:%ld", hostname, port];
[self performSelectorInBackground:@selector(processDataOnBackgroundThreadFromHostnameAndPort:) withObject:hostnamePort];
}
- (void)processDataOnBackgroundThreadFromHostnameAndPort:(NSString *)hostnameAndPort
{
NSArray *parsedContents = [self downloadAndProcessDataFromServer:hostnameAndPort];
if ([self.delegate respondsToSelector:@selector(sharedLibraryDataProcessings:)])
[self.delegate sharedLibraryDataProcessings:parsedContents];
}
- (NSArray *)downloadAndProcessDataFromServer:(NSString *)hostnamePort
{ {
_containerInfo = [[NSMutableArray alloc] init]; _containerInfo = [[NSMutableArray alloc] init];
[_containerInfo removeAllObjects]; [_containerInfo removeAllObjects];
_dicoInfo = [[NSMutableDictionary alloc] init]; _dicoInfo = [[NSMutableDictionary alloc] init];
_libraryServerUrl = [NSString stringWithFormat:@"http://%@%@", adress, port]; NSString *serverURL = [NSString stringWithFormat:@"http://%@/%@", hostnamePort, kLibraryXmlFile];
NSString *mediaServerUrl = [NSString stringWithFormat:@"%@/%@", _libraryServerUrl, kLibraryXmlFile];
NSURL *url = [[NSURL alloc] initWithString:mediaServerUrl]; NSURL *url = [[NSURL alloc] initWithString:serverURL];
NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:url]; NSXMLParser *xmlparser = [[NSXMLParser alloc] initWithContentsOfURL:url];
[xmlparser setDelegate:self]; [xmlparser setDelegate:self];
if (![xmlparser parse]) if (![xmlparser parse]) {
APLog(@"VLC Library Parser url Errors : %@", url); APLog(@"VLC Library Parser url Errors : %@", url);
return [NSArray array];
return _containerInfo;
}
- (BOOL)isVLCMediaServer:(NSString *)adress port:(NSString *)port
{
NSMutableArray *mutableObjectList = [self VLCLibraryServerParser:adress port:port];
if (mutableObjectList.count > 0) {
NSString *identifier = [[mutableObjectList objectAtIndex:0] objectForKey:@"identifier"];
if ([identifier isEqualToString:@"org.videolan.vlc-ios"])
return YES;
} }
return NO;
return [NSArray arrayWithArray:_containerInfo];
} }
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict - (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{ {