uses "console", "rmi", "array", "date", "filesystem";

namespace ANSI {
	string black    = "\033[30m";
	string red      = "\033[31m";
	string green    = "\033[32m";
	string yellow   = "\033[33m";
	string blue     = "\033[34m";
	string magenta  = "\033[35m";
	string purple   = "\033[35m";
	string cyan     = "\033[36m";
	string white    = "\033[37m";
	string darkgray = "\033[30m";

	string normal     = "\033[00m";
	string background = "\033[07m";
	string brighter   = "\033[01m";
	string underline  = "\033[04m";
	string blink      = "\033[05m";
}

function output( string message ) {
	string type = "rmi";
	Console.println("${ANSI.red}${ANSI.brighter}${Date.date(Date.now()).format('%H:%M')} ${ANSI.blue}${ANSI.brighter}[$type]${ANSI.normal} $message");
}

object o = null, p = null, server = null;
string host = "localhost";

if( Array.size(argv) > 0 )
  host = argv[0];

RMI.debugOn = false;

o = RMI.connectToService( "::tcpip", "connect:$host:", "SomeResource" );
p = RMI.connectToService( "::tcpip", "connect:$host:", "AnotherResource" );
server = RMI.connectToService( "::tcpip", "connect:$host:", "RMIServerControl" );

if( o != null )
{
    output( "Testing o" );
	output( "Testing direct access:");
    output( "o.someValue = '${o.someValue}'" );
	output( "Testing function call access:");
    output( "Return from o.getValue() == '${o.getValue()}'" );
	output( "Testing direct access:");
    output( "Before: o.someValue = '${o.someValue}'" );    
    o.someValue = "100566 (lite osäker på ID´t) Christer Rudolfsson kommer sedan länge inte in på sin hemsida.<br/>Han har dessutom begärt nytt lösenord (ett flertal gånger)<br/>Han var Pre-member före join….<br/><br/>Lägg in ”hemligt”<br/>/Bo<br/> | missing member - Carl Runsten";
    output( "After: o.someValue = '${o.someValue}'" );    
    o.attribute_set( "someValue", "Harrow Taxi Driver" );
    output( "Return from o.getValue() == '${o.getValue()}'" );
}
if( p != null )
{
	string image = File.open("client-binary.jpg").toString();
    output( "Testing p" );
    output( "Return from p.getValue() == '${p.getValue()}'" );
    p.someValue = "This should change from the above value of p.getValue()";
    output( "Return from p.getValue() == '${p.getValue()}'" );
	output( "Saving image" );
	p.saveImage(image);
	output( "Image should be saved! (check for server-binary.jpg)" );
}

output( "Asking Server To Shutdown" );
server.shutdown();