VLCLocalServerDiscoveryController.m 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. [_reachability stopNotifier];
  56. [self stopDiscovery];
  57. }
  58. - (void)startDiscovery
  59. {
  60. if (_reachability.currentReachabilityStatus != ReachableViaWiFi) {
  61. return;
  62. }
  63. [_serviceBrowsers makeObjectsPerformSelector:@selector(startDiscovery)];
  64. }
  65. - (void)stopDiscovery
  66. {
  67. [_serviceBrowsers makeObjectsPerformSelector:@selector(stopDiscovery)];
  68. }
  69. - (BOOL)refreshDiscoveredData
  70. {
  71. if (_reachability.currentReachabilityStatus != ReachableViaWiFi)
  72. return NO;
  73. [self stopDiscovery];
  74. [self startDiscovery];
  75. return YES;
  76. }
  77. #pragma mark - Reachability
  78. - (void)netReachabilityChanged
  79. {
  80. if (_reachability.currentReachabilityStatus == ReachableViaWiFi) {
  81. [self startDiscovery];
  82. } else {
  83. [self stopDiscovery];
  84. }
  85. }
  86. #pragma mark - data source
  87. - (NSUInteger)numberOfSections {
  88. return _serviceBrowsers.count;
  89. }
  90. - (NSUInteger)numberOfItemsInSection:(NSUInteger)section
  91. {
  92. id<VLCLocalNetworkServiceBrowser> browser = _serviceBrowsers[section];
  93. return browser.numberOfItems;
  94. }
  95. - (id<VLCLocalNetworkService>)networkServiceForIndexPath:(NSIndexPath *)indexPath
  96. {
  97. NSUInteger section = indexPath.section;
  98. NSUInteger row = indexPath.row;
  99. id<VLCLocalNetworkServiceBrowser> browser = _serviceBrowsers[section];
  100. return [browser networkServiceForIndex:row];
  101. }
  102. - (BOOL)foundAnythingAtAll
  103. {
  104. NSUInteger serviceCount = _serviceBrowsers.count;
  105. BOOL ret = NO;
  106. for (NSUInteger i = 0; i < serviceCount; i++) {
  107. ret = [_serviceBrowsers[i] numberOfItems] > 0;
  108. if (ret)
  109. break;
  110. }
  111. return ret;
  112. }
  113. #pragma mark - VLCLocalNetworkServiceBrowserDelegate
  114. - (void)localNetworkServiceBrowserDidUpdateServices:(id<VLCLocalNetworkServiceBrowser>)serviceBrowser {
  115. if ([self.delegate respondsToSelector:@selector(discoveryFoundSomethingNew)]) {
  116. [self.delegate discoveryFoundSomethingNew];
  117. }
  118. }
  119. @end