\documentclass[]{article}
\usepackage{amsmath,graphicx,supertabular,hyperref,tabularx,ifthen}
\title{TIP \#230: Tcl Channel Transformation Reflection API}
\date{November 02, 2004}
\author{Andreas Kupries, Andreas Kupries, Andreas Kupries}
\urlstyle{sf}
\setlength{\parskip}{1ex}
\setlength{\parindent}{0pt}
\def\tipversion$#1${\texttt{\$#1\$}}
\def\tiplangle#1{\ensuremath{<}}
\def\tiprangle#1{\ensuremath{>}}
\def\tipbar#1{\ensuremath{|}}
\def\tipmail#1#2{\(\langle\){\small\expandafter\url{#1@#2}}\(\rangle\)}
\ifx\pdfoutput\undefined
\newcommand{\tipimage}[2]{\typeout{Make sure you download #1.eps}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.eps}}{\includegraphics[width=0.8\textwidth]{#1.eps}}}
\newcommand{\tipxref}[1]{}
\newcommand{\tipxrefend}{}
\else
\newcommand{\tipimage}[2]{\typeout{Make sure you create #1.pdf}\ifthenelse{\lengthtest{0.8\textwidth>#2}\and\lengthtest{0pt<#2}}{\includegraphics{#1.pdf}}{\includegraphics[width=0.8\textwidth]{#1.pdf}}}
\pdfcatalog{/PageMode /UseOutlines}
\newcommand{\tipxref}[1]{\pdfannotlink attr {/C [0.5 0.5 1.0] /Border [0 0 1]} goto name {#1}}
\newcommand{\tipxrefend}{\pdfendlink}
\fi
\newenvironment{tipabstract}{\begin{abstract}}{\end{abstract}}
\begin{document}\maketitle
\begin{center}\begin{tabularx}{\linewidth}{|r@{: }X|}\hline
\textbf{TIP \#230}&\textbf{Tcl Channel Transformation Reflection API}\\\hline
Author&

Andreas Kupries \tipmail{andreas_kupries}{users.sf.net}
\par
Andreas Kupries \tipmail{akupries}{shaw.ca}
\par
Andreas Kupries \tipmail{andreask}{activestate.com}
\\
Created&Tuesday, \(\text{2}^{\text{nd}}\) November 2004\\
Type&Project\\
Tcl Version&\(8.6\)\\
State&Final\\
Vote&Done\\
Version&\tipversion$Revision: 1.17 $\\
Post History&\\
\hline\end{tabularx}\end{center}
\thispagestyle{empty}\pagestyle{empty}
\begin{tipabstract}

This document describes an API which reflects the Channel Transformation API of the core I/O system up into the Tcl level, for the implementation of channel transformations in Tcl. It is built on top of \cite{tip208} ('Add a chan command') and also an independent companion to \cite{tip219} ('Tcl Channel Reflection API') and \cite{tip228} ('Tcl Filesystem Reflection API'). As the latter TIPs bring the ability of writing channel drivers and filesystems in Tcl itself so this TIP provides the facilities for the implementation of new channel transformations in Tcl. This document specifies version \textit{1} of the transformation reflection API.
\end{tipabstract}
\tableofcontents\setcounter{page}{0}\clearpage\pagestyle{plain}

\section{Background and Motivation}

The purpose of this and the other reflection TIPs is to provide all the facilities required for the creation and usage of wrapped files (i.e. virtual filesystems attached to executables and binary libraries) within the core.

While it is possible to implement and place all the proposed reflectivity in separate and external packages, this however means that the core itself cannot make use of wrapping technology and virtual filesystems to encapsulate and attach its own data and library files to itself. Something which is desirable as it can make the deployment and embedding of the core easier, due to having less files to deal with, and a higher degree of self-containment.

One possible application of a completely self-contained core library would be, for example, the Tcl browser plugin.

It is also possible to create a special purpose filesystem and channel driver in the core for this type of thing, but it is my belief that the general purpose framework specified here is a better solution as it will also give users of the core the freedom to experiment with their own ideas, instead of constraining them to what we managed to envision.

Another use for reflected transformations is as a helper for testing the generic I/O layer of Tcl, by creating transformations which forcibly return errors, bogus data, and the like.

An implementation of this TIP is already present in the core, as part of the special test commands for exercising the various internal of the Tcl core during test. This TIP asks to make that mechanism publicly available to script and package authors, with a bit of cleanup regarding the Tcl level API. The roots of that mechanism can be traced back to the Trf package which implemented channel transformations first and provides a similar command.

\section{Transform Management API Specification}

The Tcl level API consists of two new subcommands added to the ensemble command \textbf{chan}' specified by \cite{tip208}. Both subcommands are completely generic, i.e. they can be applied to any type of channel, without restrictions. There is no C API to specify. The Tcl core already has a standard API for the creation of channel transformations from the C level.

\subsection{The push Subcommand}

\begin{quote}
\textbf{chan push} \textit{channel cmdprefix}
\end{quote}

This subcommand creates a new script level transformation using the command prefix \textit{cmdprefix} as its handler and attaches it to the specified channel. The new transformation is always added on top of any other transformations which may be present.

The handle of the new transformation is returned as the result of the command. This handle is the first argument given to all handler methods, to allow the code to distinguish between the various instances of the same transformation, if necessary.

Use the new \textbf{chan pop} command to remove the transformation. See below.

The API this handler has to provide is specified below, in the section ``Command Handler API Specification''.

We have chosen to use \textit{late-binding} of the handler command. See the section ``Early- versus Late-Binding of the Handler Command'' for more detailed explanations.

The command invokes the handler method \textbf{initialize} to determine the supported methods before it pushes the transformation. It will throw an error if a read-only transformation is pushed on a write-only channel, or vice versa. In general if the r/w-mode of transformation and channel together cause the result to be neither readable nor writable.

\subsection{The pop Subcommand}

\begin{quote}
\textbf{chan pop} \textit{channel}
\end{quote}

This subcommand removes the topmost transformation from the \textit{channel}, if there is any. This command is equivalent to the builtin command \textit{close} if the channel had no transformations added to it.

Note: If the removal of the topmost transformation uncovers inactive transformations (See section ``Interaction with Threads and Other Interpreters''), then these will be removed now as well.

\section{Command Handler API Specification}

The Tcl-level handler command for a reflected channel transformation is an ensemble that has to support the following subcommands, as listed below. Note that the term \textit{ensemble} is used to generically describe all command (prefixes) which are able to process subcommands. This TIP is \textbf{not} tied to the recently introduced 'namespace ensemble's (though they may be used to implement such handlers.)

\subsection{The initialize Subcommand}

\begin{quote}
\textit{handler} \textbf{initialize} \textit{handle mode}
\end{quote}

This method is called first, and then never again (for the given \textit{handle}). Its responsibility is to initialize all parts of the transformation at the Tcl level. The MODE is a list containing any of \textbf{read} and \textbf{write}.
\begin{itemize}
\item{}
\textbf{write} contained in \textit{mode} implies that the channel is writable.
\item{}
\textbf{read} contained in \textit{mode} implies that the channel is readable.
\end{itemize}

The return value of the method has to be a list containing the names of all methods which are supported by this handler. Any error thrown by the method will prevent the creation of the transformation. The thrown error will appear as error thrown by \textbf{chan push}.

The current version is \textit{1}.

This method has no equivalent at the C level.

\subsection{The finalize Subcommand}

\begin{quote}
\textit{handler} \textbf{finalize} \textit{handle}
\end{quote}

This method is called last for the given \textit{handle}, just before the destruction of the C level data structures. It is now its responsibility to clean up all parts of the transformation at the Tcl level.

Any result returned by the method will be ignored. The same is true for errors thrown by the method.

This method has no equivalent at the C level.

\subsection{The flush Subcommand}

\begin{quote}
\textit{handler} \textbf{flush} \textit{handle}
\end{quote}

This method is called whenever data in the transformation 'write' buffer has to be forced downward, i.e. towards the base channel. The result returned by the method is taken as the \textit{binary} data to write to the transformation below the current transformation. This can be the base channel as well.

In other words, when this method is called the transformation cannot defer the actual transformation operation anymore and has to transform all data waiting in its internal write buffers and return the result of that action.

The method is optional. However if this method is supported then \textbf{write} has to be supported as well. The reverse is not true.

\subsection{The write Subcommand}

\begin{quote}
\textit{handler} \textbf{write} \textit{handle buffer}
\end{quote}

This method is optional. If it is not present it means that the transformation does not support writing, and the channel it was pushed on becomes non-writable as well.

It will be called whenever the user, or a transformation above this transformation writes data downward. The \textit{buffer} contains the \textbf{binary} data which has been written to us. It is the responsibility of this method to actually transform the data.

The result returned by the method is taken as the \textbf{binary} data to write to the transformation below this transformation. This can be the base channel as well. Note that the result is allowed to be empty, or less than the data we got. The transformation is not required to transform everything which was written to it right now. It is allowed to store this data in internal buffers and to defer the actual transformation until it has more data.

\subsection{The drain Subcommand}

\begin{quote}
\textit{handler} \textbf{drain} \textit{handle}
\end{quote}

This method is called whenever data in the transformation input (i.e. read) buffer has to be forced upward, i.e. towards the user, i.e. the script. The result returned by the method is taken as the \textbf{binary} data to push upward to the transformation above this transformation. This can be the script as well.

In other words, when this method is called the transformation cannot defer the actual transformation operation anymore and has to transform all data waiting in its internal read buffers and return the result of that action.

The method is optional. However if this method is supported then \textbf{read} has to be supported as well. The reverse is not true.

\subsection{The read Subcommand}

\begin{quote}
\textit{handler} \textbf{read} \textit{handle buffer}
\end{quote}

This method is optional. If it is not present it means that the transformation does not support reading, and the channel it was pushed on becomes non-readable as well.

It is called whenever the base channel, or a transformation below this transformation pushes data upward. The \textit{buffer} contains the \textbf{binary} data which has been given to us from below. It is the responsibility of this method to actually transform data. The result returned by the method is taken as the \textbf{binary} data to push further upward to the transformation above this transformation. This can be the user, i.e. the script as well.

Note that the result is allowed to be empty, or even less than the data we got. The transformation is not required to transform everything given to it right now. It is allowed to store incoming data in internal buffers and to defer the actual transformation until it has more data.

\subsection{The limit? Subcommand}

\begin{quote}
\textit{handler} \textbf{limit?} \textit{handle}
\end{quote}

This method is optional. If it is not present it means that the transformation is allowed to read ahead as much as it likes.

This method is called during input processing and allows the Tcl level part of the transformation to restrict the number of bytes read from the downward transformation or base channel before its method \textbf{read} is called with the resulting buffer.

The result of the method has to be an integer number not equal to zero. A negative result signals that the transformation allows the I/O system to read an unlimited number of bytes. A positive number on the other hand is interpreted as the maximum number of bytes the I/O system is allowed to read from the downward transformation or base channel.

This method is necessary for transformations where the data is bounded at the end in some way. In that case the transformation has to prevent the system from reading beyond the boundary as otherwise data behind it will be given to the transformation and then lost when the transformation is removed. \textbf{This is a limitation of the current I/O core} as it does not allow a transformation to push unused data back into the I/O core when the transformation is removed from the channel.

Fixing this \textbf{limitation} requires a separate TIP, as either the public API of the I/O core has to be extended, or the public structure of channel drivers.

\subsection{The clear Subcommand}

\begin{quote}
\textit{handler} \textbf{clear} \textit{handle}
\end{quote}

This method is called to clear any data stored in the internal input buffers of the transformation. This happens only when the user seeks the channel the transformation is attached to.

Any result returned by the method is ignored.

The method is optional. I.e. a transformation not having any internal buffers to clear can leave out its implementation.

\section{Hardwired Behavior of Reflected Transformations}

Not all functions of the channel driver implementing the reflected transformation are directly reflected into the Tcl level. Their behavior is hardwired in the C level implementing the reflection and specified now.
\begin{description}
\item[BlockModeProc]
Records the chosen blocking mode in the C-level data structures. This influences the low-level write and read behavior. However the Tcl level is shielded from this, so a handler method is not required.
\item[CloseProc]
Invokes the methods \textbf{drain} and \textbf{flush} to clean up any buffers managed by the Tcl level of the transformation, and then \textbf{finalize}. \textbf{drain} is not called if a previous call to it has not been invalidated yet.
\item[InputProc]
Tries to satisfies the incoming read request from the input result buffer first. If that is not enough it invokes the \textbf{limit?} method to establish the current read limit, reads data from the downward transformation or base channel per the limit and feeds the data it got to the Tcl level of the transformation, via an invocation of the method \textbf{read}. The result of that call is added to the input result buffer and used to further satisfy the read request.

If the channel is blocking the system will iterate until the request is fulfilled completely or EOF has been signaled from below.

In non-blocking mode however the loop will stop if either the request was fulfilled completely, or if we would block. Note that reaching EOF in this situation causes a flush of the Tcl side input buffers via an invocation of the method \textbf{drain}. Otherwise the data stored in them would be lost when the transformation is removed or the channel closed completely.
\item[OutputProc]
Simply forwards the written buffer to the method \textbf{write} for processing and then writes any returned result to the transformation or base channel below.
\item[SeekProc]
Recognizes requests made by \textbf{tell} and passes them down without doing anything else. The result generated by the base channel is then passed back up unchanged.

Any other request causes it to flush all write buffers on the Tcl side via an invocation of the method \textbf{flush}, and clear all input buffers on the Tcl side via an invocation of the method \textbf{clear} before passing the request down. Note that the calls mentioned above are not made if the channel is not writable, or not readable. Further note that the results of the \textbf{flush} are discarded, not written, as they would otherwise move the current location and throw off our expectations regarding where we are now and will end up after the seek.

This implements the most simple seek behavior as it was found in the very earliest incarnation of the transform reflection functionality, i.e. passing down any seek request unchanged until the base channel is reached. This also means that the internal state of transformations is not adjusted after a seek and may generate bogus results.

The reflection (actually any transformation) provided by the package \textit{Trf} has much more complex seek behavior. This was left out for now to keep the scope of this TIP relatively focused. A follow-up TIP can be written for a deeper discussion of the interaction between seeking and any type of transformation, not only reflected ones.

See \url{http://www.oche.de/~akupries/soft/trf/trf_seek.html} for the description of the complex seeking model used by the transformation reflection in Trf.
\item[SetOptionProc, GetOptionProc, GetFileHandleProc]
The calls are passed down without change, any results are passed back to the caller, again without any changes.

Transformations have no options which can be configured when they are attached to a channel, hence the pass-through and no handler method at the Tcl level.
\item[WatchProc]
Remembers the interest mask provided by the caller and uses this to manage the internal timer used to generate fake file events when data is buffered.
\item[NotifyProc]
Manages the internal timer. Will pass the incoming mask of triggered events upward without change.
\end{description}

\section{Interaction with Threads and Other Interpreters}

Adding a reflected transformation to a channel does not create any restrictions on the sharing of the channel with other interpreters, nor with moving the channel to different interpreters or threads.

Like for reflected channels (See \cite{tip219} ('Tcl Channel Reflection API')), the implementation ensures that the handler command is always executed in the original interpreter and thread. The latter is done by posting specialized events to the original thread, essentially forwarding driver invocations to the correct thread.

When a thread or interpreter is deleted all transformations it owns are deleted as far as possible, and any remnants are marked as dead. The latter occurs only if the channel using the deleted transformation is outside of the deleted thread or interpreter. Such channels will throw errors when accessed, until the offending transformation is removed from them via \textbf{chan pop} (Multiple pop's will be necessary if the deleted transformation sits in the middle of a stack).

\section{Interaction with Safe Interpreters}

The new subcommands \textbf{push} and \textbf{pop} of \textbf{chan} are both safe and therefore made accessible to safe interpreters.

While \textbf{push}ing a transformation arranges for the execution of code this code is always executed within the safe interpreter, even if the channel was moved (See previous section).

That the data flowing through the channel is modified by the transformation is no problem either, because to attach the transformation to the channel it has to have been given to the safe interpreter in the beginning, in other words, the interpreter doing this already trusted the safe interpreter in some way, and the fact that we can now add a transformation does not change this.

Equivalent reasoning applies if the channel was created by the safe interpreter and then shared/moved into the trusted interpreter. The transformation has no effect on the trust already given to the safe interpreter through the share/move operation.

The same holds for the subcommand \textbf{pop}. If the safe interpreter can execute it on a channel it has the channel already in its possession, either because it created the channel, or because the channel was shared/moved into it.

\section{Event processing}

It is specified that reflected transformations do not support any type of user-visible event handling.

The only event handling done is the invisible passing of current interest from higher to lower layers, and the (again invisible) behaviour needed to drive the flushing of transformation buffers to higher layers. See the descriptions of \textit{WatchProc} and \textit{NotifyProc} in the section \textbf{Hardwired Behavior of Reflected Transformations}.

This is different from the previous revisions of this TIP which included language and definitions to support event processing by transformations up to and including revision 1.11, i.e. allowing transformations to register interest in events, and then process them.

The main example use case for this was that it would give a transformation the ability to initialize itself with data coming from the channel (like a key exchange) before switching to the plain transformative behaviour. While working on the design of how to support this on the Tcl script level I ran into lots of complicated edge cases and convolutions.

Based on this I have now come to the belief that this has been a misfeature from the beginning and that the use case this was based on (see above) is an example of comingling concerns which should be separate, i.e an example of what should not be done.

The transformation's concern is transforming data, nothing else. Any parameters needed for this, like keys, seeds, etc. should come from the outside, at the time it is pushed on the channel stack. Initializations like key exchanges, cipher negotiations, etc. should and are not be a concern of the transformation. They should and can be done \textbf{before} the transformation is configured and pushed.

This last means that the removal of user-visible event handling support is by no means a restriction on the type of things we can do. Even so, should we find other use cases for event handling by transformations we can always write another TIP within which we specify how to extend reflected transformations with that feature.

\section{Early- versus Late-Binding of the Handler Command}

We have two principal methods for using the handler command. These are called early- and late-binding.

\textbf{Early-binding} means that the command implementation to use is determined at the time of the creation of the channel, i.e. when \textit{chan push} is executed, before any methods are called. Afterward it cannot change. The result of the command resolution is stored internally and used until the channel is destroyed. Renaming the handler command has no effect. In other words, the system will automatically call the command under the new name. The destruction of the handler command is intercepted and causes the channel to close as well.

\textbf{Late-binding} means that the handler command is stored internally essentially as a string, and this string is mapped to the implementation to use for each and every call to a method of the handler. Renaming the command, or destroying it means that the next call of a handler method will fail, causing the higher level channel command to fail as well. Depending on the method the error message may not be able to explain the reason of that failure.

Another problem with the late-binding approach is that the context for the resolution of the command name has to be specified explicitly to avoid problems with relative names. Early-binding resolves once, in the context of the \textit{chan create}. Late-binding performs resolution anywhere where channel commands like \textit{puts}, \textit{gets}, etc. are called, i.e. in a random context. To prevent problems with different commands of the same name in several namespace it becomes necessary to force the usage of a specific fixed context for the resolution.

Note that moving a different command into place after renaming the original handler allows the Tcl level to change the implementation dynamically at runtime. This however is not really an advantage over early-binding as the early-bound command can be written such that it delegates to the actual implementation, and that can then be changed dynamically as well.

However, despite all this late binding is so far the method of choice for the implementation of callbacks, be they in Tcl, or Tk; and has been chosen for the reflection as well.

The context for all handler method invokations is the \textbf{global scope}.

\section{Limitations}

The method \textbf{limit?} is required to limit reading from below to prevent reading over transformation specific boundaries. This is a direct consequence of not being able to push unused data back into the I/O core when a transformation is removed.

The reflection implements the very simple seek behavior found in the earliest incarnation of this functionality, i.e. passing down any seek request unchanged until the base channel is reached. This also means that the internal state of transformations is not adjusted after a seek and may generate bogus results. The reflection (actually any transformation) provided by the package \textit{Trf} has much more complex seek behavior. This was left out for now to keep the scope of this TIP relatively focused. A follow-up TIP can be written for a deeper discussion of the interaction between seeking and any type of transformation, not only reflected ones.

See \url{http://www.oche.de/~akupries/soft/trf/trf_seek.html} for the description of the complex seeking model used by the transformation reflection in Trf.

\section{Miscellanea}

The transform reflection API reserves the driver type ``tclrtransform'' for itself, and uses it to detect its own transformations. Usage of this driver type by other transformations is not allowed.

\section{Examples}

\subsection{Transformation Implementations}

A simple way of implementing new transformations is to use any of the various object systems for Tcl. Create a class for the transformation. \textbf{chan push} the transformation in the constructor for new objects and store the transformation handle. Make the new object the command handler for the transformation. This automatically translates the sub commands for the command handler into object methods. Implement the various methods required. When the object is deleted deactivate the transformation, and delete the object when the channel announces that the transformation has been \textbf{chan pop}ped. This part is a bit tricky, flags have to be used to break the potential cycle.

Another possibility is to implement the command handler as a regular command, together with a creation command wrapping around \textit{chan push} and a backend which keeps track of all handles created by it and their state, associated data, etc.

\subsection{Possible Transformations}
\begin{itemize}
\item{}
Identity
\item{}
Gathering statistics about the channel. The simplest would be to count bytes, for example.
\item{}
Divert a copy of all data to a separate channel. In other words, spying on the channel.
\item{}
Give a channel which normally cannot seek backward the ability to do so through buffering (a limited amount of) the data read from it.
\item{}
Limit reading from a channel to a specific finite number of characters (This can be done via a suitable implementation of the method ``limit?'' in the transformation handler).
\end{itemize}

\section{Reference Implementation}

A variant implementation of this TIP is already present in the core, as part of the special test commands for exercising the various internal of the Tcl core during test.

The relevant files are
\begin{itemize}
\item{}
``tclIOGT.c'' (base implementation) and
\item{}
``tclTest.c'' (command interface).
\end{itemize}

The command interface specified here is different from the current interface.
\begingroup\small\begin{verbatim}
 test transform channel -command command
\end{verbatim}
\endgroup

versus
\begingroup\small\begin{verbatim}
 chan push channel cmdprefix
 chan pop
\end{verbatim}
\endgroup

The handler methods are different as well. This TIP consolidated a number of methods, gave them better names and removed unnecessary arguments. It also removed some limitations.

The actual reference implementation is provided at SourceForge [\url{http://sourceforge.net/support/tracker.php?aid=1163274}].

\section{Comments}

[ Add comments on the document here ]

\section{Copyright}

This document has been placed in the public domain.

\section{Colophon}

\textit{TIP AutoGenerator --- written by Donal K. Fellows}
\begin{thebibliography}{TIP \#3}
\addcontentsline{toc}{section}{References}
\bibitem[TIP \#208]{tip208}
Jeff Hobbs,
\emph{Add a 'chan' Command},
on-line at \url{http://purl.org/tcl/tip/208.html}
\bibitem[TIP \#219]{tip219}
Andreas Kupries,
Andreas Kupries,
\emph{Tcl Channel Reflection API},
on-line at \url{http://purl.org/tcl/tip/219.html}
\bibitem[TIP \#228]{tip228}
Andreas Kupries,
Andreas Kupries,
Vince Darley,
\emph{Tcl Filesystem Reflection API},
on-line at \url{http://purl.org/tcl/tip/228.html}
\end{thebibliography}
\end{document}
