/**
An example illustrating the implementation of a server in the
");
else
try
{
new Adder( args[0], Integer.parseInt( args[1] ) );
}
catch( UnknownHostException exception )
{
System.err.println("The broker host is unknown ('" +
exception.getMessage() + "'");
System.exit(-1);
}
catch( ServerDownException e )
{
System.err.println("Either the network is down, or there is no " +
"broker running on the specified host and port" );
System.exit(-1);
}
catch( IOException e )
{
System.err.println("An I/O error occurred: " + e.getMessage() );
System.exit(-1);
}
// The Adder will create a BrokerClient with a thread of its own, therefore
// the Java program will not terminate after the new Adder
}
private Adder( String host, int port )
throws UnknownHostException, ServerDownException, IOException
{
BrokerClient brokerClient;
brokerClient = new BrokerClient( "Adder", host, port, this, true );
brokerClient.connect();
}
// implementation of the ServiceProvider interface
public Object brokerCall( BrokerClient brokerClient, String data ) throws BrokerException
{
StringTokenizer tokenizer;
String method;
int sum = 0;
String numberName = null;
tokenizer = new StringTokenizer( data );
if( !tokenizer.hasMoreTokens() )
throw new BrokerException( "No method specified" );
method = tokenizer.nextToken();
if( !method.equalsIgnoreCase( "add" ) )
throw new BrokerException( "Function '" + method + "' not supported." );
// now read the numbers
try
{
while( tokenizer.hasMoreTokens() )
{
int number;
numberName = tokenizer.nextToken();
number = Integer.parseInt( numberName );
sum += number;
}
}
catch( NumberFormatException e )
{
// error message with empathy are strongly supported by the Broker
// architecture
throw new BrokerException( "The Adder deeply regrets that '" +
numberName + "' is not a number." );
}
// this method must return either a String or a Vector of Strings
return String.valueOf( sum );
}
public Object brokerCall( BrokerClient brokerClient, Vector data ) throws BrokerException
{
throw new BrokerException( "This server doesn't support multiline calls");
}
}