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 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
|
/* Classes for integration of avahi-client into NSRunLoop.
Copyright (C) 2010 Free Software Foundation, Inc.
Written by: Niels Grewe <niels.grewe@halbordnung.de>
Date: March 2010
This file is part of the GNUstep Base Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#import "GSAvahiRunLoopIntegration.h"
#define CTX(x) GSAvahiRunLoopContext *ctx = (GSAvahiRunLoopContext*)x
@interface GSAvahiWatcher: NSObject <RunLoopEvents>
{
//The callback to call for avahi.
AvahiWatchCallback callback;
BOOL callbackInProgress;
AvahiWatchEvent oldEvents;
AvahiWatchEvent lastEvent;
int fileDesc;
GSAvahiRunLoopContext *ctx;
void *userData;
}
- (void)listenForEvents: (AvahiWatchEvent)events;
- (AvahiWatchEvent)getEvents;
- (void)removeFromContext;
- (void)setContext: (GSAvahiRunLoopContext*)aCtxt;
- (void)unschedule;
- (void)reschedule;
@end
@implementation GSAvahiWatcher
- (void) listenForEvents: (AvahiWatchEvent)events
saveState: (BOOL)saveState
{
/* FIXME: NSRunLoop doesn't expose equivalents for POLLERR and POLLHUP but
* Avahi doesn't seem to strictly require them (their Qt API doesn't handle
* them either). Still, it would be nice to handle AVAHI_WATCH_(ERR|HUP)
* here.
*/
// Remove old events:
if (!(events & AVAHI_WATCH_IN)
&& (oldEvents & AVAHI_WATCH_IN))
{
[[ctx runLoop] removeEvent: (void*)(intptr_t)fileDesc
type: ET_RDESC
forMode: [ctx mode]
all: NO];
}
if (!(events & AVAHI_WATCH_OUT)
&& (oldEvents & AVAHI_WATCH_OUT))
{
[[ctx runLoop] removeEvent: (void*)(intptr_t)fileDesc
type: ET_WDESC
forMode: [ctx mode]
all: NO];
}
// Remember event state:
if (saveState)
{
oldEvents = events;
}
// Dispatch new events to the runLoop:
if (events & AVAHI_WATCH_IN)
{
[[ctx runLoop] addEvent: (void*)(intptr_t)fileDesc
type: ET_RDESC
watcher: self
forMode: [ctx mode]];
}
if (events & AVAHI_WATCH_OUT)
{
[[ctx runLoop] addEvent: (void*)(intptr_t)fileDesc
type: ET_WDESC
watcher: self
forMode: [ctx mode]];
}
}
- (void) listenForEvents: (AvahiWatchEvent)events
{
[self listenForEvents: events
saveState: YES];
}
- (void) unschedule
{
/* Don't save the new event state (i.e. no events) if we are unscheduling
* the watcher. We might want to reschedule it with the prior state.
*/
[self listenForEvents: (AvahiWatchEvent)0
saveState: NO];
}
- (void) reschedule
{
[self listenForEvents: oldEvents
saveState: NO];
}
- (id) initWithCallback: (AvahiWatchCallback)cback
andContext: (GSAvahiRunLoopContext*)aCtx
onEvent: (AvahiWatchEvent)someEvents
forFd: (int)fd
userData: (void*)ud
{
if (nil == (self = [super init]))
{
return nil;
}
fileDesc = fd;
// The context retains its watchers and timers:
ctx = aCtx;
callback = cback;
userData = ud;
[self listenForEvents: someEvents];
return self;
}
- (AvahiWatchEvent) getEvents
{
if (callbackInProgress)
{
return (AvahiWatchEvent)0;
}
return lastEvent;
}
- (void) removeFromContext
{
[self unschedule];
[ctx removeWatcher: self];
// Don't reference the context anymore, since it won't have any chance of
// notifying us if it goes away.
ctx = nil;
}
- (void) receivedEvent: (void*)data
type: (RunLoopEventType)type
extra: (void*)extra
forMode: (NSString*)mode
{
int fd = (int)(intptr_t)data;
if (fileDesc != fd)
{
//Not good
return;
}
/* FIXME ... in the following switch, as well as setting lastEvent the
* code was clearing the corresponding bit in the oldEvents bitmask.
* This was causng a crash becasue it meant that we didn't unregister
* the event watcher from the run loop before deallocating it and a
* new incoming event was sent to the deallocated instance.
* I therefore removed that code, but can't see what it was intended
* to do.
*/
switch (type)
{
case ET_RDESC:
lastEvent = AVAHI_WATCH_IN;
break;
case ET_WDESC:
lastEvent = AVAHI_WATCH_OUT;
break;
default:
return;
}
/* FIXME: NSRunLoop doesn't expose equivalents for POLLERR and POLLHUP but
* Avahi doesn't seem to strictly require them (their Qt API doesn't handle
* them either).
*/
callbackInProgress = YES;
callback((AvahiWatch*)self, fd, lastEvent, userData);
callbackInProgress = NO;
}
- (void) setContext: (GSAvahiRunLoopContext*)aCtxt
{
ctx = aCtxt;
}
- (void) dealloc
{
// Remove all leftover event-handlers from the runLoop:
[self listenForEvents: (AvahiWatchEvent)0];
[super dealloc];
}
@end
@interface GSAvahiTimer: NSObject
{
GSAvahiRunLoopContext *ctx;
AvahiTimeoutCallback callback;
NSTimer *timer;
NSDate *fireDate;
void *userData;
}
@end
@implementation GSAvahiTimer
- (void) didTimeout: (NSTimer*)timer
{
callback((AvahiTimeout*)self, userData);
}
- (void) setTimerToInterval: (NSTimeInterval)interval
{
// Invalidate the old timer;
if (timer != nil)
{
[timer invalidate];
timer = nil;
}
// NOTE: the timer ivar is a weak reference; runloops retain their
// timers.
timer = [NSTimer timerWithTimeInterval: interval
target: self
selector: @selector(didTimeout:)
userInfo: nil
repeats: NO];
[[ctx runLoop] addTimer: timer
forMode: [ctx mode]];
}
- (void) setTimerToTimeval: (const struct timeval*)tv
{
// Invalidate the old timer
if (timer != nil)
{
[timer invalidate];
timer = nil;
}
if (NULL != tv)
{
// Construct a NSTimeInterval for the timer:
NSTimeInterval interval = (NSTimeInterval)tv->tv_sec;
interval += (NSTimeInterval)(tv->tv_usec / 1000000.0);
[self setTimerToInterval: interval];
}
}
- (id) initWithCallback: (AvahiTimeoutCallback)aCallback
andContext: (GSAvahiRunLoopContext*)aCtx
forTimeval: (const struct timeval*)tv
userData: (void*)ud
{
if (nil == (self = [super init]))
{
return nil;
}
// The context retains its watchers and timeouts:
ctx = aCtx;
callback = aCallback;
userData = ud;
[self setTimerToTimeval: tv];
return self;
}
- (void) unschedule
{
if ([timer isValid])
{
fireDate = [[timer fireDate] retain];
[timer invalidate];
timer = nil;
}
}
- (void) removeFromContext
{
[self unschedule];
[ctx removeTimeout: self];
ctx = nil;
}
- (void) setContext: (GSAvahiRunLoopContext*)aCtxt
{
ctx = aCtxt;
}
- (void) reschedule
{
// Only reschedule if fireDate has been set, otherwise the Avahi layer will
// schedule a new timer.
if (nil != fireDate)
{
NSTimeInterval interval = [fireDate timeIntervalSinceNow];
[self setTimerToInterval: MAX(0.1,interval)];
[fireDate release];
fireDate = nil;
}
}
- (void) dealloc
{
if (nil != timer)
{
[timer invalidate];
}
[super dealloc];
}
@end
static AvahiWatch*
GSAvahiWatchNew(const AvahiPoll *api, int fd, AvahiWatchEvent
event, AvahiWatchCallback callback, void *userData)
{
// NOTE: strangly enough, the userData parameter is not the userdata we
// passed to the poll structure (it is somehow related to the dbus
// internals).
CTX(api->userdata);
GSAvahiWatcher *w = [ctx avahiWatcherWithCallback: callback
onEvent: event
forFileDescriptor: fd
userData: userData];
// NOTE: avahi defines AvahiWatch as a struct, since we only pass around
// pointers to those, we can just cast the pointer to our watcher object to
// AvahiWatch*.
return (AvahiWatch*)w;
}
static void
GSAvahiWatchUpdate(AvahiWatch *watch, AvahiWatchEvent event)
{
[(GSAvahiWatcher*)watch listenForEvents: event];
}
static AvahiWatchEvent
GSAvahiWatchGetEvents(AvahiWatch *watch)
{
return [(GSAvahiWatcher*)watch getEvents];
}
static void
GSAvahiWatchFree(AvahiWatch *watch)
{
[(GSAvahiWatcher*)watch removeFromContext];
}
static AvahiTimeout*
GSAvahiTimeoutNew(const AvahiPoll *api,
const struct timeval *tv, AvahiTimeoutCallback callback, void *userData)
{
// NOTE: strangly enough, the userData parameter is not the userdata we
// passed to the poll structure (it is somehow related to the dbus
// internals.)
CTX(api->userdata);
GSAvahiTimer *t = [ctx avahiTimerWithCallback: callback
withTimeval: tv
userData: userData];
// NOTE: Cf. GSAvahiWatchNew().
return (AvahiTimeout*)t;
}
static void
GSAvahiTimeoutUpdate(AvahiTimeout* timeout,
const struct timeval *tv)
{
[(GSAvahiTimer*)timeout setTimerToTimeval: tv];
}
static void
GSAvahiTimeoutFree(AvahiTimeout* timeout)
{
[(GSAvahiTimer*)timeout removeFromContext];
}
@implementation GSAvahiRunLoopContext
- (id) initWithRunLoop: (NSRunLoop*)rl
forMode: (NSString*)aMode
{
if (nil == (self = [super init]))
{
return nil;
}
lock = [[NSLock alloc] init];
[lock setName: @"GSAvahiRunLoopContextLock"];
poll = malloc(sizeof(AvahiPoll));
NSAssert(poll, @"Could not allocate avahi polling structure.");
poll->userdata = (void*)self; //userInfo
poll->watch_new = GSAvahiWatchNew; //create a new GSAvahiWatcher
poll->watch_update = GSAvahiWatchUpdate; //update the watcher
poll->watch_get_events = GSAvahiWatchGetEvents; //retrieve events
poll->watch_free = GSAvahiWatchFree; //remove watcher from context
poll->timeout_new = GSAvahiTimeoutNew; //create a new GSAvahiTimer
poll->timeout_update = GSAvahiTimeoutUpdate; //update the timer
poll->timeout_free = GSAvahiTimeoutFree; //remove the timer from context
//Runloops don't need to be retained;
runLoop = rl;
ASSIGNCOPY(mode,aMode);
children = [[NSMutableArray alloc] init];
return self;
}
- (NSRunLoop*) runLoop
{
// NOTE: We don't protect this with the lock because it will only ever be
// changed by -removeFromRunLoop:forMode: or -scheduleInRunLoop:forMode:,
// which is where we do the locking.
return runLoop;
}
- (NSString*) mode
{
/* NOTE: We don't protect this with the lock because it will only ever be
* changed by -removeFromRunLoop:forMode: or -scheduleInRunLoop:forMode:,
* which is where we do the locking.
*/
return mode;
}
- (const AvahiPoll*) avahiPoll
{
return (const AvahiPoll*)poll;
}
- (GSAvahiTimer*) avahiTimerWithCallback: (AvahiTimeoutCallback)callback
withTimeval: (const struct timeval*)tv
userData: (void*)ud
{
GSAvahiTimer *timer = nil;
[lock lock];
timer = [[[GSAvahiTimer alloc] initWithCallback: callback
andContext: self
forTimeval: tv
userData: ud] autorelease];
if (nil != timer)
{
[children addObject: timer];
}
[lock unlock];
return timer;
}
- (GSAvahiWatcher*) avahiWatcherWithCallback: (AvahiWatchCallback)callback
onEvent: (AvahiWatchEvent)someEvents
forFileDescriptor: (NSInteger)fd
userData: (void*)ud
{
GSAvahiWatcher *w = nil;
[lock lock];
w = [[[GSAvahiWatcher alloc] initWithCallback: callback
andContext: self
onEvent: someEvents
forFd: fd
userData: ud] autorelease];
if (nil != w)
{
[children addObject: w];
}
[lock unlock];
return w;
}
- (void) removeChild: (id)c
{
if (nil != c)
{
[lock lock];
[children removeObject: c];
[lock unlock];
}
}
- (void) removeWatcher: (GSAvahiWatcher*)w
{
[self removeChild: w];
}
- (void) removeTimeout: (GSAvahiTimer*)at
{
[self removeChild: at];
}
- (void) removeFromRunLoop: (NSRunLoop*)rl
forMode: (NSString*)m
{
[lock lock];
if ((rl == runLoop) && [mode isEqualToString: m])
{
FOR_IN(GSAvahiWatcher*, child, children)
{
[child unschedule];
}
END_FOR_IN(children)
runLoop = nil;
[mode release];
mode = nil;
}
[lock unlock];
}
- (void) scheduleInRunLoop: (NSRunLoop*)rl
forMode: (NSString*)m
{
[lock lock];
if ((runLoop == nil) && (mode == nil)
&& ((rl != nil) && (m != nil)))
{
runLoop = rl;
ASSIGNCOPY(mode,m);
FOR_IN(GSAvahiWatcher*, child, children)
{
[child reschedule];
}
END_FOR_IN(children)
}
[lock unlock];
}
- (void) dealloc
{
/* Some avahi internals might still reference the poll structure and could
* try to create additional watchers and timers on the runloop, so we should
* clean it up properly:
*/
poll->userdata = (void*)NULL;
[self removeFromRunLoop: runLoop
forMode: mode];
FOR_IN(GSAvahiWatcher*, child, children)
{
[child setContext: nil];
}
END_FOR_IN(children)
free(poll);
poll = NULL;
[children release];
[mode release];
[lock release];
[super dealloc];
}
@end
|