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

📄 ch08.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
application class diagram.</I><H3><A NAME="Heading5"></A><FONT COLOR="#000077">Two New Exceptions: AuthorizationExceptionand InvalidATMException</FONT></H3><P>The operations in the <TT>ATM</TT> class suggest the need for a new exception.To meet this need, define a new exception (call it <TT>AuthorizationException</TT>)that can be thrown by any of the <TT>ATM</TT> methods. Later, you'll implement thesemethods to raise an <TT>AuthorizationException</TT> if there is a problem authenticatingthe customer (for example, the customer-supplied PIN doesn't match the <TT>pin</TT>stored in the <TT>ATMCard</TT>) or authorizing the customer (for example, if the<TT>Account</TT> passed to a method in <TT>ATM</TT> is not authorized on the given<TT>ATMCard</TT>).</P><P>Also, recall the new methods added to the <TT>BankServer</TT> interface. Becausethey essentially duplicate the <TT>Bank</TT>-related operations, which raise <TT>InvalidBankException</TT>s,it follows that there ought to be an analogous exception for the <TT>ATM</TT>-relatedoperations. Therefore, you'll want to define the <TT>InvalidATMException</TT>, whichis used just as you might expect.<H2><A NAME="Heading6"></A><FONT COLOR="#000077">Modifying the IDL Specification</FONT></H2><P>The modification of the IDL specifications is straightforward. Start with themodified <TT>Bank</TT> interface, which appears in Listing 8.1 with changes highlightedin<B> bold.</B><H4><FONT COLOR="#000077">Listing 8.1. Modified Bank.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // Bank.idl</TT><TT> 2: </TT><TT> 3: // Forward declaration of Bank interface.</TT><TT> 4: interface Bank;</TT><TT> 5: </TT><TT> 6: #ifndef Bank_idl</TT><TT> 7: #define Bank_idl</TT><TT> 8: </TT><TT> 9: // sequence of Banks</TT><TT>10: typedef sequence&lt;Bank&gt; BankList;</TT><TT>11: </TT><TT>12: #include &quot;Customer.idl&quot;</TT><TT>13: #include &quot;Account.idl&quot;</TT><B><TT>14: #include &quot;ATMCard.idl&quot;</TT></B><TT>15: #include &quot;Exceptions.idl&quot;</TT><TT>16: </TT><TT>17: // A Bank provides access to Accounts. It can create an Account</TT><TT>18: // on behalf of a Customer, delete an Account, or list the current</TT><TT>19: // Accounts with the Bank.</TT><TT>20: interface Bank {</TT><TT>21: </TT><TT>22:     // This Bank's name.</TT><TT>23:     attribute string name;</TT><TT>24: </TT><TT>25:     // This Bank's address.</TT><TT>26:     attribute string address;</TT><TT>27: </TT><TT>28:     // Create an Account on behalf of the given Customer, with the</TT><TT>29:     // given account type (&quot;savings&quot; or &quot;checking&quot;, where case is</TT><TT>30:     // significant), and the given opening balance.</TT><TT>31:     Account createAccount(in Customer customer, in string</TT><TT>32:             accountType, in float openingBalance);</TT><TT>33: </TT><TT>34:     // Delete the given Account. If the Account is not with this</TT><TT>35:     // Bank, this operation does nothing.</TT><TT>36:     void deleteAccount(in Account account)</TT><TT>37:             raises (InvalidAccountException);</TT><TT>38: </TT><TT>39:     // List all Accounts with this Bank.</TT><TT>40:     AccountList getAccounts();</TT><TT>41: </TT><B><TT>42:</TT></B><TT>     <B>// Issue an ATMCard with the initial PIN and initially</B></TT><B><TT>43:</TT></B><TT>     <B>// authorized on the given Account. If the Account is not with</B></TT><B><TT>44:</TT></B><TT>     <B>// this Bank, this operation does nothing.</B></TT><B><TT>45:</TT></B><TT>     <B>ATMCard issueATMCard(in short pin, in Account account)</B></TT><B><TT>46:</TT></B><TT>             <B>raises (InvalidAccountException);</B></TT><B><TT>47:</TT></B><TT> <B>};</B></TT><TT>48: 49: #endif</TT> </FONT></PRE><P>Turn your attention to the modified <TT>Exceptions.idl</TT>, which now containsthe newly created <TT>AuthorizationException</TT> and <TT>InvalidATMException</TT>.The modified file appears in Listing 8.2.<H4><FONT COLOR="#000077">Listing 8.2. Modified Exceptions.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // Exceptions.idl</TT><TT> 2: </TT><TT> 3: #ifndef Exceptions_idl</TT><TT> 4: #define Exceptions_idl</TT><TT> 5: </TT><B><TT> 6: // This exception is thrown when an authorization fails; for</TT></B><B><TT> 7: // instance, if a Customer-entered PIN does not match the PIN for</TT></B><B><TT> 8: // the supplied ATMCard.</TT></B><B><TT> 9: exception AuthorizationException {</TT></B><B><TT>10:</TT></B><TT> </TT><B><TT>11: };</TT></B><TT>12: </TT><TT>13: // This exception is thrown when an invalid amount is passed to a</TT><TT>14: // method; for instance, if an account is asked to deposit a</TT><TT>15: // negative amount of funds.</TT><TT>16: exception InvalidAmountException {</TT><TT>17: </TT><TT>18: };</TT><TT>19: </TT><TT>20: // This exception is thrown when an invalid Account is passed to a</TT><TT>21: // method expecting an Account object.</TT><TT>22: exception InvalidAccountException {</TT><TT>23: </TT><TT>24: };</TT><TT>25: </TT><TT>26: // This exception is thrown when an invalid Bank is passed to a</TT><TT>27: // method expecting a Bank object.</TT><TT>28: exception InvalidBankException {</TT><TT>29: </TT><TT>30: };</TT><TT>31: </TT><B><TT>32: // This exception is thrown when an invalid ATM is passed to a</TT></B><B><TT>33: // method expecting an ATM object.</TT></B><B><TT>34: exception InvalidATMException {</TT></B><B><TT>35:</TT></B><TT> </TT><B><TT>36: };</TT></B><TT>37: </TT><TT>38: // This exception is thrown when there are insufficient funds to</TT><TT>39: // cover a transaction; for instance, if a withdrawal attempts to</TT><TT>40: // remove more funds than are available in an account.</TT><TT>41: exception InsufficientFundsException {</TT><TT>42: </TT><TT>43: };</TT><TT>44: 45: #endif</TT></FONT> </PRE><P>Now consider the IDL mappings for the newly created classes <TT>ATM</TT> and <TT>ATMCard</TT>,which appear in Listings 8.3 and 8.4. There are no surprises in the IDL interfacedefinitions; all member data and methods map to IDL as you might expect.<H4><FONT COLOR="#000077">Listing 8.3. ATM.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // ATM.idl</TT><TT> 2: </TT><TT> 3: // Forward declaration of ATM interface.</TT><TT> 4: interface ATM;</TT><TT> 5: </TT><TT> 6: #ifndef ATM_idl</TT><TT> 7: #define ATM_idl</TT><TT> 8: </TT><TT> 9: // sequence of ATMs</TT><TT>10: typedef sequence&lt;ATM&gt; ATMList;</TT><TT>11: </TT><TT>12: #include &quot;Account.idl&quot;</TT><TT>13: #include &quot;ATMCard.idl&quot;</TT><TT>14: #include &quot;Exceptions.idl&quot;</TT><TT>15: </TT><TT>16: // An ATM is used to (indirectly) access Accounts. Each operation</TT><TT>17: // through the ATM is verified through the ATMCard given; for</TT><TT>18: // example, an Account being operated on must be authorized by the</TT><TT>19: // given ATMCard.</TT><TT>20: interface ATM {</TT><TT>21: </TT><TT>22:     // This ATM's name.</TT><TT>23:     attribute string name;</TT><TT>24: </TT><TT>25:     // Withdraw the given amount from the given Account. Returns</TT><TT>26:     // the new account balance. If the given ATMCard is not</TT><TT>27:     // authorized on the given Account, or the given PIN does not</TT><TT>28:     // match the ATMCard's PIN, this operation does nothing.</TT><TT>29:     float withdraw(in ATMCard card, in Account account, in short</TT><TT>30:             pin, in float amount) raises (AuthorizationException,</TT><TT>31:             InvalidAmountException, InsufficientFundsException);</TT><TT>32: </TT><TT>33:     // Deposit the given amount into the given Account. Returns the</TT><TT>34:     // new account balance. If the given ATMCard is not authorized</TT><TT>35:     // on the given Account, or the given PIN does not match the</TT><TT>36:     // ATMCard's PIN, this operation does nothing.</TT><TT>37:     float deposit(in ATMCard card, in Account account, in short</TT><TT>38:             pin, in float amount) raises (AuthorizationException,</TT><TT>39:             InvalidAmountException);</TT><TT>40: </TT><TT>41:     // Return the current balance of the given Account. If the</TT><TT>42:     // given ATMCard is not authorized on the given Account, or the</TT><TT>43:     // given PIN does not match the ATMCard's PIN, this operation</TT><TT>44:     // does nothing.</TT><TT>45:     float getBalance(in ATMCard card, in Account account, in short</TT><TT>46:             pin) raises (AuthorizationException);</TT><TT>47: };</TT><TT>48: 49: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 8.4. ATMCard.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // ATMCard.idl</TT><TT> 2: </TT><TT> 3: // Forward declaration of ATMCard interface.</TT><TT> 4: interface ATMCard;</TT><TT> 5: </TT><TT> 6: #ifndef ATMCard_idl</TT><TT> 7: #define ATMCard_idl</TT><TT> 8: </TT><TT> 9: // sequence of ATMCards</TT><TT>10: typedef sequence&lt;ATMCard&gt; ATMCardList;</TT><TT>11: </TT><TT>12: #include &quot;Account.idl&quot;</TT><TT>13: #include &quot;Exceptions.idl&quot;</TT><TT>14: </TT><TT>15: // An ATMCard is used to access an ATM. It maintains a list of</TT><TT>16: // Accounts which it is authorized to access, as well as a PIN</TT><TT>17: // which must be provided when the ATMCard is used.</TT><TT>18: interface ATMCard {</TT><TT>19: </TT><TT>20:     // This ATMCard's PIN.</TT><TT>21:     attribute short pin;</TT><TT>22: </TT><TT>23:     // List all Accounts which this ATMCard is authorized to use.</TT><TT>24:     AccountList getAccounts();</TT><TT>25: </TT><TT>26:     // Add the given Account to the list of Accounts which this</TT><TT>27:     // ATMCard is authorized to use.</TT><TT>28:     void addAccount(in Account account)</TT><TT>29:             raises (InvalidAccountException);</TT><TT>30: </TT><TT>31:     // Remove the given Account from the list of Accounts which</TT><TT>32:     // this ATMCard is authorized to use.</TT><TT>33:     void removeAccount(in Account account)</TT><TT>34:             raises (InvalidAccountException);</TT><TT>35: </TT><TT>36:     // Return true if the given Account is authorized on this</TT><TT>37:     // ATMCard.</TT><TT>38:     boolean isAuthorized(in Account account);</TT><TT>39: };</TT><TT>40: 41: #endif</TT> </FONT></PRE><P>This concludes the analysis and design portion of the ATM functionality enhancements;you're now ready to begin implementing the changes in the code itself.<H2><A NAME="Heading7"></A><FONT COLOR="#000077">Implementing the New Functionality</FONT></H2><P>To implement the <TT>ATM</TT>-related enhancements, you must modify the implementationfiles to provide the new functionality. As a quick overview, the changes you needto make are as follows:<UL>	<LI><I>Enhance the </I><TT>BankServer</TT><I>.</I> Essentially, everything that the	<TT>BankServer</TT> currently does for <TT>Bank</TT>s, you will extend it to do for	<TT>ATM</TT>s as well.<BR>	<BR>		<LI><I>Enhance the </I><TT>Bank</TT><I>.</I> In addition to requiring the capability	to issue <TT>ATMCards</TT>, the <TT>Bank</TT> also provides the implementation for	the <TT>ATMCard</TT> as well.<BR>	<BR>		<LI><I>Implement the </I><TT>ATM</TT><I> server.</I> Essentially, the <TT>ATM</TT>	accepts transaction requests from <TT>Customer</TT>s, validates the <TT>Customer</TT>'s	authorization against an <TT>ATMCard</TT>, and, if authorized, forwards the requests	to the appropriate <TT>Account</TT>s.<BR>	<BR>		<LI><I>Implement the </I><TT>ATM</TT><I> client.</I> This application is an extension	of the previous <TT>Bank</TT> client, modified to create an <TT>Account</TT> and	subsequently access that <TT>Account</TT> through an <TT>ATM</TT> rather than directly.</UL><H3><A NAME="Heading8"></A><FONT COLOR="#000077">Enhancing the BankServer</FONT></H3><P>The first step in the implementation of ATM functionality is to enhance the <TT>BankServer</TT>to support <TT>ATM</TT>s as well as <TT>Bank</TT>s. Because the support is exactlythe same for each type of object (for example, <TT>ATM</TT>s and <TT>Bank</TT>s canboth register and unregister with the <TT>BankServer</TT>), the <TT>ATM</TT>-relatedmethods can be copied almost directly from their <TT>Bank</TT>-related counterparts.The necessary modifications to the <TT>BankServer</TT> implementation files appearin Listings 8.5-8.7, with changes from the previous version highlighted in <B>bold.</B><H4><FONT COLOR="#000077">Listing 8.5. BankServerImpl.h.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankServerImpl.h</TT><TT> 2: </TT><TT> 3: #ifndef BankServerImpl_h</TT><TT> 4: #define BankServerImpl_h</TT><TT> 5: </TT><TT> 6: #include &lt;vector&gt;</TT><TT> 7: </TT><TT> 8: #include &quot;../BankServer_s.h&quot;</TT><TT> 9: </TT><TT>10: class BankServerImpl : public _sk_BankServer {</TT><TT>11: </TT><TT>12: public:</TT><TT>13: </TT><TT>14:     // Constructor.</TT><TT>15:     BankServerImpl(const char* name);</TT><TT>16: </TT><TT>17:     // Destructor.</TT><TT>18:     ~BankServerImpl();</TT><TT>19: </TT><TT>20:     // These methods are described in BankServer.idl.</TT><TT>21:     virtual void registerBank(Bank_ptr bank) throw</TT><TT>22:             (InvalidBankException);</TT><TT>23:     virtual void unregisterBank(Bank_ptr bank) throw</TT><TT>24:             (InvalidBankException);</TT><TT>25:     virtual BankList* getBanks();</TT><B><TT>26:     virtual void registerATM(ATM_ptr atm) throw</TT></B><B><TT>27:             (InvalidATMException);</TT></B><B><TT>28:     virtual void unregisterATM(ATM_ptr atm) throw</TT></B><B><TT>29:             (InvalidATMException);</TT></B><B><TT>30:     virtual ATMList* getATMs();</TT></B><TT>31: </TT><TT>32: private:</TT><TT>33: </TT><TT>34:     // Default constructor.</TT>

⌨️ 快捷键说明

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