22
22
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
24
24
<title>NSDistantObject class reference</title>
25
$Date: 2002/02/03 17:21:20 $ $Revision: 1.41 $
25
$Date: 2005/02/22 11:22:44 $ $Revision: 1.65 $
29
#include <base/preface.h>
30
#include <base/DistributedObjects.h>
31
#include <Foundation/NSLock.h>
32
#include <Foundation/NSPort.h>
33
#include <Foundation/NSMethodSignature.h>
34
#include <Foundation/NSException.h>
35
#include <Foundation/NSObjCRuntime.h>
36
#include <Foundation/NSInvocation.h>
29
#include "GNUstepBase/preface.h"
30
#include "GNUstepBase/DistributedObjects.h"
31
#include "Foundation/NSDebug.h"
32
#include "Foundation/NSLock.h"
33
#include "Foundation/NSPort.h"
34
#include "Foundation/NSMethodSignature.h"
35
#include "Foundation/NSException.h"
36
#include "Foundation/NSObjCRuntime.h"
37
#include "Foundation/NSInvocation.h"
38
#define DO_FORWARD_INVOCATION(_SELX, _ARG1) \
39
#define DO_FORWARD_INVOCATION(_SELX, _ARG1) ({ \
39
40
sig = [self methodSignatureForSelector: @selector(_SELX)]; \
40
inv = [NSInvocation invocationWithMethodSignature: sig]; \
41
[inv setSelector: @selector(_SELX)]; \
42
[inv setTarget: self]; \
43
[inv setArgument: (void*)&_ARG1 atIndex: 2]; \
44
[self forwardInvocation: inv]; \
45
[inv getReturnValue: &m]
43
inv = [NSInvocation invocationWithMethodSignature: sig]; \
44
[inv setSelector: @selector(_SELX)]; \
45
[inv setTarget: self]; \
46
[inv setArgument: (void*)&_ARG1 atIndex: 2]; \
47
[self forwardInvocation: inv]; \
48
[inv getReturnValue: &m]; \
52
NSWarnLog(@"DO_FORWARD_INVOCATION failed, got nil signature for '%@'.", \
53
NSStringFromSelector(@selector(_SELX))); \
48
57
static int debug_proxy = 0;
53
62
@defs(NSDistantObject)
65
@interface Object (NSConformsToProtocolNamed)
66
- (BOOL) _conformsToProtocolNamed: (char*)aName;
68
@interface NSObject (NSConformsToProtocolNamed)
69
- (BOOL) _conformsToProtocolNamed: (char*)aName;
72
* Evil hack ... if a remote system wants to know if we conform
73
* to a protocol we pretend we have a local protocol with the same name.
78
@implementation Object (NSConformsToProtocolNamed)
79
- (BOOL) _conformsToProtocolNamed: (char*)aName
82
p.protocol_name = (char*)aName;
83
return [self conformsTo: (Protocol*)&p];
86
@implementation NSObject (NSConformsToProtocolNamed)
87
- (BOOL) _conformsToProtocolNamed: (char*)aName
90
p.protocol_name = (char*)aName;
91
return [self conformsToProtocol: (Protocol*)&p];
56
95
@interface NSConnection (DistantObjectHacks)
57
- (BOOL) includesProxyForTarget: (unsigned)target;
58
- (void) addLocalObject: (id)obj;
59
- (void) addProxy: (id)obj;
96
- (void) aquireProxyForTarget: (unsigned)target;
97
- (NSDistantObject*) retainOrAddLocal: (NSDistantObject*)aProxy
98
forObject: (id)anObject;
99
- (NSDistantObject*) retainOrAddProxy: (NSDistantObject*)aProxy
100
forTarget: (unsigned)aTarget;
60
101
- (void) removeProxy: (id)obj;
102
- (void) vendLocal: (NSDistantObject*)aProxy;
63
105
/* This is the proxy tag; it indicates where the local object is,
64
106
and determines whether the reply port to the Connection-where-the-
65
107
proxy-is-local needs to encoded/decoded or not. */
68
110
PROXY_LOCAL_FOR_RECEIVER = 0,
69
111
PROXY_LOCAL_FOR_SENDER,
70
112
PROXY_REMOTE_FOR_BOTH
512
581
- (id) initWithCoder: (NSCoder*)aCoder
516
id decoder_connection;
519
if ([aCoder isKindOfClass: [NSPortCoder class]] == NO)
522
[NSException raise: NSGenericException
523
format: @"NSDistantObject objects only decode with "
524
@"NSPortCoder class"];
528
decoder_connection = [(NSPortCoder*)aCoder connection];
529
NSAssert(decoder_connection, NSInternalInconsistencyException);
531
/* First get the tag, so we know what values need to be decoded. */
532
[aCoder decodeValueOfObjCType: @encode(typeof(proxy_tag))
537
case PROXY_LOCAL_FOR_RECEIVER:
539
* This was a proxy on the other side of the connection, but
541
* Lookup the target handle to ensure that it exists here.
542
* Return a retained copy of the local target object.
544
[aCoder decodeValueOfObjCType: @encode(typeof(target))
548
NSLog(@"Receiving a proxy for local object 0x%x "
549
@"connection 0x%x\n", target, (gsaddr)decoder_connection);
551
if (![[decoder_connection class] includesLocalTarget: target])
554
[NSException raise: @"ProxyDecodedBadTarget"
555
format: @"No local object with given target (0x%x)",
562
o = [decoder_connection includesLocalTarget: target];
565
NSLog(@"Local object is 0x%x (0x%x)\n",
566
(gsaddr)o, (gsaddr)o ? o->_object : 0);
569
return o ? RETAIN(o->_object) : nil;
572
case PROXY_LOCAL_FOR_SENDER:
574
* This was a local object on the other side of the connection,
575
* but here it's a proxy object. Get the target address, and
576
* send [NSDistantObject +proxyWithTarget:connection:]; this will
577
* return the proxy object we already created for this target, or
578
* create a new proxy object if necessary.
580
[aCoder decodeValueOfObjCType: @encode(typeof(target))
583
NSLog(@"Receiving a proxy, was local 0x%x connection 0x%x\n",
584
target, (gsaddr)decoder_connection);
586
return RETAIN([NSDistantObject proxyWithTarget: target
587
connection: decoder_connection]);
589
case PROXY_REMOTE_FOR_BOTH:
591
* This was a proxy on the other side of the connection, and it
592
* will be a proxy on this side too; that is, the local version
593
* of this object is not on this host, not on the host the
594
* NSPortCoder is connected to, but on a *third* host.
595
* This is why I call this a "triangle connection". In addition
596
* to decoding the target, we decode the OutPort object that we
597
* will use to talk directly to this third host. We send
598
* [NSConnection +newForInPort:outPort:ancestorConnection:]; this
599
* will either return the connection already created for this
600
* inPort/outPort pair, or create a new connection if necessary.
603
NSDistantObject *result;
604
NSConnection *proxy_connection;
605
NSPort *proxy_connection_out_port = nil;
606
unsigned intermediary;
609
* There is an object on the intermediary host that is keeping
610
* that hosts proxy for the original object retained, thus
611
* ensuring that the original is not released. We create a
612
* proxy for that intermediate proxy. When we release this
613
* proxy, the intermediary will be free to release it's proxy
614
* and the original can then be released. Of course, by that
615
* time we will have obtained our own proxy for the original
618
[aCoder decodeValueOfObjCType: @encode(typeof(intermediary))
620
[NSDistantObject proxyWithTarget: intermediary
621
connection: decoder_connection];
624
* Now we get the target number and port for the orignal object
625
* and (if necessary) get the originating process to retain the
628
[aCoder decodeValueOfObjCType: @encode(typeof(target))
631
[aCoder decodeValueOfObjCType: @encode(id)
632
at: &proxy_connection_out_port];
634
NSAssert(proxy_connection_out_port, NSInternalInconsistencyException);
636
# If there already exists a connection for talking to the
637
* out port, we use that one rather than creating a new one from
638
* our listening port.
640
* First we try for a connection from our receive port,
641
* Then we try any connection to the send port
642
* Finally we resort to creating a new connection - we don't
643
* release the newly created connection - it will get released
644
* automatically when no proxies are left on it.
646
proxy_connection = [[decoder_connection class]
647
connectionWithReceivePort: [decoder_connection receivePort]
648
sendPort: proxy_connection_out_port];
651
NSLog(@"Receiving a triangle-connection proxy 0x%x "
652
@"connection 0x%x\n", target, (gsaddr)proxy_connection);
654
NSAssert(proxy_connection != decoder_connection,
655
NSInternalInconsistencyException);
656
NSAssert([proxy_connection isValid],
657
NSInternalInconsistencyException);
660
* If we don't already have a proxy for the object on the
661
* remote system, we must tell the other end to retain its
662
* local object for our use.
664
if ([proxy_connection includesProxyForTarget: target] == NO)
665
[proxy_connection retainTarget: target];
667
result = RETAIN([NSDistantObject proxyWithTarget: target
668
connection: proxy_connection]);
672
* Finally - we have a proxy via a direct connection to the
673
* originating server. We have also created a proxy to an
674
* intermediate object - but this proxy has not been retained
675
* and will therefore go away when the current autorelease
682
/* xxx This should be something different than NSGenericException. */
684
[NSException raise: NSGenericException
685
format: @"Bad proxy tag"];
584
[NSException raise: NSGenericException
585
format: @"NSDistantObject decodes from placeholder"];
590
* Initialises and returns a proxy associated with aConnection
591
* which will hold a reference to the local object anObject.
691
593
- (id) initWithLocal: (id)anObject connection: (NSConnection*)aConnection
693
NSDistantObject *new_proxy;
695
595
NSAssert([aConnection isValid], NSInternalInconsistencyException);
698
* If there already is a local proxy for this target/connection
699
* combination, don't create a new one, just return the old one.
701
if ((new_proxy = [aConnection localForObject: anObject]))
709
* We don't need to retain the oibject here - the connection
710
* will retain the proxies local object if necessary (and release it
711
* when all proxies referring to it have been released).
716
* We register this proxy with the connection using it.
597
_object = RETAIN(anObject);
718
599
_connection = RETAIN(aConnection);
719
[_connection addLocalObject: self];
600
self = [_connection retainOrAddLocal: self forObject: anObject];
722
603
NSLog(@"Created new local=0x%x object 0x%x target 0x%x connection 0x%x\n",
801
692
struct objc_method_description* mth;
803
mth = [_protocol descriptionForInstanceMethod: aSelector];
694
/* Older gcc versions may not initialise Protocol objects properly
695
* so we have an evil hack which checks for a known bad value of
696
* the class pointer, and uses an internal function
697
* (implemented in NSObject.m) to examine the protocol contents
698
* without sending any ObjectiveC message to it.
700
if ((int)GSObjCClass(_protocol) == 0x2)
702
extern struct objc_method_description*
703
GSDescriptionForInstanceMethod();
704
mth = GSDescriptionForInstanceMethod(_protocol, aSelector);
708
mth = [_protocol descriptionForInstanceMethod: aSelector];
806
mth = [_protocol descriptionForClassMethod: aSelector];
712
if ((int)GSObjCClass(_protocol) == 0x2)
714
extern struct objc_method_description*
715
GSDescriptionForClassMethod();
716
mth = GSDescriptionForClassMethod(_protocol, aSelector);
720
mth = [_protocol descriptionForClassMethod: aSelector];
810
725
types = mth->types;
816
return [NSMethodSignature signatureWithObjCTypes: types];
728
return [NSMethodSignature signatureWithObjCTypes: types];
733
#if defined(USE_FFCALL) || defined(USE_LIBFFI)
840
751
retframe = [self forward: _cmd : args];
841
752
m = retframe_id(retframe);
843
types = [m methodType];
844
return [NSMethodSignature signatureWithObjCTypes: types];
754
if ([m isProxy] == YES)
756
const char *types = [m methodType];
758
m = [NSMethodSignature signatureWithObjCTypes: types];
766
* <p>A key method for Distributed Objects performance. This sets the
767
* a protocol that the distant object referred to by the proxy should
768
* conform to. When messages in that protocol are sent to the proxy,
769
* the proxy knows that it does not need to ask the remote object for
770
* the method siagnature in order to send the message to it, but can
771
* send the message straight away based on the local method signature
772
* information obtained from the protocol.
775
* if ([anObj isProxy] == YES)
777
* [anObj setProtocolForProxy: @protocol(MyProtocol)];
780
* <p>It is <em>highly recommended</em> that you make use of this facility,
781
* but you must beware that versions of the compiler prior to 3.3 suffer a
782
* serious bug with respect to the @protocol directive. If the protocol
783
* referred to is not declared and implemented in the file where @protocol
784
* is used to refer to the protocol by name, a runtime error will occur
785
* when you try to use it.
787
* <p>Beware, if you don't use this method to set the protocol, the system
788
* might well ask the remote process for method signature information, and
789
* the remote process might get it <em>wrong</em>. This is because the
790
* class of the remote object needs to have been declared to conform to the
791
* protocol in order for it to know about any protocol qualifiers (the
792
* keywords <code>bycopy, byref, in, out, inout,</code> and
793
* <code>oneway</code>). If the author of the server process forgot to do
794
* this, the type information returned from that process may not be what
797
* The class of the server object should be declared like this ...
799
* @interface MyServerClass : NSObject <MyProtocol>
849
804
- (void) setProtocolForProxy: (Protocol*)aProtocol
851
806
_protocol = aProtocol;
863
824
NSLog(@"retain count for connection (0x%x) is now %u\n",
864
825
(gsaddr)_connection, [_connection retainCount]);
866
* A proxy for local object does not retain it's target - the
867
* NSConnection class does that for us - so we need not release it.
827
* A proxy for local object retains its target - so we release it.
868
828
* For a local object the connection also retains this proxy, so we
869
829
* can't be deallocated unless we are already removed from the
830
* connection, and there is no need to remove self from connection.
872
* A proxy retains it's connection so that the connection will
873
* continue to exist as long as there is a something to use it.
832
* A proxy has a nil local object, and retains it's connection so
833
* that the connection will continue to exist as long as there is
834
* a something to use it.
874
835
* So we release our reference to the connection here just as soon
875
836
* as we have removed ourself from the connection.
877
838
if (_object == nil)
878
839
[_connection removeProxy: self];
879
842
RELEASE(_connection);
883
- (id) awakeAfterUsingCoder: (NSCoder*)aDecoder
888
846
static inline BOOL class_is_kind_of (Class self, Class aClassObject)
892
for (class = self; class!=Nil; class = class_get_super_class(class))
850
for (class = self; class!=Nil; class = GSObjCSuper(class))
893
851
if (class==aClassObject)