`
zuroc
  • 浏览: 1319486 次
  • 性别: Icon_minigender_1
  • 来自: 江苏
社区版块
存档分类
最新评论

开始玩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)
分享到:
评论

相关推荐

    VB ICE手机游戏

    用户在下载游戏后,通常需要运行这个主程序才能开始玩游戏。游戏的其他组成部分,如音效、图像、关卡数据等,可能会以资源文件的形式存储在游戏目录下,由主程序动态加载和管理。 总的来说,VB ICE手机游戏是一款...

    一年级英语下册Unit2WhatdoyoudoLesson7学案无答案北京版202004292121

    4. Translate: 这部分要求学生将中文短语翻译成英文,如"在晚上"译为"in the evening","玩游戏"译为"play games"。 总的来说,这个学案设计了一套系统的学习流程,旨在通过互动和实践帮助一年级的学生掌握基本的...

    Bitsmashers:每周挑战一场

    捣碎者 充满像素感的史诗般的8位格斗游戏 ... 加入方接受答案后,便开始交换ICE候选人,并建立连接。 不幸的是,实际的WebRTC数据通道并不总是能正常工作,特别是当浏览器窗口失去焦点时,事情就会

    nessie:Nessie为您的Data Lake提供了类似Git的功能

    Nessie项目是一个为Iceberg Tables,Delta Lake Tables,Hive Tables和Sql Views提供类似Git功能的系统。 可以在上找到更多信息。 使用内西 您可以使用我们小的快速docker镜像快速开始使用Nessie。 docker pull ...

    java餐饮管理源码-sunmughan:孙穆汉

    我喜欢音乐,当我有足够的时间时,我习惯于录制并开始混合和掌握它。 我过去经常在博客中介绍随机提示/技巧和(主要是)Android [at] 关于我的有趣事实:我喜欢玩 :joystick: 复古游戏并继续生存 :ice_cream: 而不是...

    四年级英语上册期中测试卷及听力材料2精选.doc

    Because I can eat ice cream.(你为什么喜欢夏天?因为我可以吃冰淇淋。) 2. Boys and girls. Let’s play a game. OK? OK. Let’s begin.(孩子们,让我们玩个游戏吧。好吗?好的,开始吧。) 3. Do you like ...

    学第一学期六年级英语期中试卷2精选.doc

    7. "I'm interested in playing c_______ games." 可能是喜欢玩"computer"游戏;8. "What's your d________ hobby?" 可能是问对方的"daily"爱好;9. "C______ the door, please." 要求"Close"门;10. "She goes to ...

    HD2安卓4.0.1--GAPPS

    因此,尽管这是一款较早的设备,但它的可定制性和可玩性非常高,受到了极客和技术爱好者的喜爱。 #### 二、Android 4.0.1 系统简介 Android 4.0.1 Ice Cream Sandwich(简称ICS)是Google发布的Android操作系统的...

    人教版五年级英语下册期末复习知识点归纳.pdf

    如`go on a picnic`(去野餐)、`pick apples`(摘苹果)、`make a snowman`(堆雪人)、`go swimming`(去游泳)、`fly ...玩)、`plant flowers`(种花)、`eat ice cream`(吃冰激凌)、`paint a picture`(画画)等...

    batocera巴托塞拉系统,游戏U盘专用系统,制作游戏U盘必备,纯净原版双版本32位64位

    5. **游戏体验**:连接控制器,即可开始享受游戏乐趣。 **注意**:由于此版本是纯净版,不包含使用教程。如果你是初次接触,可能需要自行查找相关教程来了解如何操作和管理游戏库。 **源码软件与游戏引擎** 尽管 ...

    使用matplotlib中scatter方法画散点图

    ### 使用matplotlib中scatter方法画散点图 ...我们从最简单的散点图开始,逐渐深入到更复杂的案例,并详细解析了 `scatter` 函数的各个参数。掌握这些技巧后,你可以更加自如地利用散点图来探索和展示数据之间的关系。

Global site tag (gtag.js) - Google Analytics