#import <Foundation/Foundation.h>

#import "Protocol.h"

@interface NSPortMessagePoser : NSPortMessage
- (BOOL)sendBeforeDate:(NSDate *)aDate;
@end

@implementation NSPortMessagePoser
- (BOOL)sendBeforeDate:(NSDate *)aDate
{
	NSLog(@"NSPortMessage sendBeforeDate:%@", aDate);
	NSLog(@"  msgid=%d", [self msgid]);
	NSLog(@"  sendPort=%@", [self sendPort]);
	NSLog(@"  receivePort=%@", [self receivePort]);
	NSLog(@"  components=%@", [self components]);
	return [super sendBeforeDate:aDate];
}

@end

@interface NSPortCoderPoser : NSPortCoder
- (id)initWithReceivePort:(NSPort *)receiverPort sendPort:(NSPort *)sendPort components:(NSArray *)components;
@end

@implementation NSPortCoderPoser
- (id)initWithReceivePort:(NSPort *)receiverPort sendPort:(NSPort *)sendPort components:(NSArray *)components;
{
	NSLog(@"NSPortCoder initWithReceivePort:%@ sendPort:%@ components:%@", receiverPort, sendPort, components);
	return [super initWithReceivePort:receiverPort sendPort:sendPort components:components];
}
@end

@interface PortDelegate : NSObject
- (void)handlePortMessage:(NSPortMessage *)portMessage;
@end

@implementation PortDelegate

- (void)handlePortMessage:(NSPortMessage *)portMessage;
{
	NSLog(@"portmessage: %@", portMessage);
}

@end

int main(int argc, const char *argv[])
{
	NSAutoreleasePool *arp=[NSAutoreleasePool new];
	NSLog(@"started");

	// result: it appears that these are not used for communicating with self!
	
	[NSPortMessagePoser poseAsClass:[NSPortMessage class]];
	[NSPortCoderPoser poseAsClass:[NSPortCoder class]];
#if 0	// with default connection - result: appears to optimize and not send NSPortMessages at all!
	{
		NSConnection *c;
		NSLog(@"thread dictionary %@", [[NSThread currentThread] threadDictionary]);
		NSLog(@"default connection %@", [NSConnection defaultConnection]);
		NSLog(@"thread dictionary %@", [[NSThread currentThread] threadDictionary]);
		[[NSConnection defaultConnection] setRootObject:[MyClass new]];	// vend some object
		[[NSConnection defaultConnection] registerName:@"test"];
		NSLog(@"registered default connection %@", [NSConnection defaultConnection]);
		NSLog(@"thread dictionary %@", [[NSThread currentThread] threadDictionary]);	// is stored at @"NSPerThreadConnection"
		NSLog(@"connections %@", [NSConnection allConnections]);
		c=[NSConnection connectionWithRegisteredName:@"test" host:nil];
		NSLog(@"new connection %@", c);
		NSLog(@"connections %@", [NSConnection allConnections]);
		NSLog(@"root proxy %@", [c rootProxy]);
	}
#endif
#if 0
	{ // Client
	PortClass *port = (id) [[NameServerClass sharedInstance] portForName:@"fexpserver" host:nil];
	NSConnection *client;
	id <MyServer> proxy;
	NSString *m2;
	NSLog(@"port: %@", port);
	client = [NSConnection connectionWithReceivePort:nil sendPort:port];
	NSLog(@"client connection: %@", client);
	NSLog(@"connections: %@", [NSConnection allConnections]);
	proxy=(id <MyServer>) [client rootProxy];
	[(NSDistantObject *) proxy setProtocolForProxy:@protocol(MyServer)];
	NSLog(@"root proxy: %@", proxy);	// this calls remotely -description
	NSLog(@"root proxy's -method result: %@", [proxy method]);
	m2=[proxy method2:@"byref"];
	NSLog(@"root proxy's -method2 result: %@", m2);
	NSLog(@"root proxy's -method2 result class: %@", NSStringFromClass([m2 class]));
	NSLog(@"local objects: %@", [client localObjects]);
	NSLog(@"remote objects: %@", [client localObjects]);
	}
#endif
#if 1
	{ // simple NSSocketPort tests
	}
#endif
	[arp release];
	exit(0);
}
