Java Reference
In-Depth Information
A final point about DBSocketRequest is that DBSocketRequest is where we implemented
our application protocol. We have separated the protocol from the actual socket,
DVDSocketServer .
The Application Protocol
The socket client transmits a serialized object to the socket server. But how does the server
know how to respond to the object? A serialized object is technically just data in the form of a
byte stream; on the surface it does not communicate action. This is where we need a protocol ,
or set of rules, that defines how our client is to interact with our server.
At a high level, here is how our protocol will work:
1. The client will make a request, such as rent or return rental, of the server.
2. The server will execute the request.
3. The result status or return value will be sent to the client.
Deliberating on the preceding list should lead to the following two questions: “How will
the server interpret the request?” and “How will the result be sent back to the client?”
The Denny's DVDs application adopts an approach of encapsulation. The request is
encapsulated in a command object and the result is encapsulated in a result object. Let's take
a closer look at the command and result objects.
The Command Enum
The DvdCommand class encapsulates the client request by storing it as a SocketCommand member,
commandId . When a GUI client calls one of the DBClient methods on DVDSocketClient , the
socket client sets the commandId property and sends it off to the socket server, DVDSocketServer .
Since DVDCommand is sent, or marshaled, across the wire, it must be serializable. When the
server receives the DVDCommand object, it uses the commandId to call the corresponding method
on the DVDDatabase , which is a local call to our server. Any parameters (for instance, the UPC
value) that are required for the request are passed in the DVD class member. The regex attribute
is used exclusively for the find method.
Listing 7-4 shows the constructors that take a SocketCommand enum ID (see the sidebar
“Using Enum Constants”) and a dvd object as a parameter. The dvd object is useful for storing
the UPC for rent and return. For the modify method, a dvd parameter can be used to set the
other dvd attributes for a particular DVD. We do not actually use the modify method publicly in
our implementation, but the class has been designed with this enhancement in mind.
Note The DVDCommand object is an example of the Command pattern. A command object encapsulates
a request as an object. You can find more information about the Command pattern in the topic Design
Patterns: Elements of Reusable Object-Oriented Software , by Erich Gamma, Richard Helm, Ralph Johnson,
and John M. Vlissides (Addison-Wesley, 1995). The Proxy and Adapter patterns are also described in this
literary software masterpiece.
Search WWH ::




Custom Search