2001-12-17 14:31:42 +00:00
|
|
|
|
/** Implementation of NSPortNameServer class for Distributed Objects
|
2000-02-24 22:45:18 +00:00
|
|
|
|
Copyright (C) 1998,1999,2000 Free Software Foundation, Inc.
|
1998-10-29 12:50:23 +00:00
|
|
|
|
|
|
|
|
|
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
|
2007-09-14 11:36:11 +00:00
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
1998-10-29 12:50:23 +00:00
|
|
|
|
License as published by the Free Software Foundation; either
|
2008-06-08 10:38:33 +00:00
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
1998-10-29 12:50:23 +00:00
|
|
|
|
|
|
|
|
|
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
|
2019-12-09 23:36:00 +00:00
|
|
|
|
Lesser General Public License for more details.
|
1998-10-29 12:50:23 +00:00
|
|
|
|
|
2007-09-14 11:36:11 +00:00
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
1998-10-29 12:50:23 +00:00
|
|
|
|
License along with this library; if not, write to the Free
|
2024-11-07 13:37:59 +00:00
|
|
|
|
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
|
2001-12-18 16:54:15 +00:00
|
|
|
|
|
|
|
|
|
<title>NSPortNameServer class reference</title>
|
|
|
|
|
$Date$ $Revision$
|
1998-10-29 12:50:23 +00:00
|
|
|
|
*/
|
|
|
|
|
|
2010-02-19 08:12:46 +00:00
|
|
|
|
#import "common.h"
|
2024-05-30 09:40:52 +00:00
|
|
|
|
#import "GSPThread.h"
|
2007-11-29 20:53:26 +00:00
|
|
|
|
#import "Foundation/NSException.h"
|
|
|
|
|
#import "Foundation/NSEnumerator.h"
|
|
|
|
|
#import "Foundation/NSPort.h"
|
|
|
|
|
#import "Foundation/NSPortNameServer.h"
|
|
|
|
|
#import "Foundation/NSLock.h"
|
|
|
|
|
#import "Foundation/NSUserDefaults.h"
|
|
|
|
|
#import "GSPortPrivate.h"
|
1998-10-29 12:50:23 +00:00
|
|
|
|
|
1999-09-21 19:07:27 +00:00
|
|
|
|
|
2003-07-22 08:52:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* The abstract port name server class. This defines an API for
|
|
|
|
|
* working with port name servers ... objects used to manage access
|
|
|
|
|
* to ports in the distributed objects system (see [NSConnection]).
|
|
|
|
|
*/
|
1999-09-21 19:07:27 +00:00
|
|
|
|
@implementation NSPortNameServer
|
1998-10-29 12:50:23 +00:00
|
|
|
|
|
|
|
|
|
+ (id) allocWithZone: (NSZone*)aZone
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException
|
2005-02-22 11:22:44 +00:00
|
|
|
|
format: @"attempt to create extra port name server"];
|
1998-10-29 12:50:23 +00:00
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
|
{
|
|
|
|
|
if (self == [NSPortNameServer class])
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-22 08:52:37 +00:00
|
|
|
|
/**
|
2005-11-21 09:59:42 +00:00
|
|
|
|
* <p>Returns the default port name server for the process.<br />
|
|
|
|
|
* This is a nameserver for host-local connections private to the current
|
|
|
|
|
* user. If you with to create public connections or connections to other
|
|
|
|
|
* hosts, you must use [NSSocketPortNameServer+sharedInstance] instead.
|
|
|
|
|
* </p>
|
|
|
|
|
* This default behavior may be altered by setting the
|
|
|
|
|
* <code>NSPortIsMessagePort</code> user default to NO, in which case
|
2005-11-28 15:41:35 +00:00
|
|
|
|
* an [NSSocketPortNameServer] will be used as the default system name server
|
2005-11-21 09:59:42 +00:00
|
|
|
|
* and you will have to use [NSMessagePortNameServer+sharedInstance]
|
|
|
|
|
* for host-local, private connections.
|
2003-07-22 08:52:37 +00:00
|
|
|
|
*/
|
2000-07-04 09:49:31 +00:00
|
|
|
|
+ (id) systemDefaultPortNameServer
|
1998-10-29 12:50:23 +00:00
|
|
|
|
{
|
2005-11-21 09:59:42 +00:00
|
|
|
|
static id nameServer = nil;
|
2005-11-09 09:39:25 +00:00
|
|
|
|
|
2005-11-21 09:59:42 +00:00
|
|
|
|
if (nameServer == nil)
|
2004-08-07 00:44:15 +00:00
|
|
|
|
{
|
2024-05-30 09:40:52 +00:00
|
|
|
|
static gs_mutex_t classLock = GS_MUTEX_INIT_STATIC;
|
|
|
|
|
|
|
|
|
|
GS_MUTEX_LOCK(classLock);
|
2005-11-21 09:59:42 +00:00
|
|
|
|
if (nameServer == nil)
|
2005-04-12 09:48:04 +00:00
|
|
|
|
{
|
2005-11-21 09:59:42 +00:00
|
|
|
|
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
|
|
|
|
|
id o;
|
|
|
|
|
|
|
|
|
|
if ([defs objectForKey: @"NSPortIsMessagePort"] != nil
|
|
|
|
|
&& [defs boolForKey: @"NSPortIsMessagePort"] == NO)
|
|
|
|
|
{
|
|
|
|
|
o = [NSSocketPortNameServer sharedInstance];
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
o = [NSMessagePortNameServer sharedInstance];
|
|
|
|
|
}
|
|
|
|
|
nameServer = RETAIN(o);
|
2005-04-12 09:48:04 +00:00
|
|
|
|
}
|
2024-05-30 09:40:52 +00:00
|
|
|
|
GS_MUTEX_UNLOCK(classLock);
|
2005-04-12 09:48:04 +00:00
|
|
|
|
}
|
2005-11-21 09:59:42 +00:00
|
|
|
|
return nameServer;
|
1998-10-29 12:50:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
|
|
|
|
[NSException raise: NSGenericException
|
2005-02-22 11:22:44 +00:00
|
|
|
|
format: @"attempt to deallocate default port name server"];
|
2006-06-04 06:42:10 +00:00
|
|
|
|
GSNOSUPERDEALLOC;
|
1998-10-29 12:50:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-22 08:52:37 +00:00
|
|
|
|
/**
|
|
|
|
|
* Looks up the port with the specified name on the local host and
|
|
|
|
|
* returns it or nil if no port is found with that name.<br />
|
|
|
|
|
* Different nameservers have different namespaces appropriate to the
|
|
|
|
|
* type of port they deal with, so failing to find a named port with one
|
|
|
|
|
* nameserver does not guarantee that a port does with that name does
|
|
|
|
|
* not exist.<br />
|
|
|
|
|
* This is a convenience method calling -portForName:onHost: with a nil
|
|
|
|
|
* host argument.
|
|
|
|
|
*/
|
1998-10-29 12:50:23 +00:00
|
|
|
|
- (NSPort*) portForName: (NSString*)name
|
|
|
|
|
{
|
|
|
|
|
return [self portForName: name onHost: nil];
|
|
|
|
|
}
|
|
|
|
|
|
2003-07-22 08:52:37 +00:00
|
|
|
|
/** <override-subclass />
|
|
|
|
|
* Looks up the port with the specified name on host and returns it
|
|
|
|
|
* or nil if no port is found with that name.<br />
|
|
|
|
|
* Different nameservers have different namespaces appropriate to the
|
|
|
|
|
* type of port they deal with, so failing to find a named port with one
|
|
|
|
|
* nameserver does not guarantee that a port does with that name does
|
|
|
|
|
* not exist.
|
|
|
|
|
*/
|
2002-10-05 17:47:54 +00:00
|
|
|
|
- (NSPort*) portForName: (NSString*)name
|
|
|
|
|
onHost: (NSString*)host
|
|
|
|
|
{
|
2003-07-15 05:21:34 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return nil;
|
1998-10-29 12:50:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-22 08:52:37 +00:00
|
|
|
|
/** <override-subclass />
|
|
|
|
|
* Registers port with the supplied name, so that other processes can
|
|
|
|
|
* look it up to contact it. A port may be registered with more than
|
|
|
|
|
* one name by making multiple calls to this method.<br />
|
|
|
|
|
* Returns YES on success, NO otherwise.<br />
|
|
|
|
|
* The common cause for failure is that another port is already registered
|
|
|
|
|
* with the name.
|
|
|
|
|
* Raises NSInvalidArgumentException if given bad arguments.
|
|
|
|
|
*/
|
1998-10-29 12:50:23 +00:00
|
|
|
|
- (BOOL) registerPort: (NSPort*)port
|
|
|
|
|
forName: (NSString*)name
|
|
|
|
|
{
|
2003-07-15 05:21:34 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return NO;
|
1998-10-29 12:50:23 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-22 08:52:37 +00:00
|
|
|
|
/** <override-subclass />
|
|
|
|
|
* Removes any port registration for the supplied name (whether
|
|
|
|
|
* registered in the current process or another).<br />
|
|
|
|
|
* The common cause for failure is that no port is registered
|
|
|
|
|
* with the name.<br />
|
|
|
|
|
* Raises NSInvalidArgumentException if given bad arguments.
|
|
|
|
|
*/
|
2001-07-15 09:08:27 +00:00
|
|
|
|
- (BOOL) removePortForName: (NSString*)name
|
1998-10-29 12:50:23 +00:00
|
|
|
|
{
|
2003-07-15 05:21:34 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return NO;
|
1998-10-29 12:50:23 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
2003-07-15 05:21:34 +00:00
|
|
|
|
/**
|
|
|
|
|
* Some extensions to make cleaning up port names easier.
|
|
|
|
|
*/
|
1998-10-30 08:40:03 +00:00
|
|
|
|
@implementation NSPortNameServer (GNUstep)
|
2003-07-22 08:52:37 +00:00
|
|
|
|
/** <override-subclass />
|
|
|
|
|
* Return all names that have been registered with the receiver for port.
|
2000-02-04 18:14:13 +00:00
|
|
|
|
*/
|
|
|
|
|
- (NSArray*) namesForPort: (NSPort*)port
|
|
|
|
|
{
|
2003-07-15 05:21:34 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return nil;
|
2000-02-04 18:14:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-15 05:21:34 +00:00
|
|
|
|
/**
|
2003-07-22 08:52:37 +00:00
|
|
|
|
* Remove all names registered with the receiver for port.
|
|
|
|
|
* Probably inefficient ... subclasses might want to override this.
|
1998-10-30 08:40:03 +00:00
|
|
|
|
*/
|
2001-07-15 09:08:27 +00:00
|
|
|
|
- (BOOL) removePort: (NSPort*)port
|
1998-10-30 08:40:03 +00:00
|
|
|
|
{
|
2003-07-15 05:21:34 +00:00
|
|
|
|
NSEnumerator *e = [[self namesForPort: port] objectEnumerator];
|
|
|
|
|
NSString *n;
|
|
|
|
|
BOOL removed = NO;
|
1998-10-30 08:40:03 +00:00
|
|
|
|
|
2003-07-15 05:21:34 +00:00
|
|
|
|
while ((n = [e nextObject]) != nil)
|
|
|
|
|
{
|
|
|
|
|
if ([self removePort: port forName: n] == YES)
|
1998-10-30 08:40:03 +00:00
|
|
|
|
{
|
2003-07-15 05:21:34 +00:00
|
|
|
|
removed = YES;
|
1998-10-30 08:40:03 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2003-07-15 05:21:34 +00:00
|
|
|
|
return removed;
|
2000-02-04 18:14:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2003-07-22 08:52:37 +00:00
|
|
|
|
/** <override-subclass />
|
|
|
|
|
* Remove the name if and only if it is registered with the receiver
|
|
|
|
|
* for the given port.
|
2000-02-04 18:14:13 +00:00
|
|
|
|
*/
|
2001-07-15 09:08:27 +00:00
|
|
|
|
- (BOOL) removePort: (NSPort*)port forName: (NSString*)name
|
2000-02-04 18:14:13 +00:00
|
|
|
|
{
|
2003-07-15 05:21:34 +00:00
|
|
|
|
[self subclassResponsibility: _cmd];
|
|
|
|
|
return NO;
|
1998-10-30 08:40:03 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
1999-09-21 19:07:27 +00:00
|
|
|
|
|