TableOfContents

Include(ZPyUGnav)

= 用Pythoon玩ICE=

开始

最近分布式很流行,Message Queue也频频出场.

作为C++死忠,我当然不会忘记ICE,何况ICE还有跨语言的特性.

以前玩过C++版本的hello world,这一次打算稍微多看一点.

不过,这次只玩python版本的.

先整理一下网络上的google到的资料

http://gigaboy.bokee.com/1945079.html ICE简介

http://blog.chaoskey.com/2008/01/06/54/ ICE构架

http://www.zeroc.com/customers.html ICE的客户,其中最为大家所知的是

Skype uses Ice as part of its communications infrastructure. Skype is the world's fastest-growing Internet communication offering, allowing people everywhere to make unlimited voice and video communication for free between the users of Skype software. Skype is available in 27 languages and is used in almost every country around the world.

安装方法昨天记录了. http://zsp.javaeye.com/blog/236377

还是从 hello world 开始

复制文档,需要注意的是,新建Printer.ice这个文件结尾需要空一行. 而且文件存放的目录不要有空格和中文.

The first step in writing any Ice application is to write a Slice definition containing the interfaces that are used by the application. For our minimal printing application, we write the following Slice definition:

module Demo {

};

We save this text in a file called Printer.ice.

Our Slice definitions consist of the module Demo containing a single interface called Printer. For now, the interface is very simple and provides only a single operation, called printString. The printString operation accepts a string as its sole input parameter; the text of that string is what appears on the (possibly remote) printer.

然后到这个目录下执行如下命令

D:\dev\c++\ice\learn>slice2py Printer.ice

生成了 Printer_ice.py 和 Demo 子目录

在和 Printer_ice.py 同一级的目录下 新建 Server.py 代码如下.看英文文档的同学注意,他网页上给出源代码"default ‑p 10000",里有一个诡异的字符:

#coding:utf-8 import sys, traceback, Ice import Demo

class PrinterI(Demo.Printer):

status = 0 ic = None try:

except:

if ic:

sys.exit(status)

这是官方的演示代码和注释,不过感觉这个ic可以用python的新语法with封装一下,用起来应该会更加简单.

接下来写客户端,运行这个客户端就会在服务器上出现一个,"Hello World!"

   1 #coding:utf-8
   2 
   3 import sys, traceback, Ice
   4 import Demo
   5 
   6 status = 0
   7 ic = None
   8 try:
   9     ic = Ice.initialize(sys.argv)
  10     
  11     """
  12     The proxy returned by stringToProxy is of type Ice::ObjectPrx, which is at the root of the inheritance tree for interfaces and classes.
  13     stringToProxy返回了一个Ice::ObjectPrx对象,这个在继承树中是接口和类的根对象
  14     """
  15     base = ic.stringToProxy("SimplePrinter:default -p 10000")
  16     
  17     """
  18     But to actually talk to our printer, we need a proxy for a Demo::Printer interface, not an Object interface. To do this, we need to do a down-cast by calling Demo.PrinterPrx.checkedCast.
  19     但是,实际上我们需要的是一个Demo::Printer接口,不是一个Object的接口,因此我们需要向下转型.
  20     
  21     A checked cast sends a message to the server, effectively asking "is this a proxy for a Demo::Printer interface?" If so, the call returns a proxy of type Demo.PrinterPrx; otherwise, if the proxy denotes an interface of some other type, the call returns None.
  22     checkedCast向服务器发送消息,询问"这是一个Demo::Printer的代理吗?"
  23     如果是,返回一个Demo.PrinterPrx
  24     否则,这个一个其他类型的代理,该调用返回None
  25     """
  26     printer = Demo.PrinterPrx.checkedCast(base)
  27     if not printer:
  28         raise RuntimeError("Invalid proxy")
  29 
  30     printer.printString("Hello World!")
  31 except:
  32     traceback.print_exc()
  33     status = 1
  34 
  35 if ic:
  36     # Clean up
  37     try:
  38         ic.destroy()
  39     except:
  40         traceback.print_exc()
  41         status = 1
  42 
  43 sys.exit(status)


反馈

创建 by -- ["zuroc"] [DateTime(2008-09-04T13:11:07Z)]

PageComment2

[:/PageCommentData:PageCommentData]