//
//  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

- (oneway void) passMessage:(NSString *) str;
{ // we received a message
	[[[[NSApp delegate] valueForKey:@"server"] textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString:str] autorelease]];	// show what we have received on server side
	abort();
}

- (bycopy NSString *) pleaseRespond;
{
	NSString *str=[NSString stringWithFormat:@"This is %@ at %@\n", [[NSHost currentHost] name], [NSDate date]];
	abort();
	return str;
}

@end

@implementation AppController

- (BOOL) applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)theApplication
{
	return YES;
}

- (IBAction) registerAsServer:(id) sender;
{
	if([[sender title] length] > 0)
		{
		NSSocketPort *port = [[[NSSocketPort alloc] init] autorelease];	// assign free IP port number
		NSConnection *connection = [[NSConnection connectionWithReceivePort:port sendPort:nil] retain];
		NSLog(@"registerAsServer connection = %@", connection);
		if(![[NSSocketPortNameServer sharedInstance] registerPort:port name:[sender title]])	// register with zeroconf
			[[result textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString:@"Not published\n"] autorelease]];
		else
			[connection setRootObject:[[Responder alloc] init]];
		}
}

- (IBAction) registerAsMSGPortServer:(id) sender;
{
	if([[sender title] length] > 0)
		{
		NSMessagePort *port = [[[NSMessagePort alloc] init] autorelease];	// create new message port
		NSConnection *connection = [[NSConnection connectionWithReceivePort:port sendPort:nil] retain];
		NSLog(@"registerAsMSGPortServer connection = %@", connection);
		if(![[NSMessagePortNameServer sharedInstance] registerPort:port name:[sender title]])	// register as message port
			[[result textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString:@"Not published\n"] autorelease]];
		else
			[connection setRootObject:[[Responder alloc] init]];
		}
}

- (IBAction) connectToServer:(id) sender;
{
	if([[sender title] length] > 0)
		{
		NSPort *port = [[NSSocketPortNameServer sharedInstance] portForName:[sender title] host:@"*"];
		NSConnection *connection = [NSConnection connectionWithReceivePort:nil sendPort:port];
		NSLog(@"connectToServer connection = %@", connection);
		if(!connection)
			{
			[[result textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"could not connect to %@\n", [sender title]]] autorelease]];
			[sender selectItemAtIndex:0];
			return;
			}
		[[connection rootProxy] setProtocolForProxy:@protocol(Respond)];
		rootProxy=[[connection rootProxy] retain];
		}
}

- (IBAction) connectToMSGServer:(id) sender;
{
	if([[sender title] length] > 0)
		{
		NSPort *port = [[NSMessagePortNameServer sharedInstance] portForName:[sender title] host:@""];
		NSConnection *connection = [NSConnection connectionWithReceivePort:nil sendPort:port];
		NSLog(@"connectToMSGServer connection = %@", connection);
		if(!connection)
			{
			[[result textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"could not connect to %@\n", [sender title]]] autorelease]];
			[sender selectItemAtIndex:0];
			return;
			}
		[[connection rootProxy] setProtocolForProxy:@protocol(Respond)];
		rootProxy=[[connection rootProxy] retain];
		}
}

- (IBAction) sendMessage:(id) sender;
{
	if(!rootProxy)
		{
		[[result textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString:@"Not connected\n"] autorelease]];
		return;
		}
	[rootProxy passMessage:[NSString stringWithFormat:@"%@\n", [message stringValue]]];
	[message setStringValue:@""];
}

- (IBAction) getResponse:(id) sender;
{
	if(!rootProxy)
		{
		[[result textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString:@"Not connected\n"] autorelease]];
		return;
		}
	[[result textStorage] appendAttributedString:[[[NSAttributedString alloc] initWithString:[rootProxy pleaseRespond]] autorelease]];
}

@end
