blob: 900401cc36969a71b4219275232060e92ac2aff6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
# -*- tcl -*-
# ### ### ### ######### ######### #########
##
# Transfer class. Sending of data.
##
# Utilizes data source and connect components to handle the
# general/common parts.
# ### ### ### ######### ######### #########
## Requirements
package require snit
package require transfer::data::source ; # Data source
package require transfer::connect ; # Connection startup
# ### ### ### ######### ######### #########
## Implementation
snit::type ::transfer::transmitter {
# ### ### ### ######### ######### #########
## Convenient fire and forget file/channel transmission operations.
typemethod {stream channel} {chan host port args} {
# Select stream configuration ( host => active, otherwise
# passive)
if {$host eq {}} {
set cmd [linsert $args 0 $type %AUTO% \
-channel $chan -port $port \
-mode passive -translation binary]
} else {
set cmd [linsert $args 0 $type %AUTO% \
-channel $chan -host $host -port $port \
-mode active -translation binary]
}
# Create a transient transmitter controller, and wrap our own
# internal completion handling around the user supplied
# callback.
set transmitter [eval $cmd]
$transmitter configure \
-command [mytypemethod Done \
$chan [$transmitter cget -command]]
# Begin transmission (or wait for other side to connect).
return [$transmitter start]
}
typemethod {stream file} {file host port args} {
set chan [open $file r]
fconfigure $chan -translation binary
return [eval [linsert $args 0 $type stream channel $chan $host $port]]
}
typemethod Done {chan command transmitter n {err {}}} {
close $chan
$transmitter destroy
if {![llength $command]} return
after 0 [linsert $command end $n $err]
return
}
# ### ### ### ######### ######### #########
## API
## Data source sub component
delegate option -string to mysource
delegate option -channel to mysource
delegate option -file to mysource
delegate option -variable to mysource
delegate option -size to mysource
delegate option -progress to mysource
## Connection management sub component
delegate option -host to myconnect
delegate option -port to myconnect
delegate option -mode to myconnect
delegate option -translation to myconnect
delegate option -encoding to myconnect
delegate option -eofchar to myconnect
delegate option -socketcmd to myconnect
## Transmitter configuration, and API
option -command -default {}
option -blocksize -default 1024 -type {snit::integer -min 1}
constructor {args} {}
method start {} {}
method busy {} {}
# ### ### ### ######### ######### #########
## Implementation
constructor {args} {
set mybusy 0
install mysource using ::transfer::data::source ${selfns}::source
install myconnect using ::transfer::connect ${selfns}::conn
$self configurelist $args
return
}
method start {} {
if {$mybusy} {
return -code error "Object is busy"
}
if {![$mysource valid msg]} {
return -code error $msg
}
if {$options(-command) eq ""} {
return -code error "Completion callback is missing"
}
set mybusy 1
return [$myconnect connect [mymethod Begin]]
}
method busy {} {
return $mybusy
}
# ### ### ### ######### ######### #########
## Internal helper commands.
method Begin {__ sock} {
# __ <=> myconnect
$mysource transmit $sock \
$options(-blocksize) \
[mymethod Done $sock]
return
}
method Done {sock args} {
# args is either (n),
# or (n errormessage)
set mybusy 0
close $sock
$self Complete $args
return
}
method Complete {arguments} {
# 8.5: {*}$options(-command) $self {*}$arguments
set cmd $options(-command)
lappend cmd $self
foreach a $arguments {lappend cmd $a}
uplevel #0 $cmd
return
}
# ### ### ### ######### ######### #########
## Data structures
component mysource ; # Data source providing the bytes to transfer
component myconnect ; # Connector controlling where to the data transfered to.
variable mybusy 0 ; # Transfer status.
##
# ### ### ### ######### ######### #########
}
# ### ### ### ######### ######### #########
## Ready
package provide transfer::transmitter 0.2
|