/*
* Run this like
* java Tutorial2.Tutorial2Server servername
*/
package Tutorial2;
import xerox.ilu.Ilu;
import xerox.ilu.IluSystemException;
import xerox.ilu.IluServer;
class Factory2Impl implements Tutorial2.Factory {
public Factory2Impl() {
}
public Tutorial.Calculator CreateCalculator()
throws xerox.ilu.IluSystemException {
System.out.println("Factory2Impl: request for a simple calculator");
return new Tutorial2.TapeCalculatorImpl();
} //CreateCalculator
public Tutorial2.TapeCalculator CreateTapeCalculator()
throws xerox.ilu.IluSystemException {
System.out.println("Factory2Impl: request for a tape calculator");
return new Tutorial2.TapeCalculatorImpl();
} //CreateTapeCalculator
} //Factory2Impl
public class Tutorial2Server {
static Factory2Impl factory;
static xerox.ilu.IluServer trueServer;
public static void main(String argv[]) {
String serverId;
if (argv.length != 1) {
System.err.println("usage: java Tutorial2.Tutorial2Server servername");
System.exit(1);
}
System.out.println("Create the server");
try {
//Create a server with appropriate server id (which is
//taken from the first argument)
serverId = argv[0];
trueServer = xerox.ilu.IluServer.createServer(serverId);
} catch (xerox.ilu.IluSystemException e) {
System.err.println("Failed creating server: " + e);
System.exit(1);
}
System.out.println("Create the factory");
try {
//Now create an instance of a Factory object on the server
//with an instance handle "theFactory"
factory = new Factory2Impl();
Tutorial2.FactoryStub.registerTrueObject(
"theFactory",
factory,
trueServer
);
//Make the factory well known by publishing it
xerox.ilu.IluSimpleBinding.publish(factory);
} catch (xerox.ilu.IluSystemException e) {
System.err.println("Failed creating Factory: " + e);
System.exit(1);
}
//Now we print the string binding handle (the object's name
//plus its location) of the new Factory instance
System.out.println("Factory instance published");
System.out.println("Its SBH is '" + Ilu.sbhOfObject(factory) + "'");
//the program doesn't terminate because the server is still alive...
} //main
} //Tutorial2Server
simple4.java
/*
* A simple client program that finds the TapeCalculator-Factory,
* creates a calculator and executes the users input.
*/
/*
* Run this like
* java Tutorial2.simple4 serverId
*/
package Tutorial2;
public class simple4 {
/* We define a new routine, "Get_Tutorial_Calculator", which
* finds the tutorial factory, then creates a new TapeCalculator
* object for us.
*/
public static Tutorial2.TapeCalculator
GetTutorialTapeCalculator(String serverId, String factoryId) {
Tutorial2.Factory factory = null;
Tutorial2.TapeCalculator calc = null;
System.out.println("Looking up factory");
try {
/* We have to call lookup with the object ID of
* the factory object, and the "type" of the object we're looking
* for.
*/
factory = (Tutorial2.Factory)
xerox.ilu.IluSimpleBinding.lookup(
serverId,
factoryId,
Tutorial2.FactoryStub.iluClass()
);
} catch (xerox.ilu.IluSystemException e) {
System.err.println("Failed to get factory: " + e);
System.exit(1);
}
if (factory==null) {
System.err.println("Got null factory");
System.exit(1);
}
System.out.println("Got factory " + factory);
System.out.println("Looking up TapeCalculator");
try {
calc = factory.CreateTapeCalculator();
} catch (xerox.ilu.IluSystemException e) {
System.err.println("Failed to get TapeCalculator: " + e);
System.exit(1);
}
if (calc==null) {
System.err.println("Got null TapeCalculator");
System.exit(1);
}
System.out.println("Got TapeCalculator " + calc);
return calc;
} //GetTutorialTapeCalculator
static String opNames[]
= {"SetValue", "Add", "Subtract", "Multiply", "Divide"};
static double argToDouble(String inputLine) {
return Double.valueOf(inputLine.substring(1)).doubleValue();
} //argToDouble
public static void main(String argv[]) {
Tutorial2.TapeCalculator calc;
Tutorial2.Operation[] tape;
boolean quitFlag = false;
double value = 0.0;
String line;
java.io.DataInputStream sysIn =
new java.io.DataInputStream(System.in);
if (argv.length < 1) {
System.err.println("USAGE: java Tutorial2.simple4 serverid");
System.exit(1);
}
//Find a calculator
String serverId = argv[0];
calc = GetTutorialTapeCalculator(serverId, "theFactory");
if (calc==null) {
System.err.println("Didn't get a calculator");
System.exit(1);
}
System.out.println("Got tape calculator");
try {
//Clear the calculator before using it
calc.SetValue(0.0);
//Loop over user inputs and perform the requested operation
while (!quitFlag) {
value = calc.GetValue();
System.out.print(value + "\n> "); System.out.flush();
line = sysIn.readLine();
if (line == null) line = "q";
switch (line.charAt(0)) {
case '\n':
break;
case '+':
value = argToDouble(line);
calc.Add(value);
break;
case '-':
value = argToDouble(line);
calc.Subtract(value);
break;
case '*':
value = argToDouble(line);
calc.Multiply(value);
break;
case '/':
try {
value = argToDouble(line);
calc.Divide(value);
} catch (Tutorial.DivideByZero e) {
System.out.println("** division by zero " + e);
}
break;
case 'q':
quitFlag = true;
break;
case 't':
tape = calc.GetTape();
for (int i = 0; i < tape.length; i++) {
System.out.println(" "
+ opNames[tape[i].op.value()]
+ "(" + tape[i].value + ") => "
+ tape[i].accumulator
);
}
break;
case 'c':
calc.SetValue(0.0);
break;
default:
System.out.println("Invalid Operation <" + line + ">");
System.out.println("Valid ops are +, -, *, /, "
+ "t (for tape),"
+ "c (for clear),"
+ "q (for quit),"
);
}
}
} catch (java.io.IOException e) {
System.err.println("Example raised IOException: " + e);
} catch (xerox.ilu.IluSystemException e) {
System.err.println("Example raised IluSystemException: " + e);
}
} //main
} //simple4
Tutorial.idl
module Tutorial {
exception DivideByZero {};
interface Calculator {
// Set the value of the calculator to `v'
void SetValue (in double v);
// Return the value of the calculator
double GetValue ();
// Adds `v' to the calculator's value
void Add (in double v);
// Subtracts `v' from the calculator's value
void Subtract (in double v);
// Multiplies the calculator's value by `v'
void Multiply (in double v);
// Divides the calculator's value by `v'
void Divide (in double v) raises (DivideByZero);
};
interface Factory {
// Create and return an instance of a Calculator object
Calculator CreateCalculator();
};
};
Tutorial2.idl
#include "Tutorial.idl"
module Tutorial2 {
enum OpType { SetValue, Add, Subtract, Multiply, Divide };
struct Operation {
OpType op;
double value;
double accumulator;
};
typedef sequence<Operation> RegisterTape;
// A four function calculator with a register tape
interface TapeCalculator : Tutorial::Calculator {
RegisterTape GetTape ();
};
// A factory that produces TapeCalculators
interface Factory : Tutorial::Factory {
TapeCalculator CreateTapeCalculator ();
};
};