📄 ch06.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD><SCRIPT LANGUAGE="JavaScript"><!--function popUp(pPage) { var fullURL = document.location; var textURL = fullURL.toString(); var URLlen = textURL.length; var lenMinusPage = textURL.lastIndexOf("/"); lenMinusPage += 1; var fullPath = textURL.substring(0,lenMinusPage); popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394'); figDoc= popUpWin.document; zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>'; zhtm += '</HEAD>'; zhtm += '<BODY bgcolor="#FFFFFF">'; zhtm += '<IMG SRC="' + fullPath + pPage + '">'; zhtm += '<P><B>' + pPage + '</B>'; zhtm += '</BODY></HTML>'; window.popUpWin.document.write(zhtm); window.popUpWin.document.close(); // Johnny Jackson 4/28/98 }//--> </SCRIPT> <META NAME="Author" Content="Bryan Flores"> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"> <TITLE>Teach Yourself CORBA In 14 Days -- Ch 6 -- Implementing Basic Application Capabilities</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><CENTER><H1><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR><FONT COLOR="#000077">Teach Yourself CORBA In 14 Days</FONT></H1></CENTER><CENTER><P><A HREF="../ch05/ch05.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch07/ch07.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><CENTER><H1><FONT COLOR="#000077">Day 6<BR>Implementing Basic Application Capabilities</FONT><A HREF="#Heading1"></A></H1></CENTER><UL> <LI><A HREF="#Heading1">Implementing Basic Bank Server Capabilities</A> <UL> <LI><A HREF="#Heading2">Implementing the BankServer Interface</A> <LI><A HREF="#Heading3">Implementing the Bank Interface</A> <LI><A HREF="#Heading4">Implementing the Account Interface</A> <LI><A HREF="#Heading5">Implementing the CheckingAccount Interface</A> <LI><A HREF="#Heading6">Implementing the SavingsAccount Interface</A> </UL> <LI><A HREF="#Heading7">Implementing Basic Client Capabilities</A> <UL> <LI><A HREF="#Heading8">Implementing the Customer Interface</A> <LI><A HREF="#Heading9">Implementing Additional Client Functionality</A> </UL> <LI><A HREF="#Heading10">Running the Examples</A> <UL> <LI><A HREF="#Heading11">Starting the CORBA Naming Service</A> <LI><A HREF="#Heading12">Starting the BankServer Component</A> <LI><A HREF="#Heading13">Starting the Bank Component</A> <LI><A HREF="#Heading14">Running the Client Application</A> </UL> <LI><A HREF="#Heading15">Summary</A> <LI><A HREF="#Heading16">Q&A</A> <LI><A HREF="#Heading17">Workshop</A> <UL> <LI><A HREF="#Heading18">Quiz</A> <LI><A HREF="#Heading19">Exercise</A> </UL></UL><P><HR SIZE="4"><CENTER><H1><FONT COLOR="#000077"></FONT></H1></CENTER><P>On Day 5, "Designing the System: A Crash Course in Object-Oriented Analysisand Design," you mapped an application design to a set of IDL interfaces thatdefined the structure on which that design would be realized. Today you'll implementthose interfaces, thus creating an operational set of servers and clients that implementthe basic capabilities of the <TT>Bank</TT> application. You'll enhance the applicationwith additional functionality in future chapters, but in this chapter you'll concentrateon implementing the core set of features of the application.<BLOCKQUOTE> <P><HR><B>Note:</B>The examples in this chapter have been developed using Visigenic Software's (<TT>http://www.visigenic.com/</TT>) VisiBroker/C++ product. Despite the existence of a standard IDL language mapping for C++, various inconsistencies still exist between CORBA products. If you are using a different product, such as IONA Technologies' Orbix, you might need to modify the sample code slightly, although these changes will be minimal. Consult your product documentation for language mapping information if you experience difficulty compiling the examples. <HR></BLOCKQUOTE><H2><A NAME="Heading1"></A><FONT COLOR="#000077">Implementing Basic Bank Server Capabilities</FONT></H2><P>The server functionality of the <TT>Bank</TT> application is encapsulated in threemain interfaces: the <TT>BankServer</TT>, the <TT>Bank</TT>, and the <TT>Account</TT>.The <TT>Account</TT> interface is subdivided into two derived interfaces, <TT>CheckingAccount</TT>and <TT>SavingsAccount</TT>. This set of interfaces defines the core functionalityof the <TT>Bank</TT> application. After you provide implementations for these interfaces,you move on to implement the client capabilities as well. The sole interface implementedby the client is the <TT>Customer</TT> interface, used by the client to access variousbank services.<H3><A NAME="Heading2"></A><FONT COLOR="#000077">Implementing the BankServer Interface</FONT></H3><P>The first server interface to implement is the <TT>BankServer</TT>. Recall thatthe purpose of the <TT>BankServer</TT> is to enable clients to locate <TT>Bank</TT>objects. <TT>BankServer</TT> objects, in turn, are located by clients and <TT>Bank</TT>objects through the CORBA Naming Service or another similar mechanism. When a <TT>Bank</TT>object is created, it locates and registers with a <TT>BankServer</TT>; in the samefashion, when the <TT>Bank</TT> object is ready to shut down, it unregisters withthe <TT>BankServer</TT>.</P><P>The IDL for the <TT>BankServer</TT> interface (from Day 5) is defined in Listing6.1.<H4><FONT COLOR="#000077">Listing 6.1. BankServer.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankServer.idl</TT><TT> 2: </TT><TT> 3: #ifndef BankServer_idl</TT><TT> 4: #define BankServer_idl</TT><TT> 5: </TT><TT> 6: #include "Bank.idl"</TT><TT> 7: </TT><TT> 8: // A BankServer provides clients with visibility to Bank objects.</TT><TT> 9: interface BankServer {</TT><TT>10: </TT><TT>11: // Register the given Bank with this BankServer. The Bank will</TT><TT>12: // be listed by getBanks() until unregisterBank() is called with</TT><TT>13: // that Bank.</TT><TT>14: void registerBank(in Bank bank);</TT><TT>15: </TT><TT>16: // Unregister the given Bank from this BankServer. If the Bank</TT><TT>17: // was not previously registered, this operation does nothing.</TT><TT>18: void unregisterBank(in Bank bank);</TT><TT>19: </TT><TT>20: // Return a list of all Banks currently registered with this</TT><TT>21: // BankServer.</TT><TT>22: BankList getBanks();</TT><TT>23: };</TT><TT>24: 25: #endif</TT> </FONT></PRE><P>It is up to you to provide implementations for the <TT>registerBank()</TT>, <TT>unregisterBank()</TT>,and <TT>getBanks()</TT> methods, as well as the constructor (or constructors) anddestructor for this class.</P><P>Examining <TT>BankServerImpl.h</TT> in Listing 6.2, notice first (in line 10)that the <TT>BankServerImpl</TT> class extends the <TT>_sk_BankServer</TT> class.<TT>_sk_BankServer</TT> is the server skeleton for the <TT>BankServer</TT> interface.If you were to examine the source file for this class, you would see that it providespure virtual methods corresponding to the IDL methods you defined earlier. Becauseit is a skeleton, though, it doesn't provide any implementations for these methods;that is the job of the <TT>BankServerImpl</TT> class. Also, note that the name <TT>BankServerImpl</TT>was chosen arbitrarily; you can name the class whatever you want, but it is recommendedthat you devise and follow a naming convention for your implementation classes.<H4><FONT COLOR="#000077">Listing 6.2. 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 <vector></TT><TT> 7: </TT><TT> 8: #include "../BankServer_s.h"</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();</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);</TT><TT>22: virtual void unregisterBank(Bank_ptr bank);</TT><TT>23: virtual BankList* getBanks();</TT><TT>24: </TT><TT>25: private:</TT><TT>26: </TT><TT>27: // This BankServer's list of Banks.</TT><TT>28: std::vector<Bank_ptr> myBanks;</TT><TT>29: };</TT><TT>30: 31: #endif</TT> </FONT>Also, notice the following:</PRE><PRE><FONT COLOR="#0066FF"><TT>#include <vector></TT></FONT></PRE><P>and</P><PRE><FONT COLOR="#0066FF"><TT>// This BankServer's list of Banks.</TT><TT>std::vector<Bank_ptr> myBanks;</TT></FONT></PRE><P>If you guessed that the implementation utilizes C++'s Standard Template Library(STL), you are correct. Most modern C++ compilers include STL; if yours doesn't,you can either obtain an implementation of STL or modify the sample code to avoidSTL. One source for STL implementations is <TT>ObjectSpace</TT> (at <TT>http://www.objectspace.com/</TT>),which provides an STL implementation for many platforms and compilers free of charge.</P><P>Further examining <TT>BankServerImpl.h</TT>, you'll see that the IDL methods definedpreviously map to the following C++ methods:</P><PRE><FONT COLOR="#0066FF"><TT>virtual void registerBank(Bank_ptr bank);</TT><TT>virtual void unregisterBank(Bank_ptr bank);</TT><TT>virtual BankList* getBanks();</TT></FONT></PRE><P>Notice in particular that the <TT>Bank</TT> references are mapped to the <TT>Bank_ptr</TT>type, and the <TT>BankList</TT> to <TT>BankList*</TT>. Other than these changes andthe appearance of the <TT>virtual</TT> keyword (which is unnecessary for CORBA implementationclasses but usually preferable), the C++ method definitions strongly resemble theirIDL counterparts.</P><P>Listing 6.3 contains the implementation class <TT>BankServerImpl.cpp</TT>, whichprovides the implementation for the <TT>_sk_BankServer</TT> interface.<H4><FONT COLOR="#000077">Listing 6.3. BankServerImpl.cpp.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // BankServerImpl.cpp</TT><TT> 2: </TT><TT> 3: #include "BankServerImpl.h"</TT><TT> 4: </TT><TT> 5: #include <algorithm></TT><TT> 6: #include <functional></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<Bank_ptr, bool> {</TT><TT>10: public:</TT><TT>11: IsBankEqual(argument_type bank) { myBank = bank; }</TT><TT>12: result_type operator()(argument_type bank) { return bank-></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() : myBanks() {</TT><TT>20: </TT><TT>21: }</TT><TT>22: </TT><TT>23: // Destructor.</TT><TT>24: BankServerImpl::~BankServerImpl() {</TT><TT>25: </TT><TT>26: }</TT><TT>27: </TT><TT>28: void BankServerImpl::registerBank(Bank_ptr bank) {</TT><TT>29: </TT><TT>30: // Add the given Bank at the end of the list.</TT><TT>31: cout << "BankServerImpl: Registering Bank \"" << bank->name() <<</TT><TT>32: "\"." << endl;</TT><TT>33: myBanks.push_back(Bank::_duplicate(bank));</TT><TT>34: }</TT><TT>35: </TT><TT>36: void BankServerImpl::unregisterBank(Bank_ptr bank) {</TT><TT>37: </TT><TT>38: std::vector<Bank_ptr>::iterator first = myBanks.begin();</TT><TT>39: std::vector<Bank_ptr>::iterator last = myBanks.end();</TT><TT>40: IsBankEqual predicate(bank);</TT><TT>41: </TT><TT>42: std::vector<Bank_ptr>::iterator matchedBank = std::</TT><TT>43: find_if(first, last, predicate);</TT><TT>44: if (matchedBank == last) {</TT><TT>45: </TT><TT>46: // Invalid Bank; do nothing.</TT><TT>47: cout << "BankServerImpl: Ignored attempt to unregister "</TT><TT>48: "invalid Bank." << endl;</TT><TT>49: return;</TT><TT>50: }</TT><TT>51: cout << "BankServerImpl: Unregistering Bank \"" << bank->name()</TT><TT>52: << "\"." << endl;</TT><TT>53: </TT><TT>54: // Delete the given Bank.</TT><TT>55: myBanks.erase(matchedBank);</TT><TT>56: bank->_release();</TT><TT>57: }</TT><TT>58: </TT><TT>59: BankList* BankServerImpl::getBanks() {</TT><TT>60: </TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -