⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch12.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	<LI><I>The Naming Service.</I> If the <TT>Bank</TT> application were to grow into	a highly distributed system, it would benefit greatly from the use of the Naming	Service. Rather than use the standard CORBA bind mechanism, you could locate <TT>Bank</TT>s	through a federated Naming Service.<BR>	<BR>		<LI><I>The Object Trader Service.</I> Because the <TT>Bank</TT> application components	are well-known and localized, it is unlikely that components will need to locate	objects based on the services they provide. The Trader Service is geared more towards	applications that require richer facilities for locating services. (Had the scope	of the <TT>Bank</TT> application been more extensive, it might have been worthwhile	to provide the capability to look up <TT>Bank</TT>s based on location, for example.)<BR>	<BR>		<LI><I>The Persistent Object Service.</I> The Persistent Object Service would be	worthwhile, for example, to a <TT>Bank</TT> that needs to store customer and account	data in persistent storage while the information is not in use.<BR>	<BR>		<LI><I>The Property Service.</I> Although the Property Service can be used, for example,	to represent customer or account information, it is not particularly advantageous	to the <TT>Bank</TT> application.<BR>	<BR>		<LI><I>The Query Service.</I> As it stands, the <TT>Bank</TT> application would not	benefit greatly from the Query Service. However, if reporting tools were developed	to work with the <TT>Bank</TT> application, the Query Service would prove useful.<BR>	<BR>		<LI><I>The Relationship Service.</I> For the <TT>Bank</TT> application, there is	little value in using the Relationship Service to model the relationships between	objects (for example, between <TT>Bank</TT>s and <TT>Account</TT>s or between <TT>Customer</TT>s	and <TT>Account</TT>s). However, if new objects were added that might increase the	complexity of relationships, the Relationship Service would be helpful.<BR>	<BR>		<LI><I>The Security Service.</I> Because a <TT>Bank</TT> needs to be a highly secure	institution, the Security Service is indispensable to a production <TT>Bank</TT>	application. Numerous aspects of the Security Service can be utilized, such as user	identification and authorization, security auditing, communication security, and	non-repudiation. The Security Service is a perfect choice for the <TT>Bank</TT> application.<BR>	<BR>		<LI><I>The Time Service.</I> The Time Service is valuable; it ensures that various	<TT>Bank</TT>s are in sync with each other with respect to time.<BR>	<BR>		<LI><I>The Transaction Service.</I> The Transaction Service is another service that	is highly useful to a <TT>Bank</TT> application. In particular, the interoperability	with a TP monitor can coordinate transactions between <TT>Account</TT>s and between	<TT>Bank</TT>s.</UL><P>Looking at the CORBAservices from the perspective of what would provide the mostutility for the <TT>Bank</TT> application, the answer would probably be the SecurityService and the Transaction Service. However, looking at the services from the perspectiveof what's readily available (and what space is available in this book) a more practicalchoice would be the Naming Service.<H3><A NAME="Heading21"></A><FONT COLOR="#000077">Implementing the New Functionality</FONT></H3><P>Deciding where to use the functionality provided by the Naming Service is easy.The Naming Service can replace the use of the standard CORBA bind mechanism; ratherthan bind to objects directly by name, CORBA objects can use the Naming Service tolook up other objects. In the <TT>Bank</TT> application, the only object that islocated in this manner is the <TT>BankServer</TT>; all other server objects (<TT>Bank</TT>sand <TT>ATM</TT>s) are located through the <TT>BankServer</TT> itself. Because thesechanges aren't major, they fit well within the scope of this chapter.<H4><FONT COLOR="#000077">Using the CORBA Naming Service</FONT></H4><P>Rather than bind directly to the <TT>BankServer</TT> using the CORBA bind mechanism,application components instead bind to a Naming Context and then use that NamingContext to resolve a <TT>BankServer</TT> by name. A Naming Context object makes thefunctionality of the CORBA Naming Service available to applications. Note that theNaming Context is simply a CORBA object: Its interface is expressed in IDL, and itis manipulated by an application just as any other CORBA object is.</P><P><B>New Term: </B>A <I>Naming Context</I> is simply a collection of naming structuresthat associate names with either CORBA object references or other Naming Contexts.Taken together, these collections of associations form a hierarchical naming structurethat CORBA objects use to locate other objects.<BLOCKQUOTE>	<P><HR><B>Note:</B>In previous chapters, you built on the application source code developed	in the preceding chapter. This chapter is an exception, however, using the code from	Day 9, not Day 11, as a baseline. This spares you the complexity of using the DII	and CORBAservices in the same application. <HR></BLOCKQUOTE><P>The first modification to the <TT>Bank</TT> application is to change the <TT>BankServer</TT>application so that it registers itself with the Naming Service. None of the changes,to either the <TT>BankServer</TT> or any other application component, need to bemade in the object implementations themselves. The only changes required are in the<TT>main</TT> section of each application, where the object is created. The firstsuch modifications, in <TT>BankServerMain.cpp</TT>, are highlighted in <B>bold</B>in Listing 12.1.<H4><FONT COLOR="#000077">Listing 12.1. BankServerMain.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankServerMain.cpp</TT><TT> 2: </TT><B><TT> 3: #include &lt;CosNaming_c.hh&gt;</TT></B><TT> 4: </TT><TT> 5: #include &quot;BankServerImpl.h&quot;</TT><TT> 6: #include &lt;iostream.h&gt;</TT><TT> 7: </TT><TT> 8: int main(int argc, char *const *argv) {</TT><TT> 9: </TT><B><TT>10:     // Check the number of arguments; there should be exactly one</TT></B><B><TT>11:     // (two counting the executable name itself).</TT></B><B><TT>12:     if (argc != 2) {</TT></B><B><TT>13:         cout &lt;&lt; &quot;Usage: BankServer &lt;bankservername&gt;&quot; &lt;&lt; endl;</TT></B><B><TT>14:         return 1;</TT></B><B><TT>15:     }</TT></B><B><TT>16: </TT></B><B><TT>17:     // Assign the BankServer name to the first argument.</TT></B><B><TT>18:     const char* bankServerName = argv[1];</TT></B><TT>19: </TT><TT>20:     // Initialize the ORB and BOA.</TT><TT>21:     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);</TT><TT>22:     CORBA::BOA_var boa = orb-&gt;BOA_init(argc, argv);</TT><TT>23: </TT><TT>24:     // Create a BankServerImpl object.</TT><TT>25:     BankServerImpl bankServer(bankServerName);</TT><TT>26: </TT><TT>27:     // Notify the BOA that the BankServerImpl object is ready.</TT><TT>28:     boa-&gt;obj_is_ready(&amp;bankServer);</TT><TT>29: </TT><B><TT>30:     // Locate a Naming Service and register the BankServer object</TT></B><B><TT>31:     // with it.</TT></B><B><TT>32:     CosNaming::NamingContext_var context;</TT></B><B><TT>33:     try {</TT></B><B><TT>34:         CORBA::Object_ptr contextObj = orb-&gt;</TT></B><B><TT>35:                 resolve_initial_references(&quot;NameService&quot;);</TT></B><B><TT>36:         CosNaming::NamingContext_var context = CosNaming::</TT></B><B><TT>37:                 NamingContext::_narrow(contextObj);</TT></B><B><TT>38:     } catch (const CORBA::Exception&amp; ex) {</TT></B><B><TT>39:         cout &lt;&lt; &quot;BankServerMain: Could not connect to Naming &quot;</TT></B><B><TT>40:                 &quot;Service:&quot; &lt;&lt; endl &lt;&lt; ex &lt;&lt; endl;</TT></B><B><TT>41:         return 1;</TT></B><B><TT>42:     }</TT></B><B><TT>43:     CosNaming::Name name;</TT></B><B><TT>44:     name.length(1);</TT></B><B><TT>45:     name[0].id = bankServerName;</TT></B><B><TT>46:     name[0].kind = &quot;BankServer&quot;;</TT></B><B><TT>47:     context-&gt;bind(name, &amp;bankServer);</TT></B><TT>48: </TT><TT>49:     // Wait for CORBA events.</TT><TT>50:     cout &lt;&lt; &quot;BankServer ready.&quot; &lt;&lt; endl;</TT><TT>51:     boa-&gt;impl_is_ready();</TT><TT>52: </TT><TT>53:     // When this point is reached, the application is finished.</TT><TT>54:     return 0;55: }</TT> </FONT></PRE><P>Note the process of binding the object to the Naming Service. Previously, a namewas not required for the <TT>BankServer</TT>; now the <TT>BankServer</TT> takes itsname as a command-line argument. The <TT>BankServer</TT> then locates the defaultNaming Context (created by the Naming Service when it is started), creates a <TT>Name</TT>entry corresponding to the <TT>BankServer</TT> object, and finally binds to the NamingService using that <TT>Name</TT> entry. Note also that the <TT>kind</TT> of the object,which can be any arbitrary string, uses the name <TT>&quot;BankServer&quot;</TT>.</P><P>Now direct your attention to the next file to be changed, <TT>BankMain.cpp</TT>,in Listing 12.2. Again, rather than bind directly to a <TT>BankServer</TT>, the <TT>Bank</TT>(and, similarly, the <TT>ATM</TT>) locates a <TT>BankServer</TT> object through theNaming Service. Note that when the <TT>Name</TT> object is created, it is given the<TT>ID</TT> of <TT>&quot;BankServer1&quot;</TT> and the <TT>kind</TT> of <TT>&quot;BankServer&quot;</TT>.This means that the <TT>Bank</TT> expects to connect to an object whose <TT>kind</TT>is <TT>&quot;BankServer&quot;</TT> (which you can verify is the case in <TT>BankServerMain.cpp</TT>,Listing 12.1) and whose <TT>ID</TT> is <TT>&quot;BankServer1&quot;</TT>. Becausethe <TT>BankServer</TT> gets its <TT>ID</TT> (also called the <TT>Name</TT>) fromthe command line, you'll want to start the <TT>BankServer</TT> with the argument<TT>&quot;BankServer1&quot;</TT> when the time comes.<H4><FONT COLOR="#000077">Listing 12.2. BankMain.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankMain.cpp</TT><TT> 2: </TT><TT> 3: #include &quot;BankImpl.h&quot;</TT><TT> 4: </TT><TT> 5: #include &lt;iostream.h&gt;</TT><TT> 6: </TT><B><TT> 7: #include &lt;CosNaming_c.hh&gt;</TT></B><TT> 8: </TT><TT> 9: #include &quot;../BankServer_c.h&quot;</TT><TT>10: </TT><TT>11: CORBA::BOA_var boa;</TT><TT>12: </TT><TT>13: int main(int argc, char *const *argv) {</TT><TT>14: </TT><TT>15:     // Check the number of arguments; there should be exactly one</TT><TT>16:     // (two counting the executable name itself).</TT><TT>17:     if (argc != 2) {</TT><TT>18:         cout &lt;&lt; &quot;Usage: Bank &lt;bankname&gt;&quot; &lt;&lt; endl;</TT><TT>19:         return 1;</TT><TT>20:     }</TT><TT>21: </TT><TT>22:     // Assign the bank name to the first argument.</TT><TT>23:     const char* bankName = argv[1];</TT><TT>24: </TT><TT>25:     // Initialize the ORB and BOA.</TT><TT>26:     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);</TT><TT>27:     ::boa = orb-&gt;BOA_init(argc, argv);</TT><TT>28: </TT><TT>29:     // Create a Bank object.</TT><TT>30:     BankImpl bank(bankName);</TT><TT>31: </TT><TT>32:     // Notify the BOA that the BankImpl object is ready.</TT><TT>33:     ::boa-&gt;obj_is_ready(&amp;bank);</TT><TT>34: </TT><B><TT>35:     // Locate a BankServer object in the Naming Service.</TT></B><B><TT>36:     BankServer_var bankServer;</TT></B><B><TT>37:     try {</TT></B><B><TT>38:         CORBA::Object_ptr contextObj = orb-&gt;</TT></B><B><TT>39:                 resolve_initial_references(&quot;NameService&quot;);</TT></B><B><TT>40:         CosNaming::NamingContext_var context = CosNaming::</TT></B><B><TT>41:                 NamingContext::_narrow(contextObj);</TT></B><B><TT>42:         CosNaming::Name name;</TT></B><B><TT>43:         name.length(1);</TT></B><B><TT>44:         name[0].id = &quot;BankServer1&quot;;</TT></B><B><TT>45:         name[0].kind = &quot;BankServer&quot;;</TT></B><B><TT>46:         CORBA::Object_var object = context-&gt;resolve(name);</TT></B><B><TT>47:         bankServer = BankServer::_narrow(object);</TT></B><B><TT>48:     } catch (const CORBA::Exception&amp; ex) {</TT></B><B><TT>49: </TT></B><B><TT>50:         // The bind attempt failed...</TT></B><B><TT>51:         cout &lt;&lt; &quot;BankImpl: Unable to bind to a BankServer:&quot; &lt;&lt; endl;</TT></B><B><TT>52:         cout &lt;&lt; ex &lt;&lt; endl;</TT></B><B><TT>53:         return 1;</TT></B><B><TT>54:     }</TT></B><TT>55: </TT><TT>56:     // Register with the BankServer.</TT><TT>57:     try {</TT><TT>58:         bankServer-&gt;registerBank(&amp;bank);</TT><TT>59:     } catch (const CORBA::Exception&amp; ex) {</TT><TT>60: </TT><TT>61:         // The registerBank() attempt failed...</TT><TT>62:         cout &lt;&lt; &quot;BankImpl: Unable to register Bank.&quot; &lt;&lt; endl;</TT>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -