开始玩ICE


代码见附件

http://wiki.woodpecker.org.cn/moin/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.iteye.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",里有一个诡异的字符:
#coding:utf-8
import sys, traceback, Ice
import Demo

class PrinterI(Demo.Printer):
    def printString(self, s, current=None):
        print s

status = 0
ic = None
try:
    """
    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.)
    The call to initialize returns an Ice::Communicator reference, which is the main handle to the Ice run time.
    """
    ic = Ice.initialize(sys.argv)
   
    """
    We create an object adapter by calling createObjectAdapterWithEndpoints on the Communicator instance.
    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.
    监听一个端口
    """
    adapter = ic.createObjectAdapterWithEndpoints(
        "SimplePrinterAdapter", "default -p 10000"
    )
   
    object = PrinterI()
   
    """
    We inform the object adapter of the presence of a new servant by calling add on the adapter;
    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.)
    意译:
    用一个实例监听这个服务,"SimplePrinter"是这个监听者的名称.如果有多个监听者,每一个都要有不同的名字,更专业的说,一个个唯一的对象标识
    """
    adapter.add(object, ic.stringToIdentity("SimplePrinter"))
   
    """
    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.)
    意译:
    激活adapter
    adapter初始化的时候仅仅是持有状态.
    因为如果我们有多个监听者,在没有全部安装之前,我们不希望它处理请求
    """
    adapter.activate()
   
    """
    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.)
    """
    ic.waitForShutdown()
except:
    traceback.print_exc()
    status = 1


if ic:

    # Clean up
    try:
        """
        清理ic
        """
        ic.destroy()
    except:
        traceback.print_exc()
        status = 1

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

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

#coding:utf-8

import sys, traceback, Ice
import Demo

status = 0
ic = None
try:
    ic = Ice.initialize(sys.argv)
   
    """
    The proxy returned by stringToProxy is of type Ice::ObjectPrx, which is at the root of the inheritance tree for interfaces and classes.
    stringToProxy返回了一个Ice::ObjectPrx对象,这个在继承树中是接口和类的根对象
    """
    base = ic.stringToProxy("SimplePrinter:default -p 10000")
   
    """
    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.
    但是,实际上我们需要的是一个Demo::Printer接口,不是一个Object的接口,因此我们需要向下转型.
   
    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.
    checkedCast向服务器发送消息,询问"这是一个Demo::Printer的代理吗?"
    如果是,返回一个Demo.PrinterPrx
    否则,这个一个其他类型的代理,该调用返回None
    """
    printer = Demo.PrinterPrx.checkedCast(base)
    if not printer:
        raise RuntimeError("Invalid proxy")

    printer.printString("Hello World!")
except:
    traceback.print_exc()
    status = 1

if ic:
    # Clean up
    try:
        ic.destroy()
    except:
        traceback.print_exc()
        status = 1

sys.exit(status)

你可能感兴趣的:(C++,c,python,C#,UP)