import Tutorial2, Tutorial2__skel, TapeCalculatorImpl
class Factory (Tutorial2__skel.Factory):
# have the __init__ method take handle and server args
# so that we can control which ILU kernel server is used,
# and what the instance handle of the Factory object on
# that server is. This allows us to control the object ID
# of the new Factory instance.
def __init__(self, handle=None, server=None):
self.IluInstHandle = handle
self.IluServer = server
def CreateCalculator (self):
return (TapeCalculatorImpl.TapeCalculator())
CreateTapeCalculator = CreateCalculator
server2.py
# server2.py -- a program that runs a Tutorial2.TapeCalculator factory
#
import ilu, FactoryImpl2, sys
def main(argv):
if (len(argv) < 2):
print "Usage: python server2.py SERVER-ID"
sys.exit(1)
theServer = ilu.CreateServer (argv[1])
theFactory = FactoryImpl2.Factory ("theFactory", theServer)
# Now make the Factory object "well-known" by publishing it.
theFactory.IluPublish()
# Now we print the string binding handle (the object's name plus
# its location) of the new instance.
print "Factory2 instance published."
print "Its SBH is '" + theFactory.IluSBH() + "'"
handle = ilu.CreateLoopHandle ();
ilu.RunMainLoop (handle)
main(sys.argv)
simple4.py
# simple4.py -- a simple client program that finds the TapeCalculator Factory,
# creates a calculator, and provides a simple interactive calculator
#
# to run: python simple4.py ARG [ARG...]
import Tutorial2, ilu, sys, string
# We define a new routine, "Get_Tutorial_Calculator", which
# finds the tutorial factory, then creates a new Calculator
# object for us.
def Get_Tutorial_Calculator (factoryObjectID):
# We have to call ilu.LookupObject() with the object ID of
# the factory object, and the "type" of the object we're looking
# for, which is always available as MODULE.TYPENAME
f = ilu.LookupObject (factoryObjectID, Tutorial2.Factory)
if not f:
print "Can't find Tutorial.Factory instance " + factoryObjectID
sys.exit(1)
c = f.CreateTapeCalculator()
return (c)
opname = ['SetValue', 'Add', 'Subtract', 'Divide', 'Multiply']
def Print_Tape (tape):
# print out the Calculator tape nicely
for op in tape:
print " %s(%f) => %f" % (opname[op['op']], op['value'], op['accumulator'])
def main (argv):
if (len(argv) < 2):
print "Usage: python simple3.py FACTORY-OBJECT-ID NUMBER [NUMBER...]\n",
sys.exit(1)
c = Get_Tutorial_Calculator(argv[1])
if not c:
print "Couldn't create calculator"
sys.exit(1)
# clear the calculator before using it
newval = 0.0
c.SetValue (newval)
quitflag = 0
while not quitflag:
sys.stdout.write("%.5f\n> " % newval)
sys.stdout.flush()
line = sys.stdin.readline()
if (not line):
sys.exit(0)
try:
if (line[0] == '\n'):
pass
elif (line[0] == '+'):
val = string.atof(line[1:-1])
c.Add(val)
elif (line[0] == '-'):
val = string.atof(line[1:-1])
c.Subtract(val)
elif (line[0] == '*'):
val = string.atof(line[1:-1])
c.Multiply(val)
elif (line[0] == '/'):
val = string.atof(line[1:-1])
c.Divide(val)
elif (line[0] == 'q'):
quitflag = 1
elif (line[0] == 'c'):
c.SetValue(0.0)
elif (line[0] == 't'):
Print_Tape ( c.GetTape() )
else:
print "Invalid operation <" + line[:-1] + ">"
print "Valid ops are +, -, *, /, tape, clear, quit"
newval = c.GetValue()
except:
print "Operation <%s> signals error <%s>." % (line[:-1], sys.exc_type)
sys.exit(0)
main(sys.argv)
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 ();
};
};
server3.py
# server3.py -- a program that runs a Tutorial.Calculator server
# Puts up a Tk button to kill it with.
import ilu, FactoryImpl2, sys, Tkinter, ilu_tk
def main(argv):
def quit():
sys.exit(0)
if (len(argv) < 2):
print "Usage: python server3.py SERVER-ID"
sys.exit(1)
theServer = ilu.CreateServer (argv[1])
theFactory = FactoryImpl2.Factory ("theFactory", theServer)
theFactory.IluPublish()
# Now we put up a Tk button so that the user can kill the
# server by pressing the button
f = Tkinter.Frame() ; Tkinter.Pack.config(f)
b = Tkinter.Button (f, {'text' : theFactory.IluObjectID(),\
'command': quit})
b.pack ({'side': 'left', 'fill': 'both'})
# Then we wait in the ilu_tk mainloop, instead of either
# the ILU mainloop or the Tkinter mainloop
ilu_tk.RunMainLoop()
main(sys.argv)