# ACTIVESTATE TEAPOT-PKG BEGIN TM -*- tcl -*- # -- Tcl Module # @@ Meta Begin # Package transfer::connect 0.1 # Meta as::build::date 2009-03-30 # Meta as::origin http://sourceforge.net/projects/tcllib # Meta category Data transfer facilities # Meta description Connection setup # Meta license BSD # Meta platform tcl # Meta require {Tcl 8.4} # Meta require snit # Meta subject active connection channel transfer passive # Meta summary transfer::connect # @@ Meta End # ACTIVESTATE TEAPOT-PKG BEGIN REQUIREMENTS package require Tcl 8.4 package require snit # ACTIVESTATE TEAPOT-PKG END REQUIREMENTS # ACTIVESTATE TEAPOT-PKG BEGIN DECLARE package provide transfer::connect 0.1 # ACTIVESTATE TEAPOT-PKG END DECLARE # ACTIVESTATE TEAPOT-PKG END TM # -*- tcl -*- # ### ### ### ######### ######### ######### ## # Class for handling of active/passive connectivity. # ### ### ### ######### ######### ######### ## Requirements package require snit # ### ### ### ######### ######### ######### ## Implementation snit::type ::transfer::connect { # ### ### ### ######### ######### ######### ## API option -host localhost option -port 0 option -mode active option -translation {} option -encoding {} option -eofchar {} method connect {command} {} # active: # - connect to host/port # # passive: # - listen on port for connection # ### ### ### ######### ######### ######### ## Implementation method connect {command} { if {$options(-mode) eq "active"} { set sock [socket $options(-host) $options(-port)] $self Setup $sock lappend command $self $sock uplevel \#0 $command return } else { set server [socket -server [mymethod Start $command] \ $options(-port)] return [lindex [fconfigure $server -sockname] 2] } return } method Start {command sock peerhost peerport} { close $server $self Setup $sock lappend command $self $sock uplevel \#0 $command return } method Setup {sock} { foreach o {-translation -encoding -eofchar} { if {$options($o) eq ""} continue fconfigure $sock $o $options($o) } return } # ### ### ### ######### ######### ######### ## Internal helper commands. onconfigure -mode {newvalue} { upvar 0 options(-mode) value if {$value eq $newvalue} return switch -exact -- $newvalue { passive - active { set value $newvalue } default { return -code error "Bad value \"$newvalue\", expected active, or passive" } } return } # ### ### ### ######### ######### ######### ## Data structures variable server {} ## # ### ### ### ######### ######### ######### } # ### ### ### ######### ######### ######### ## Ready package provide transfer::connect 0.1