Java Reference
In-Depth Information
communication between
Client
and
MyServer
. In particular,
DSocketActivity
receives a message with the name of a requested service, creates the
corresponding
DParser
object, which decodes the message and starts
MyService
.
Phase 2:
Client
writes a message on the socket including a command to
MyService
.
DSocketActivity
receives the message, decodes the command
and invokes the corresponding method of
MyService
.
■
Phase 3:
DSocketActivity
is listening to events raised by
MyService
. When it
notifies an event,
DSocketActivity
encodes the event in a socket message
and sends it to
Client
.
■
Decision point
How does the control module support HTTP-based communication?
Since the HTTP protocol does not support “push” operations, the remote
client, i.e. a web browser running on a desktop computer or on a mobile
phone, cannot be notified of events raised by the control module. Thus, we
need first to extend the MMI framework in order to add support for the pull
model of event notification. When
Client
requests the activation of a service
(e.g.
MyService
) it specifies whether it supports the push model or not. If it
does not support this model, class
DSocketActivity
catches all the events
raised by
MyService
and registers them in an event list.
Client
is in charge of
querying
DSocketActivity
periodically about registered events.
The bridge between the HTTP communication protocol and the TCP
IP
Socket mechanism is implemented using a servlet component that needs to
be implemented for each specific client application. The servlet runs inside
a web server, implements the basic
doPOST()
and
doGET()
methods to inter-
act with the remote web browser, and communicates with
DSocketActivity
through sockets. Whenever the web browser refreshes the HTTP connection
with the web server, the servlet writes a query message on the socket.
DSocketActivity
parses the message, extracts registered events from the event
list, and composes them into a response message that it writes back to the
servlet. Finally, the servlet composes a new web page according to the
received events and forwards it to the web browser.
19.6.3
Implementation
The implementation of class
DSocketListener
, class
DSocketActivity
and
DParser
is quite simple. It should be noted that method
step()
of class
DSocketActivity
uses reflection to create an instance of the parser associated
with the requested service and that the
DSocketActivity
thread dies when the
remote client closes the socket. When this occurs, it catches a
SocketException
and invokes method
stopActivity()
of class
DProcess
. Finally, method
quit()
of
class
SocketActivity
, closes the socket's input
output buffers and stops the
service.