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

📄 ch09.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TT>17:         myName(strdup(name)),</TT><TT>18:         mySocialSecurityNumber(strdup(socialSecurityNumber)),</TT><TT>19:         myAddress(strdup(address)),</TT><TT>20:         myMothersMaidenName(strdup(mothersMaidenName)),</TT><TT>21:         myAccounts(), myATMCards() {</TT><TT>22: </TT><TT>23: }</TT><TT>24: </TT><TT>25: // Default constructor.</TT><TT>26: CustomerImpl::CustomerImpl() : myName(NULL),</TT><TT>27:         mySocialSecurityNumber(NULL), myAddress(NULL),</TT><TT>28:         myMothersMaidenName(NULL), myAccounts(), myATMCards() {</TT><TT>29: </TT><TT>30: }</TT><TT>31: </TT><TT>32: // Destructor.</TT><TT>33: CustomerImpl::~CustomerImpl() {</TT><TT>34: </TT><TT>35:     free(myName);</TT><TT>36:     free(mySocialSecurityNumber);</TT><TT>37:     free(myAddress);</TT><TT>38:     free(myMothersMaidenName);</TT><TT>39: }</TT><TT>40: </TT><TT>41: char* CustomerImpl::name() {</TT><TT>42: </TT><TT>43:     return CORBA::strdup(myName);</TT><TT>44: }</TT><TT>45: </TT><TT>46: void CustomerImpl::name(const char* val) {</TT><TT>47: </TT><TT>48:     free(myName);</TT><TT>49:     myName = strdup(val);</TT><TT>50: }</TT><TT>51: </TT><TT>52: char* CustomerImpl::socialSecurityNumber() {</TT><TT>53: </TT><TT>54:     return CORBA::strdup(mySocialSecurityNumber);</TT><TT>55: }</TT><TT>56: </TT><TT>57: char* CustomerImpl::address() {</TT><TT>58: </TT><TT>59:     return CORBA::strdup(myAddress);</TT><TT>60: }</TT><TT>61: </TT><TT>62: void CustomerImpl::address(const char* val) {</TT><TT>63: </TT><TT>64:     free(myAddress);</TT><TT>65:     myAddress = strdup(val);</TT><TT>66: }</TT><TT>67: </TT><TT>68: char* CustomerImpl::mothersMaidenName() {</TT><TT>69: </TT><TT>70:     return CORBA::strdup(myMothersMaidenName);</TT><TT>71: }</TT><TT>72: </TT><TT>73: AccountList* CustomerImpl::getAccounts() {</TT><TT>74: </TT><TT>75:     return &amp;myAccounts;</TT><TT>76: }</TT><TT>77: </TT><B><TT>78: void CustomerImpl::updateAccountBalance(Account_ptr account,</TT></B><B><TT>79:         CORBA::Float balance) {</TT></B><B><TT>80: </TT></B><B><TT>81:     cout &lt;&lt; &quot;CustomerImpl: Received account update:&quot; &lt;&lt; endl &lt;&lt;</TT></B><B><TT>82:             &quot;  New balance is $&quot; &lt;&lt; balance &lt;&lt; endl;</TT></B><TT>83: }</TT></FONT></PRE><H3><A NAME="Heading8"></A><FONT COLOR="#000077">Enhancing the ATMClient</FONT></H3><P>The modifications to <TT>ATMClientMain.cpp</TT> are easy to follow (see Listing9.7). The only additions are that the <TT>ATMClient</TT> now requests the accountupdate service from the <TT>Bank</TT> when the <TT>Account</TT> is created, and whenthe <TT>ATMClient</TT> is finished, it waits for two minutes to give the <TT>Bank</TT>a chance to call <TT>updateAccountBalance()</TT> once or twice before the <TT>ATMClient</TT>exits. (Like <TT>BankImpl</TT>, <TT>ATMClient</TT> uses the Win32 API to cause thecurrent thread to sleep; again, non-Windows developers need to substitute the appropriatemethod call here.)<H4><FONT COLOR="#000077">Listing 9.7. 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><TT>  8: #include &quot;../Bank_c.h&quot;</TT><TT>  9: #include &quot;../BankServer_c.h&quot;</TT><TT> 10: #include &quot;../ATM_c.h&quot;</TT><TT> 11: </TT><TT> 12: int main(int argc, char *const *argv) {</TT><TT> 13: </TT><TT> 14:     // Check the number of arguments; there should be exactly five</TT><TT> 15:     // (six counting the executable name itself).</TT><TT> 16:     if (argc != 6) {</TT><TT> 17:         cout &lt;&lt; &quot;Usage: ATMClient &lt;name&gt; &lt;social security number&gt;&quot;</TT><TT> 18:                 &quot; &lt;address&gt; &lt;mother's maiden name&gt; &lt;PIN&gt;&quot; &lt;&lt; endl;</TT><TT> 19:         return 1;</TT><TT> 20:     }</TT><TT> 21: </TT><TT> 22:     // Assign the command line arguments to the Customer attributes.</TT><TT> 23:     const char* name = argv[1];</TT><TT> 24:     const char* socialSecurityNumber = argv[2];</TT><TT> 25:     const char* address = argv[3];</TT><TT> 26:     const char* mothersMaidenName = argv[4];</TT><TT> 27:     CORBA::Short pin = atoi(argv[5]);</TT><TT> 28: </TT><TT> 29:     // Initialize the ORB and BOA.</TT><TT> 30:     CORBA::ORB_var orb = CORBA::ORB_init(argc, argv);</TT><TT> 31:     CORBA::BOA_var boa = orb-&gt;BOA_init(argc, argv);</TT><TT> 32: </TT><TT> 33:     // Create a Customer object.</TT><TT> 34:     cout &lt;&lt; &quot;ATMClient: Creating new Customer:&quot; &lt;&lt; endl;</TT><TT> 35:     cout &lt;&lt; &quot;  name: &quot; &lt;&lt; name &lt;&lt; endl;</TT><TT> 36:     cout &lt;&lt; &quot;  Social Security number: &quot; &lt;&lt; socialSecurityNumber &lt;&lt;</TT><TT> 37:             endl;</TT><TT> 38:     cout &lt;&lt; &quot;  address: &quot; &lt;&lt; address &lt;&lt; endl;</TT><TT> 39:     cout &lt;&lt; &quot;  mother's maiden name: &quot; &lt;&lt; mothersMaidenName &lt;&lt; endl;</TT><TT> 40:     CustomerImpl customer(name, socialSecurityNumber, address,</TT><TT> 41:             mothersMaidenName);</TT><TT> 42: </TT><TT> 43:     // Notify the BOA that the CustomerImpl object is ready.</TT><TT> 44:     boa-&gt;obj_is_ready(&amp;customer);</TT><TT> 45: </TT><TT> 46:     // Locate a BankServer object and try to get a list of Banks</TT><TT> 47:     // and ATMs from it.</TT><TT> 48:     BankServer_var bankServer;</TT><TT> 49:     try {</TT><TT> 50:         bankServer = BankServer::_bind();</TT><TT> 51:     } catch (const CORBA::Exception&amp; ex) {</TT><TT> 52: </TT><TT> 53:         // The bind attempt failed...</TT><TT> 54:         cout &lt;&lt; &quot;ATMClient: Unable to bind to a BankServer:&quot; &lt;&lt;</TT><TT> 55:                 endl;</TT><TT> 56:         cout &lt;&lt; ex &lt;&lt; endl;</TT><TT> 57:         return 1;</TT><TT> 58:     }</TT><TT> 59:     cout &lt;&lt; &quot;ATMClient: Successfully bound to a BankServer.&quot; &lt;&lt;</TT><TT> 60:             endl;</TT><TT> 61: </TT><TT> 62:     BankList_ptr banks;</TT><TT> 63:     ATMList_ptr ATMs;</TT><TT> 64: </TT><TT> 65:     try {</TT><TT> 66:         banks = bankServer-&gt;getBanks();</TT><TT> 67:         ATMs = bankServer-&gt;getATMs();</TT><TT> 68:     } catch (const CORBA::Exception&amp; ex) {</TT><TT> 69: </TT><TT> 70:         // The attempt failed...</TT><TT> 71:         cout &lt;&lt; &quot;ATMClient: Unable to get lists of Banks and ATMs:&quot;</TT><TT> 72:                 &lt;&lt; endl;</TT><TT> 73:         cout &lt;&lt; ex &lt;&lt; endl;</TT><TT> 74:         return 1;</TT><TT> 75:     }</TT><TT> 76: </TT><TT> 77:     // Use the first Bank and the first ATM that appear in the</TT><TT> 78:     // lists.</TT><TT> 79:     if (banks-&gt;length() == 0) {</TT><TT> 80: </TT><TT> 81:         // No Banks were available.</TT><TT> 82:         cout &lt;&lt; &quot;ATMClient: No Banks available.&quot; &lt;&lt; endl;</TT><TT> 83:         return 1;</TT><TT> 84:     }</TT><TT> 85:     if (ATMs-&gt;length() == 0) {</TT><TT> 86: </TT><TT> 87:         // No ATMs were available.</TT><TT> 88:         cout &lt;&lt; &quot;ATMClient: No ATMs available.&quot; &lt;&lt; endl;</TT><TT> 89:         return 1;</TT><TT> 90:     }</TT><TT> 91:     Bank_var bank = (*banks)[0];</TT><TT> 92:     ATM_var atm = (*ATMs)[0];</TT><TT> 93:     cout &lt;&lt; &quot;ATMClient: Using Bank \&quot;&quot; &lt;&lt; bank-&gt;name() &lt;&lt; &quot;\&quot; and&quot;</TT><TT> 94:             &lt;&lt; &quot; ATM \&quot;&quot; &lt;&lt; atm-&gt;name() &lt;&lt; &quot;\&quot;.&quot; &lt;&lt; endl;</TT><TT> 95: </TT><TT> 96:     // Do some cool stuff.</TT><TT> 97: </TT><TT> 98:     Account_var account;</TT><TT> 99:     ATMCard_var atmCard;</TT><TT>100: </TT><TT>101:     try {</TT><TT>102:         account = bank-&gt;createAccount(&amp;customer, &quot;checking&quot;, 0.0);</TT><TT>103:     } catch (const CORBA::Exception&amp; ex) {</TT><TT>104: </TT><TT>105:         // The createAccount() attempt failed...</TT><TT>106:         cout &lt;&lt; &quot;ATMClient: Unable to create Account.&quot; &lt;&lt; endl;</TT><TT>107:         cout &lt;&lt; ex &lt;&lt; endl;</TT><TT>108:         return 1;</TT><TT>109:     }</TT><TT>110: </TT><B><TT>111:     try {</TT></B><B><TT>112: </TT></B><B><TT>113:         // Request the automatic account update service from the</TT></B><B><TT>114:         // Bank.</TT></B><B><TT>115:         bank-&gt;requestUpdateService(account);</TT></B><B><TT>116:     } catch (const CORBA::Exception&amp; ex) {</TT></B><B><TT>117: </TT></B><B><TT>118:         // The requestUpdateService() attempt failed...</TT></B><B><TT>119:         cout &lt;&lt; &quot;ATMClient: Unable to create Account.&quot; &lt;&lt; endl;</TT></B><B><TT>120:         cout &lt;&lt; ex &lt;&lt; endl;</TT></B><B><TT>121:         return 1;</TT></B><B><TT>122:     }</TT></B><TT>123: </TT><TT>124:     try {</TT><TT>125: </TT><TT>126:         // Print out some Account statistics.</TT><TT>127:         cout &lt;&lt; &quot;ATMClient: Opened new Account:&quot; &lt;&lt; endl;</TT><TT>128:         cout &lt;&lt; &quot;  account number: &quot; &lt;&lt; account-&gt;accountNumber() &lt;&lt;</TT><TT>129:                 endl;</TT><TT>130:         cout &lt;&lt; &quot;  creation date: &quot; &lt;&lt; account-&gt;creationDate() &lt;&lt;</TT><TT>131:                 endl;</TT><TT>132:         cout &lt;&lt; &quot;  account balance: &quot; &lt;&lt; account-&gt;balance() &lt;&lt; endl;</TT><TT>133: </TT><TT>134:         // Ask the Bank to issue an ATMCard for the newly-created</TT><TT>135:         // Account.</TT><TT>136:         cout &lt;&lt; &quot;ATMClient: Getting ATMCard from Bank.&quot; &lt;&lt; endl;</TT><TT>137:         try {</TT><TT>138:             atmCard = bank-&gt;issueATMCard(pin, account);</TT><TT>139:         } catch (const InvalidAccountException&amp;) {</TT><TT>140: </TT><TT>141:             // For some reason, the Account was invalid (this</TT><TT>142:             // shouldn't happen).</TT><TT>143:             cout &lt;&lt; &quot;ATMClient: Exception caught: Invalid Account&quot;</TT><TT>144:                     &lt;&lt; endl;</TT><TT>145:             return 1;</TT><TT>146:         }</TT><TT>147: </TT><TT>148:         // Perform some transactions on the Account through the</TT><TT>149:         // ATM.</TT><TT>150:         cout &lt;&lt; &quot;ATMClient: Performing transactions.&quot; &lt;&lt; endl;</TT><TT>151:         try {</TT><TT>152:             cout &lt;&lt; &quot;  Depositing $250.00...&quot; &lt;&lt; endl;</TT><TT>153:             cout &lt;&lt; &quot;  New balance is $&quot; &lt;&lt; atm-&gt;deposit(atmCard,</TT><TT>154:                     account, pin, 250.00) &lt;&lt; endl;</TT><TT>155: </TT><TT>156:             // This will throw an exception since we're trying to</TT><TT>157:             // withdraw too much.</TT><TT>158:             cout &lt;&lt; &quot;  Withdrawing $500.00...&quot; &lt;&lt; endl;</TT><TT>159:             cout &lt;&lt; &quot;  New balance is $&quot; &lt;&lt; atm-&gt;withdraw(atmCard,</TT><TT>160:                     account, pin, 500.00) &lt;&lt; endl;</TT><TT>161:         } catch (AuthorizationException&amp;) {</TT><TT>162:             cout &lt;&lt; &quot;ATMClient: Exception caught: Invalid PIN or &quot;</TT><TT>163:                     &lt;&lt; &quot;No authorization (as expected)&quot; &lt;&lt; endl;</TT><TT>164:         } catch (InvalidAmountException&amp;) {</TT><TT>165:             cout &lt;&lt; &quot;ATMClient: Exception caught: Invalid amount&quot;</TT><TT>166:                     &lt;&lt; endl;</TT><TT>167:         } catch (InsufficientFundsException&amp;) {</TT><TT>168:             cout &lt;&lt; &quot;ATMClient: Exception caught: Insufficient &quot; &lt;&lt;</TT><TT>169:                     &quot;funds&quot; &lt;&lt; endl;</TT><TT>170:         }</TT><TT>171: </TT><TT>172:         // Perform some more transactions on the Account through</TT><TT>173:         // the ATM.</TT><TT>174:         cout &lt;&lt; &quot;ATMClient: Performing more transactions.&quot; &lt;&lt; endl;</TT><TT>175:         try {</TT><TT>176:             cout &lt;&lt; &quot;  Depositing $500.00...&quot; &lt;&lt; endl;</TT><TT>177:             cout &lt;&lt; &quot;  New balance is $&quot; &lt;&lt;</TT><TT>178:                     atm-&gt;deposit(atmCard, account, pin, 500.00) &lt;&lt;</TT><TT>179:                     endl;</TT><TT>180: </TT><TT>181:             // This will throw an exception since we're using the</TT><TT>182:             // wrong PIN.</TT><TT>183:             cout &lt;&lt; &quot;  Withdrawing $250.00 with incorrect PIN...&quot;</TT><TT>184:                     &lt;&lt; endl;</TT><TT>185:             cout &lt;&lt; &quot;  New balance is $&quot; &lt;&lt; atm-&gt;withdraw(atmCard,</TT>

⌨️ 快捷键说明

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