slice及其编译
slice编译 与 Icepy方式
获得slice2py.exe后, 赶紧体验了一把 map to python
按照Ice3.1.1帮助文件 [3.7Writing an Ice Application with Python]
copy & paste printer.ice
在dos窗口下
F:\ICEstudy>slice2py printer.ice
生成一个Demo目录和printer_ice.py
生成的文件略感繁琐(比我事先想象的)
又按帮助 paste server.py和client.py,
然后先后运行server和client,一试就灵. 稍稍遗憾的就是代码有点不够好看,比如server.py
import sys, traceback, Ice
import Demo
class PrinterI(Demo.Printer):
def printString(self, s, current=None):
print s
status = 0
ic = None
try:
ic = Ice.initialize(sys.argv)
adapter = ic.createObjectAdapterWithEndpoints(
"SimplePrinterAdapter", "default -p 10000")
object = PrinterI()
adapter.add(object, Ice.stringToIdentity("SimplePrinter"))
adapter.activate()
ic.waitForShutdown()
except:
traceback.print_exc()
status = 1
if ic:
# Clean up
try:
ic.destroy()
except:
traceback.print_exc()
status = 1
sys.exit(status)
----Icepy3.1.1
发现Icepy下的代码形式不错, 而且竟然不用slice2py.exe,早知如此我就不用费劲要它了
于是仿照改写了server.py
import sys, traceback, Ice
Ice.loadSlice('printer.ice')
import Demo
class PrinterI(Demo.Printer):
def printString(self, s, current=None):
print s
class Server(Ice.Application):
def run(self, args):
adapter = self.communicator().createObjectAdapter("SimplePrinterAdapter")
adapter.add(PrinterI(), self.communicator().stringToIdentity("SimplePrinter"))
adapter.activate()
self.communicator().waitForShutdown()
return True
app = Server()
sys.exit(app.main(sys.argv, "config.server"))
其中的配置参数都设置在config.server文件(保存于同目录)
#
# The server creates one single object adapter with the name
# "SimplePrinterAdapter". The following line sets the endpoints for this
# adapter.
#
SimplePrinterAdapter.Endpoints=tcp -p 10000:udp -p 10000
#
# Warn about connection exceptions
#
Ice.Warn.Connections=1
#
# We want a faster ACM for this demo.
#
Ice.ACM.Server=10
#
# Network Tracing
#
# 0 = no network tracing
# 1 = trace connection establishment and closure
# 2 = like 1, but more detailed
# 3 = like 2, but also trace data transfer
#
#Ice.Trace.Network=1
#
# Protocol Tracing
#
# 0 = no protocol tracing
# 1 = trace protocol messages
#
#Ice.Trace.Protocol=1