|
Fifth Annual Tcl/Tk Workshop, 1997
   
[Technical Program]
Redesigning Tcl-DPMike Perham, Brian C. Smith, Tibor J·nosi AbstractTcl-DP is a loadable module for Tcl that adds advanced communication features to Tcl/Tk. Tcl-DP supports communication by serial links, IP-multicast, TCP, UDP, and email, contains a remote procedure call (RPC) mechanism, and supports the design of new protocols using modules called filters. Tcl-DP 1.0 [Smi93] was released four years ago and has since been used for numerous commercial and academic projects. With age, however, the code became so brittle that adding new features and porting to new versions of Tcl was nearly impossible. Furthermore, many of Tcl-DPs original features were incorporated in the Tcl core, making them redundant in Tcl-DP. Hence, we decided to write the latest version of Tcl-DP (version 4.0) from scratch. In this paper, we describe the new features of Tcl-DP 4.0, its architecture and implementation, and problems we encountered with Tcls new I/O system. 1. The Tcl-DP APIThe Tcl-DP API contains two main parts: commands for general communication and command for remote procedure call (RPC). Figure 1 demonstrates Tcl-DPs RPC mechanism by showing how to implement a network-wide server for generating unique identifiers. The left side of the figure shows the code that is executed on the server, the right shows the code that is executed on the client.
Figure 1: A Simple ID server The first command executed on both client and server is the
Tcl The client connects to the server using the Tcl-DP also contains commands to support basic network
communication. Figure 2 shows how to use Tcl-DPs
communication facilities to transfer the file
Figure 2: Simple network communication After loading Tcl-DP using the Note the use of the The client connects to the server using Note that once a channel is opened, it is treated as any other file: it can be read from, written to, or checked for the EOF condition (which, in the case of TCP, means the connection has been closed). 2. New Features2.1 ChannelsThe features described above were available in earlier versions of Tcl-DP. In version 4.0, we added several new features. In addition to TCP, UDP and IP multicast, DP 4.0 supports
communication via serial ports and email. No matter what its
type, a channel is opened using the
Figure 3: Communication with serial connection 2.2 RPCThe Tcl-DP RPC mechanism has also been changed and enhanced.
Recursive and out-of-order RPCs are now supported. This point
will be covered in detail below. Perhaps the most important new
ability is that RPCs can now be performed over any Tcl channel as
long as it has been registered with the 2.3 FiltersAnother new feature in DP 4.0 is filters . Filters sit between the program and the channel, transparently modifying data (e.g., encrypting/decrypting it). DP 4.0 has two types of filters: plugin filters and filter channels . A plugin filter is designed for the common case where the data
is modified using a functional interface. Encryption and
decryption are examples of plugin filters. To connect a plugin
filter, the programmer calls Figure 4 shows how to use a plugin filter. In this example, a
server accepts a connection from a client (the first three lines
of code), and then attaches an exclusive-or (xor) filter to that
channel to weakly encrypt the data. Any data written to the
channel is transparently filtered by exclusive-oring it
with a key (the key can be changed using the
DP 4.0 comes with a uuencode/decode filter, a xor excryption
filter, a hex to binary conversion filter, and a The filter mechanism is general and powerful. For example, by using uuencode filters, binary data can be read from, or written to, channels. We have used filters to provide a simple command line interface to a video camera whose pan/zoom/tilt is controlled by a binary protocol over a serial interface. We believe it is possible to use plugin filters to provide APIs for secure communication, authentication, and gateways to binary RPC mechanisms. On the other hand, certain tasks are difficult or impossible
to perform using plugin filters. For these cases, filter channels
are appropriate. Filter channels are complete channels with
configurable options and I/O routines. A filter channel is
created using the To understand why filter channels are needed, consider the following scenario. A user needs to transmit datagrams over a stream channel (e.g., TCP). Since the channel has stream semantics, the datagram boundaries will be lost in transmission. In other words, if the user sends two datagrams "message1" and "message2" over the channel, the receiver might read "mess" on the first read, and "age1message2" on a second read. A simple way to keep the datagram boundaries is to build a packetizer , which attaches a length field to each message. At the receiver, a depacketizer reads the length field and returns either a whole packet or nothing (in the case of a partial read). It is difficult to build a packetizer using plugin because they only provide functional filtering. In this case, a filter channel is used. With filter channels, it is possible to build protocol stacks that provide, for example, flow control and guaranteed transmission over unreliable channels (any takers?). 3. Implementation3.1 ChannelsSince version 7.5, Tcl has performed all I/O via Tcl_Channels
. Figure 5 shows a block diagram of Tcl-DPs I/O system.
The Tcl_Channel abstraction pushes all I/O operations into a channel
driver , so that a generic set of operations can be performed
on any communications method. For instance, to read data from a
channel, one calls the Figure 5: Tcl-DPs I/O System 3.2 RPCThe implementation of RPCs changed significantly in DP 4.0. The new mechanism allows RPCs to be issued over any Tcl channel, and handles duplicate or out of order return of RPCs. In addition, the protocol contains no binary data (unlike previous versions), which allows a Tcl-only implementation of RPC handlers using Tcls socket command. These changes make the new protocol incompatible with previous versions, but we felt the new features and bug fixes justified the incompatibility. Figure 6 shows the layout of an RPC/RDO packet. Length is a six digit decimal string that gives the size of the entire packet, including the length field itself. Space is a delimiter between fields and is simply the space character. Type is a one byte token character that denotes the type of this packet. For types of packets are defined: RPC (e), RDO (d), error (x), or return value (r). ID is a six digit decimal string that specifies an identifier for the RPC message (and its return value). It is used in processing out-of-order RPCs, as explained below. Finally, msg is the Tcl command to be evaluated.
Figure 6: RPC packet format For example, the command
When a message arrives on an RPC channel, Tcl executes a file callback to process the message. This callback reads the message, buffering partial reads if necessary. It then processes the packet as follows:
Whenever an RPC is sent, an identifier ( id ) is generated and an activation record is created. This activation record tracks all currently executing RPCs, and allows for out-of-order and recursive RPCs. It is stored in a table, using id as an index. To understand the function of the activation record, consider the following example. A Tcl-DP process sends an RPC (id=1) to host A. DP creates an activation record for the RPC and waits in an event loop for the return message. While waiting, another event (e.g., a timer event) triggers the issuance of a second RPC (id=2) to host B. Again, DP creates an activation record for the RPC and waits in a new event loop for the return message. If RPC #1 now returns, it can not be processed immediately, since the current event loop, and associated call stack, is for RPC #2. Thus, DP simply records the RPC return value in the activation record associated with RPC #1. When RPC #2 returns, it is marked as received, breaking the event loop and return to the original loop. This loop exits immediately, since the activation record indicates the RPC is received. The surprising thing about the mechanism is that its expression in C is extremely elegant. The callback for processing a return value or error message just marks the appropriate activation record. The event loop simply processes one event until the associated activation record indicates the return value has been received. 4. Open IssuesWhile designing DP 4.0, we ran into several problems, which we discuss in this section. 4.1 Peeking and Tcl I/OIn previous versions of DP, peeking was allowed on socket
reads. Peeking allows a The Tcl I/O subsystem buffers all channel input [3].
Unfortunately, this buffering also makes peeking on a channel
impossible. Consider the following example: a user wishes to peek
at the first 2 bytes of a 12-byte message on a UDP channel.
Suppose the message is "123456789abc". Assume we We provide a work-around for this problem using the 4.2 Peeking and Tcl I/OThe 5. Status and ConclusionBecause we have just finished rewriting Tcl-DP, several of the higher-level details are conspicuously missing. Foremost would be a security scheme. Tcl-DP 3.5 provided RPC checking on a line-by-line basis while, currently, 4.0 provides only a one-time RPC check. We plan to add 3.xs security scheme into 4.0 before the final 4.0 release. Also, with the release of 4.0, we have dropped support for the nameserver in 3.3. Like DP itself, the nameserver aged poorly and we did not have the manpower to rewrite it. In this paper, we described the new features and implementation of Tcl-DP 4.0. Tcl-DP 4.0 is a dynamically loadable version of Tcl-DP which adds several new features including new channel types, channel filters, and better RPC support. Tcl-DP is freely available from https://www.cs.cornell.edu/Info/Projects/zeno/Projects/Tcl-DP.html. References[Smi93] Brian C. Smith, Lawrence Rowe, Stephen Yen. "Tcl Distributed Programming" In Proceedings of the 1 st Tcl/Tk Workshop . June 1993. Footnotes[1] All Tcl-DP commands begin with " [2] Tcl-DP also allows commands to be invoked
on the server using [3] Tcl imposes a minimum buffer size of 10
bytes on all channels. If you try to use fconfigure -buffersize to set the buffer size to less than 10 bytes, Tcl will use a 10-byte buffer. |
This paper was originally published in the
Proceedings of the Fifth Annual Tcl/Tk Workshop 1997,
July 14-17, 1997,
Boston, Massachusetts, USA
Last changed: 16 April 2002 aw |
|