forked from ConnectSDK/Connect-SDK-iOS-Samsung-MultiScreen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiScreenDiscoveryProvider.m
177 lines (134 loc) · 4.37 KB
/
MultiScreenDiscoveryProvider.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//
// Created by Jeremy White on 6/16/14.
// Copyright (c) 2014 LG Electronics. All rights reserved.
//
#import "MultiScreenDiscoveryProvider.h"
#import "ServiceDescription.h"
#import <SamsungMultiscreen/SamsungMultiscreen.h>
#import "MultiScreenService.h"
@interface MultiScreenDiscoveryProvider ()
@property (nonatomic) NSTimer *searchTimer;
@end
@implementation MultiScreenDiscoveryProvider
{
NSMutableDictionary *_devices;
BOOL _isEmpty;
}
- (id) init
{
self = [super init];
if (self)
{
_isEmpty = YES;
[self setUpDiscoveryCallback];
}
return self;
}
- (void) setUpDiscoveryCallback
{
__weak MultiScreenDiscoveryProvider *weakSelf = self;
_findDevicesCallback = ^(NSArray *array) {
MultiScreenDiscoveryProvider *strongSelf = weakSelf;
if (strongSelf)
[strongSelf updateDevices:array];
};
}
- (void) updateDevices:(NSArray *)array
{
NSMutableArray *newDevices = [NSMutableArray new];
NSMutableArray *lostDevices = [NSMutableArray new];
[array enumerateObjectsUsingBlock:^(MSDevice *device, NSUInteger idx, BOOL *stop) {
if (self.devices[device.deviceId])
return;
[newDevices addObject:device];
}];
[self.devices enumerateKeysAndObjectsUsingBlock:^(NSString *preExistingDeviceId, MSDevice *preExistingDevice, BOOL *searchStop) {
__block BOOL foundDevice = NO;
[array enumerateObjectsUsingBlock:^(MSDevice *device, NSUInteger deviceIdx, BOOL *deviceStop) {
if ([device.deviceId isEqualToString:preExistingDeviceId])
{
foundDevice = YES;
*deviceStop = YES;
}
}];
if (!foundDevice)
[lostDevices addObject:preExistingDevice];
}];
for (MSDevice *device in newDevices)
[self addDevice:device];
for (MSDevice *device in lostDevices)
[self removeDevice:device];
}
- (void) addDevice:(MSDevice *)device
{
if (!device)
return;
if (self.devices[device.deviceId])
return;
_devices[device.deviceId] = device;
if (self.delegate && [self.delegate respondsToSelector:@selector(discoveryProvider:didFindService:)])
{
ServiceDescription *description = [ServiceDescription descriptionWithAddress:device.ipAddress UUID:device.deviceId];
description.serviceId = kConnectSDKMultiScreenTVServiceId;
description.friendlyName = device.name;
description.locationResponseHeaders = device.attributes;
[self.delegate discoveryProvider:self didFindService:description];
}
}
- (void) removeDevice:(MSDevice *)device
{
if (!device)
return;
if (!self.devices[device.deviceId])
return;
[_devices removeObjectForKey:device.deviceId];
if (self.delegate && [self.delegate respondsToSelector:@selector(discoveryProvider:didLoseService:)])
{
ServiceDescription *description = [ServiceDescription descriptionWithAddress:device.ipAddress UUID:device.deviceId];
description.serviceId = kConnectSDKMultiScreenTVServiceId;
description.friendlyName = device.name;
description.locationResponseHeaders = device.attributes;
[self.delegate discoveryProvider:self didLoseService:description];
}
}
- (NSDictionary *) devices
{
return [NSDictionary dictionaryWithDictionary:_devices];
}
- (void) startDiscovery
{
if (self.isRunning)
return;
_devices = [NSMutableDictionary new];
self.isRunning = YES;
self.searchTimer = [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(search) userInfo:nil repeats:YES];
[self.searchTimer fire];
}
- (void) stopDiscovery
{
if (self.searchTimer)
{
[self.searchTimer invalidate];
self.searchTimer = nil;
}
self.isRunning = NO;
}
- (void) search
{
[MSDevice searchWithCompletionBlock:_findDevicesCallback queue:dispatch_get_main_queue()];
}
- (void) addDeviceFilter:(NSDictionary *)parameters
{
// a full implementation of this method (see SSDPDiscoveryProvider) is not needed at this time, since only one filter will ever be added
_isEmpty = NO;
}
- (void) removeDeviceFilter:(NSDictionary *)parameters
{
// a full implementation of this method (see SSDPDiscoveryProvider) is not needed at this time, since only one filter will ever be added
_isEmpty = YES;
}
- (BOOL) isEmpty
{
return _isEmpty;
}
@end