用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 {
    interface Printer {
        void printString(string s);
    };
};

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",里有一个诡异的字符:

   1 #coding:utf-8
   2 import sys, traceback, Ice
   3 import Demo
   4 
   5 class PrinterI(Demo.Printer):
   6     def printString(self, s, current=None):
   7         print s
   8 
   9 status = 0
  10 ic = None
  11 try:
  12     """
  13     We initialize the Ice run time by calling Ice.initialize. (We pass sys.argv to this call because the server may have command-line arguments that are of interest to the run time; for this example, the server does not require any command-line arguments.) 
  14     The call to initialize returns an Ice::Communicator reference, which is the main handle to the Ice run time.
  15     """
  16     ic = Ice.initialize(sys.argv)
  17 
  18     """
  19     We create an object adapter by calling createObjectAdapterWithEndpoints on the Communicator instance.
  20     The arguments we pass are "SimplePrinterAdapter" (which is the name of the adapter) and "default ‑p 10000", which instructs the adapter to listen for incoming requests using the default protocol (TCP/IP) at port number 10000.
  21     监听一个端口
  22     """
  23     adapter = ic.createObjectAdapterWithEndpoints(
  24         "SimplePrinterAdapter", "default -p 10000"
  25     )
  26 
  27     object = PrinterI()
  28 
  29     """
  30     We inform the object adapter of the presence of a new servant by calling add on the adapter;
  31     the arguments to add are the servant we have just instantiated, plus an identifier. In this case, the string "SimplePrinter" is the name of the servant. (If we had multiple printers, each would have a different name or, more correctly, a different object identity.)
  32     意译:
  33     用一个实例监听这个服务,"SimplePrinter"是这个监听者的名称.如果有多个监听者,每一个都要有不同的名字,更专业的说,一个个唯一的对象标识
  34     """
  35     adapter.add(object, ic.stringToIdentity("SimplePrinter"))
  36 
  37     """
  38     Next, we activate the adapter by calling its activate method. (The adapter is initially created in a holding state; this is useful if we have many servants that share the same adapter and do not want requests to be processed until after all the servants have been instantiated.)
  39     意译:
  40     激活adapter
  41     adapter初始化的时候仅仅是持有状态.
  42     因为如果我们有多个监听者,在没有全部安装之前,我们不希望它处理请求
  43     """
  44     adapter.activate()
  45 
  46     """
  47     Finally, we call waitForShutdown. This call suspends the calling thread until the server implementation terminates, either by making a call to shut down the run time, or in response to a signal. (For now, we will simply interrupt the server on the command line when we no longer need it.)
  48     """
  49     ic.waitForShutdown()
  50 except:
  51     traceback.print_exc()
  52     status = 1
  53 
  54 if ic:
  55     # Clean up
  56     try:
  57         """
  58         清理ic
  59         """
  60         ic.destroy()
  61     except:
  62         traceback.print_exc()
  63         status = 1
  64 
  65 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 [2008-09-04 13:11:07]

Name Password4deL ;) :( X-( B-)

PageCommentData