Change many ivar names.

(debug_proxy): Temporarily set to 1.
([Proxy +newForRemoteTarget:connection:]): Set new ivar names.
Embellish debugging message to include connection.
([Proxy -invalidateProxy]): Return void instead of self.
([Proxy +encodeObject:withConnectedCoder:]): Overhauled, encode tag,
and change order of encoding for more efficient encoding.  Added many
comments.
([Proxy +newWithCoder:]): Likewise, to match.  Also added a check to
make sure we don't return a bogus pointer for a local object; do this
by checking with connection's +includesLocalObject; if it's not
there, raise an exception.
([Proxy -forward::]): Call the new forwarding method name in Connection.


git-svn-id: svn+ssh://svn.gna.org/svn/gnustep/libs/base/trunk@1062 72102866-910b-0410-8b05-ffd578937521
This commit is contained in:
Andrew McCallum 1996-03-06 14:16:04 +00:00
parent 7cda86ef49
commit e2b839730f

View file

@ -26,10 +26,13 @@
#include <objects/stdobjects.h> #include <objects/stdobjects.h>
#include <objects/Proxy.h> #include <objects/Proxy.h>
#include <objects/Connection.h> #include <objects/Connection.h>
#include <objects/Port.h>
#include <objects/TcpPort.h>
#include <objects/ConnectedCoder.h> #include <objects/ConnectedCoder.h>
#include <Foundation/NSException.h>
#include <assert.h> #include <assert.h>
static BOOL debug_proxy = NO; static int debug_proxy = 1;
#if NeXT_runtime #if NeXT_runtime
static id tmp_kludge_protocol = nil; static id tmp_kludge_protocol = nil;
@ -51,31 +54,41 @@ static id tmp_kludge_protocol = nil;
} }
#endif #endif
/* This is the designated initializer. */ /* This is the designated initializer. */
+ newForRemoteTarget: (unsigned)aTarget connection: (Connection*)c
+ newForRemoteTarget: (unsigned)target connection: (Connection*)connection
{ {
Proxy *newProxy; Proxy *new_proxy;
if ((newProxy = [c proxyForTarget: aTarget])) /* If there already is a proxy for this target/connection combination,
return newProxy; don't create a new one, just return the old one. */
if ((new_proxy = [connection proxyForTarget: target]))
return new_proxy;
newProxy = class_create_instance ([Proxy class]); /* There isn't one already created; make a new proxy object,
newProxy->target = aTarget; and set its ivars. */
newProxy->connection = c; new_proxy = class_create_instance (self);
newProxy->retain_count = 0; new_proxy->_target = target;
new_proxy->_connection = connection;
new_proxy->_retain_count = 0;
#if NeXT_runtime #if NeXT_runtime
newProxy->_method_types = coll_hash_new(32, new_proxy->_method_types = coll_hash_new(32,
elt_hash_void_ptr, elt_hash_void_ptr,
elt_compare_void_ptrs); elt_compare_void_ptrs);
newProxy->protocol = nil; new_proxy->protocol = nil;
#endif #endif
if (debug_proxy) if (debug_proxy)
printf("%s: proxy=0x%x name %u\n", printf("Created new proxy=0x%x target 0x%x conn 0x%x\n",
sel_get_name(_cmd), (unsigned)newProxy, newProxy->target); (unsigned)new_proxy,
(unsigned)new_proxy->_target,
(unsigned)connection);
[c addProxy: newProxy]; /* Register this proxy with the connection. */
return newProxy; [connection addProxy: new_proxy];
return new_proxy;
} }
- notImplemented: (SEL)aSel - notImplemented: (SEL)aSel
@ -101,11 +114,10 @@ static id tmp_kludge_protocol = nil;
} }
#endif #endif
- invalidateProxy - (void) invalidateProxy
{ {
/* What should go here? */ /* What should go here? */
[connection removeProxy: self]; [_connection removeProxy: self];
return self;
} }
- (BOOL) isProxy - (BOOL) isProxy
@ -124,7 +136,7 @@ static id tmp_kludge_protocol = nil;
return object_get_class (self); return object_get_class (self);
} }
static inline BOOL class_is_kind_of(Class self, Class aClassObject) static inline BOOL class_is_kind_of (Class self, Class aClassObject)
{ {
Class class; Class class;
@ -134,123 +146,203 @@ static inline BOOL class_is_kind_of(Class self, Class aClassObject)
return NO; return NO;
} }
/* Encoding and Decoding Proxies on the wire. */
/* 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
};
+ (void) encodeObject: anObject withConnectedCoder: aRmc + (void) encodeObject: anObject withConnectedCoder: aRmc
{ {
unsigned aTarget; unsigned proxy_target;
BOOL willBeLocal; unsigned char proxy_tag;
assert([aRmc connection]); Connection *encoder_connection;
if (class_is_kind_of (object_get_class (anObject), [Proxy class]))
encoder_connection = [aRmc connection];
assert (encoder_connection);
/* Find out if anObject is a proxy or not. */
if (class_is_kind_of (object_get_class (anObject), self))
{ {
/* anObject is a Proxy, or a Proxy subclass */ /* anObject is a Proxy, or a Proxy subclass */
aTarget = [anObject targetForProxy]; Connection *proxy_connection = [anObject connectionForProxy];
if ([aRmc connection] == [anObject connectionForProxy]) proxy_target = [anObject targetForProxy];
if (encoder_connection == proxy_connection)
{ {
/* This proxy is local on the other side */ /* This proxy is a local object on the other side */
willBeLocal = YES; proxy_tag = PROXY_LOCAL_FOR_RECEIVER;
/* xxx Perhaps we could re-order these encodings, and if (debug_proxy)
not bother to send this `nil' if willBeLocal is YES. */ fprintf(stderr, "Sending a proxy, will be local 0x%x "
[aRmc encodeBycopyObject:nil "connection 0x%x\n",
withName:@"Proxy is local on other side"]; [anObject targetForProxy],
[aRmc encodeValueOfObjCType: @encode(unsigned) (unsigned)proxy_connection);
at: &aTarget [aRmc encodeValueOfCType: @encode(typeof(proxy_tag))
withName: @"Object Proxy target"]; at: &proxy_tag
[aRmc encodeValueOfObjCType: @encode(BOOL) withName: @"Proxy is local for receiver"];
at: &willBeLocal [aRmc encodeValueOfCType: @encode(typeof(proxy_target))
withName: @"Proxy willBeLocal"]; at: &proxy_target
withName: @"Proxy target"];
} }
else else
{ {
/* This proxy will still be remote on the other side */ /* This proxy will still be remote on the other side */
id op = [[anObject connectionForProxy] outPort]; OutPort *proxy_connection_out_port = [proxy_connection outPort];
willBeLocal = NO;
assert (proxy_connection_out_port);
assert (proxy_connection_out_port != [encoder_connection outPort]);
/* xxx Remove this after debugging, because it won't be true
for connections across different hosts. */
assert ([(id)proxy_connection_out_port portNumber]
!= [(id)[encoder_connection outPort] portNumber]);
assert ([proxy_connection inPort] == [encoder_connection inPort]);
proxy_tag = PROXY_REMOTE_FOR_BOTH;
if (debug_proxy) if (debug_proxy)
fprintf(stderr, "Sending a triangle-connection proxy\n"); fprintf(stderr, "Sending triangle-connection proxy 0x%x "
"proxy-conn 0x%x to-conn 0x%x\n",
[anObject targetForProxy],
(unsigned)proxy_connection, (unsigned)encoder_connection);
/* It's remote here, so we need to tell other side where to form /* It's remote here, so we need to tell other side where to form
triangle connection to */ triangle connection to */
[aRmc encodeBycopyObject: op [aRmc encodeValueOfCType: @encode(typeof(proxy_tag))
at: &proxy_tag
withName: @"Proxy is 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"]; withName: @"Proxy outPort"];
[aRmc encodeValueOfObjCType: @encode(unsigned)
at: &aTarget
withName: @"Object Proxy target"];
[aRmc encodeValueOfObjCType: @encode(BOOL)
at: &willBeLocal
withName: @"Proxy willBeLocal"];
} }
} }
else else
{ {
/* anObject is a non-Proxy object, e.g. NSObject. */ /* anObject is a non-Proxy object, e.g. NSObject. */
/* Now were sending this object across the wire in proxy form. */ /* But now were sending this object across the wire in proxy form. */
aTarget = PTR2LONG(anObject); proxy_target = PTR2LONG(anObject);
willBeLocal = NO; proxy_tag = PROXY_LOCAL_FOR_SENDER;
/* Let the connection know that we're going, this also retains anObj */ if (debug_proxy)
fprintf(stderr, "Sending a proxy for local 0x%x\n",
(unsigned)anObject);
/* Let the connection know that we're going; this also retains anObj;
it's OK to send -addLocalObject: more than once for the same
object, because it will only really get added and retained once. */
[[aRmc connection] addLocalObject: anObject]; [[aRmc connection] addLocalObject: anObject];
/* if nil port, other connection will use ConnectedCoder replyPort */
[aRmc encodeBycopyObject: nil [aRmc encodeValueOfCType: @encode(typeof(proxy_tag))
withName: @"Proxy outPort == remotePort"]; at: &proxy_tag
[aRmc encodeValueOfObjCType: @encode(unsigned) withName: @"Proxy is local for the sender"];
at: &aTarget [aRmc encodeValueOfCType: @encode(typeof(proxy_target))
withName: @"Object Proxy target"]; at: &proxy_target
[aRmc encodeValueOfObjCType: @encode(BOOL) withName: @"Proxy target"];
at: &willBeLocal
withName: @"Proxy willBeLocal"];
} }
} }
+ newWithCoder: aRmc + newWithCoder: aRmc
{ {
unsigned new_target; unsigned char proxy_tag;
id newConnectionOutPort; unsigned target;
id c; id proxy_connection;
BOOL willBeLocal; id decoder_connection;
if ([aRmc class] != [ConnectedCoder class]) if ([aRmc class] != [ConnectedDecoder class])
[self error:"Proxy objects only code with ConnectedCoder class"]; [self error:"Proxy objects only decode with ConnectedDecoder class"];
assert([aRmc connection]);
[aRmc decodeObjectAt: &newConnectionOutPort withName: NULL];
[aRmc decodeValueOfObjCType: @encode(unsigned)
at: &new_target
withName: NULL];
[aRmc decodeValueOfObjCType: @encode(BOOL)
at: &willBeLocal
withName: NULL];
if (newConnectionOutPort)
{
c = [Connection newForInPort:[[aRmc connection] inPort]
outPort:newConnectionOutPort
ancestorConnection:[aRmc connection]];
}
else
{
c = [aRmc connection];
}
if (!willBeLocal) decoder_connection = [aRmc connection];
assert (decoder_connection);
/* First get the tag, so we know what values need to be decoded. */
[aRmc decodeValueOfCType: @encode(typeof(proxy_tag))
at: &proxy_tag
withName: NULL];
switch (proxy_tag)
{ {
case PROXY_LOCAL_FOR_RECEIVER:
/* This was a proxy on the other side of the connection, but
here it's local. Just get the target address, make sure
that it is indeed the address of a local object that we
vended to the remote connection, then simply return the target
casted to (id). */
[aRmc decodeValueOfCType: @encode(typeof(target))
at: &target
withName: NULL];
if (debug_proxy) if (debug_proxy)
printf("returning remote Proxy, target=0x%x\n", new_target); fprintf(stderr, "Recieving a proxy for local object 0x%x "
return [self newForRemoteTarget: new_target connection: c]; "connection 0x%x\n", target, (unsigned)decoder_connection);
} if (![[decoder_connection class] includesLocalObject: (id)target])
else [NSException raise: @"ProxyDecodedBadTarget"
format: @"No local object with given address"];
return (id) target;
case PROXY_LOCAL_FOR_SENDER:
/* 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 [Proxy +newForRemoteTarget:connection:]; this will return
the proxy object we already created for this target, or create
a new proxy object if necessary. */
[aRmc decodeValueOfCType: @encode(typeof(target))
at: &target
withName: NULL];
if (debug_proxy)
fprintf(stderr, "Receiving a proxy, was local 0x%x connection 0x%x\n",
(unsigned)target, (unsigned)decoder_connection);
return [self newForRemoteTarget: target
connection: decoder_connection];
case PROXY_REMOTE_FOR_BOTH:
/* 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
ConnectedDecoder 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
[Connection +newForInPort:outPort:ancestorConnection:]; this
will either return the connection already created for this
inPort/outPort pair, or create a new connection if necessary. */
{ {
assert(new_target); Connection *proxy_connection;
id proxy_connection_out_port = nil;
[aRmc decodeValueOfCType: @encode(typeof(target))
at: &target
withName: NULL];
[aRmc decodeObjectAt: &proxy_connection_out_port
withName: NULL];
assert (proxy_connection_out_port);
proxy_connection = [[decoder_connection class]
newForInPort: [decoder_connection inPort]
outPort: proxy_connection_out_port
ancestorConnection: decoder_connection];
assert (proxy_connection != decoder_connection);
if (debug_proxy) if (debug_proxy)
printf("returning local Object, target=0x%x\n", new_target); fprintf(stderr, "Receiving a triangle-connection proxy 0x%x "
/* xxx I should add something that makes sure this number is a "connection 0x%x\n", target, (unsigned)proxy_connection);
valid object address... offer a little protection against bad return [self newForRemoteTarget: target
clients. */ connection: proxy_connection];
return (id)new_target;
} }
default:
[self error: "Bad proxy tag"];
}
/* Not reached. */
return nil;
} }
- (unsigned) targetForProxy - (unsigned) targetForProxy
{ {
return target; return _target;
} }
- connectionForProxy - connectionForProxy
{ {
return connection; return _connection;
} }
- (const char *) selectorTypeForProxy: (SEL)selector - (const char *) selectorTypeForProxy: (SEL)selector
@ -290,8 +382,8 @@ static inline BOOL class_is_kind_of(Class self, Class aClassObject)
return t; return t;
} }
#endif /* 1 */ #endif /* 1 */
#else #else /* NeXT_runtime */
return sel_get_type(selector); return sel_get_type (selector);
#endif #endif
} }
@ -299,7 +391,7 @@ static inline BOOL class_is_kind_of(Class self, Class aClassObject)
- (oneway void) release - (oneway void) release
{ {
if (!retain_count--) if (!_retain_count--)
{ {
[self invalidateProxy]; [self invalidateProxy];
[self dealloc]; [self dealloc];
@ -308,15 +400,15 @@ static inline BOOL class_is_kind_of(Class self, Class aClassObject)
- retain - retain
{ {
retain_count++; _retain_count++;
return self; return self;
} }
- (void) dealloc - (void) dealloc
{ {
#if NeXT_runtime #if NeXT_runtime
coll_hash_delete(_method_types); coll_hash_delete (_method_types);
object_dispose((Object*)self); object_dispose ((Object*)self);
#else #else
NSDeallocateObject ((id)self); NSDeallocateObject ((id)self);
#endif #endif
@ -326,7 +418,7 @@ static inline BOOL class_is_kind_of(Class self, Class aClassObject)
{ {
if (debug_proxy) if (debug_proxy)
printf("Proxy forwarding %s\n", sel_get_name(aSel)); printf("Proxy forwarding %s\n", sel_get_name(aSel));
return [connection forwardForProxy: self return [_connection forwardForProxy: self
selector: aSel selector: aSel
argFrame: frame]; argFrame: frame];
} }
@ -336,7 +428,7 @@ static inline BOOL class_is_kind_of(Class self, Class aClassObject)
- (unsigned) retainCount - (unsigned) retainCount
{ {
return retain_count; return _retain_count;
} }
- autorelease - autorelease
@ -366,7 +458,7 @@ static inline BOOL class_is_kind_of(Class self, Class aClassObject)
return NULL; return NULL;
} }
#else #else
return sel_get_type(selector); return sel_get_type (selector);
#endif #endif
} }
@end @end