1997-09-01 21:59:51 +00:00
|
|
|
|
/* Implementation for GNU Objective-C version of NSDistantObject
|
|
|
|
|
Copyright (C) 1997 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
|
|
Written by: Richard Frith-Macdonald <richard@brainstorm.co.uk>
|
|
|
|
|
Based on code by: Andrew Kachites McCallum <mccallum@gnu.ai.mit.edu>
|
|
|
|
|
Created: August 1997
|
|
|
|
|
|
|
|
|
|
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 Library 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
|
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Library General Public
|
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
|
Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
1997-11-06 00:51:23 +00:00
|
|
|
|
#include <config.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
|
#include <Foundation/DistributedObjects.h>
|
1999-01-24 09:29:13 +00:00
|
|
|
|
#include <Foundation/NSLock.h>
|
1998-09-26 05:07:02 +00:00
|
|
|
|
#include <Foundation/NSMethodSignature.h>
|
1997-09-01 21:59:51 +00:00
|
|
|
|
#include <Foundation/NSException.h>
|
|
|
|
|
|
|
|
|
|
static int debug_proxy;
|
|
|
|
|
|
1998-06-26 20:39:50 +00:00
|
|
|
|
@interface NSDistantObject (Debug)
|
|
|
|
|
+ (void) setDebug: (int)val;
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation NSDistantObject (Debug)
|
|
|
|
|
+ (void) setDebug: (int)val
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
debug_proxy = val;
|
1998-06-26 20:39:50 +00:00
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
@implementation NSDistantObject
|
|
|
|
|
|
|
|
|
|
/* This is the proxy tag; it indicates where the local object is,
|
|
|
|
|
and determines whether the reply port to the Connection-where-the-
|
|
|
|
|
proxy-is-local needs to encoded/decoded or not. */
|
|
|
|
|
enum
|
|
|
|
|
{
|
|
|
|
|
PROXY_LOCAL_FOR_RECEIVER = 0,
|
|
|
|
|
PROXY_LOCAL_FOR_SENDER,
|
|
|
|
|
PROXY_REMOTE_FOR_BOTH
|
|
|
|
|
};
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
+ (NSDistantObject*) proxyWithLocal: (id)anObject
|
1997-09-01 21:59:51 +00:00
|
|
|
|
connection: (NSConnection*)aConnection
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
NSDistantObject *new_proxy;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
NSAssert([aConnection isValid], NSInternalInconsistencyException);
|
|
|
|
|
if ((new_proxy = [aConnection localForObject: anObject]))
|
|
|
|
|
{
|
|
|
|
|
return new_proxy;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return [[[NSDistantObject alloc] initWithLocal: anObject
|
|
|
|
|
connection: aConnection] autorelease];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-29 21:29:32 +00:00
|
|
|
|
+ (NSDistantObject*) proxyWithTarget: (unsigned)anObject
|
1997-09-01 21:59:51 +00:00
|
|
|
|
connection: (NSConnection*)aConnection
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
NSDistantObject *new_proxy;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
NSAssert([aConnection isValid], NSInternalInconsistencyException);
|
|
|
|
|
if ((new_proxy = [aConnection proxyForTarget: anObject]))
|
|
|
|
|
{
|
|
|
|
|
return new_proxy;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return [[[NSDistantObject alloc] initWithTarget: anObject
|
|
|
|
|
connection: aConnection] autorelease];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (NSConnection*) connectionForProxy
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return _connection;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (_object)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* A proxy for local object retains it's target so that it
|
|
|
|
|
* will continue to exist as long as there is a remote
|
|
|
|
|
* application using it - so we release the object here.
|
|
|
|
|
*/
|
|
|
|
|
[_object release];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* A proxy retains it's connection so that the connection will
|
|
|
|
|
* continue to exist as long as there is a somethig to use it.
|
|
|
|
|
* So we release our reference to the connection here.
|
|
|
|
|
*/
|
|
|
|
|
[_connection release];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
[super dealloc];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
- (void) encodeWithCoder: (NSCoder*)aRmc
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-29 21:29:32 +00:00
|
|
|
|
unsigned proxy_target;
|
1999-01-24 09:29:13 +00:00
|
|
|
|
gsu8 proxy_tag;
|
|
|
|
|
NSConnection *encoder_connection;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if ([aRmc class] != [PortEncoder class])
|
|
|
|
|
[NSException raise: NSGenericException
|
1997-09-01 21:59:51 +00:00
|
|
|
|
format: @"NSDistantObject objects only encode with PortEncoder class"];
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
encoder_connection = [(NSPortCoder*)aRmc connection];
|
|
|
|
|
NSAssert(encoder_connection, NSInternalInconsistencyException);
|
|
|
|
|
if (![encoder_connection isValid])
|
|
|
|
|
[NSException
|
1997-09-01 21:59:51 +00:00
|
|
|
|
raise: NSGenericException
|
|
|
|
|
format: @"Trying to encode to an invalid Connection.\n"
|
|
|
|
|
@"You should request NSConnectionDidDieNotification's and\n"
|
|
|
|
|
@"release all references to the proxy's of invalid Connections."];
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
proxy_target = _handle;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (encoder_connection == _connection)
|
|
|
|
|
{
|
|
|
|
|
if (_object)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* This proxy is a local to us, remote to other side.
|
|
|
|
|
*/
|
|
|
|
|
proxy_tag = PROXY_LOCAL_FOR_SENDER;
|
|
|
|
|
|
|
|
|
|
if (debug_proxy)
|
|
|
|
|
NSLog(@"Sending a proxy, will be remote 0x%x connection 0x%x\n",
|
|
|
|
|
(unsigned)proxy_target,
|
1997-10-01 18:53:50 +00:00
|
|
|
|
(unsigned)_connection);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
[aRmc encodeValueOfCType: @encode(typeof(proxy_tag))
|
|
|
|
|
at: &proxy_tag
|
|
|
|
|
withName: @"Proxy is local for sender"];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
[aRmc encodeValueOfCType: @encode(typeof(proxy_target))
|
|
|
|
|
at: &proxy_target
|
|
|
|
|
withName: @"Proxy target"];
|
1997-10-01 18:53:50 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* This proxy is a local object on the other side.
|
|
|
|
|
*/
|
|
|
|
|
proxy_tag = PROXY_LOCAL_FOR_RECEIVER;
|
|
|
|
|
|
|
|
|
|
if (debug_proxy)
|
|
|
|
|
NSLog(@"Sending a proxy, will be local 0x%x connection 0x%x\n",
|
|
|
|
|
(unsigned)proxy_target,
|
1997-10-01 18:53:50 +00:00
|
|
|
|
(unsigned)_connection);
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
[aRmc encodeValueOfCType: @encode(typeof(proxy_tag))
|
|
|
|
|
at: &proxy_tag
|
|
|
|
|
withName: @"Proxy is local for receiver"];
|
1997-10-01 18:53:50 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
[aRmc encodeValueOfCType: @encode(typeof(proxy_target))
|
|
|
|
|
at: &proxy_target
|
|
|
|
|
withName: @"Proxy target"];
|
1997-10-01 18:53:50 +00:00
|
|
|
|
}
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* This proxy will still be remote on the other side
|
|
|
|
|
*/
|
|
|
|
|
NSPort *proxy_connection_out_port = [_connection sendPort];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
NSAssert(proxy_connection_out_port, NSInternalInconsistencyException);
|
|
|
|
|
NSAssert([proxy_connection_out_port isValid], NSInternalInconsistencyException);
|
|
|
|
|
NSAssert(proxy_connection_out_port != [encoder_connection sendPort], NSInternalInconsistencyException);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
proxy_tag = PROXY_REMOTE_FOR_BOTH;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (debug_proxy)
|
|
|
|
|
NSLog(@"Sending triangle-connection proxy 0x%x "
|
|
|
|
|
@"proxy-conn 0x%x to-conn 0x%x\n",
|
1997-09-01 21:59:51 +00:00
|
|
|
|
(unsigned)_object,
|
|
|
|
|
(unsigned)_connection, (unsigned)encoder_connection);
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
/*
|
|
|
|
|
* It's remote here, so we need to tell other side where to form
|
|
|
|
|
* triangle connection to
|
|
|
|
|
*/
|
|
|
|
|
[aRmc encodeValueOfCType: @encode(typeof(proxy_tag))
|
|
|
|
|
at: &proxy_tag
|
|
|
|
|
withName: @"Proxy remote for both sender and receiver"];
|
|
|
|
|
|
|
|
|
|
[aRmc encodeValueOfCType: @encode(typeof(proxy_target))
|
|
|
|
|
at: &proxy_target
|
|
|
|
|
withName: @"Proxy target"];
|
|
|
|
|
|
|
|
|
|
[aRmc encodeBycopyObject: proxy_connection_out_port
|
|
|
|
|
withName: @"Proxy outPort"];
|
|
|
|
|
/*
|
|
|
|
|
* Make a note that we have passed this on to another process.
|
|
|
|
|
*/
|
|
|
|
|
_isVended = YES;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* This method needs to be implemented to actually do anything.
|
|
|
|
|
*/
|
|
|
|
|
- (void) forwardInvocation: (NSInvocation*)anInvocation
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Not yet implemented '%s'", sel_get_name(_cmd)];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
- (id) initWithCoder: (NSCoder*)coder
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
[NSException raise: NSInvalidArgumentException
|
|
|
|
|
format: @"Not yet implemented '%s'", sel_get_name(_cmd)];
|
|
|
|
|
return nil;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
- (id) initWithLocal: (id)anObject connection: (NSConnection*)aConnection
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
NSDistantObject *new_proxy;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
NSAssert([aConnection isValid], NSInternalInconsistencyException);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
/*
|
|
|
|
|
* If there already is a local proxy for this target/connection
|
|
|
|
|
* combination, don't create a new one, just return the old one.
|
|
|
|
|
*/
|
|
|
|
|
if ((new_proxy = [aConnection localForObject: anObject]))
|
|
|
|
|
{
|
|
|
|
|
[self dealloc];
|
|
|
|
|
return [new_proxy retain];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
_connection = aConnection;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* We retain our target object so it can't disappear while a remote
|
|
|
|
|
* application wants to use it.
|
|
|
|
|
*/
|
|
|
|
|
_object = [anObject retain];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
/*
|
|
|
|
|
* We register this object with the connection using it.
|
|
|
|
|
*/
|
|
|
|
|
[_connection addLocalObject: self];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (debug_proxy)
|
|
|
|
|
NSLog(@"Created new local=0x%x object 0x%x connection 0x%x\n",
|
1997-09-01 21:59:51 +00:00
|
|
|
|
(unsigned)self, (unsigned)_object, (unsigned)_connection);
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return self;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-29 21:29:32 +00:00
|
|
|
|
- (id) initWithTarget: (unsigned)target connection: (NSConnection*)aConnection
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
NSDistantObject *new_proxy;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
NSAssert([aConnection isValid], NSInternalInconsistencyException);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
/*
|
|
|
|
|
* If there already is a proxy for this target/connection combination,
|
|
|
|
|
* don't create a new one, just return the old one.
|
|
|
|
|
*/
|
|
|
|
|
if ((new_proxy = [aConnection proxyForTarget: target]))
|
|
|
|
|
{
|
|
|
|
|
[self dealloc];
|
|
|
|
|
return [new_proxy retain];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
_object = nil;
|
|
|
|
|
_handle = target;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
/*
|
|
|
|
|
* We retain our connection so it can't disappear while the app
|
|
|
|
|
* may want to use it.
|
|
|
|
|
*/
|
|
|
|
|
_connection = [aConnection retain];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
/*
|
|
|
|
|
* We register this object with the connection using it.
|
|
|
|
|
*/
|
|
|
|
|
[_connection addProxy: self];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (debug_proxy)
|
|
|
|
|
NSLog(@"Created new proxy=0x%x object 0x%x connection 0x%x\n",
|
|
|
|
|
(unsigned)self, (unsigned)_object, (unsigned)_connection);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return self;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1998-09-26 05:07:02 +00:00
|
|
|
|
- (NSMethodSignature*) methodSignatureForSelector: (SEL)aSelector
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (_object)
|
|
|
|
|
{
|
|
|
|
|
return [_object methodSignatureForSelector: aSelector];
|
1998-09-26 05:07:02 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (_protocol)
|
|
|
|
|
{
|
|
|
|
|
const char *types = 0;
|
1998-09-29 11:32:53 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
struct objc_method_description* mth;
|
1998-09-29 11:32:53 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
mth = [_protocol descriptionForInstanceMethod: aSelector];
|
|
|
|
|
if (mth == 0)
|
|
|
|
|
{
|
|
|
|
|
mth = [_protocol descriptionForClassMethod: aSelector];
|
1998-09-29 11:32:53 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (mth != 0)
|
|
|
|
|
{
|
|
|
|
|
types = mth->types;
|
1998-09-29 11:32:53 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (types == 0)
|
|
|
|
|
{
|
|
|
|
|
return nil;
|
1998-09-29 11:32:53 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return [NSMethodSignature signatureWithObjCTypes: types];
|
1998-09-29 11:32:53 +00:00
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
arglist_t args;
|
1998-09-29 11:32:53 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
/*
|
|
|
|
|
* No protocol - so try forwarding the message.
|
|
|
|
|
*/
|
|
|
|
|
args = __builtin_apply_args();
|
|
|
|
|
__builtin_return([self forward: _cmd : args]);
|
1998-09-29 11:32:53 +00:00
|
|
|
|
}
|
1998-09-26 05:07:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
1997-09-01 21:59:51 +00:00
|
|
|
|
- (void) setProtocolForProxy: (Protocol*)aProtocol
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
_protocol = aProtocol;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (void) release
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if ([self retainCount] == 2)
|
|
|
|
|
{
|
|
|
|
|
if (_object == nil)
|
|
|
|
|
{
|
|
|
|
|
/*
|
|
|
|
|
* If the only thing retaining us after this release is our
|
|
|
|
|
* connection we must be removed from the connection.
|
|
|
|
|
* Bracket that removal with a retain and release to ensure
|
|
|
|
|
* that we don't have problems when the connection releases us.
|
|
|
|
|
*/
|
|
|
|
|
[super retain];
|
|
|
|
|
[_connection removeProxy: self];
|
|
|
|
|
[super release];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
1999-01-24 09:29:13 +00:00
|
|
|
|
[super release];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation NSDistantObject(GNUstepExtensions)
|
|
|
|
|
|
1999-01-29 21:29:32 +00:00
|
|
|
|
+ newForRemoteTarget: (unsigned)target connection: (NSConnection*)conn
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return [[NSDistantObject alloc] initWithTarget: target connection: conn];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- awakeAfterUsingCoder: aDecoder
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return self;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline BOOL class_is_kind_of (Class self, Class aClassObject)
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
Class class;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
for (class = self; class!=Nil; class = class_get_super_class(class))
|
|
|
|
|
if (class==aClassObject)
|
|
|
|
|
return YES;
|
|
|
|
|
return NO;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ newWithCoder: aRmc
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
gsu8 proxy_tag;
|
1999-01-29 21:29:32 +00:00
|
|
|
|
unsigned target;
|
1999-01-24 09:29:13 +00:00
|
|
|
|
id decoder_connection;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if ([aRmc class] != [PortDecoder class])
|
|
|
|
|
[NSException raise: NSGenericException
|
1997-09-01 21:59:51 +00:00
|
|
|
|
format: @"NSDistantObject objects only decode with PortDecoder class"];
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
decoder_connection = [aRmc connection];
|
|
|
|
|
NSAssert(decoder_connection, NSInternalInconsistencyException);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
/* First get the tag, so we know what values need to be decoded. */
|
|
|
|
|
[aRmc decodeValueOfCType: @encode(typeof(proxy_tag))
|
|
|
|
|
at: &proxy_tag
|
|
|
|
|
withName: NULL];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
switch (proxy_tag)
|
|
|
|
|
{
|
|
|
|
|
case PROXY_LOCAL_FOR_RECEIVER:
|
1997-09-01 21:59:51 +00:00
|
|
|
|
/*
|
|
|
|
|
* This was a proxy on the other side of the connection, but
|
|
|
|
|
* here it's local.
|
1999-01-24 09:29:13 +00:00
|
|
|
|
* Lookup the target handle to ensure that it exists here.
|
1997-09-01 21:59:51 +00:00
|
|
|
|
* Return a retained copy of the local target object.
|
|
|
|
|
*/
|
|
|
|
|
[aRmc decodeValueOfCType: @encode(typeof(target))
|
|
|
|
|
at: &target
|
|
|
|
|
withName: NULL];
|
|
|
|
|
|
|
|
|
|
if (debug_proxy)
|
1999-01-24 09:29:13 +00:00
|
|
|
|
NSLog(@"Receiving a proxy for local object 0x%x "
|
1998-06-26 20:39:50 +00:00
|
|
|
|
@"connection 0x%x\n", target, (unsigned)decoder_connection);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (![[decoder_connection class] includesLocalTarget: target])
|
|
|
|
|
[NSException raise: @"ProxyDecodedBadTarget"
|
|
|
|
|
format: @"No local object with given address"];
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
NSDistantObject *o;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
o = [decoder_connection includesLocalTarget: target];
|
|
|
|
|
if (debug_proxy)
|
|
|
|
|
{
|
|
|
|
|
NSLog(@"Local object is 0x%x (0x%x)\n",
|
|
|
|
|
(unsigned)o, (unsigned)[o localForProxy]);
|
|
|
|
|
}
|
|
|
|
|
[self release];
|
|
|
|
|
return [[o localForProxy] retain];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case PROXY_LOCAL_FOR_SENDER:
|
1997-09-01 21:59:51 +00:00
|
|
|
|
/*
|
|
|
|
|
* This was a local object on the other side of the connection,
|
|
|
|
|
* but here it's a proxy object. Get the target address, and
|
|
|
|
|
* send [NSDistantObject +proxyWithTarget:connection:]; this will
|
|
|
|
|
* return the proxy object we already created for this target, or
|
|
|
|
|
* create a new proxy object if necessary.
|
|
|
|
|
*/
|
1999-01-24 09:29:13 +00:00
|
|
|
|
[aRmc decodeValueOfCType: @encode(typeof(target))
|
|
|
|
|
at: &target
|
|
|
|
|
withName: NULL];
|
|
|
|
|
if (debug_proxy)
|
|
|
|
|
NSLog(@"Receiving a proxy, was local 0x%x connection 0x%x\n",
|
|
|
|
|
(unsigned)target, (unsigned)decoder_connection);
|
|
|
|
|
return [[NSDistantObject proxyWithTarget: target
|
|
|
|
|
connection: decoder_connection] retain];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
case PROXY_REMOTE_FOR_BOTH:
|
1997-09-01 21:59:51 +00:00
|
|
|
|
/*
|
|
|
|
|
* This was a proxy on the other side of the connection, and it
|
|
|
|
|
* will be a proxy on this side too; that is, the local version
|
|
|
|
|
* of this object is not on this host, not on the host the
|
|
|
|
|
* NSPortCoder is connected to, but on a *third* host.
|
|
|
|
|
* This is why I call this a "triangle connection". In addition
|
|
|
|
|
* to decoding the target, we decode the OutPort object that we
|
|
|
|
|
* will use to talk directly to this third host. We send
|
|
|
|
|
* [NSConnection +newForInPort:outPort:ancestorConnection:]; this
|
|
|
|
|
* will either return the connection already created for this
|
|
|
|
|
* inPort/outPort pair, or create a new connection if necessary.
|
|
|
|
|
*/
|
|
|
|
|
{
|
1997-11-26 21:15:12 +00:00
|
|
|
|
NSDistantObject *result;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
NSConnection *proxy_connection;
|
|
|
|
|
NSPort* proxy_connection_out_port = nil;
|
|
|
|
|
|
|
|
|
|
[aRmc decodeValueOfCType: @encode(typeof(target))
|
|
|
|
|
at: &target
|
|
|
|
|
withName: NULL];
|
|
|
|
|
|
|
|
|
|
[aRmc decodeObjectAt: &proxy_connection_out_port
|
|
|
|
|
withName: NULL];
|
|
|
|
|
|
1998-11-19 21:26:27 +00:00
|
|
|
|
NSAssert(proxy_connection_out_port, NSInternalInconsistencyException);
|
1997-11-26 21:15:12 +00:00
|
|
|
|
/* xxx - if there already exists a connection for talking to the
|
|
|
|
|
* out port, we use that one rather than creating a new one from
|
|
|
|
|
* our listening port.
|
|
|
|
|
*
|
|
|
|
|
* First we try for a connection from our receive port,
|
|
|
|
|
* Then we try any connection to the send port
|
|
|
|
|
* Finally we resort to creating a new connection - we don't
|
|
|
|
|
* release the newly created connection - it will get released
|
|
|
|
|
* automatically when no proxies are left on it.
|
|
|
|
|
*/
|
1997-09-01 21:59:51 +00:00
|
|
|
|
proxy_connection = [[decoder_connection class]
|
1997-11-26 21:15:12 +00:00
|
|
|
|
connectionByInPort:
|
|
|
|
|
[decoder_connection receivePort]
|
|
|
|
|
outPort:
|
|
|
|
|
proxy_connection_out_port];
|
|
|
|
|
if (proxy_connection == nil)
|
|
|
|
|
proxy_connection = [[decoder_connection class]
|
|
|
|
|
connectionByOutPort:
|
|
|
|
|
proxy_connection_out_port];
|
|
|
|
|
if (proxy_connection == nil)
|
|
|
|
|
proxy_connection = [[decoder_connection class]
|
1997-09-01 21:59:51 +00:00
|
|
|
|
newForInPort: [decoder_connection receivePort]
|
|
|
|
|
outPort: proxy_connection_out_port
|
|
|
|
|
ancestorConnection: decoder_connection];
|
|
|
|
|
|
|
|
|
|
if (debug_proxy)
|
1998-06-26 20:39:50 +00:00
|
|
|
|
NSLog(@"Receiving a triangle-connection proxy 0x%x "
|
|
|
|
|
@"connection 0x%x\n", target, (unsigned)proxy_connection);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1998-11-19 21:26:27 +00:00
|
|
|
|
NSAssert(proxy_connection != decoder_connection, NSInternalInconsistencyException);
|
|
|
|
|
NSAssert([proxy_connection isValid], NSInternalInconsistencyException);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1997-11-26 21:15:12 +00:00
|
|
|
|
/*
|
|
|
|
|
* If we don't already have a proxy for the object on the
|
|
|
|
|
* remote system, we must tell the other end to retain its
|
|
|
|
|
* local object for our use.
|
|
|
|
|
*/
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if ([proxy_connection includesProxyForTarget: target] == NO)
|
|
|
|
|
[proxy_connection retainTarget: target];
|
1997-11-26 21:15:12 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
result = [[NSDistantObject proxyWithTarget: target
|
1997-09-01 21:59:51 +00:00
|
|
|
|
connection: proxy_connection] retain];
|
1997-11-26 21:15:12 +00:00
|
|
|
|
return result;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
/* xxx This should be something different than NSGenericException. */
|
|
|
|
|
[NSException raise: NSGenericException
|
|
|
|
|
format: @"Bad proxy tag"];
|
|
|
|
|
}
|
|
|
|
|
/* Not reached. */
|
|
|
|
|
return nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- (const char *) selectorTypeForProxy: (SEL)selector
|
|
|
|
|
{
|
|
|
|
|
#if NeXT_runtime
|
|
|
|
|
{
|
|
|
|
|
elt e;
|
|
|
|
|
const char *t;
|
|
|
|
|
e = coll_hash_value_for_key(_method_types, selector);
|
|
|
|
|
t = e.char_ptr_u;
|
|
|
|
|
if (!t)
|
|
|
|
|
{
|
|
|
|
|
/* This isn't what we want, unless the remote machine has
|
|
|
|
|
the same architecture as us. */
|
|
|
|
|
t = [connection _typeForSelector:selector remoteTarget:target];
|
|
|
|
|
coll_hash_add(&_method_types, (void*)selector, t);
|
|
|
|
|
}
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
#else /* NeXT_runtime */
|
|
|
|
|
return sel_get_type (selector);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
- (id) localForProxy
|
|
|
|
|
{
|
|
|
|
|
return _object;
|
|
|
|
|
}
|
|
|
|
|
|
1999-01-29 21:29:32 +00:00
|
|
|
|
- (unsigned) targetForProxy
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return _handle;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- forward: (SEL)aSel :(arglist_t)frame
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (debug_proxy)
|
|
|
|
|
NSLog(@"NSDistantObject forwarding %s\n", sel_get_name(aSel));
|
1997-09-01 21:59:51 +00:00
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if (![_connection isValid])
|
|
|
|
|
[NSException
|
1997-09-01 21:59:51 +00:00
|
|
|
|
raise: NSGenericException
|
|
|
|
|
format: @"Trying to send message to an invalid Proxy.\n"
|
|
|
|
|
@"You should request NSConnectionDidDieNotification's and\n"
|
|
|
|
|
@"release all references to the proxy's of invalid Connections."];
|
|
|
|
|
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return [_connection forwardForProxy: self
|
|
|
|
|
selector: aSel
|
|
|
|
|
argFrame: frame];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- classForCoder
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return object_get_class (self);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- classForPortCoder
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return object_get_class (self);
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
1997-09-12 17:54:10 +00:00
|
|
|
|
- replacementObjectForCoder:(NSCoder*)aCoder
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return self;
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- replacementObjectForPortCoder:(NSPortCoder*)aCoder
|
|
|
|
|
{
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@implementation NSObject (NSDistantObject)
|
|
|
|
|
- (const char *) selectorTypeForProxy: (SEL)selector
|
|
|
|
|
{
|
|
|
|
|
#if NeXT_runtime
|
|
|
|
|
{
|
|
|
|
|
Method m = class_get_instance_method(isa, selector);
|
|
|
|
|
if (m)
|
|
|
|
|
return m->method_types;
|
|
|
|
|
else
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
return sel_get_type (selector);
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
|
@implementation Protocol (DistributedObjectsCoding)
|
|
|
|
|
|
1997-09-29 14:39:53 +00:00
|
|
|
|
- (Class) classForPortCoder
|
1997-09-01 21:59:51 +00:00
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
return [self class];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
- replacementObjectForPortCoder: (NSPortCoder*)aRmc;
|
|
|
|
|
{
|
1999-01-24 09:29:13 +00:00
|
|
|
|
if ([aRmc isBycopy])
|
|
|
|
|
return self;
|
|
|
|
|
else
|
|
|
|
|
return [NSDistantObject proxyWithLocal: self
|
|
|
|
|
connection: [aRmc connection]];
|
1997-09-01 21:59:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|