|
||||
EIFFELNET MANUAL9 EVENT-DRIVEN COMMAND EXECUTION
9.1 Commands and eventsIn the preceding examples each participant in a communication had to get ready to send or receive at specific stages of its life. Although this did not preclude asynchronous communication, it is sometimes desirable to make the scheme even more asynchronous, and control more decentralized, by letting each system simply specify certain communication events that it wants to monitor, and certain commands to be executed on occurrence of the specified events. This event-command model of computation is of course not new with EiffelNet. In ISE Eiffel 3, it is already used in EiffelVision and especially in EiffelBuild, which makes it possible to build a graphical application by specifying which command should be executed in response to each of a set of specified interface events, such as mouse click or keyboard entry. The commands are objects, instances of a general-purpose class COMMAND or its proper descendants. Class COMMAND has, among its features, a procedure execute which executes the current command; some commands are undoable and have an undo procedure. Class COMMAND and related classes used to be in EiffelVision but because of their generality were moved to [2], although this occurred after the publication of [2]. In EiffelNet the possible events associated with a socket will be of three kind: a read event; a write event; or a special event (out of bounds operation). The command classes will be descendants of POLL_COMMAND, an heir of COMMAND. 9.2 Command classesThe example uses four command classes: CLIENT_DATAGRAM_READER, CLIENT_DATAGRAM_WRITER and their counterpart for servers, representing operations that must be triggered in the case of a read event and a write event. Here is the reader command for clients:
The execute procedure reads a packet of ten characters and prints these characters. Its counterpart in the writing command will produce these ten packets:
9.3 The server and the clientOnce the commands have been defined, it suffices for the server and the client to associate instances of these commands with the appropriate. The abstraction needed for this purpose is provided by class MEDIUM_POLLER. An instance of this class knows about a number of commands, each associated with a certain socket in read, write or special event mode. By applying procedure execute to such a medium poller, you direct it to monitor these sockets for the corresponding events and to execute the command associated with each event that will be received. Procedure execute takes two integer arguments: the maximum number of sockets to monitor, and the timeout in milliseconds. Here is the server built with this mechanism:
Procedure make creates three objects: a socket, which it associates with a specific port; a poller; and a read command (an instance of SERVER_DATAGRAM_READER), which it attaches to the socket. It then enters the read command into the poller, and does the same thing with a write command. It sets up the poller to accept read commands only and then executes the poller; this will enable the server to get the read event triggered by the client's write command (as it appears below in the text of class POLLING_CLIENT). Then the server reverses the poller's set-up to write-only, and calls execute again. The procedures make_read_only and make_write_only are creation procedures, so that it is possible in a single instruction to create a poller and set it up for read-only or write-only, as in create pollerlmake_read_only. For clarity, however, the above class and the next separate calls to these procedures from the creation of the poller, which uses make as creation procedure. The client follows the same scheme, reversing the order of read and write operations:
9.4 A less deterministic schemeAlthough the example uses the event-driven mechanisms of EiffelNet, it is still relatively deterministic in that it follows a precise protocol defined by a strict sequence of read and write operations on both sides. This is why every call to execute is preceded by a call to either make_read_only or make_write_only to set up the poller in the appropriate mode. A less deterministic scheme may often be desirable, where you simply enter a number of commands (read, write, out of bounds processing) into a poller and then wait for arbitrary events to occur and trigger commands. There is no need with this scheme to know in advance the order in which events may occur: a read event will trigger the command entered into the poller through put_read_command; a write event will trigger the command entered through put_write_command. To achieve this behavior, simply create the poller using make as creation procedure. This will set up the poller so as to accept all socket events, and enter into event-driven command execution by calling execute on the poller. Previous Chapter Table of Contents Next Chapter
|