From: <Dan...@at...> - 2002-01-07 17:03:57
|
Hi all. Last week or so, I asked about strategies for using jython to access COM objects. Various (good) responses came back, and I put them to use. The best suggestion was to use XML-RPC to allow a jython instance (which doesn't support COM) to communicate with a CPython instance (which does support COM). Here's a cutesy demo that runs a Microsoft Agent (one of their little anthropomorphs like the MSOffice paper clips) from a JavaAwt controller. The in-machine protocol is XML-RPC. (Apologies for any damage to the source done by my mailer program.) Windows 2000 should have at least one agent character (Merlin.acs) installed in <root-drive>:\WINNT\MSAGENT\CHARS 1). here's the Server side which should be run from CPython: _________________ CUT HERE__________________________ # Run this under Python21 with the XMLRPC lib stuff inserted into PythonPath # must have installed ActiveState Python (ActivePython- 2.1.1.msi)and win32all.exe from win32com.client import * import SimpleXMLRPCServer import os,sys,time,string class Anthropomorph: def __init__ (self, agent="Merlin"): self.agentName=agent def initAgent(self): self.dispatchID = Dispatch ("Agent.Control.2") # load the control self.dispatchID.Connected=1 # Connect! fullName = self.agentName+'.acs' print "initAgent",fullName self.dispatchID.Characters.Load (self.agentName, fullName) self.anAgent = self.dispatchID.Characters (self.agentName) #print "initAgent", self.anAgent return "Anthropomorph: initAgent done." def startAgent (self): print "startAgent",self.anAgent self.anAgent.Show() self.anAgent.Play("Greet") self.anAgent.MoveTo(820,600,1000) time.sleep(5) # padding so that the anthropomorph can perform return "Anthropomorph: startAgent done." def play(self, gesture, delay=5): print "play",self.anAgent self.anAgent.Play(gesture) time.sleep(delay) # padding so that the anthropomorph can perform return delay def say (self, phrase, delay=5): print "say",self.anAgent self.anAgent.Speak(phrase) time.sleep(delay) # padding so that the anthropomorph can perform return delay def hide(self, delay=0): time.sleep(delay) # padding so that the anthropomorph can perform self.anAgent.Hide() return delay server = SimpleXMLRPCServer.SimpleXMLRPCServer (("localhost", 8000)) server.register_instance(Anthropomorph()) print 'SimpleXMLRPCServer @ localhost:8000' server.serve_forever() _____________________ END SERVER CODE______________ 2). Client Code, simply creates an awt fixture and communicates with the Server. run this with jython __________________ CUT HERE_________________________ # Client code import xmlrpclib from java import awt class Marionette: def __init__(self): self.agent = xmlrpclib.Server ('http://localhost:8000') print self.agent.initAgent() print self.agent.startAgent() def saySomething(self,anExpression): self.agent.say(anExpression) def doSomething(self,anAction): self.agent.play(anAction) def hideMe(self): self.agent.hide() if __name__ == '__main__': def animate(e): #print 'animate called' animation.caretPosition=0 #animation.selectAll() puppet.doSomething(animation.getText()) def speak(e): #print 'speak called' speech.caretPosition=0 #speech.selectAll() puppet.saySomething(speech.getText()) puppet = Marionette() panel0= awt.Panel(layout=awt.BorderLayout()) panel1= awt.Panel(layout=awt.GridLayout()) label0 = awt.Label('Animation:') animation = awt.TextField(text='Greet', actionPerformed=animate) label1 = awt.Label("Say This:") speech = awt.TextField(text='Hello Folks', actionPerformed=speak) panel1.add(label0) panel1.add(animation) panel1.add(label1) panel1.add(speech) panel0.add(panel1, 'Center') import pawt pawt.test(panel0, size=(300,56)) animate(None) ____________________________________________________ -- _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ Dana Moore - BBN Technologies 703.284.4740 (W) 240.350.4196 (M) dan...@at... _/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/ |