multiplexer

http://tcllib.sourceforge.net/doc/multiplexer.html

There is a multiplexer module in Tcllib now as well. See the above URL for details of that package.


multiplexer.tgz is a Tcl "multiplexer" - or, in other words, a server that handles one-to-many comunication. One client sends it some data, and it sends that to all the other clients. Originally by davidw, it is now part of tcllib.

CMcC I have rewritten this as an incrtcl class called Mux, it can be found here: [L1 ]


schlenk A multiplexer can also be created with the new Tcl chan create command introduced with TIP 219.

This is a simple example multiplexer, which creates a new writable channel, which multiplexes its output to a number of external channels.

  package require Tcl 8.5
  package require snit

  snit::type multiplexer {
       constructor {mode args} {
           # Handle args ...
           set targets $args
           set dead 0
           set chan [chan create $mode $self]
       }
       destructor {
           # ... delete internal state ...
           if {$dead} return
           set dead 1
           catch {close $chan}
       }

       method handle {} {return $chan}
       variable chan
       variable dead 0
       variable targets ""

       method finalize {dummy} {
           if {$dead} return
           set dead 1
           $self destroy
       }
       method initialize {dummy mode} {
         if {"read" in $mode} {
             error "Only writing is supported for this channel type"
         }
               return {initialize finalize write watch}
       }
       method read       {dummy count} {}
       method write      {dummy data} {
         # multiplex the written data,
         # may need extra handling if the encodings of the target
         foreach targetchan $targets {
             set state [chan configure $targetchan]
             chan configure $targetchan -encoding binary \
                                        -translation binary \
                                        -eofchar {}
             puts -nonewline $targetchan $data
             chan configure $targetchan {*}$state
         }
         string length $data
       }
       method seek       {dummy offset base} {}
       method configure  {dummy args} {}
       method watch      {dummy events} {}
       method blocking   {dummy isblocking} {}
   }

   proc multiplexer_open {args} {
       return [[multiplexer %AUTO% {*}$args] handle]
   }

Usage example:

 set fd [open somefile.txt w+]
 set ch [multiplexer_open write stderr $fd]
 puts $ch "Some message"
 # did not implement buffering control, so flush explicitly...
 flush $ch