-
Notifications
You must be signed in to change notification settings - Fork 82
/
SyphonServerDirectory.m
377 lines (335 loc) · 13.3 KB
/
SyphonServerDirectory.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
SyphonServerDirectory.m
Syphon
Copyright 2010-2011 bangnoise (Tom Butterworth) & vade (Anton Marini).
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "SyphonServerDirectory.h"
#import "SyphonPrivate.h"
#import <Cocoa/Cocoa.h>
#import <pthread.h>
#define kSyphonServerDirectoryAnnounceTimeout 6
NSString * const SyphonServerAnnounceNotification = @"SyphonServerAnnounceNotification";
NSString * const SyphonServerUpdateNotification = @"SyphonServerUpdateNotification";
NSString * const SyphonServerRetireNotification = @"SyphonServerRetireNotification";
@interface NSDictionary (SyphonServerDirectoryPimpMyDictionary)
- (NSDictionary *)pimpedVersionForSyphon;
@end
@interface NSArray (SyphonServerDirectoryServerSearch)
- (NSUInteger)indexOfDescriptionForSyphonServerUUID:(NSString *)uuid;
@end
@interface SyphonServerDirectory (Private)
- (id)initOnce;
- (void)requestServerAnnounce;
@end
@implementation SyphonServerDirectory
{
@private
NSMutableArray *_servers;
pthread_mutex_t _generalLock;
pthread_mutex_t _mutateLock;
NSMutableSet *_pings;
}
+ (BOOL)automaticallyNotifiesObserversForKey:(NSString *)theKey
{
BOOL automatic;
if ([theKey isEqualToString:@"servers"])
{
automatic=NO;
}
else
{
automatic=[super automaticallyNotifiesObserversForKey:theKey];
}
return automatic;
}
#pragma mark Singleton Instance
+ (void)load
{
// cause our instantiation so we always post notifications
[SyphonServerDirectory sharedDirectory];
}
+ (SyphonServerDirectory *)sharedDirectory
{
static SyphonServerDirectory *sharedDirectory = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedDirectory = [[self alloc] init];
});
return sharedDirectory;
}
- (id)init
{
self = [super init];
if (self)
{
if (pthread_mutex_init(&_generalLock, NULL) != 0
|| pthread_mutex_init(&_mutateLock, NULL) != 0)
{
return nil;
}
_servers = [[NSMutableArray alloc] initWithCapacity:4];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(handleServerAnnounce:) name:SyphonServerAnnounce object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(handleServerRetire:) name:SyphonServerRetire object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(handleServerUpdate:) name:SyphonServerUpdate object:nil];
[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(handleAccounceRequest:) name:SyphonServerAnnounceRequest object:nil];
[self requestServerAnnounce];
}
return self;
}
- (void)dealloc
{
// This will never get called as long as we're a singleton object,
// but maintain it for completeness, and in case we add dealloc on
// framework unload or something later
[[NSDistributedNotificationCenter defaultCenter] removeObserver:self];
pthread_mutex_destroy(&_generalLock);
pthread_mutex_destroy(&_mutateLock);
}
- (NSArray *)servers
{
pthread_mutex_lock(&_generalLock);
NSArray *array = [NSArray arrayWithArray:_servers];
pthread_mutex_unlock(&_generalLock);
return array;
}
- (NSArray *)serversMatchingName:(NSString *)name appName:(NSString *)appname
{
if ([name length] == 0)
{
name = nil;
}
if ([appname length] == 0)
{
appname = nil;
}
pthread_mutex_lock(&_generalLock);
NSIndexSet *indexes = [_servers indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
if ((name == nil || [[obj objectForKey:SyphonServerDescriptionNameKey] isEqualToString:name])
&& (appname == nil || [[obj objectForKey:SyphonServerDescriptionAppNameKey] isEqualToString:appname]))
{
return YES;
} else {
return NO;
}
}];
NSArray *array = [_servers objectsAtIndexes:indexes];
pthread_mutex_unlock(&_generalLock);
return array;
}
- (void)requestServerAnnounce
{
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:SyphonServerAnnounceRequest object:nil userInfo:nil deliverImmediately:YES];
}
#pragma mark Notification Handling
- (void)handleAccounceRequest:(NSNotification *)aNotification
{
/*
We watch for a global announce request to check the servers we know about are still alive.
This could have come from any application.
When we get this notification we dispatch a block to be invoked at timeout, and then check that all the
servers we know about responded. If they didn't we remove them from our array and post retirement notifications for them.
*/
pthread_mutex_lock(&_generalLock);
// If _pings != nil then we're already in the process of doing this, do nothing
if (_pings == nil)
{
_pings = [[NSMutableSet alloc] initWithCapacity:[_servers count]];
dispatch_time_t when = dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * kSyphonServerDirectoryAnnounceTimeout);
dispatch_after(when, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
// Lock so nobody mutates for the duration
pthread_mutex_lock(&self->_mutateLock);
// Lock for access
pthread_mutex_lock(&self->_generalLock);
// Get the servers we know about which haven't responded to our announce request
NSIndexSet *indices = [self->_servers indexesOfObjectsPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
if ([self->_pings containsObject:[obj objectForKey:SyphonServerDescriptionUUIDKey]])
{
return NO;
}
else
{
return YES;
}
}];
// Unlock for access as we're either finished or about to post a change, in which case others need access
pthread_mutex_unlock(&self->_generalLock);
NSArray *retired = nil;
if ([indices count] > 0)
{
SYPHONLOG(@"Removing servers which didn't respond to an announce request.");
[self willChange:NSKeyValueChangeRemoval valuesAtIndexes:indices forKey:@"servers"];
// Lock for access
pthread_mutex_lock(&self->_generalLock);
// Save server descriptions for the notifications we will post
retired = [self->_servers objectsAtIndexes:indices];
// Make the removal
[self->_servers removeObjectsAtIndexes:indices];
// Unlock for access so others can access in response to didChange
pthread_mutex_unlock(&self->_generalLock);
[self didChange:NSKeyValueChangeRemoval valuesAtIndexes:indices forKey:@"servers"];
}
pthread_mutex_lock(&self->_generalLock);
// Reset _pings so we will handle the next announce request
self->_pings = nil;
pthread_mutex_unlock(&self->_generalLock);
pthread_mutex_unlock(&self->_mutateLock);
for (NSDictionary *description in retired) {
[[NSNotificationCenter defaultCenter] postNotificationName:SyphonServerRetireNotification object:self userInfo:description];
}
});
}
pthread_mutex_unlock(&_generalLock);
}
- (void)handleServerAnnounce:(NSNotification *)aNotification
{
// SYPHONLOG(@"new server description: %@", serverInfo);
NSDictionary* serverInfo = [[aNotification userInfo] pimpedVersionForSyphon];
NSString *uuid = [serverInfo objectForKey:SyphonServerDescriptionUUIDKey];
// Lock so nobody mutates for the duration
pthread_mutex_lock(&_mutateLock);
// Lock for access
pthread_mutex_lock(&_generalLock);
NSUInteger index = [_servers indexOfDescriptionForSyphonServerUUID:uuid];
NSUInteger count = [_servers count];
// Add the UUID to _pings so we know the server is alive
if (uuid) [_pings addObject:uuid];
// Unlock for access, so others can access in response to the willChange
pthread_mutex_unlock(&_generalLock);
if (index == NSNotFound)
{
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndexesInRange:NSMakeRange(count, 1)];
[self willChange:NSKeyValueChangeInsertion valuesAtIndexes:indexSet forKey:@"servers"];
// lock for access
pthread_mutex_lock(&_generalLock);
[_servers addObject:serverInfo];
// unlock for access so others can access in response to the didChange
pthread_mutex_unlock(&_generalLock);
[self didChange:NSKeyValueChangeInsertion valuesAtIndexes:indexSet forKey:@"servers"];
[[NSNotificationCenter defaultCenter] postNotificationName:SyphonServerAnnounceNotification object:self userInfo:serverInfo];
}
// unlock mutate lock
pthread_mutex_unlock(&_mutateLock);
}
- (void)handleServerRetire:(NSNotification *)aNotification
{
// SYPHONLOG(@"retire server description: %@", serverInfo);
NSDictionary* serverInfo = [[aNotification userInfo] pimpedVersionForSyphon];
NSString *uuid = [serverInfo objectForKey:SyphonServerDescriptionUUIDKey];
// Lock so nobody mutates for the duration
pthread_mutex_lock(&_mutateLock);
// lock for access
pthread_mutex_lock(&_generalLock);
NSUInteger index = [_servers indexOfDescriptionForSyphonServerUUID:uuid];
// unlock for access so others can access in response to willChange
pthread_mutex_unlock(&_generalLock);
if(index != NSNotFound)
{
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
[self willChange:NSKeyValueChangeRemoval valuesAtIndexes:indexSet forKey:@"servers"];
// lock for access
pthread_mutex_lock(&_generalLock);
[_servers removeObjectAtIndex:index];
// unlock for access so others can access in response to didChange
pthread_mutex_unlock(&_generalLock);
[self didChange:NSKeyValueChangeRemoval valuesAtIndexes:indexSet forKey:@"servers"];
[[NSNotificationCenter defaultCenter] postNotificationName:SyphonServerRetireNotification object:self userInfo:serverInfo];
}
// unlock mutate lock
pthread_mutex_unlock(&_mutateLock);
}
- (void)handleServerUpdate:(NSNotification *)aNotification
{
// SYPHONLOG(@"updated server description: %@", serverInfo);
NSDictionary* serverInfo = [[aNotification userInfo] pimpedVersionForSyphon];
NSString *uuid = [serverInfo objectForKey:SyphonServerDescriptionUUIDKey];
// Lock so nobody mutates for the duration
pthread_mutex_lock(&_mutateLock);
// lock for access
pthread_mutex_lock(&_generalLock);
NSUInteger index = [_servers indexOfDescriptionForSyphonServerUUID:uuid];
// unlock for access so others can access in response to willChange
pthread_mutex_unlock(&_generalLock);
if(index != NSNotFound)
{
NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:index];
[self willChange:NSKeyValueChangeReplacement valuesAtIndexes:indexSet forKey:@"servers"];
// lock for access
pthread_mutex_lock(&_generalLock);
[_servers replaceObjectAtIndex:index withObject:serverInfo];
// unlock for access so others can access in response to didChange
pthread_mutex_unlock(&_generalLock);
[self didChange:NSKeyValueChangeReplacement valuesAtIndexes:indexSet forKey:@"servers"];
[[NSNotificationCenter defaultCenter] postNotificationName:SyphonServerUpdateNotification object:self userInfo:serverInfo];
}
pthread_mutex_unlock(&_mutateLock);
}
@end
@implementation NSDictionary (SyphonServerDirectoryPimpMyDictionary)
/*
This all seems a bit silly, and it is probably easier and has no performance penalty to pass the NSImage through the notification.
NSImage is NSCoding compliant, so that should work and save us a bit of hassle and this bit of code.
*/
- (NSDictionary *)pimpedVersionForSyphon
{
if ([self objectForKey:SyphonServerDescriptionIconKey] == nil)
{
NSString *appName = [self objectForKey:SyphonServerDescriptionAppNameKey];
NSImage *appImage = nil;
for(NSRunningApplication* app in [[NSWorkspace sharedWorkspace] runningApplications])
{
if([appName isEqualToString:[app localizedName]])
{
appImage = [app icon];
}
}
if(appImage != nil)
{
NSMutableDictionary *newDictionary = [NSMutableDictionary dictionaryWithDictionary:self];
[newDictionary setObject:appImage forKey:SyphonServerDescriptionIconKey];
return newDictionary;
}
}
return self;
}
@end
@implementation NSArray (SyphonServerDirectoryServerSearch)
/*
UUID is the only sure identity, as other members of a dictionary may change, unhelpfully yeilding a NO for isEqual
*/
- (NSUInteger)indexOfDescriptionForSyphonServerUUID:(NSString *)uuid
{
if (uuid == nil)
{
return NSNotFound;
}
else
{
return [self indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
if ([[obj objectForKey:SyphonServerDescriptionUUIDKey] isEqualToString:uuid])
{
return YES;
} else {
return NO;
}
}];
}
}
@end