source: tol/tolsh/tolsh.vfs/lib/tcllib1.10/transfer/ddest.tcl @ 8227d4c

1941-docker-image-to-testdevel
Last change on this file since 8227d4c was 8597867d, checked in by Jorge Pérez <josp.jorge@…>, 16 years ago

closes #976, se incluye tcllib1.10, base64 debe eliminarse del raiz de lib ya que esta incluido en tcllib

git-svn-id: ​https://www.tol-project.org/svn/tolp/trunk@2556 be4846ae-f051-483a-831f-b64e7d08527f

  • Property mode set to 100644
File size: 3.3 KB
Line 
1# -*- tcl -*-
2# ### ### ### ######### ######### #########
3##
4
5# Class for the handling of stream destinations.
6
7# ### ### ### ######### ######### #########
8## Requirements
9
10package require snit
11
12# ### ### ### ######### ######### #########
13## Implementation
14
15snit::type ::transfer::data::destination {
16
17    # ### ### ### ######### ######### #########
18    ## API
19
20    #                       Destination is ...
21    option -channel  {} ; # an open & writable channel.
22    option -file     {} ; # a writable file.
23    option -variable {} ; # the named variable.
24
25    method put   {chunk} {}
26    method done  {}      {}
27    method valid {mv}    {}
28
29    method receive {sock done} {}
30
31    # ### ### ### ######### ######### #########
32    ## Implementation
33
34    method put {chunk} {
35        if {$xtype eq "file"} {
36            set value [open $value w]
37            set xtype  channel
38            set close 1
39        }
40
41        switch -exact -- $xtype {
42            variable {
43                upvar \#0 $value var
44                append var $chunk
45            }
46            channel {
47                puts -nonewline $value $chunk
48            }
49        }
50        return
51    }
52
53    method done {} {
54        switch -exact -- $xtype {
55            file - variable {}
56            channel {
57                if {$close} {close $value}
58            }
59        }
60    }
61
62    method valid {mv} {
63        upvar 1 $mv message
64        switch -exact -- $xtype {
65            undefined {
66                set message "Data destination is undefined"
67                return 0
68            }
69            default {}
70        }
71        return 1
72    }
73
74    method receive {sock done} {
75        set ntransfered 0
76        set old [fconfigure $sock -blocking]
77        fconfigure $sock -blocking 0
78        fileevent $sock readable \
79                [mymethod Read $sock $old $done]
80        return
81    }
82
83    method Read {sock oldblock done} {
84        set chunk [read $sock]
85        if {[set l [string length $chunk]]} {
86            $self put $chunk
87            incr ntransfered $l
88        }
89        if {[eof $sock]} {
90            $self done
91            fileevent  $sock readable {}
92            fconfigure $sock -blocking $oldblock
93
94            lappend done $ntransfered
95            uplevel #0 $done
96        }
97        return
98    }
99
100    # ### ### ### ######### ######### #########
101    ## Internal helper commands.
102
103    onconfigure -variable {newvalue} {
104        set etype variable
105        set xtype string
106
107        if {![uplevel \#0 {info exists $newvalue}]} {
108            return -code error "Bad variable \"$newvalue\", does not exist"
109        }
110
111        set value $newvalue
112        return
113    }
114
115    onconfigure -channel {newvalue} {
116        if {![llength [file channels $newvalue]]} {
117            return -code error "Bad channel handle \"$newvalue\", does not exist"
118        }
119        set etype channel
120        set xtype channel
121        set value $newvalue
122        return
123    }
124
125    onconfigure -file {newvalue} {
126        if {![file exists $newvalue]} {
127            set d [file dirname $newvalue]
128            if {![file writable $d]} {
129                return -code error "File \"$newvalue\" not creatable"
130            }
131            if {![file isdirectory $d]} {
132                return -code error "File \"$newvalue\" not creatable"
133            }
134        } else {
135            if {![file writable $newvalue]} {
136                return -code error "File \"$newvalue\" not writable"
137            }
138            if {![file isfile $newvalue]} {
139                return -code error "File \"$newvalue\" not a file"
140            }
141        }
142        set etype channel
143        set xtype file
144        set value $newvalue
145        return
146    }
147
148    # ### ### ### ######### ######### #########
149    ## Data structures
150
151    variable etype  undefined
152    variable xtype  undefined
153    variable value
154    variable close 0
155
156    variable ntransfered
157
158    ##
159    # ### ### ### ######### ######### #########
160}
161
162# ### ### ### ######### ######### #########
163## Ready
164
165package provide transfer::data::destination 0.1
Note: See TracBrowser for help on using the repository browser.