📄 ch04.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 4 -- Building a CORBA Application</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="../ch03/ch03.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch05/ch05.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 4<BR>Building a CORBA Application</FONT><A HREF="#Heading1"></A></H1></CENTER><UL> <LI><A HREF="#Heading1">Building a CORBA Server</A> <UL> <LI><A HREF="#Heading2">Defining the Server Interfaces</A> <LI><A HREF="#Heading3">Choosing an Implementation Approach</A> <LI><A HREF="#Heading4">Using the IDL Compiler</A> <LI><A HREF="#Heading5">Implementing the Server Interfaces</A> <LI><A HREF="#Heading6">Compiling and Running the Server</A> </UL> <LI><A HREF="#Heading7">Building a CORBA Client</A> <UL> <LI><A HREF="#Heading8">Implementing the Client</A> <LI><A HREF="#Heading9">Compiling and Running the Client</A> </UL> <LI><A HREF="#Heading10">Summary</A> <LI><A HREF="#Heading11">Q&A</A> <LI><A HREF="#Heading12">Workshop</A> <UL> <LI><A HREF="#Heading13">Quiz</A> <LI><A HREF="#Heading14">Exercises</A> </UL></UL><P><HR SIZE="4"><CENTER><H1><FONT COLOR="#000077"></FONT></H1></CENTER><P>Up to this point, you've spent most of your time learning about CORBA's InterfaceDefinition Language (IDL). You saw that IDL is used to define the interfaces of CORBAobjects; you've even created some IDL interfaces of your own. Now it's time to putyour IDL knowledge to use by not only defining object interfaces, but also by implementingthose interfaces to build a CORBA server. This chapter will walk you through thatprocess, from defining object interfaces to running the server. The outline of theprocess is this:<DL> <DD><B>1</B>. Define the server's interfaces using IDL.<BR> <B><BR> 2</B>. Choose an implementation approach for the server's interfaces. (You'll see that CORBA provides two such approaches: inheritance and delegation.)<BR> <B><BR> 3</B>. Use the IDL compiler to generate client stubs and server skeletons for the server interfaces. (For now, you'll only be concerned with the server skeletons.)<BR> <B><BR> 4</B>. Implement the server interfaces.<BR> <B><BR> 5</B>. Compile the server application.<BR> <B><BR> 6</B>. Run the server application. (With any luck, everything will fall into place!)</DL><P>Your next task will be to build a client that uses the services you implementedin the first part of this chapter. Conceptually speaking, the process of buildingthe client is much simpler; you only need to decide what you want the client to do,include the appropriate client stubs for the types of server objects you want touse, and implement the client functionality. Then you'll be ready to compile andrun the client.<H2><A NAME="Heading1"></A><FONT COLOR="#000077">Building a CORBA Server</FONT></H2><P>The first step in building a CORBA application is usually to implement the serverfunctionality. The reason for this is that while a server can be tested (at leastin a limited way) without a client, it is generally much more difficult to test aclient without a working server. There are exceptions to this, of course, but typicallyyou'll need to at least define the server interfaces before implementing the client;server and client functionality can then be developed in parallel. For the sake ofsimplicity, in this book you'll implement server functionality first, followed byclient functionality.</P><P>On a high level, to build the server you'll need to first define the server interfaces(which define the capabilities that will be made available by the server and howthose capabilities are accessed), implement those interfaces, and finally compileand run the server. There are a few issues you'll encounter along the way, whichwill be discussed in this section.<H3><A NAME="Heading2"></A><FONT COLOR="#000077">Defining the Server Interfaces</FONT></H3><P>This is where you begin to apply the knowledge you assimilated on Day 3, "Masteringthe Interface Definition Language (IDL)." There you learned much about IDL buthad no real chance to apply that knowledge. Now you can use that knowledge to transforma clean slate into a system design.<H4><FONT COLOR="#000077">A Few Words About System Design</FONT></H4><P>Obviously, in order to build a system, one must first have an idea of what thatsystem is supposed to accomplish. Before you're ready to write a single line of IDL,you must first have a notion of what you're trying to achieve. Although the subjectof system design is far beyond the scope of this book, on Day 5, "Designingthe System: A Crash Course in Object-Oriented Analysis and Design," you'll beexposed to some of the basic concepts of designing a system and mapping a systemdesign into IDL. In this chapter, the work will be done for you; in the real world,this is seldom the case.<H4><FONT COLOR="#000077">The Stock Market Server Example</FONT></H4><P>At this point, it's preferable to examine a simple example to help you focus onthe process of implementing a CORBA server. A complex example would likely bog youdown in various implementation details and is best avoided until later.</P><P>Consider a stock market-related example: A service is desired that, when givena stock symbol, will return the value of that stock at that particular time. As anadded convenience, the service will also return a list of all known stock symbolsupon request.</P><P>A cursory analysis of this scenario suggests that a <TT>StockServer</TT> interfacecould be defined that provides two services (methods): <TT>getStockValue()</TT> and<TT>getStockSymbols()</TT>. <TT>getStockValue()</TT> should take a <TT>StockSymbol</TT>as a parameter and return a floating-point result (<TT>float</TT> will probably do).<TT>getStockSymbols()</TT> need not take any arguments and should return a list of<TT>StockSymbol</TT> objects.<BLOCKQUOTE> <P><HR><B>Note:</B>During the process of determining <TT>StockServer</TT> system capability, the <TT>StockSymbol</TT> class was inadvertently produced. This spontaneous generation of classes, often a by-product of object-oriented analysis, can lead to a better understanding of how a particular system works. <HR></BLOCKQUOTE><H4><FONT COLOR="#000077">Listing 4.1. StockMarket.idl.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT> 1: // StockMarket.idl</TT><TT> 2: </TT><TT> 3: // The StockMarket module consists of definitions useful</TT><TT> 4: // for building stock market-related applications.</TT><TT> 5: module StockMarket {</TT><TT> 6: </TT><TT> 7: // The StockSymbol type is used for symbols (names)</TT><TT> 8: // representing stocks.</TT><TT> 9: typedef string StockSymbol;</TT><TT>10: </TT><TT>11: // A StockSymbolList is simply a sequence of</TT><TT>12: // StockSymbols.</TT><TT>13: typedef sequence<StockSymbol> StockSymbolList;</TT><TT>14: </TT><TT>15: // The StockServer interface is the interface for a</TT><TT>16: // server which provides stock market information.</TT><TT>17: // (See the comments on the individual methods for</TT><TT>18: // more information.)</TT><TT>19: interface StockServer {</TT><TT>20: </TT><TT>21: // getStockValue() returns the current value for</TT><TT>22: // the given StockSymbol. If the given StockSymbol</TT><TT>23: // is unknown, the results are undefined (this</TT><TT>24: // would be a good place to raise an exception).</TT><TT>25: float getStockValue(in StockSymbol symbol);</TT><TT>26: </TT><TT>27: // getStockSymbols() returns a sequence of all</TT><TT>28: // StockSymbols known by this StockServer.</TT><TT>29: StockSymbolList getStockSymbols();</TT><TT>30: };31: };</TT> </FONT></PRE><P><BR>Mapping this particular design to IDL is a clear-cut process; the final result, <TT>StockMaret.idl</TT>,appears in Listing 4.1. First, because it's good practice to group together relatedinterfaces and types into IDL <TT>module</TT>s, start by including all the definitionsin a <TT>module</TT> called <TT>StockMarket</TT>:</P><PRE><FONT COLOR="#0066FF"><TT>module StockMarket {</TT></FONT></PRE><P>Next, consider the use of the <TT>StockSymbol</TT> class. For the purposes ofthis example, it really doesn't require any functionality over and above the <TT>string</TT>type. Thus, you can either substitute the <TT>string</TT> type anywhere that the<TT>StockSymbol</TT> type would have been used, or you can use <TT>typedef</TT> todefine <TT>StockSymbol</TT> as a <TT>string</TT>. In a complex system, using specificdata types such as <TT>StockSymbol</TT> makes the system design easier to comprehend.To reinforce this practice, use <TT>typedef</TT> to define <TT>StockSymbol</TT> asa <TT>string</TT>:</P><PRE><FONT COLOR="#0066FF"><TT> typedef string StockSymbol;</TT></FONT></PRE><P>You're now ready to define the interface for the <TT>StockServer</TT> object:</P><PRE><FONT COLOR="#0066FF"><TT>interface StockServer {</TT></FONT></PRE><P>The first method in <TT>StockServer</TT> is a method that takes a <TT>StockSymbol</TT>as input and returns a <TT>float</TT>. Expressing this in IDL is uncomplicated:</P><PRE><FONT COLOR="#0066FF"><TT>float getStockValue(in StockSymbol symbol);</TT></FONT></PRE><BLOCKQUOTE> <P><HR><B>Note:</B>It's conceivable that a client could call <TT>getStockValue()</TT> with an invalid <TT>StockSymbol</TT> name. To handle this, <TT>getStockValue()</TT> could (and probably should) raise an exception when an invalid name is passed to it. For the sake of simplicity, though, this example does not make use of exceptions. <HR></BLOCKQUOTE><P>The other <TT>StockServer</TT> method takes no arguments and returns a list of<TT>StockSymbol</TT>s. Recall that IDL offers two constructs to represent lists:the <TT>sequence</TT> and the array. Because the size of the list is unknown in thiscase, it might be advantageous to use a <TT>sequence</TT>. However, a method cannotreturn a <TT>sequence</TT> directly; you'll first need to <TT>typedef</TT> a <TT>sequence</TT>of <TT>StockSymbol</TT>s to use with this method. For the sake of convenience, addthe <TT>typedef</TT> immediately following the <TT>typedef</TT> of the <TT>StockSymbol</TT>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -