实现库(Implementation Repository)
在先前的示例中,客户端与服务端必须至少联系一次。如果服务器被移动到不同的主机或端口,或服务器已关掉,服务器与客户端之间的绑就定会失败。通过外在的定位代理间接绑定,像实现仓库可以解决上述问题。
实现仓库维护一个数据结构,这个数据结构作为保存跟踪服务器的服务表。它维护已知服务的注册表、正在运行的服务器的记录和对应的端口号,并且如果服务器已注册,还可以根据需要启动服务器。
当服务器创建一个持久化的时候,它设置地址和端口号到IOR的profile body中,这是为了实现响应服务器的实现仓库。当客户端使用这个IOR的时候,如果实现仓库没有关掉,它就与这个实现仓库进行连接。仓库解码它的IOR和使用根据对象key从服务表在索引到的POA的名称。仓库返回当前实际服务器的地址信息。
在这个示例中,让我们继续修改我们先前的股票工厂来支持通过实现仓库的间接绑定。
我们必须做的唯一事情是把我们的子POA与实现仓库注册在注册。
orb->_tao_add_to_IOR_table ("childPOA", stock_factory.in ());
然后,我们和往常一样将对象引用字符串化后打印出来。
CORBA::String_var ior = orb->object_to_string (stock_factory.in ());
练习
修改在简单服务器中的 server.cpp 文件用于创建持久化的子POA。您可以使用相同的 Quoter.idl Stock_i.h Stock_i.cpp Stock_Factory_i.h Stock_Factory_i.cpp Client.cpp ,您可以使用MPC 文件。.解决方案
把您的server.cpp 与 server.cpp 文件作比较。测试
为了测试您的修改,您需要运行四个程序。第一步是启动TAO提供的实现仓库。实现仓库被包括至少一个定位器和一个激活器。定位品为期望使用仓库的应用程序(在这里指客户端)提供可见的接口,而激活器执行激活和维护服务器的实际工作。
首先,我们启动定位器。我们需要输出ImplRepo_Service的IOR到一个文件中,以便于激活器和客户端都能找到定位器。
$ $TAO_ROOT/orbsvcs/ImplRepo_Service/ImplRepo_Service -o implrepo.ior -d 0 -ORBobjrefstyle URL &
其次,我们启动激活器,指向定位器的IOR文件。
$ $TAO_ROOT/orbsvcs/ImplRepo_Service/ImR_Activator -ORBInitRef ImplRepoService=file://implrepo.ior
ImR Activator: Starting doc.ece.uci.edu
ImR Activator: Registered with ImR.
再次,我们要用实现库注册我们的服务器。我们可以使用TAO提供应用程序 $TAO_ROOT/orbsvcs/ImplRepo_Service/tao_imr 来添加我们的服务器到实现仓库中的服务器数据库中。
$ $TAO_ROOT/orbsvcs/ImplRepo_Service/tao_imr -ORBInitRef
ImplRepoService=file://implrepo.ior add childPOA -c
"./server -ORBUseIMR 1 -ORBobjrefstyle URL -ORBInitRef
ImplRepoService=file://implrepo.ior"
Successfully registered server
在Windows平台中,用下面的命令行往实现仓库中注册服务器。
$ACE_ROOT/bin/tao_imr -ORBInitRef ImplRepoService=file://implrepo.ior
add childPOA -c "./server -ORBUseIMR 1 -ORBobjrefstyle URL
-ORBInitRef ImplRepoService=file://implrepo.ior"
我们指定-ORBInitRef
选项来使用在implrepo.ior 文件中的IOR和指定-ORBUseIMR 选项分辩服务器启动还是关闭的通知而使用的IMR。
第二步是为与IMR一起使用的我们的服务器生成简单的IOR,使用ior选项,并写进stock_factory.ior文件。
$tao_imr -ORBInitRef ImplRepoService=file://implrepo.ior ior
childPOA -f stock_factory.ior
corbaloc:iiop:1.1@doc.ece.uci.edu:2690/childPOA
从现在开始,实现库用于对我们服务器的跟踪已全部设置好了并执行我们服务器必要的有关方法调用。
现在,向以前哪样执行客户端。
./client file://stock_factory.ior MSFT RHAT
The price of a stock in "Microsoft, Inc." is $91
The price of a stock in "RedHat, Inc." is $210
为了测试POA的持久化,让我们关掉服务器然后运行客户端。
$tao_imr -ORBInitRef ImplRepoService=file://implrepo.ior
shutdown childPOA
Successfully shutdown server
./client file://stock_factory.ior MSFT RHAT
The price of a stock in "RedHat, Inc." is $210
The price of a stock in "Microsoft, Inc." is $91
更多的阅读资料
欲知更多的实现库,请点这里 here。
The Henning and Vinoski CORBA book discusses POA policies in detail. Likewise, the Schmidt and Vinoski columns in C++ Report also include several articles about the POA. Finally, the TAO distribution includes examples that illustrate how to use the POA policies.