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

📄 ch07.htm

📁 corba比较入门级的介绍详细间接了corba访问发布各种细节。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<TT> 8: </TT><TT> 9: // sequence of Customers</TT><TT>10: typedef sequence&lt;Customer&gt; CustomerList;</TT><TT>11: </TT><TT>12: #include &quot;Account.idl&quot;</TT><TT>13: </TT><TT>14: // A Customer can hold one or more Accounts. Presumably, the</TT><TT>15: // Customer is what drives the rest of this application.</TT><TT>16: interface Customer {</TT><TT>17: </TT><TT>18:     // This Customer's name.</TT><TT>19:     attribute string name;</TT><TT>20: </TT><TT>21:     // This Customer's Social Security number.</TT><TT>22:     readonly attribute string socialSecurityNumber;</TT><TT>23: </TT><TT>24:     // This Customer's address.</TT><TT>25:     attribute string address;</TT><TT>26: </TT><TT>27:     // This Customer's mother's maiden name.</TT><TT>28:     readonly attribute string mothersMaidenName;</TT><TT>29: </TT><TT>30:     // Return a list of Accounts held (or co-held) by this</TT><TT>31:     // Customer.</TT><TT>32:     AccountList getAccounts();</TT><TT>33: };</TT><TT>34: 35: #endif</TT> </FONT></PRE><P>After you have modified the IDL interface definitions to raise the proper exceptionsin the proper methods, you can recompile the IDL files to generate new client stubsand server skeletons. When you have generated those, you can begin modifying thecode to use the new exceptions.<H2><A NAME="Heading9"></A><FONT COLOR="#000077">Modifying Server Code to Throw Exceptions</FONT></H2><P>The IDL interface definitions specify <I>which</I> exceptions can be thrown bywhich methods, but they don't specify <I>when,</I> or under what circumstances, thoseexceptions are thrown. Thus, after modifying the IDL definitions, you need to modifythe server code to throw the proper exception at the proper time.<H3><A NAME="Heading10"></A><FONT COLOR="#000077">BankServerImpl</FONT></H3><P>You can start with the simpler server, the <TT>BankServer</TT>. Recall that the<TT>BankServer</TT> interface contains two methods that raise exceptions: <TT>registerBank()</TT>and <TT>unregisterBank()</TT>. The changes need to be made in <TT>BankServerImpl.h</TT>as well as <TT>BankServerImpl.cpp</TT> as they appear in Listings 7.14 and 7.15,again highlighted in<B> bold.</B><BLOCKQUOTE>	<P><HR><B>Note:</B>In C++, it is legal for a method to throw an exception without declaring	that it does so (with the <TT>throw</TT> clause in the method signature). However,	this practice is considered poor style in some circles. It is recommended that all	exceptions thrown by a C++ method be declared in that method's header; this makes	it more apparent to the caller of the method that a particular set of exceptions	might be raised. (Unlike C++, Java enforces this practice.) <HR></BLOCKQUOTE><H4><FONT COLOR="#000077">Listing 7.14. Modified 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><B><TT>21:</TT></B><TT>     virtual void registerBank(Bank_ptr bank) <B>throw</B></TT><B><TT>22:</TT></B><TT>             <B>(InvalidBankException);</B></TT><B><TT>23:</TT></B><TT>     virtual void unregisterBank(Bank_ptr bank) <B>throw</B></TT><B><TT>24:</TT></B><TT>             <B>(InvalidBankException);</B></TT><TT>25:     virtual BankList* getBanks();</TT><TT>26: </TT><TT>27: private:</TT><TT>28: </TT><TT>29:     // Default constructor.</TT><TT>30:     BankServerImpl();</TT><TT>31: </TT><TT>32:     // This BankServer's list of Banks.</TT><TT>33:     std::vector&lt;Bank_ptr&gt; myBanks;</TT><TT>34: };</TT><TT>35: </TT></FONT><FONT COLOR="#0033FF"><TT>36: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 7.15. Modified BankServerImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankServerImpl.cpp</TT><TT> 2: </TT><TT> 3: #include &quot;BankServerImpl.h&quot;</TT><TT> 4: </TT><TT> 5: #include &lt;algorithm&gt;</TT><TT> 6: #include &lt;functional&gt;</TT><TT> 7: </TT><TT> 8: // STL-derived unary function which returns TRUE if Banks are equal.</TT><TT> 9: class IsBankEqual : public std::unary_function&lt;Bank_ptr, bool&gt; {</TT><TT>10: public:</TT><TT>11:     IsBankEqual(argument_type bank) { myBank = bank; }</TT><TT>12:     result_type operator()(argument_type bank) { return bank-&gt;</TT><TT>13:             _is_equivalent(myBank) != 0; }</TT><TT>14: private:</TT><TT>15:     argument_type myBank;</TT><TT>16: };</TT><TT>17: </TT><TT>18: // Constructor.</TT><TT>19: BankServerImpl::BankServerImpl(const char* name) :</TT><TT>20:         _sk_BankServer(name), myBanks() {</TT><TT>21: </TT><TT>22: }</TT><TT>23: </TT><TT>24: // Destructor.</TT><TT>25: BankServerImpl::~BankServerImpl() {</TT><TT>26: </TT><TT>27: }</TT><TT>28: </TT><B><TT>29:</TT></B><TT> void BankServerImpl::registerBank(Bank_ptr bank) <B>throw</B></TT><B><TT>30:</TT></B><TT>         <B>(InvalidBankException)</B> {</TT><TT>31: </TT><TT>32:     // First, ensure that the given Bank doesn't exist already.</TT><TT>33:     std::vector&lt;Bank_ptr&gt;::iterator first = myBanks.begin();</TT><TT>34:     std::vector&lt;Bank_ptr&gt;::iterator last = myBanks.end();</TT><TT>35:     IsBankEqual predicate(bank);</TT><TT>36: </TT><TT>37:     std::vector&lt;Bank_ptr&gt;::iterator matchedBank = std::</TT><TT>38:             find_if(first, last, predicate);</TT><TT>39:     if (matchedBank == last) {</TT><TT>40: </TT><TT>41:         // Bank was not found, so add the given Bank to the end of</TT><TT>42:         // the list.</TT><TT>43:         cout &lt;&lt; &quot;BankServerImpl: Registering Bank \&quot;&quot; &lt;&lt; bank-&gt;</TT><TT>44:                 name() &lt;&lt; &quot;\&quot;.&quot; &lt;&lt; endl;</TT><TT>45:         myBanks.push_back(Bank::_duplicate(bank));</TT><TT>46:         return;</TT><TT>47:     } else {</TT><TT>48: </TT><B><TT>49:</TT></B><TT>         <B>// The Bank was already registered, so throw an exception.</B></TT><B><TT>50:</TT></B><TT>         <B>throw InvalidBankException();</B></TT><TT>51:   }</TT><TT>52: }</TT><TT>53: </TT><B><TT>54:</TT></B><TT> void BankServerImpl::unregisterBank(Bank_ptr bank) <B>throw</B></TT><B><TT>55:</TT></B><TT>         <B>(InvalidBankException) </B>{</TT><TT>56: </TT><TT>57:     std::vector&lt;Bank_ptr&gt;::iterator first = myBanks.begin();</TT><TT>58:     std::vector&lt;Bank_ptr&gt;::iterator last = myBanks.end();</TT><TT>59:     IsBankEqual predicate(bank);</TT><TT>60: </TT><TT>61:     std::vector&lt;Bank_ptr&gt;::iterator matchedBank = std::</TT><TT>62:             find_if(first, last, predicate);</TT><TT>63:     if (matchedBank == last) {</TT><TT>64: </TT><B><TT>65:</TT></B><TT>         <B>// Invalid Bank; throw an exception.</B></TT><B><TT>66:</TT></B><TT>         <B>cout &lt;&lt; &quot;BankServerImpl: Attempted to unregister invalid &quot;</B></TT><B><TT>67:</TT></B><TT>                 <B>&quot;Bank.&quot; &lt;&lt; endl;</B></TT><B><TT>68:</TT></B><TT>         <B>throw InvalidBankException();</B></TT><TT>69:     }</TT><TT>70:     cout &lt;&lt; &quot;BankServerImpl: Unregistering Bank \&quot;&quot; &lt;&lt; bank-&gt;name()</TT><TT>71:             &lt;&lt; &quot;\&quot;.&quot; &lt;&lt; endl;</TT><TT>72: </TT><TT>73:     // Delete the given Bank.</TT><TT>74:     myBanks.erase(matchedBank);</TT><TT>75:     bank-&gt;_release();</TT><TT>76: }</TT><TT>77: </TT><TT>78: BankList* BankServerImpl::getBanks() {</TT><TT>79: </TT><TT>80:     BankList* list = new BankList(myBanks.size());</TT><TT>81:     CORBA::Long i;</TT><TT>82: </TT><TT>83:     for (i = 0; i &lt; myBanks.size(); i++) {</TT><TT>84:         (*list)[i] = Bank::_duplicate(myBanks[i]);</TT><TT>85:     }</TT><TT>86: </TT><TT>87:     return list;88: }</TT> </FONT></PRE><P>Note that the pre-exception version of <TT>registerBank()</TT> did nothing whena client attempted to register a duplicate <TT>Bank</TT> object. The new and improvedversion, however, treats this as an error condition; a duplicate <TT>Bank</TT> registrationresults in an <TT>InvalidBankException</TT> being thrown. Similarly, <TT>unregisterBank()</TT>now throws an <TT>InvalidBankException</TT> when a client attempts to unregistera <TT>Bank</TT> that is not registered with the <TT>BankServer</TT>.<H3><A NAME="Heading11"></A><FONT COLOR="#000077">AccountImpl</FONT></H3><P>Having dealt with the <TT>BankServer</TT> implementation, you can now turn yourattention to the various interfaces contained in the <TT>Bank</TT> application, startingwith <TT>AccountImpl.h</TT> and <TT>AccountImpl.cpp</TT>. As with <TT>BankServerImpl.h</TT>,modify the method signatures in <TT>AccountImpl.h</TT> to specify the exceptionsthrown by <TT>AccountImpl</TT> methods. Similarly, <TT>AccountImpl.cpp</TT> willspecify the conditions under which those exceptions are thrown. The modified <TT>AccountImpl.h</TT>and <TT>AccountImpl.cpp</TT> appear in Listings 7.16 and 7.17.<H4><FONT COLOR="#000077">Listing 7.16. Modified AccountImpl.h.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // AccountImpl.h</TT><TT> 2: </TT><TT> 3: #ifndef AccountImpl_h</TT><TT> 4: #define AccountImpl_h</TT><TT> 5: </TT><TT> 6: #include &quot;../Account_s.h&quot;</TT><TT> 7: </TT><TT> 8: class AccountImpl : public _sk_Account {</TT><TT> 9: </TT><TT>10: // Allow CheckingAccountImpl and SavingsAccountImpl access to the</TT><TT>11: // protected constructor.</TT><TT>12: friend class CheckingAccountImpl;</TT><TT>13: friend class SavingsAccountImpl;</TT><TT>14: </TT><TT>15: public:</TT><TT>16: </TT><TT>17:     // Destructor.</TT><TT>18:     ~AccountImpl();</TT><TT>19: </TT><TT>20:     // These methods are described in Account.idl.</TT><TT>21:     virtual char* accountNumber();</TT><TT>22:     virtual char* creationDate();</TT><TT>23:     virtual CORBA::Float balance();</TT><TT>24:     virtual CustomerList* getCustomers();</TT><B><TT>25:</TT></B><TT>     virtual CORBA::Float withdraw(CORBA::Float amount) <B>throw</B></TT><B><TT>26:</TT></B><TT>         <B>(InvalidAmountException, InsufficientFundsException);</B></TT><B><TT>27:</TT></B><TT>     virtual CORBA::Float deposit(CORBA::Float amount) <B>throw</B></TT><B><TT>28:</TT></B><TT>         <B>(InvalidAmountException);</B></TT><TT>29: </TT><TT>30: protected:</TT><TT>31: </TT><TT>32:     // Constructor.</TT><TT>33:     //</TT><TT>34:     // accountNumber - Account number.</TT><TT>35:     // creationDate - Account creation date.</TT><TT>36:     // initialBalance - Initial Account balance.</TT><TT>37:     // customer - Initial Account owner.</TT><TT>38:     AccountImpl(const char* accountNumber, const char*</TT><TT>39:             creationDate, CORBA::Float initialBalance, Customer_ptr</TT><TT>40:             customer);</TT><TT>41: </TT><TT>42: private:</TT><TT>43: </TT><TT>44:     // Default constructor.</TT><TT>45:     AccountImpl();</TT><TT>46: </TT><TT>47:     // This Account's account number.</TT><TT>48:     char* myAccountNumber;</TT><TT>49: </TT><TT>50:     // This Account's creation date.</TT><TT>51:     char* myCreationDate;</TT><TT>52: </TT><TT>53:     // This Account's current balance.</TT><TT>54:     CORBA::Float myBalance;</TT><TT>55: </TT><TT>56:     // This Account's owners.</TT><TT>57:     CustomerList myOwners;</TT><TT>58: };</TT><TT>59: 60: #endif</TT></FONT></PRE><H4><FONT COLOR="#000077">Listing 7.17. Modified AccountImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // AccountImpl.cpp</TT><TT> 2: </TT><TT> 3: #include &quot;AccountImpl.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: // accountNumber - Account number.</TT><TT>11: // creationDate - Account creation date.</TT><TT>12: // initialBalance - Initial Account balance.</TT><TT>13: // customer - Initial Account owner.</TT><TT>14: AccountImpl::AccountImpl(const char* accountNumber, const char*</TT><TT>15:         creationDate, CORBA::Float initialBalance, Customer_ptr</TT><TT>16:         customer) : _sk_Account(accountNumber),</TT><TT>17:         myAccountNumber(strdup(accountNumber)),</TT><TT>18:         myCreationDate(strdup(creationDate)),</TT><TT>19:         myBalance(initialBalance), myOwners() {</TT><TT>20: </TT><TT>21:     // Add the Customer to the owner list.</TT><TT>22:     myOwners.length(1);</TT><TT>23:     myOwners[0] = Customer::_duplicate(customer);</TT><TT>24: }</TT><TT>25: </TT><TT>26: // Default constructor.</TT><TT>27: AccountImpl::AccountImpl() : myAccountNumber(NULL),</TT><TT>28:         myCreationDate(NULL), myBalance(0.0), myOwners() {</TT><TT>29: </TT><TT>30: }</TT><TT>31: </TT>

⌨️ 快捷键说明

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