python写的一个omniORB的调用用例
当初项目选型时,在Ice,Net Remoting,omniORB徘徊许久,最终还是选用了Ice。这是omniORB的一个简单用例。
idl接口描述
module messenger
{
interface SecretMessage
{
string get_message();
};
};
服务端实现:
#coding=utf-8
import messenger, messenger__POA
#定义业务服务对象
class OurSecretMessage(messenger__POA.SecretMessage):
def __init__(self, secret_message):
self.secret_message = secret_message
def get_message(self):
lst=[]
for i in range(1000):
lst.append("this is %d"%(i+1))
return ",".join(lst)
#初始化业务服务对象实例
ourMessage =OurSecretMessage("Hello,omniORBpy!")
#初始化ORA POA POA_MANAGER对象
import sys
from omniORB import CORBA
sys.argv.extend(("-ORBInitRef","NameService=corbaname::localhost"))
orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
poa = orb.resolve_initial_references("RootPOA")
poa_manager = poa._get_the_POAManager()
poa_manager.activate()
#
import CosNaming
name_service_obj=orb.resolve_initial_references("NameService")
name_service_root=name_service_obj._narrow(CosNaming.NamingContext)
message_obj=ourMessage._this()
#将对象饮用字符串IOR写入message.ior文件
#output_handle = open("message.ior","w")
#string_ior = orb.object_to_string(message_obj)
#output_handle.write(string_ior)
#output_handle.close()
#将对象绑定到逻辑名称Messenger.SecretMessage
message_name=[CosNaming.NameComponent("Messenger","SecretMessage")]
name_service_root.bind(message_name,message_obj)
print "server is run..."
orb.run()
客户端实现:
#coding=utf-8
import sys
from omniORB import CORBA
import CosNaming
import messenger
#
#input_handle = open("message.ior","r")
#ior = input_handle.read()
#input_handle.close()
#orb = CORBA.ORB_init(sys.argv,CORBA.ORB_ID)
#
#obj = orb.string_to_object(ior)
#mo = obj._narrow(messenger.SecretMessage)
#result = mo.get_message()
#print result
#初始化客户ORB,指定访问的命名服务器地址
sys.argv.extend(("-ORBInitRef", "NameService=corbaname::localhost"))
orb = CORBA.ORB_init(sys.argv, CORBA.ORB_ID)
name_service_obj = orb.resolve_initial_references("NameService")
name_service_root = name_service_obj._narrow(CosNaming.NamingContext)
assert name_service_root is not None, "Failed to narrow to NamingContext."
#获取命名服务对象,解析服务对象的逻辑名Messenger.SecretMessage
message_name = [CosNaming.NameComponent("Messenger", "SecretMessage")]
message_obj = name_service_root.resolve(message_name)
result = message_obj.get_message()
print result
posted on 2010-03-22 11:03 reck for zhou 阅读(418) 评论(0) 编辑 收藏 举报