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

📄 ch09.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TT>113:     Account_ptr newAccount;</TT><TT>114: </TT><TT>115:     if (strcmp(accountType, &quot;savings&quot;) == 0) {</TT><TT>116: </TT><TT>117:         // Create a new SavingsAccountImpl object for the Account.</TT><TT>118:         cout &lt;&lt; &quot;BankImpl: Creating new SavingsAccount for &quot;</TT><TT>119:                 &quot;Customer &quot; &lt;&lt; customer-&gt;name() &lt;&lt; &quot;.&quot; &lt;&lt; endl;</TT><TT>120:         newAccount = new SavingsAccountImpl(getNextAccountNumber(),</TT><TT>121:                 getCurrentDate(), openingBalance, customer, 10.0);</TT><TT>122:     } else if (strcmp(accountType, &quot;checking&quot;) == 0) {</TT><TT>123: </TT><TT>124:         // Create a new CheckingAccountImpl object for the Account.</TT><TT>125:         cout &lt;&lt; &quot;BankImpl: Creating new CheckingAccount for &quot;</TT><TT>126:                 &quot;Customer &quot; &lt;&lt; customer-&gt;name() &lt;&lt; &quot;.&quot; &lt;&lt; endl;</TT><TT>127:         newAccount = new CheckingAccountImpl(getNextAccountNumber(),</TT><TT>128:                 getCurrentDate(), openingBalance, customer);</TT><TT>129:     } else {</TT><TT>130: </TT><TT>131:         // Invalid Account type; do nothing.</TT><TT>132:         cout &lt;&lt; &quot;BankImpl: Customer &quot; &lt;&lt; customer-&gt;name() &lt;&lt;</TT><TT>133:                 &quot; requested invalid Account type \&quot;&quot; &lt;&lt; accountType</TT><TT>134:                 &lt;&lt; &quot;\&quot;.&quot; &lt;&lt; endl;</TT><TT>135:         return Account::_nil();</TT><TT>136:     }</TT><TT>137: </TT><TT>138:     // Add the created Account at the end of the list and return it.</TT><TT>139:     ::boa-&gt;obj_is_ready(newAccount);</TT><TT>140:     myAccounts.push_back(Account::_duplicate(newAccount));</TT><TT>141:     return newAccount;</TT><TT>142: }</TT><TT>143: </TT><TT>144: void BankImpl::deleteAccount(Account_ptr account) throw</TT><TT>145:         (InvalidAccountException) {</TT><TT>146: </TT><TT>147:     std::vector&lt;Account_ptr&gt;::iterator first = myAccounts.begin();</TT><TT>148:     std::vector&lt;Account_ptr&gt;::iterator last = myAccounts.end();</TT><TT>149:     IsAccountEqual predicate(account);</TT><TT>150: </TT><TT>151:     std::vector&lt;Account_ptr&gt;::iterator matchedAccount = std::</TT><TT>152:             find_if(first, last, predicate);</TT><TT>153:     if (matchedAccount == last) {</TT><TT>154: </TT><TT>155:         // Invalid Account; throw an exception.</TT><TT>156:         cout &lt;&lt; &quot;BankImpl: Attempted to delete invalid Account.&quot; &lt;&lt;</TT><TT>157:                 endl;</TT><TT>158:         throw InvalidAccountException();</TT><TT>159:     }</TT><TT>160:     cout &lt;&lt; &quot;BankImpl: Deleting Account \&quot;&quot; &lt;&lt; account-&gt;</TT><TT>161:             accountNumber() &lt;&lt; &quot;\&quot;.&quot; &lt;&lt; endl;</TT><TT>162: </TT><TT>163:     // Delete the given Account.</TT><TT>164:     myAccounts.erase(matchedAccount);</TT><TT>165:     account-&gt;_release();</TT><TT>166: }</TT><TT>167: </TT><TT>168: AccountList* BankImpl::getAccounts() {</TT><TT>169: </TT><TT>170:     AccountList* list = new AccountList(myAccounts.size());</TT><TT>171:     CORBA::Long i;</TT><TT>172: </TT><TT>173:     for (i = 0; i &lt; myAccounts.size(); i++) {</TT><TT>174:         (*list)[i] = Account::_duplicate(myAccounts[i]);</TT><TT>175:     }</TT><TT>176: </TT><TT>177:     return list;</TT><TT>178: }</TT><TT>179: </TT><TT>180: ATMCard_ptr BankImpl::issueATMCard(CORBA::Short pin, Account_ptr</TT><TT>181:         account) throw (InvalidAccountException) {</TT><TT>182: </TT><TT>183:     // First check to see if the Account is with this Bank.</TT><TT>184:     std::vector&lt;Account_ptr&gt;::iterator first = myAccounts.begin();</TT><TT>185:     std::vector&lt;Account_ptr&gt;::iterator last = myAccounts.end();</TT><TT>186:     IsAccountEqual predicate(account);</TT><TT>187: </TT><TT>188:     std::vector&lt;Account_ptr&gt;::iterator matchedAccount = std::</TT><TT>189:             find_if(first, last, predicate);</TT><TT>190:     if (matchedAccount == last) {</TT><TT>191: </TT><TT>192:         // Invalid Account; throw an exception.</TT><TT>193:         throw InvalidAccountException();</TT><TT>194:     }</TT><TT>195: </TT><TT>196:     // If we got this far, the Account must exist with this Bank,</TT><TT>197:     // so we can proceed.</TT><TT>198:     ATMCard_ptr newCard = new ATMCardImpl(pin, account);</TT><TT>199: </TT><TT>200:     return ATMCard::_duplicate(newCard);</TT><TT>201: }</TT><TT>202: </TT><B><TT>203: void BankImpl::requestUpdateService(Account_ptr account) throw</TT></B><B><TT>204:         (InvalidAccountException) {</TT></B><B><TT>205: </TT></B><B><TT>206:     // First check to see if the Account is with this Bank.</TT></B><B><TT>207:     std::vector&lt;Account_ptr&gt;::iterator first = myAccounts.begin();</TT></B><B><TT>208:     std::vector&lt;Account_ptr&gt;::iterator last = myAccounts.end();</TT></B><B><TT>209:     IsAccountEqual predicate(account);</TT></B><B><TT>210: </TT></B><B><TT>211:     std::vector&lt;Account_ptr&gt;::iterator matchedAccount = std::</TT></B><B><TT>212:             find_if(first, last, predicate);</TT></B><B><TT>213:     if (matchedAccount == last) {</TT></B><B><TT>214: </TT></B><B><TT>215:         // Invalid Account; throw an exception.</TT></B><B><TT>216:         throw InvalidAccountException();</TT></B><B><TT>217:     }</TT></B><B><TT>218: </TT></B><B><TT>219:     // If we got this far, the Account must exist with this Bank,</TT></B><B><TT>220:     // so we can proceed.</TT></B><B><TT>221:     mySubscribedAccounts.push_back(Account::_duplicate(account));</TT></B><B><TT>222: }</TT></B><TT>223: </TT><TT>224: // Return the next available account number. The result is returned</TT><TT>225: // in a static buffer.</TT><TT>226: char* BankImpl::getNextAccountNumber() {</TT><TT>227: </TT><TT>228:     static char accountNumber[16] = &quot;Account        &quot;;</TT><TT>229: </TT><TT>230:     sprintf(accountNumber + 7, &quot;%08u&quot;, myNextAccountNumber++);</TT><TT>231: </TT><TT>232:     return accountNumber;</TT><TT>233: }</TT><TT>234: </TT><TT>235: // Return the current date in the form &quot;Mmm DD YYYY&quot;. The result is</TT><TT>236: // returned in a static buffer.</TT><TT>237: char* BankImpl::getCurrentDate() {</TT><TT>238: </TT><TT>239:     static char currentDate[12] = &quot;           &quot;;</TT><TT>240: </TT><TT>241:     time_t ltime;</TT><TT>242:     time(&amp;ltime);</TT><TT>243:     char* ctimeResult = ctime(&amp;ltime);</TT><TT>244: </TT><TT>245:     memcpy(currentDate, ctimeResult + 4, 3);</TT><TT>246:     memcpy(currentDate + 4, ctimeResult + 8, 2);</TT><TT>247:     memcpy(currentDate + 7, ctimeResult + 20, 4);</TT><TT>248: </TT><TT>249:     return currentDate;250: }</TT></FONT></PRE><BLOCKQUOTE>	<P><HR><B>Warning: </B><TT>BankImpl.cpp</TT>, as it appears in Listing 9.4, introduces the	use of threads in the server application. Depending on your operating system, however,	the file will not compile as listed. <TT>BankImpl.cpp</TT> makes use of the Win32	APIs for using threads, so it will compile on Windows 95 and NT. Users of other platforms,	particularly UNIX platforms, need to modify the code slightly to use the thread API	(such as POSIX threads) for their operating system. This is a trivial matter because	only one new thread is created in the <TT>BankImpl.cpp</TT> implementation.<BR>	<BR>	Also, as a reminder, the code presented here is not thread-safe--for the sake of	clarity, no checks are made to ensure that both threads don't access the <TT>mySubscribedAccounts</TT>	vector simultaneously. This non-thread-safe code works for demonstration purposes,	but for a production system, you'll definitely want to ensure that all code is thread-safe	when using multithreading in an application. <HR></BLOCKQUOTE><P>Now take a closer look at Listing 9.4. The first thing you'll notice, in lines31-60, is the addition of a function called <TT>updateAccountThreadFunction()</TT>that executes in a second thread.</P><P>First of all, as you can see in lines 31-34, <TT>updateAccountThreadFunction()</TT>expects its argument to be a pointer to an STL vector of <TT>Account</TT>s (you'llsee later that this is the argument with which the function is actually called).</P><P>What is happening in lines 36-37 is that the thread is being set up to run foras long as the server application is running (hence, the <TT>while (1)</TT>, whichwill never exit). Also, the loop is set up to sleep for 60,000 milliseconds (oneminute) between executions.</P><P>Every minute, the <TT>for</TT> statement in line 41 will cause the thread to iteratethrough its list of <TT>Account</TT>s (lines 43-46), and then to iterate througheach of the Customers belonging to those Accounts, as you can see in lines 47-51.Also, in line 51 you see that the <TT>updateAccountBalance()</TT> message is sentto each of the <TT>Customer</TT>s.</P><P>Finally, if for some reason an exception is thrown by the remote method call,it is ignored, as you can see in lines 52-56. (<TT>updateAccountThreadFunction()</TT>catches the exception but does nothing with it.)<H3><A NAME="Heading7"></A><FONT COLOR="#000077">Enhancing the CustomerImpl</FONT></H3><P>The enhancements to <TT>CustomerImpl</TT> are simple. <TT>CustomerImpl</TT> needonly accept the <TT>updateAccountBalance()</TT> message and print a message indicatingthe new <TT>Account</TT> balance. The modified <TT>CustomerImpl.h</TT> and <TT>CustomerImpl.cpp</TT>appear in Listings 9.5 and 9.6.<H4><FONT COLOR="#000077">Listing 9.5. CustomerImpl.h.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CustomerImpl.h</TT><TT> 2: </TT><TT> 3: #ifndef CustomerImpl_h</TT><TT> 4: #define CustomerImpl_h</TT><TT> 5: </TT><TT> 6: #include &quot;../Customer_s.h&quot;</TT><TT> 7: #include &quot;../ATMCard_c.h&quot;</TT><TT> 8: </TT><TT> 9: class CustomerImpl : public _sk_Customer {</TT><TT>10: </TT><TT>11: public:</TT><TT>12: </TT><TT>13:     // Constructor.</TT><TT>14:     //</TT><TT>15:     // name - Customer's name.</TT><TT>16:     // socialSecurityNumber - Customer's Social Security number.</TT><TT>17:     // address - Customer's address.</TT><TT>18:     // mothersMaidenName - Customer's mother's maiden name.</TT><TT>19:     CustomerImpl(const char* name, const char* socialSecurityNumber,</TT><TT>20:             const char* address, const char* mothersMaidenName);</TT><TT>21: </TT><TT>22:     // Destructor.</TT><TT>23:     ~CustomerImpl();</TT><TT>24: </TT><TT>25:     // These methods are described in Customer.idl.</TT><TT>26:     virtual char* name();</TT><TT>27:     virtual void name(const char* val);</TT><TT>28:     virtual char* socialSecurityNumber();</TT><TT>29:     virtual char* address();</TT><TT>30:     virtual void address(const char* val);</TT><TT>31:     virtual char* mothersMaidenName();</TT><TT>32:     virtual AccountList* getAccounts();</TT><B><TT>33:     virtual void updateAccountBalance(Account_ptr account, CORBA::</TT></B><B><TT>34:             Float balance);</TT></B><TT>35: </TT><TT>36: private:</TT><TT>37: </TT><TT>38:     // Default constructor.</TT><TT>39:     CustomerImpl();</TT><TT>40: </TT><TT>41:     // This Customer's name.</TT><TT>42:     char* myName;</TT><TT>43: </TT><TT>44:     // This Customer's Social Security number.</TT><TT>45:     char* mySocialSecurityNumber;</TT><TT>46: </TT><TT>47:     // This Customer's address.</TT><TT>48:     char* myAddress;</TT><TT>49: </TT><TT>50:     // This Customer's mother's maiden name.</TT><TT>51:     char* myMothersMaidenName;</TT><TT>52: </TT><TT>53:     // This Customer's Accounts.</TT><TT>54:     AccountList myAccounts;</TT><TT>55: </TT><TT>56:     // This Customer's ATMCards.</TT><TT>57:     ATMCardList myATMCards;</TT><TT>58: };</TT><TT>59: 60: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 9.6. CustomerImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // CustomerImpl.cpp</TT><TT> 2: </TT><TT> 3: #include &quot;CustomerImpl.h&quot;</TT><TT> 4: </TT><TT> 5: #include &lt;iostream.h&gt;</TT><TT> 6: #include &lt;string.h&gt;</TT><TT> 7: </TT><TT> 8: // Constructor.</TT><TT> 9: //</TT><TT>10: // name - Customer's name.</TT><TT>11: // socialSecurityNumber - Customer's Social Security number.</TT><TT>12: // address - Customer's address.</TT><TT>13: // mothersMaidenName - Customer's mother's maiden name.</TT><TT>14: CustomerImpl::CustomerImpl(const char* name, const char*</TT><TT>15:         socialSecurityNumber, const char* address, const char*</TT><TT>16:         mothersMaidenName) : _sk_Customer(socialSecurityNumber),</TT>

⌨️ 快捷键说明

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