📄 apa.htm
字号:
any sort of translation. For instance, if a bitmapped image is sent across the network, it should arrive in exactly the same format in which it was sent. The image can be sent as an array of <TT>octet</TT>s, which guarantees that there is no translation from the image's source to its destination. Other types of data that can be transmitted in this way include executables, Java class files, and most multimedia files. (By contrast, all other IDL data types can undergo a format conversion when being transmitted between different hardware platforms, operating systems, and/or languages.)<BR> <B><BR> 6. Define an enumerated type containing the months of the year.<BR> </B>The type definition would look like this (though you can give it whatever name you like):<BR> <FONT COLOR="#0066FF"><TT><BR> enum MonthsOfYear {</TT><BR> <TT>January,</TT><BR> <TT>February,</TT><BR> <TT>March,</TT><BR> <TT>April,</TT><BR> <TT>May,</TT><BR> <TT>June,</TT><BR> <TT>July,</TT><BR> <TT>August,</TT><BR> <TT>September,</TT><BR> <TT>October,</TT><BR> <TT>November,</TT><BR> <TT>December</TT><BR> <TT>};</TT></FONT></DL><PRE></PRE><DL> <DD><B>7. Why might a nonblocking remote method call be advantageous, compared to a blocking method call?</B><BR> Consider the case in which a CORBA client is an interactive application. While the client makes calls to CORBA servers, the user might want to interact with the application. If remote method calls, which might be lengthy, were to block, the responsiveness of the client application would be greatly diminished. Nonblocking method calls solve this problem by allowing the client to continue processing during a method invocation so that the client can handle user input in a timely manner. (Again, the use of multithreading in the client is almost always the best solution because <TT>oneway</TT> method invocations are unreliable.) Day 10, "Learning About CORBA Design Issues," explores this and other issues in greater depth.<BR> <B><BR> 8. Imagine a compound data type with a large number of data members. This data type will frequently be used by a client application that needs to access each of the data members. Would it be more efficient to encapsulate this data type into a <TT>struct</TT> or an <TT>interface</TT>? Why?</B><BR> Given that there are a large number of data members in this data type and that the client application needs to access each of these data members, it would be more efficient to send the data to the client as a <TT>struct</TT>. The reason for this is that accessing each of the data members of an <TT>interface</TT> (recall that the implementation object is passed by reference) requires a separate method call for each member, resulting in a great deal of overhead. By contrast, a <TT>struct</TT> is sent by value; therefore, after the <TT>struct</TT> is returned to the client, the client can then access the member data with its own local copy.<BR> <B><BR> 9. Because an IDL method can return a value, what is the purpose of <TT>out</TT> and <TT>inout</TT> parameter types?</B><BR> If a method needs to simultaneously return more than one value of more than one type, <TT>out</TT> and <TT>inout</TT> parameters can be used, in addition to (or in lieu of) the return value. This is a similar mechanism to passing parameters by reference in C++. In this way, a single method call can return more than one value.<BR> Why is a <TT>oneway</TT> method unable to return any value to the caller? Can you think of a mechanism, using <TT>oneway</TT> calls, to return a result to the caller?<BR> Because a <TT>oneway</TT> method does not block, the caller does not wait for a result. Consequently, there is no way for a <TT>oneway</TT> method call to return a result to the client. (Incidentally, exceptions cannot be raised either, for the same reason.) However, a system of <TT>oneway</TT> calls can be set up as follows: The client makes a <TT>oneway</TT> call to the server and continues its processing. The server, after it has executed the method, makes a <TT>oneway</TT> call back to the client (extra credit if you identified this as a callback) with the result information for the previous call.</DL><H3><A NAME="Heading7"></A><FONT COLOR="#000077">Exercises</FONT></H3><DL> <DD><B>1. Consider the following classes: <TT>Conduit</TT>, <TT>Faucet</TT>, <TT>FuseBox</TT>, <TT>Outlet</TT>, <TT>Pipe</TT>, <TT>WaterHeater</TT>, <TT>WaterPump</TT>, and <TT>Wire</TT>. How would you partition these classes? What relationships, if any, are there between the partitions you have created?</B><BR> Often, when partitioning a system, there is no clear answer as to which classes belong in what partitions. Many are obvious, but there are those that potentially fall into one of several partitions. In such cases, there is no right or wrong answer. Also, how a particular system uses these classes might affect the partitioning; a partitioning that makes sense for one system might not make sense for another.<BR> <BR> Of the classes in this exercise, <TT>Conduit</TT>, <TT>FuseBox</TT>, <TT>Outlet</TT>, and <TT>Wire</TT> clearly belong in one partition (call it <TT>Electrical</TT>). <TT>Faucet</TT> and <TT>Pipe</TT> clearly belong in another partition (call it <TT>Plumbing</TT>). <TT>WaterHeater</TT> and <TT>WaterPump</TT>, however, could go either way. They are part of the plumbing system, to be sure, but they are also electrical devices, so they could also be considered part of the electrical system. Here there is no right or wrong answer; depending on system requirements, either partition is reasonable.<BR> <BR> Assuming you were to place <TT>WaterHeater</TT> and <TT>WaterPump</TT> in the <TT>Plumbing</TT> partition, there would probably be a relationship between <TT>WaterHeater</TT> and <TT>Outlet</TT> (or between <TT>WaterHeater</TT> and <TT>FuseBox</TT>, if they were connected directly). A similar relationship would exist for <TT>WaterPump</TT>. Because there are few relationships between the two partitions and close relationships within each partition, you can consider this partitioning scheme a reasonable one.<BR> <B><BR> 2. Create an interface that describes a clock radio (which can set hours, minutes, alarm time, and so on).</B><BR> There are many potential answers to this exercise, but your interface might resemble the following:<BR> <FONT COLOR="#0066FF"><BR> <TT>// Interface to a clock/radio device. This interface chooses to<BR> // emulate the interface of many clock/radio controls, e.g. a<BR> // "button" to set the hour, another "button" to set the minute,<BR> // and so on.<BR> interface ClockRadio {<BR> // Get the current time. The result is returned in the output<BR> // parameters hour, minute, and second.<BR> void getTime(out short hour, out short minute, out short second);<BR> // Advance the current hour by one; reset to zero if the hour<BR> // exceeds the maximum (23). This method can be used to set the<BR> // time. Returns the new value of the hour.<BR> short IncrementHour();<BR> // Advance the current minute by one; reset to zero if the minute<BR> // exceeds the maximum (59). This method can be used to set the<BR> // time. Returns the new value of the minute.<BR> short IncrementMinute();<BR> // Advance the alarm hour by one; reset to zero if the hour<BR> // exceeds the maximum (23). This method can be used to set the<BR> // alarm time. Returns the new value of the hour.<BR> short IncrementAlarmHour();<BR> // Advance the current alarm minute by one; reset to zero if the<BR> // minute exceeds the maximum (59). This method can be used to set<BR> // the alarm time. Returns the new value of the minute.<BR> short IncrementAlarmMinute();<BR> // Activate the alarm.<BR> void activateAlarm();<BR> // Deactivate the alarm.<BR> void deActivateAlarm();<BR> };</TT></FONT></DL><PRE></PRE><H2><A NAME="Heading8"></A><FONT COLOR="#000077">Day 4: Building a CORBA Application</FONT></H2><H3><A NAME="Heading9"></A><FONT COLOR="#000077">Quiz</FONT></H3><DL> <DD><B>1. What is the purpose of server skeletons and client stubs?<BR> </B>Server skeletons provide the framework (skeleton) set of classes, which provide skeleton methods for which the developer provides implementations (either through inheritance or through delegation). Client stubs provide the clients with an interface to the server methods.<BR> <B><BR> 2. Why does the server need to register the implementation object with the CORBA Naming Service?</B><BR> The server needs to register with the CORBA Naming Service because, otherwise, there is no way for clients to locate the server. There are other methods of achieving visibility, such as writing a stringified object reference to a well-known and accessible location such as a disk file (assuming a networked file system), a Web server, or an FTP server. The Naming Service provides a standard, convenient method for publishing object references.<BR> <B><BR> 3. Why do the client and server need to catch exceptions, especially when none are raised by the IDL operations you defined?</B><BR> Recall that every remote method can potentially raise an exception and that these exceptions need to be caught and handled. Even if the IDL definition for a method does not specify any exceptions, that method can still raise a CORBA system exception (which would be raised if there were a network error, for example).</DL><H3><A NAME="Heading10"></A><FONT COLOR="#000077">Exercises</FONT></H3><DL> <DD><B>1</B>. It was pointed out in the <TT>StockMarket</TT> example that it would be a good idea to raise an exception in the <TT>getStockValue()</TT> method if an invalid <TT>StockSymbol</TT> was passed in. Modify <TT>StockMarket.idl</TT> so that the method can raise an <TT>InvalidStockSymbolException</TT>. (You'll also need to add a definition for this exception.)<BR> The resulting <TT>StockMarket.idl</TT> might look like this (you might have included additional data members in the definition for <TT>InvalidStockSymbolException</TT>):<BR> <FONT COLOR="#0066FF"><TT><BR> // StockMarket.idl</TT><BR> <TT>// The StockMarket module consists of definitions useful</TT><BR> <TT>// for building stock market-related applications.</TT><BR> <TT>module StockMarket {</TT><BR> <TT>// The InvalidStockSymbolException is raised when a</TT><BR> <TT>// method is passed an invalid stock symbol.</TT><BR> <TT>exception InvalidStockSymbolException { };</TT><BR> <TT>// The StockSymbol type is used for symbols (names)</TT><BR> <TT>// representing stocks.</TT><BR> <TT>typedef string StockSymbol;</TT><BR> <TT>// The StockServer interface is the interface for a</TT><BR> <TT>// server that provides stock market information.</TT><BR> <TT>// (See the comments on the individual methods for</TT><BR> <TT>// more information.)</TT><BR> <TT>interface StockServer {</TT><BR> <TT>// getStockValue() returns the current value for</TT><BR> <TT>// the given StockSymbol. If the given StockSymbol</TT><BR> <TT>// is unknown, the results are undefined (this</TT><BR> <TT>// would be a good place to raise an exception).</TT><BR> <TT>float getStockValue(in StockSymbol symbol)</TT><BR> <TT>raises (InvalidStockSymbolException);</TT><BR> <TT>// getStockSymbols() returns a sequence of all</TT><BR> <TT>// StockSymbols known by this StockServer.</TT><BR> <TT>sequence<StockSymbol> getStockSymbols();</TT><BR> <TT>};</TT></FONT> <P><FONT COLOR="#0066FF"><TT>};</TT></FONT></DL><PRE></PRE><DL> <DD><B>2</B>. In the <TT>StockMarket</TT> example, an implementation was provided that used the delegation approach. Implement the <TT>StockServer</TT> to use the inheritance approach. (Extra credit: Include the exception-raising mechanism from the first exercise.)<BR> <BR> <B>3</B>. The changes required are to modify the server so that it actually raises the <TT>InvalidStockSymbolException</TT> when an invalid stock symbol is encountered; similarly, the client must be modified to catch and handle this exception. For the server, here is the new implementation for the <TT>getStockValue()</TT> method (all other code remains unchanged):</DL><PRE></PRE><DL> <DD><FONT COLOR="#0066FF"><TT>// Return the current value for the given StockSymbol.</TT><BR> <TT>public float getStockValue(String symbol) throws</TT><BR> <TT>InvalidStockSymbolException {</TT><BR> <TT>// Try to find the given symbol.</TT><BR> <TT>int stockIndex = myStockSymbols.indexOf(symbol);</TT><BR> <TT>if (stockIndex != -1) {</TT><BR> <TT>// Symbol found; return its value.</TT><BR> <TT>return ((Float)myStockValues.elementAt(stockIndex)).</TT><BR> <TT>floatValue();</TT><BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -