📄 ch12.htm
字号:
<TT>63: cout << ex << endl;</TT><TT>64: return 1;</TT><TT>65: }</TT><TT>66: </TT><TT>67: // Wait for CORBA events.</TT><TT>68: cout << "Bank \"" << bankName << "\" ready." << endl;</TT><TT>69: ::boa->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 "ATMImpl.h"</TT><TT> 4: </TT><TT> 5: #include <iostream.h></TT><TT> 6: </TT><B><TT> 7: #include <CosNaming_c.hh></TT></B><TT> 8: </TT><TT> 9: #include "../BankServer_c.h"</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 << "Usage: ATM <atmname>" << 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->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->obj_is_ready(&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-></TT></B><B><TT>39: resolve_initial_references("NameService");</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 = "BankServer1";</TT></B><B><TT>45: name[0].kind = "BankServer";</TT></B><B><TT>46: CORBA::Object_var object = context->resolve(name);</TT></B><B><TT>47: bankServer = BankServer::_narrow(object);</TT></B><B><TT>48: } catch (const CORBA::Exception& ex) {</TT></B><B><TT>49: </TT></B><B><TT>50: // The bind attempt failed...</TT></B><B><TT>51: cout << "ATMImpl: Unable to bind to a BankServer:" << endl;</TT></B><B><TT>52: cout << ex << 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->registerATM(&atm);</TT><TT>59: } catch (const CORBA::Exception& ex) {</TT><TT>60: </TT><TT>61: // The registerATM() attempt failed...</TT><TT>62: cout << "ATMImpl: Unable to register ATM." << endl;</TT><TT>63: cout << ex << endl;</TT><TT>64: return 1;</TT><TT>65: }</TT><TT>66: </TT><TT>67: // Wait for CORBA events.</TT><TT>68: cout << "ATM \"" << atmName << "\" ready." << endl;</TT><TT>69: ::boa->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>"BankServer1"</TT> of type <TT>"BankServer"</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 <iostream.h></TT><TT> 4: #include <stdlib.h></TT><TT> 5: </TT><TT> 6: #include "../Customer/CustomerImpl.h"</TT><TT> 7: </TT><B><TT> 8: #include <CosNaming_c.hh></TT></B><TT> 9: </TT><TT> 10: #include "../Bank_c.h"</TT><TT> 11: #include "../BankServer_c.h"</TT><TT> 12: #include "../ATM_c.h"</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 << "Usage: ATMClient <name> <social security number>"</TT><TT> 20: " <address> <mother's maiden name> <PIN>" << 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->BOA_init(argc, argv);</TT><TT> 34: </TT><TT> 35: // Create a Customer object.</TT><TT> 36: cout << "ATMClient: Creating new Customer:" << endl;</TT><TT> 37: cout << " name: " << name << endl;</TT><TT> 38: cout << " Social Security number: " << socialSecurityNumber <<</TT><TT> 39: endl;</TT><TT> 40: cout << " address: " << address << endl;</TT><TT> 41: cout << " mother's maiden name: " << mothersMaidenName << 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->obj_is_ready(&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-></TT></B><B><TT> 52: resolve_initial_references("NameService");</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 = "BankServer1";</TT></B><B><TT> 58: name[0].kind = "BankServer";</TT></B><B><TT> 59: CORBA::Object_var object = context->resolve(name);</TT></B><B><TT> 60: bankServer = BankServer::_narrow(object);</TT></B><B><TT> 61: } catch (const CORBA::Exception& ex) {</TT></B><B><TT> 62: </TT></B><B><TT> 63: // The bind attempt failed...</TT></B><B><TT> 64: cout << "ATMClient: Unable to bind to a BankServer:" <<</TT></B><B><TT> 65: endl;</TT></B><B><TT> 66: cout << ex << endl;</TT></B><B><TT> 67: return 1;</TT></B><B><TT> 68: }</TT></B><TT> 69: </TT><TT> 70: cout << "ATMClient: Successfully bound to a BankServer." <<</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->getBanks();</TT><TT> 79: ATMs = bankServer->getATMs();</TT><TT> 80: } catch (const CORBA::Exception& ex) {</TT><TT> 81: </TT><TT> 82: // The attempt failed...</TT><TT> 83: cout << "ATMClient: Unable to get lists of Banks and ATMs:"</TT><TT> 84: << endl;</TT><TT> 85: cout << ex << 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->length() == 0) {</TT><TT> 92: </TT><TT> 93: // No Banks were available.</TT><TT> 94: cout << "ATMClient: No Banks available." << endl;</TT><TT> 95: return 1;</TT><TT> 96: }</TT><TT> 97: if (ATMs->length() == 0) {</TT><TT> 98: </TT><TT> 99: // No ATMs were available.</TT><TT>100: cout << "ATMClient: No ATMs available." << 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 << "ATMClient: Using Bank \"" << bank->name() << "\" and"</TT><TT>106: << " ATM \"" << atm->name() << "\"." << 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->createAccount(&customer, "checking", 0.0);</TT><TT>115: } catch (const CORBA::Exception& ex) {</TT><TT>116: </TT><TT>117: // The createAccount() attempt failed...</TT><TT>118: cout << "ATMClient: Unable to create Account." << endl;</TT><TT>119: cout << ex << 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->requestUpdateService(account);</TT><TT>128: } catch (const CORBA::Exception& ex) {</TT><TT>129: </TT><TT>130: // The requestUpdateService() attempt failed...</TT><TT>131: cout << "ATMClient: Unable to create Account." << endl;</TT><TT>132: cout << ex << 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 << "ATMClient: Opened new Account:" << endl;</TT><TT>140: cout << " account number: " << account->accountNumber() <<</TT><TT>141: endl;</TT><TT>142: cout << " creation date: " << account->creationDate() <<</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -