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

📄 ch12.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TT>63:         cout &lt;&lt; ex &lt;&lt; endl;</TT><TT>64:         return 1;</TT><TT>65:     }</TT><TT>66: </TT><TT>67:     // Wait for CORBA events.</TT><TT>68:     cout &lt;&lt; &quot;Bank \&quot;&quot; &lt;&lt; bankName &lt;&lt; &quot;\&quot; ready.&quot; &lt;&lt; endl;</TT><TT>69:     ::boa-&gt;impl_is_ready();</TT><TT>70: </TT><TT>71:     // When this point is reached, the application is finished.</TT><TT>72:     return 0;73: }</TT> </FONT></PRE><P>The changes for <TT>ATMMain.cpp</TT>, similar to those in <TT>BankMain.cpp</TT>,appear in Listing 12.3. You will readily see that the two implementations are nearlyidentical.<H4><FONT COLOR="#000077">Listing 12.3. ATMMain.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // ATMMain.cpp</TT><TT> 2: </TT><TT> 3: #include &quot;ATMImpl.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: ATM &lt;atmname&gt;&quot; &lt;&lt; endl;</TT><TT>19:         return 1;</TT><TT>20:     }</TT><TT>21: </TT><TT>22:     // Assign the ATM name to the first argument.</TT><TT>23:     const char* atmName = 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 an ATM object.</TT><TT>30:     ATMImpl atm(atmName);</TT><TT>31: </TT><TT>32:     // Notify the BOA that the ATMImpl object is ready.</TT><TT>33:     ::boa-&gt;obj_is_ready(&amp;atm);</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;ATMImpl: 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;registerATM(&amp;atm);</TT><TT>59:     } catch (const CORBA::Exception&amp; ex) {</TT><TT>60: </TT><TT>61:         // The registerATM() attempt failed...</TT><TT>62:         cout &lt;&lt; &quot;ATMImpl: Unable to register ATM.&quot; &lt;&lt; endl;</TT><TT>63:         cout &lt;&lt; ex &lt;&lt; endl;</TT><TT>64:         return 1;</TT><TT>65:     }</TT><TT>66: </TT><TT>67:     // Wait for CORBA events.</TT><TT>68:     cout &lt;&lt; &quot;ATM \&quot;&quot; &lt;&lt; atmName &lt;&lt; &quot;\&quot; ready.&quot; &lt;&lt; endl;</TT><TT>69:     ::boa-&gt;impl_is_ready();</TT><TT>70: </TT><TT>71:     // When this point is reached, the application is finished.</TT><TT>72:     return 0;73: }</TT> </FONT></PRE><P>The modified <TT>ATMClientMain.cpp</TT>, appearing in Listing 12.4, demonstratessimilar changes. Again, the <TT>ATMClient</TT> searches the Naming Service for anobject named <TT>&quot;BankServer1&quot;</TT> of type <TT>&quot;BankServer&quot;</TT>.The remaining code for <TT>ATMClientMain.cpp</TT> is unchanged.<H4><FONT COLOR="#000077">Listing 12.4. ATMClientMain.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>  1: // ATMClientMain.cpp</TT><TT>  2: </TT><TT>  3: #include &lt;iostream.h&gt;</TT><TT>  4: #include &lt;stdlib.h&gt;</TT><TT>  5: </TT><TT>  6: #include &quot;../Customer/CustomerImpl.h&quot;</TT><TT>  7: </TT><B><TT>  8: #include &lt;CosNaming_c.hh&gt;</TT></B><TT>  9: </TT><TT> 10: #include &quot;../Bank_c.h&quot;</TT><TT> 11: #include &quot;../BankServer_c.h&quot;</TT><TT> 12: #include &quot;../ATM_c.h&quot;</TT><TT> 13: </TT><TT> 14: int main(int argc, char *const *argv) {</TT><TT> 15: </TT><TT> 16:     // Check the number of arguments; there should be exactly five</TT><TT> 17:     // (six counting the executable name itself).</TT><TT> 18:     if (argc != 6) {</TT><TT> 19:         cout &lt;&lt; &quot;Usage: ATMClient &lt;name&gt; &lt;social security number&gt;&quot;</TT><TT> 20:                 &quot; &lt;address&gt; &lt;mother's maiden name&gt; &lt;PIN&gt;&quot; &lt;&lt; endl;</TT><TT> 21:         return 1;</TT><TT> 22:     }</TT><TT> 23: </TT><TT> 24:     // Assign the command line arguments to the Customer attributes.</TT><TT> 25:     const char* name = argv[1];</TT><TT> 26:     const char* socialSecurityNumber = argv[2];</TT><TT> 27:     const char* address = argv[3];</TT><TT> 28:     const char* mothersMaidenName = argv[4];</TT><TT> 29:     CORBA::Short pin = atoi(argv[5]);</TT><TT> 30: </TT><TT> 31:     // Initialize the ORB and BOA.</TT><TT> 32:     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);</TT><TT> 33:     CORBA::BOA_var boa = orb-&gt;BOA_init(argc, argv);</TT><TT> 34: </TT><TT> 35:     // Create a Customer object.</TT><TT> 36:     cout &lt;&lt; &quot;ATMClient: Creating new Customer:&quot; &lt;&lt; endl;</TT><TT> 37:     cout &lt;&lt; &quot;  name: &quot; &lt;&lt; name &lt;&lt; endl;</TT><TT> 38:     cout &lt;&lt; &quot;  Social Security number: &quot; &lt;&lt; socialSecurityNumber &lt;&lt;</TT><TT> 39:             endl;</TT><TT> 40:     cout &lt;&lt; &quot;  address: &quot; &lt;&lt; address &lt;&lt; endl;</TT><TT> 41:     cout &lt;&lt; &quot;  mother's maiden name: &quot; &lt;&lt; mothersMaidenName &lt;&lt; endl;</TT><TT> 42:     CustomerImpl customer(name, socialSecurityNumber, address,</TT><TT> 43:             mothersMaidenName);</TT><TT> 44: </TT><TT> 45:     // Notify the BOA that the CustomerImpl object is ready.</TT><TT> 46:     boa-&gt;obj_is_ready(&amp;customer);</TT><TT> 47: </TT><B><TT> 48:     // Locate a BankServer object in the Naming Service,</TT></B><B><TT> 49:     BankServer_var bankServer;</TT></B><B><TT> 50:     try {</TT></B><B><TT> 51:         CORBA::Object_ptr contextObj = orb-&gt;</TT></B><B><TT> 52:                 resolve_initial_references(&quot;NameService&quot;);</TT></B><B><TT> 53:         CosNaming::NamingContext_var context = CosNaming::</TT></B><B><TT> 54:                 NamingContext::_narrow(contextObj);</TT></B><B><TT> 55:         CosNaming::Name name;</TT></B><B><TT> 56:         name.length(1);</TT></B><B><TT> 57:         name[0].id = &quot;BankServer1&quot;;</TT></B><B><TT> 58:         name[0].kind = &quot;BankServer&quot;;</TT></B><B><TT> 59:         CORBA::Object_var object = context-&gt;resolve(name);</TT></B><B><TT> 60:         bankServer = BankServer::_narrow(object);</TT></B><B><TT> 61:     } catch (const CORBA::Exception&amp; ex) {</TT></B><B><TT> 62: </TT></B><B><TT> 63:         // The bind attempt failed...</TT></B><B><TT> 64:         cout &lt;&lt; &quot;ATMClient: Unable to bind to a BankServer:&quot; &lt;&lt;</TT></B><B><TT> 65:                 endl;</TT></B><B><TT> 66:         cout &lt;&lt; ex &lt;&lt; endl;</TT></B><B><TT> 67:         return 1;</TT></B><B><TT> 68:     }</TT></B><TT> 69: </TT><TT> 70:     cout &lt;&lt; &quot;ATMClient: Successfully bound to a BankServer.&quot; &lt;&lt;</TT><TT> 71:             endl;</TT><TT> 72: </TT><TT> 73:     // Try to get a list of Banks and ATMs from the BankServer.</TT><TT> 74:     BankList_ptr banks;</TT><TT> 75:     ATMList_ptr ATMs;</TT><TT> 76: </TT><TT> 77:     try {</TT><TT> 78:         banks = bankServer-&gt;getBanks();</TT><TT> 79:         ATMs = bankServer-&gt;getATMs();</TT><TT> 80:     } catch (const CORBA::Exception&amp; ex) {</TT><TT> 81: </TT><TT> 82:         // The attempt failed...</TT><TT> 83:         cout &lt;&lt; &quot;ATMClient: Unable to get lists of Banks and ATMs:&quot;</TT><TT> 84:                 &lt;&lt; endl;</TT><TT> 85:         cout &lt;&lt; ex &lt;&lt; endl;</TT><TT> 86:         return 1;</TT><TT> 87:     }</TT><TT> 88: </TT><TT> 89:     // Use the first Bank and the first ATM that appear in the</TT><TT> 90:     // lists.</TT><TT> 91:     if (banks-&gt;length() == 0) {</TT><TT> 92: </TT><TT> 93:         // No Banks were available.</TT><TT> 94:         cout &lt;&lt; &quot;ATMClient: No Banks available.&quot; &lt;&lt; endl;</TT><TT> 95:         return 1;</TT><TT> 96:     }</TT><TT> 97:     if (ATMs-&gt;length() == 0) {</TT><TT> 98: </TT><TT> 99:         // No ATMs were available.</TT><TT>100:         cout &lt;&lt; &quot;ATMClient: No ATMs available.&quot; &lt;&lt; endl;</TT><TT>101:         return 1;</TT><TT>102:     }</TT><TT>103:     Bank_var bank = (*banks)[0];</TT><TT>104:     ATM_var atm = (*ATMs)[0];</TT><TT>105:     cout &lt;&lt; &quot;ATMClient: Using Bank \&quot;&quot; &lt;&lt; bank-&gt;name() &lt;&lt; &quot;\&quot; and&quot;</TT><TT>106:             &lt;&lt; &quot; ATM \&quot;&quot; &lt;&lt; atm-&gt;name() &lt;&lt; &quot;\&quot;.&quot; &lt;&lt; endl;</TT><TT>107: </TT><TT>108:     // Do some cool stuff.</TT><TT>109: </TT><TT>110:     Account_var account;</TT><TT>111:     ATMCard_var atmCard;</TT><TT>112: </TT><TT>113:     try {</TT><TT>114:         account = bank-&gt;createAccount(&amp;customer, &quot;checking&quot;, 0.0);</TT><TT>115:     } catch (const CORBA::Exception&amp; ex) {</TT><TT>116: </TT><TT>117:         // The createAccount() attempt failed...</TT><TT>118:         cout &lt;&lt; &quot;ATMClient: Unable to create Account.&quot; &lt;&lt; endl;</TT><TT>119:         cout &lt;&lt; ex &lt;&lt; endl;</TT><TT>120:         return 1;</TT><TT>121:     }</TT><TT>122: </TT><TT>123:     try {</TT><TT>124: </TT><TT>125:         // Request the automatic account update service from the</TT><TT>126:         // Bank.</TT><TT>127:         bank-&gt;requestUpdateService(account);</TT><TT>128:     } catch (const CORBA::Exception&amp; ex) {</TT><TT>129: </TT><TT>130:         // The requestUpdateService() attempt failed...</TT><TT>131:         cout &lt;&lt; &quot;ATMClient: Unable to create Account.&quot; &lt;&lt; endl;</TT><TT>132:         cout &lt;&lt; ex &lt;&lt; endl;</TT><TT>133:         return 1;</TT><TT>134:     }</TT><TT>135: </TT><TT>136:     try {</TT><TT>137: </TT><TT>138:         // Print out some Account statistics.</TT><TT>139:         cout &lt;&lt; &quot;ATMClient: Opened new Account:&quot; &lt;&lt; endl;</TT><TT>140:         cout &lt;&lt; &quot;  account number: &quot; &lt;&lt; account-&gt;accountNumber() &lt;&lt;</TT><TT>141:                 endl;</TT><TT>142:         cout &lt;&lt; &quot;  creation date: &quot; &lt;&lt; account-&gt;creationDate() &lt;&lt;</TT>

⌨️ 快捷键说明

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