-
Notifications
You must be signed in to change notification settings - Fork 5
/
SyphonNameboundClient.m
279 lines (238 loc) · 8.43 KB
/
SyphonNameboundClient.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
/*
SyphonNameboundClient.m
Syphon (Implementations)
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 "SyphonNameboundClient.h"
@interface SyphonNameboundClient (Private)
- (void)setClientFromSearchHavingLock:(BOOL)isLocked;
@end
@implementation SyphonNameboundClient
- (id)initWithContext:(CGLContextObj)context
{
self = [super init];
if (self)
{
_lock = OS_SPINLOCK_INIT;
_searchPending = YES;
_context = CGLRetainContext(context);
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleServerAnnounce:) name:SyphonServerAnnounceNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleServerUpdate:) name:SyphonServerUpdateNotification object:nil];
}
return self;
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
[_name release];
[_appname release];
[_client release];
[_lockedClient release];
if (_context)
{
CGLReleaseContext(_context);
}
[super dealloc];
}
- (NSString *)name
{
NSString *result;
OSSpinLockLock(&_lock);
result = [[_name retain] autorelease];
OSSpinLockUnlock(&_lock);
return result;
}
- (void)setName:(NSString *)name
{
OSSpinLockLock(&_lock);
[name retain];
[_name release];
_name = name;
_searchPending = YES;
OSSpinLockUnlock(&_lock);
}
- (NSString *)appName
{
NSString *result;
OSSpinLockLock(&_lock);
result = [[_appname retain] autorelease];
OSSpinLockUnlock(&_lock);
return result;
}
- (void)setAppName:(NSString *)app
{
OSSpinLockLock(&_lock);
[app retain];
[_appname release];
_appname = app;
_searchPending = YES;
OSSpinLockUnlock(&_lock);
}
- (void)lockClient
{
OSSpinLockLock(&_lock);
if (_lockedClient == nil)
{
if (_searchPending)
{
[self setClientFromSearchHavingLock:YES];
_searchPending = NO;
}
_lockedClient = [_client retain];
}
OSSpinLockUnlock(&_lock);
}
- (void)unlockClient
{
SyphonClient *doneWith;
OSSpinLockLock(&_lock);
doneWith = _lockedClient;
_lockedClient = nil;
OSSpinLockUnlock(&_lock);
[doneWith release]; // release outside the lock as it may take time
}
- (SyphonClient *)client
{
return _lockedClient;
}
- (void)setClient:(SyphonClient *)client havingLock:(BOOL)isLocked
{
SyphonClient *newClient = [client retain];
SyphonClient *oldClient;
if (!isLocked) OSSpinLockLock(&_lock);
oldClient = _client;
_client = newClient;
if (!isLocked) OSSpinLockUnlock(&_lock);
// If we were registered for notifications and no longer require them
// remove ourself from the notification center
if (oldClient == nil && newClient != nil)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:SyphonServerAnnounceNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleServerRetire:) name:SyphonServerRetireNotification object:nil];
}
// If we weren't registered already, but need to register now, do so
if (newClient == nil && oldClient != nil)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleServerAnnounce:) name:SyphonServerAnnounceNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:SyphonServerRetireNotification object:nil];
}
// Release the old client
[oldClient release];
}
- (BOOL)parametersMatchDescription:(NSDictionary *)description
{
NSString *searchName = self.name;
NSString *searchApp = self.appName;
if ([searchName length] == 0)
{
searchName = nil;
}
if ([searchApp length] == 0)
{
searchApp = nil;
}
if ((!searchName || [[description objectForKey:SyphonServerDescriptionNameKey] isEqualToString:searchName])
&& (!searchApp || [[description objectForKey:SyphonServerDescriptionAppNameKey] isEqualToString:searchApp]))
{
return YES;
}
return NO;
}
- (void)setClientFromSearchHavingLock:(BOOL)isLocked
{
SyphonClient *newClient = nil;
if (!isLocked) OSSpinLockLock(&_lock);
NSArray *matches = [[SyphonServerDirectory sharedDirectory] serversMatchingName:_name appName:_appname];
if ([matches count] != 0)
{
NSString *current = [_client.serverDescription objectForKey:SyphonServerDescriptionUUIDKey];
NSString *found = [[matches lastObject] objectForKey:SyphonServerDescriptionUUIDKey];
if (found && [current isEqualToString:found])
{
newClient = [_client retain];
}
else
{
newClient = [[SyphonClient alloc] initWithServerDescription:[matches lastObject] context:_context options:nil newFrameHandler:nil];
}
}
[self setClient:newClient havingLock:YES];
if (!isLocked) OSSpinLockUnlock(&_lock);
[newClient release];
}
+ (NSDictionary *)serverInfoFromNotification:(NSNotification *)notification
{
// This deals with a change in notifications from Syphon.
// Once all projects using SyphonNameboundClient are updated to newer Syphon.framework
// we won't need this method and can always use notification.userInfo.
if ([notification.userInfo isKindOfClass:[NSDictionary class]])
{
return notification.userInfo;
}
else
{
return notification.object;
}
}
- (void)handleServerAnnounce:(NSNotification *)notification
{
NSDictionary *newInfo = [SyphonNameboundClient serverInfoFromNotification:notification];
// If we don't have a client, or our current client doesn't match our parameters any more
if ((_client == nil || ![self parametersMatchDescription:[_client serverDescription]])
&& [self parametersMatchDescription:newInfo])
{
SyphonClient *newClient = [[SyphonClient alloc] initWithServerDescription:newInfo context:_context options:nil newFrameHandler:nil];
[self setClient:newClient havingLock:NO];
[newClient release];
}
}
- (void)handleServerUpdate:(NSNotification *)notification
{
NSDictionary *newInfo = [SyphonNameboundClient serverInfoFromNotification:notification];
NSDictionary *currentServer = [_client serverDescription];
// It's possible our client hasn't received the update yet, so we can't trust its server description
// so check if the new update is for our client...
if ([[currentServer objectForKey:SyphonServerDescriptionUUIDKey] isEqualToString:[newInfo objectForKey:SyphonServerDescriptionUUIDKey]])
{
// ...and if so, check to see if our parameters no longer describe the server
if (![self parametersMatchDescription:newInfo])
{
[self setClient:nil havingLock:NO];
}
}
// If we don't have a matching client but this client's new details match, then set up a new client
if (_client == nil && [self parametersMatchDescription:newInfo])
{
SyphonClient *newClient = [[SyphonClient alloc] initWithServerDescription:newInfo context:_context options:nil newFrameHandler:nil];
[self setClient:newClient havingLock:NO];
[newClient release];
}
}
- (void)handleServerRetire:(NSNotification *)notification
{
NSString *retiringUUID = [[SyphonNameboundClient serverInfoFromNotification:notification] objectForKey:SyphonServerDescriptionUUIDKey];
NSString *ourUUID = [[_client serverDescription] objectForKey:SyphonServerDescriptionUUIDKey];
if ([retiringUUID isEqualToString:ourUUID])
{
[self setClientFromSearchHavingLock:NO];
}
}
@end