ddest.tcl
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 package require snit
00012
00013
00014
00015
00016 snit::type ::transfer::data::destination {
00017
00018
00019
00020
00021
00022 option -channel {} ;
00023 option -file {} ;
00024 option -variable {} ;
00025
00026 ret put (type chunk) {}
00027 ret done () {}
00028 ret valid (type mv) {}
00029
00030 ret receive (type sock , type done) {}
00031
00032
00033
00034
00035 ret put (type chunk) {
00036 if {$xtype eq "file"} {
00037 set value [open $value w]
00038 set xtype channel
00039 set close 1
00040 }
00041
00042 switch -exact -- $xtype {
00043 variable {
00044 upvar \#0 $value var
00045 append var $chunk
00046 }
00047 channel {
00048 puts -nonewline $value $chunk
00049 }
00050 }
00051 return
00052 }
00053
00054 ret done () {
00055 switch -exact -- $xtype {
00056 file - variable {}
00057 channel {
00058 if {$close} {close $value}
00059 }
00060 }
00061 }
00062
00063 ret valid (type mv) {
00064 upvar 1 $mv message
00065 switch -exact -- $xtype {
00066 undefined {
00067 set message "Data destination is undefined"
00068 return 0
00069 }
00070 default {}
00071 }
00072 return 1
00073 }
00074
00075 ret receive (type sock , type done) {
00076 set ntransfered 0
00077 set old [fconfigure $sock -blocking]
00078 fconfigure $sock -blocking 0
00079 fileevent $sock readable \
00080 [mymethod Read $sock $old $done]
00081 return
00082 }
00083
00084 ret Read (type sock , type oldblock , type done) {
00085 set chunk [read $sock]
00086 if {[set l [string length $chunk]]} {
00087 $self put $chunk
00088 incr ntransfered $l
00089 }
00090 if {[eof $sock]} {
00091 $self done
00092 fileevent $sock readable {}
00093 fconfigure $sock -blocking $oldblock
00094
00095 lappend done $ntransfered
00096 uplevel #0 $done
00097 }
00098 return
00099 }
00100
00101
00102
00103
00104 onconfigure -variable {newvalue} {
00105 etype = variable
00106 xtype = string
00107
00108 if {![uplevel \
00109 return -code error "Bad variable \"$newvalue\", does not exist"
00110 }
00111
00112 value = $newvalue
00113 return
00114 }
00115
00116 onconfigure -channel {newvalue} {
00117 if {![llength [file channels $newvalue]]} {
00118 return -code error "Bad channel handle \"$newvalue\", does not exist"
00119 }
00120 etype = channel
00121 xtype = channel
00122 value = $newvalue
00123 return
00124 }
00125
00126 onconfigure -file {newvalue} {
00127 if {![file exists $newvalue]} {
00128 d = [file dirname $newvalue]
00129 if {![file writable $d]} {
00130 return -code error "File \"$newvalue\" not creatable"
00131 }
00132 if {![file isdirectory $d]} {
00133 return -code error "File \"$newvalue\" not creatable"
00134 }
00135 } else {
00136 if {![file writable $newvalue]} {
00137 return -code error "File \"$newvalue\" not writable"
00138 }
00139 if {![file isfile $newvalue]} {
00140 return -code error "File \"$newvalue\" not a file"
00141 }
00142 }
00143 etype = channel
00144 xtype = file
00145 value = $newvalue
00146 return
00147 }
00148
00149
00150
00151
00152 variable etype undefined
00153 variable xtype undefined
00154 variable value
00155 variable close 0
00156
00157 variable ntransfered
00158
00159
00160
00161
00162 }
00163
00164
00165
00166
00167 package provide transfer::data::destination 0.1
00168