VLCLocalServerDiscoveryController.m 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*****************************************************************************
  2. * VLCLocalServerListViewController.m
  3. * VLC for iOS
  4. *****************************************************************************
  5. * Copyright (c) 2013-2015 VideoLAN. All rights reserved.
  6. * $Id$
  7. *
  8. * Authors: Felix Paul Kühne <fkuehne # videolan.org>
  9. * Pierre SAGASPE <pierre.sagaspe # me.com>
  10. * Tobias Conradi <videolan # tobias-conradi.de>
  11. *
  12. * Refer to the COPYING file of the official project for license.
  13. *****************************************************************************/
  14. #import "VLCLocalServerDiscoveryController.h"
  15. #import "Reachability.h"
  16. #import "VLCLocalNetworkServiceBrowser-Protocol.h"
  17. @interface VLCLocalServerDiscoveryController () <VLCLocalNetworkServiceBrowserDelegate>
  18. {
  19. NSArray<id<VLCLocalNetworkServiceBrowser>> *_serviceBrowsers;
  20. Reachability *_reachability;
  21. }
  22. @end
  23. @implementation VLCLocalServerDiscoveryController
  24. - (instancetype)initWithServiceBrowserClasses:(NSArray<Class> *)serviceBrowserClasses
  25. {
  26. self = [super init];
  27. if (!self)
  28. return nil;
  29. NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
  30. [defaultCenter addObserver:self
  31. selector:@selector(stopDiscovery)
  32. name:UIApplicationWillResignActiveNotification
  33. object:[UIApplication sharedApplication]];
  34. [defaultCenter addObserver:self
  35. selector:@selector(startDiscovery)
  36. name:UIApplicationDidBecomeActiveNotification
  37. object:[UIApplication sharedApplication]];
  38. NSMutableArray *serviceBrowsers = [NSMutableArray new];
  39. for (Class browserClass in serviceBrowserClasses) {
  40. if ([browserClass conformsToProtocol:@protocol(VLCLocalNetworkServiceBrowser)]) {
  41. [serviceBrowsers addObject:[[browserClass alloc] init]];
  42. }
  43. }
  44. _serviceBrowsers = [serviceBrowsers copy];
  45. [_serviceBrowsers enumerateObjectsUsingBlock:^(id<VLCLocalNetworkServiceBrowser> _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
  46. obj.delegate = self;
  47. }];
  48. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netReachabilityChanged) name:kReachabilityChangedNotification object:nil];
  49. _reachability = [Reachability reachabilityForLocalWiFi];
  50. [_reachability startNotifier];
  51. return self;
  52. }
  53. - (void)dealloc
  54. {
  55. [[NSNotificationCenter defaultCenter] removeObserver:self];
  56. [_reachability stopNotifier];
  57. [self stopDiscovery];
  58. }
  59. - (void)startDiscovery
  60. {
  61. if (_reachability.currentReachabilityStatus != ReachableViaWiFi) {
  62. return;
  63. }
  64. [_serviceBrowsers makeObjectsPerformSelector:@selector(startDiscovery)];
  65. }
  66. - (void)stopDiscovery
  67. {
  68. [_serviceBrowsers makeObjectsPerformSelector:@selector(stopDiscovery)];
  69. }
  70. - (BOOL)refreshDiscoveredData
  71. {
  72. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  73. return NO;
  74. [self stopDiscovery];
  75. [self startDiscovery];
  76. return YES;
  77. }
  78. #pragma mark - Reachability
  79. - (void)netReachabilityChanged
  80. {
  81. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  82. [self startDiscovery];
  83. } else {
  84. [self stopDiscovery];
  85. }
  86. }
  87. #pragma mark - data source
  88. - (NSUInteger)numberOfSections {
  89. return _serviceBrowsers.count;
  90. }
  91. - (NSString *)titleForSection:(NSUInteger)section
  92. {
  93. id<VLCLocalNetworkServiceBrowser> browser = _serviceBrowsers[section];
  94. return browser.name;
  95. }
  96. - (NSUInteger)numberOfItemsInSection:(NSUInteger)section
  97. {
  98. id<VLCLocalNetworkServiceBrowser> browser = _serviceBrowsers[section];
  99. return browser.numberOfItems;
  100. }
  101. - (id<VLCLocalNetworkService>)networkServiceForIndexPath:(NSIndexPath *)indexPath
  102. {
  103. NSUInteger section = indexPath.section;
  104. NSUInteger row = indexPath.row;
  105. id<VLCLocalNetworkServiceBrowser> browser = _serviceBrowsers[section];
  106. return [browser networkServiceForIndex:row];
  107. }
  108. - (BOOL)foundAnythingAtAll
  109. {
  110. NSUInteger serviceCount = _serviceBrowsers.count;
  111. BOOL ret = NO;
  112. for (NSUInteger i = 0; i < serviceCount; i++) {
  113. ret = [_serviceBrowsers[i] numberOfItems] > 0;
  114. if (ret)
  115. break;
  116. }
  117. return ret;
  118. }
  119. #pragma mark - VLCLocalNetworkServiceBrowserDelegate
  120. - (void)localNetworkServiceBrowserDidUpdateServices:(id<VLCLocalNetworkServiceBrowser>)serviceBrowser {
  121. if ([self.delegate respondsToSelector:@selector(discoveryFoundSomethingNew)]) {
  122. [self.delegate discoveryFoundSomethingNew];
  123. }
  124. }
  125. @end