//
//  AppController.m
//  DO
//
//  Created by Dr. H. Nikolaus Schaller on 23.10.06.
//  Copyright 2006 __MyCompanyName__. All rights reserved.
//

#import "AppController.h"

@interface Responder : NSObject <Respond>
@end

@implementation Responder

- (bycopy NSString *) response;
{
	return [NSString stringWithFormat:@"This is %@ at %@", [[NSHost currentHost] name], [NSDate date]];
}

@end

@implementation AppController

- (IBAction) registerAsServer:(id) sender;
{
	NSSocketPort *port = [[NSSocketPort alloc] init];	// assign free IP port number
	NSConnection *connection = [[NSConnection connectionWithReceivePort:port sendPort:nil] retain];
	[[NSSocketPortNameServer sharedInstance] registerPort:port name:[sender title]];	// register with zeroconf
	[connection setRootObject:[[Responder alloc] init]];
}

- (IBAction) connectToServer:(id) sender;
{
	NSPort *port = [[NSSocketPortNameServer sharedInstance] portForName:[sender title] host:@"*"];
	NSConnection *connection = [NSConnection connectionWithReceivePort:nil sendPort:port];
	if(!connection)
		NSLog(@"could not connect to %@", [sender title]);
	[[connection rootProxy] setProtocolForProxy:@protocol(Respond)];
	rootProxy=[[connection rootProxy] retain];
}

- (IBAction) sendMessage:(id) sender;
{
	[result setString:[rootProxy response]];
}

@end
