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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
|
/**
Copyright (C) 1998-2009 Free Software Foundation, Inc.
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
Created: October 1998
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.
<title>NSDistributedNotificationCenter class reference</title>
$Date$ $Revision$
*/
#import "common.h"
#define EXPOSE_NSDistributedNotificationCenter_IVARS 1
#import "Foundation/NSConnection.h"
#import "Foundation/NSDistantObject.h"
#import "Foundation/NSException.h"
#import "Foundation/NSFileManager.h"
#import "Foundation/NSArchiver.h"
#import "Foundation/NSNotification.h"
#import "Foundation/NSDate.h"
#import "Foundation/NSPathUtilities.h"
#import "Foundation/NSRunLoop.h"
#import "Foundation/NSTask.h"
#import "GNUstepBase/NSTask+GNUstepBase.h"
#import "Foundation/NSDistributedNotificationCenter.h"
#import "Foundation/NSUserDefaults.h"
#import "Foundation/NSHost.h"
#import "Foundation/NSPortNameServer.h"
#import "Foundation/NSThread.h"
#import "../Tools/gdnc.h"
@interface NSDistributedNotificationCenter (Private)
- (void) _connect;
- (void) _invalidated: (NSNotification*)notification;
- (void) postNotificationName: (NSString*)name
object: (NSString*)object
userInfo: (NSData*)info
selector: (NSString*)aSelector
to: (uint64_t)observer;
@end
/**
* <p>The <code>NSDistributedNotificationCenter</code> provides a versatile yet
* simple mechanism for objects in different processes to communicate
* effectively while knowing very little about each others' internals.<br />
* A distributed notification center acts much like a normal
* notification center, but it handles notifications on a machine-wide
* (or local network wide) basis rather than just notifications within
* a single process. Objects are able to register themselves as
* observers for particular notification names and objects, and they
* will then receive notifications (including optional user information
* consisting of a dictionary of property-list objects) as they are posted.
* </p>
* <p>Since posting of distributed notifications involves inter-process
* (and sometimes inter-host) communication, it is fundamentally slower
* than normal notifications, and should be used relatively sparingly.
* In order to help with this, the <code>NSDistributedNotificationCenter</code>
* provides a notion of 'suspension', whereby a center can be suspended
* causing notifications for observers in the process where the center
* was suspended to cease receiving notifications. Observers can
* specify how notifications are to be handled in this case (queued
* or discarded) and posters can specify that particular notifications
* are to be delivered immediately irrespective of suspension.
* </p>
* <p>Distributed notifications are mediated by a server process which
* handles all notifications for a particular center type. In GNUstep
* this process is the <code>gdnc</code> tool, and when started without special
* options, a gdnc process acts as the local centre for the host it is
* running on. When started with the <code>GSNetwork</code> user default set
* to YES, the <code>gdnc</code> tool acts as a local network wide server (you
* should only run one copy of <code>gdnc</code> like this on your LAN).<br />
* The <code>gdnc</code> process should be started at machine boot time, but
* GNUstep will attempt to start it automatically if it can't find it.
* </p>
* <p>MacOS-X currently defines only a notification center for the
* local host. GNUstep also defines a local network center which can
* be used from multiple hosts. By default the system sends this to
* any gdnc process it can find which is configured as a network-wide
* server, but the <code>GDNCHost</code> user default may be used to specify a
* particular host to be contacted ... this may be of use where you
* wish to have logically separate clusters of machines on a shared LAN.
* </p>
*/
@implementation NSDistributedNotificationCenter
static NSDistributedNotificationCenter *locCenter = nil;
static NSDistributedNotificationCenter *pubCenter = nil;
static NSDistributedNotificationCenter *netCenter = nil;
+ (id) allocWithZone: (NSZone*)z
{
[NSException raise: NSInternalInconsistencyException
format: @"Should not call +alloc for NSDistributedNotificationCenter"];
return nil;
}
/**
* Returns the default notification center ... a shared notification
* center for the local host. This is simply a convenience method
* equivalent to calling +notificationCenterForType: with
* <code>NSLocalNotificationCenterType</code> as its argument.
*/
+ (id) defaultCenter
{
return [self notificationCenterForType: NSLocalNotificationCenterType];
}
/**
* Returns a notification center of the specified type.<br />
* The <code>NSLocalNotificationCenterType</code> provides a shared access to
* a notification center used by processes on the local host which belong to
* the current user.<br />
* The <code>GSPublicNotificationCenterType</code> provides a shared access to
* a notification center used by processes on the local host belonging to
* any user.<br />
* The <code>GSNetworkNotificationCenterType</code> provides a shared access to
* a notification center used by processes on the local network.<br />
* MacOS-X supports only <code>NSLocalNotificationCenterType</code>.
*/
+ (NSDistributedNotificationCenter*) notificationCenterForType: (NSString*)type
{
if ([type isEqual: NSLocalNotificationCenterType] == YES)
{
if (locCenter == nil)
{
[gnustep_global_lock lock];
if (locCenter == nil)
{
NS_DURING
{
NSDistributedNotificationCenter *tmp;
tmp = (NSDistributedNotificationCenter*)
NSAllocateObject(self, 0, NSDefaultMallocZone());
tmp->_centerLock = [NSRecursiveLock new];
tmp->_type = RETAIN(NSLocalNotificationCenterType);
locCenter = [NSObject leak: tmp];
[tmp release];
}
NS_HANDLER
{
[gnustep_global_lock unlock];
[localException raise];
}
NS_ENDHANDLER
}
[gnustep_global_lock unlock];
}
return locCenter;
}
else if ([type isEqual: GSPublicNotificationCenterType] == YES)
{
if (pubCenter == nil)
{
[gnustep_global_lock lock];
if (pubCenter == nil)
{
NS_DURING
{
NSDistributedNotificationCenter *tmp;
tmp = (NSDistributedNotificationCenter*)
NSAllocateObject(self, 0, NSDefaultMallocZone());
tmp->_centerLock = [NSRecursiveLock new];
tmp->_type = RETAIN(GSPublicNotificationCenterType);
pubCenter = [NSObject leak: tmp];
[tmp release];
}
NS_HANDLER
{
[gnustep_global_lock unlock];
[localException raise];
}
NS_ENDHANDLER
}
[gnustep_global_lock unlock];
}
return pubCenter;
}
else if ([type isEqual: GSNetworkNotificationCenterType] == YES)
{
if (netCenter == nil)
{
[gnustep_global_lock lock];
if (netCenter == nil)
{
NS_DURING
{
NSDistributedNotificationCenter *tmp;
tmp = (NSDistributedNotificationCenter*)
NSAllocateObject(self, 0, NSDefaultMallocZone());
tmp->_centerLock = [NSRecursiveLock new];
tmp->_type = RETAIN(GSNetworkNotificationCenterType);
netCenter = [NSObject leak: tmp];
[tmp release];
}
NS_HANDLER
{
[gnustep_global_lock unlock];
[localException raise];
}
NS_ENDHANDLER
}
[gnustep_global_lock unlock];
}
return netCenter;
}
else
{
[NSException raise: NSInvalidArgumentException
format: @"Unknown center type (%@)", type];
return nil; /* NOT REACHED */
}
}
- (void) dealloc
{
if ([[_remote connectionForProxy] isValid])
{
[_remote unregisterClient: (id<GDNCClient>)self];
}
RELEASE(_remote);
RELEASE(_type);
[super dealloc];
}
/**
* Should not be used.
*/
- (id) init
{
DESTROY(self);
[NSException raise: NSInternalInconsistencyException
format: @"Should not call -init for NSDistributedNotificationCenter"];
return nil;
}
/**
* Adds an observer to the receiver. Calls
* -addObserver:selector:name:object:suspensionBehavior: with
* <code>NSNotificationSuspensionBehaviorCoalesce</code>.
*/
- (void) addObserver: (id)anObserver
selector: (SEL)aSelector
name: (NSString*)notificationName
object: (NSString*)anObject
{
[self addObserver: anObserver
selector: aSelector
name: notificationName
object: anObject
suspensionBehavior: NSNotificationSuspensionBehaviorCoalesce];
}
/**
* Adds an observer to the receiver.<br />
* When a notification matching notificationName and anObject is
* sent to the center, the object anObserver is sent the message
* aSelector with the notification info dictionary as its argument.<br />
* The suspensionBehavior governs how the center deals with notifications
* when the process to which the notification should be delivered is
* suspended:
* <deflist>
* <term><code>NSNotificationSuspensionBehaviorDrop</code></term>
* <desc>
* Discards the notification if the observing process is suspended.
* </desc>
* <term><code>NSNotificationSuspensionBehaviorCoalesce</code></term>
* <desc>
* Discards previously queued notifications when the observing process
* is suspended, leaving only the last notification posted in the queue.
* Delivers this single notification when the process becomes unsuspended.
* </desc>
* <term><code>NSNotificationSuspensionBehaviorHold</code></term>
* <desc>
* Queues notifications when the observing process is suspended,
* delivering all the queued notifications when the process becomes
* unsuspended again.
* </desc>
* <term><code>NSNotificationSuspensionBehaviorDeliverImmediately</code></term>
* <desc>
* Deliver the notification immediately, even if the destination
* process is suspended.
* </desc>
* </deflist>
*/
- (void) addObserver: (id)anObserver
selector: (SEL)aSelector
name: (NSString*)notificationName
object: (NSString*)anObject
suspensionBehavior: (NSNotificationSuspensionBehavior)suspensionBehavior
{
if (anObserver == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"nil observer"];
}
if (aSelector == 0)
{
[NSException raise: NSInvalidArgumentException
format: @"null selector"];
}
if (notificationName != nil
&& [notificationName isKindOfClass: [NSString class]] == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"invalid notification name"];
}
if (anObject != nil && [anObject isKindOfClass: [NSString class]] == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"invalid notification object"];
}
if (anObject == nil && notificationName == nil)
{
[NSException raise: NSInvalidArgumentException
format: @"notification name and object both nil"];
}
[_centerLock lock];
NS_DURING
{
[self _connect];
[(id<GDNCProtocol>)_remote addObserver: (uint64_t)(uintptr_t)anObserver
selector: NSStringFromSelector(aSelector)
name: notificationName
object: anObject
suspensionBehavior: suspensionBehavior
for: (id<GDNCClient>)self];
}
NS_HANDLER
{
[_centerLock unlock];
[localException raise];
}
NS_ENDHANDLER
[_centerLock unlock];
}
/**
* Posts the notification to the center using
* postNotificationName:object:userInfo:deliverImmediately: with the
* delivery flag set to NO.
*/
- (void) postNotification: (NSNotification*)notification
{
[self postNotificationName: [notification name]
object: [notification object]
userInfo: [notification userInfo]
deliverImmediately: NO];
}
/**
* Posts the notificationName and anObject to the center using
* postNotificationName:object:userInfo:deliverImmediately: with the
* user info set to nil and the delivery flag set to NO.
*/
- (void) postNotificationName: (NSString*)notificationName
object: (NSString*)anObject
{
[self postNotificationName: notificationName
object: anObject
userInfo: nil
deliverImmediately: NO];
}
/**
* Posts the notificationName, anObject and userInfo to the center using
* postNotificationName:object:userInfo:deliverImmediately: with the
* delivery flag set to NO.
*/
- (void) postNotificationName: (NSString*)notificationName
object: (NSString*)anObject
userInfo: (NSDictionary*)userInfo
{
[self postNotificationName: notificationName
object: anObject
userInfo: userInfo
deliverImmediately: NO];
}
/**
* The primitive notification posting method ...<br />
* The userInfo dictionary may contain only property-list objects.<br />
* The deliverImmediately flag specifies whether the suspension
* state of the receiving process is to be ignored.
*/
- (void) postNotificationName: (NSString*)notificationName
object: (NSString*)anObject
userInfo: (NSDictionary*)userInfo
deliverImmediately: (BOOL)deliverImmediately
{
if (notificationName == nil
|| [notificationName isKindOfClass: [NSString class]] == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"invalid notification name"];
}
if (anObject != nil && [anObject isKindOfClass: [NSString class]] == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"invalid notification object"];
}
[_centerLock lock];
NS_DURING
{
NSData *d;
[self _connect];
d = [NSArchiver archivedDataWithRootObject: userInfo];
[(id<GDNCProtocol>)_remote postNotificationName: notificationName
object: anObject
userInfo: d
deliverImmediately: deliverImmediately
for: (id<GDNCClient>)self];
}
NS_HANDLER
{
[_centerLock unlock];
[localException raise];
}
NS_ENDHANDLER
[_centerLock unlock];
}
/**
* Removes the observer from the center.
*/
- (void) removeObserver: (id)anObserver
name: (NSString*)notificationName
object: (NSString*)anObject
{
if (notificationName != nil
&& [notificationName isKindOfClass: [NSString class]] == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"invalid notification name"];
}
if (anObject != nil && [anObject isKindOfClass: [NSString class]] == NO)
{
[NSException raise: NSInvalidArgumentException
format: @"invalid notification object"];
}
[_centerLock lock];
NS_DURING
{
[self _connect];
[(id<GDNCProtocol>)_remote removeObserver: (uint64_t)(uintptr_t)anObserver
name: notificationName
object: anObject
for: (id<GDNCClient>)self];
}
NS_HANDLER
{
[_centerLock unlock];
[localException raise];
}
NS_ENDHANDLER
[_centerLock unlock];
}
/**
* Sets the suspension state of the receiver ... if the receiver is
* suspended, it won't handle notification until it is unsuspended
* again, unless the notifications are posted to be delivered
* immediately.
*/
- (void) setSuspended: (BOOL)flag
{
[_centerLock lock];
NS_DURING
{
[self _connect];
_suspended = flag;
[(id<GDNCProtocol>)_remote setSuspended: flag for: (id<GDNCClient>)self];
}
NS_HANDLER
{
[_centerLock unlock];
[localException raise];
}
NS_ENDHANDLER
[_centerLock unlock];
}
/**
* Returns the current suspension state of the receiver.
*/
- (BOOL) suspended
{
return _suspended;
}
@end
/*
* The following dummy class is here solely as a workaround for pre 3.3
* versions of gcc where protocols didn't work properly unless implemented
* in the source where the '@protocol()' directive is used.
*/
@interface NSDistributedNotificationCenterDummy : NSObject <GDNCProtocol>
- (void) addObserver: (uint64_t)anObserver
selector: (NSString*)aSelector
name: (NSString*)notificationname
object: (NSString*)anObject
suspensionBehavior: (NSNotificationSuspensionBehavior)suspensionBehavior
for: (id<GDNCClient>)client;
- (oneway void) postNotificationName: (NSString*)notificationName
object: (NSString*)anObject
userInfo: (NSData*)d
deliverImmediately: (BOOL)deliverImmediately
for: (id<GDNCClient>)client;
- (void) registerClient: (id<GDNCClient>)client;
- (void) removeObserver: (uint64_t)anObserver
name: (NSString*)notificationname
object: (NSString*)anObject
for: (id<GDNCClient>)client;
- (void) setSuspended: (BOOL)flag
for: (id<GDNCClient>)client;
- (void) unregisterClient: (id<GDNCClient>)client;
@end
@implementation NSDistributedNotificationCenterDummy
- (void) addObserver: (uint64_t)anObserver
selector: (NSString*)aSelector
name: (NSString*)notificationname
object: (NSString*)anObject
suspensionBehavior: (NSNotificationSuspensionBehavior)suspensionBehavior
for: (id<GDNCClient>)client
{
}
- (oneway void) postNotificationName: (NSString*)notificationName
object: (NSString*)anObject
userInfo: (NSData*)d
deliverImmediately: (BOOL)deliverImmediately
for: (id<GDNCClient>)client
{
}
- (void) registerClient: (id<GDNCClient>)client
{
}
- (void) removeObserver: (uint64_t)anObserver
name: (NSString*)notificationname
object: (NSString*)anObject
for: (id<GDNCClient>)client
{
}
- (void) setSuspended: (BOOL)flag
for: (id<GDNCClient>)client
{
}
- (void) unregisterClient: (id<GDNCClient>)client
{
}
@end
@implementation NSDistributedNotificationCenter (Private)
/**
* Establish a connection to the server. This method should only be called
* when protected by the center's lock, so that it is thread-safe.
*/
- (void) _connect
{
if (_remote == nil)
{
NSString *host = nil;
NSString *service = nil;
NSString *description = nil;
NSString *alternate = nil;
NSPortNameServer *ns = nil;
Protocol *p = @protocol(GDNCProtocol);
NSConnection *c;
if (_type == NSLocalNotificationCenterType)
{
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
if ([defs objectForKey: @"NSPortIsMessagePort"] != nil
&& [defs boolForKey: @"NSPortIsMessagePort"] == NO)
{
ns = [NSSocketPortNameServer sharedInstance];
}
else
{
ns = [NSMessagePortNameServer sharedInstance];
}
host = @"";
service = GDNC_SERVICE;
description = @"local host";
}
else if (_type == GSPublicNotificationCenterType)
{
/*
* Connect to the NSDistributedNotificationCenter for this host.
*/
host = [[NSUserDefaults standardUserDefaults]
stringForKey: @"NSHost"];
if (host == nil)
{
host = @"";
}
else
{
NSHost *h;
/*
* If we have a host specified, but it is the current host,
* we do not need to ask for a host by name (nameserver lookup
* can be faster) and the empty host name can be used to
* indicate that we may start a <code>gdnc</code> server locally.
*/
h = [NSHost hostWithName: host];
if (h == nil)
{
NSLog(@"Unknown -NSHost '%@' ignored", host);
host = @"";
}
else if ([h isEqual: [NSHost currentHost]] == YES)
{
host = @"";
}
else
{
host = [h name];
}
if ([host isEqual: @""] == NO)
{
alternate = [service stringByAppendingFormat: @"-%@", host] ;
}
}
if ([host length] == 0
|| [host isEqualToString: @"localhost"] == YES
|| [host isEqualToString: @"127.0.0.1"] == YES)
{
host = @"";
description = @"local host";
}
else
{
description = host;
}
service = GDNC_SERVICE;
ns = [NSSocketPortNameServer sharedInstance];
}
else if (_type == GSNetworkNotificationCenterType)
{
host = [[NSUserDefaults standardUserDefaults]
stringForKey: @"GDNCHost"];
description = host;
if (host == nil)
{
host = @"*";
description = @"network host";
}
service = GDNC_NETWORK;
ns = [NSSocketPortNameServer sharedInstance];
}
else
{
[NSException raise: NSInternalInconsistencyException
format: @"Unknown center type - %@", _type];
}
_remote = [NSConnection rootProxyForConnectionWithRegisteredName: service
host: host
usingNameServer: ns];
if (_remote == nil && alternate != nil)
{
_remote = [NSConnection rootProxyForConnectionWithRegisteredName:
alternate host: @"*" usingNameServer: ns];
}
if (_remote == nil)
{
NSString *cmd = nil;
NSArray *args = nil;
NSDate *limit;
cmd = [NSTask launchPathForTool: @"gdnc"];
NSDebugMLLog(@"NSDistributedNotificationCenter",
@"\nI couldn't contact the notification server for %@ -\n"
@"so I'm attempting to to start one - which will take a few seconds.\n"
@"Trying to launch gdnc from %@ or a machine/operating-system subdirectory.\n"
@"It is recommended that you start the notification server (gdnc) either at\n"
@"login or (better) when your computer is started up.\n", description,
[cmd stringByDeletingLastPathComponent]);
if (_type == GSNetworkNotificationCenterType)
{
args = [NSArray arrayWithObjects:
@"-GSNetwork", @"YES",
@"--auto",
nil];
}
else if (_type == GSPublicNotificationCenterType)
{
args = [NSArray arrayWithObjects:
@"-GSPublic", @"YES",
@"--auto",
nil];
}
else if ([host length] > 0)
{
args = [NSArray arrayWithObjects:
@"-NSHost", host,
@"--auto",
nil];
}
else
{
args = [NSArray arrayWithObjects:
@"--auto",
nil];
}
[NSTask launchedTaskWithLaunchPath: cmd arguments: args];
limit = [NSDate dateWithTimeIntervalSinceNow: 10.0];
while (_remote == nil && [limit timeIntervalSinceNow] > 0)
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
[NSThread sleepForTimeInterval: 0.05];
_remote = [NSConnection
rootProxyForConnectionWithRegisteredName: service
host: host usingNameServer: ns];
[_remote retain];
[pool drain];
}
if (_remote == nil)
{
[NSException raise: NSInternalInconsistencyException
format: @"unable to contact GDNC server -\n"
@"please check that the gdnc process is running.\n"
@"I attempted to start it at '%@'\n", cmd];
}
}
else
{
[_remote retain];
}
c = [_remote connectionForProxy];
[_remote setProtocolForProxy: p];
/*
* Ensure that this center can be used safely from different
* threads.
*/
[c enableMultipleThreads];
/*
* Ask to be told if the connection goes away.
*/
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(_invalidated:)
name: NSConnectionDidDieNotification
object: c];
[_remote registerClient: (id<GDNCClient>)self];
}
}
- (void) _invalidated: (NSNotification*)notification
{
id connection = [notification object];
/*
* Tidy up now that the connection has gone away.
*/
[[NSNotificationCenter defaultCenter]
removeObserver: self
name: NSConnectionDidDieNotification
object: connection];
NSAssert(connection == [_remote connectionForProxy],
NSInternalInconsistencyException);
RELEASE(_remote);
_remote = nil;
}
- (void) postNotificationName: (NSString*)name
object: (NSString*)object
userInfo: (NSData*)info
selector: (NSString*)aSelector
to: (uint64_t)observer
{
id userInfo;
NSNotification *notification;
id recipient = (id)(uintptr_t)observer;
userInfo = [NSUnarchiver unarchiveObjectWithData: info];
notification = [NSNotification notificationWithName: name
object: object
userInfo: userInfo];
[recipient performSelector: GSSelectorFromNameAndTypes([aSelector cString], 0)
withObject: notification];
}
@end
|