File: receiver.tcl

package info (click to toggle)
tcllib 1.10-dfsg-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 17,708 kB
  • ctags: 6,122
  • sloc: tcl: 106,354; ansic: 9,205; sh: 8,707; xml: 1,766; yacc: 753; makefile: 115; perl: 84; f90: 84; python: 33; ruby: 13; php: 11
file content (126 lines) | stat: -rw-r--r-- 2,557 bytes parent folder | download
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
# -*- tcl -*-
# ### ### ### ######### ######### #########
##
# Transfer class. Reception of data.
##
# Utilizes data destination and connect components to handle the
# general/common parts.

# ### ### ### ######### ######### #########
## Requirements

package require snit
package require transfer::data::destination ; # Data destination
package require transfer::connect           ; # Connection startup

# ### ### ### ######### ######### #########
## Implementation

snit::type ::transfer::receiver {

    # ### ### ### ######### ######### #########
    ## API

    ## Data destination sub component

    delegate option -channel  to dest
    delegate option -file     to dest
    delegate option -variable to dest

    ## Connection management sub component

    delegate option -host        to conn
    delegate option -port        to conn
    delegate option -mode        to conn
    delegate option -translation to conn
    delegate option -encoding    to conn
    delegate option -eofchar     to conn

    ## Receiver configuration, and API

    option -command {}

    constructor {args} {}

    method start {} {}
    method busy  {} {}

    # ### ### ### ######### ######### #########
    ## Implementation

    constructor {args} {
	set dest [::transfer::data::destination ${selfns}::dest]
	set conn [::transfer::connect           ${selfns}::conn]
	set busy 0

	$self configurelist $args
	return
    }

    method start {} {
	if {$busy} {
	    return -code error "Object is busy"
	}

	if {![$dest valid msg]} {
	    return -code error $msg
	}

	if {$options(-command) eq ""} {
	    return -code error "Completion callback is missing"
	}

	set busy 1
	return [$conn connect [mymethod Begin]]
    }

    method busy {} {
	return $busy
    }

    # ### ### ### ######### ######### #########
    ## Internal helper commands.

    method Begin {__ sock} {
	# __ == conn
	$dest receive $sock \
		[mymethod Done $sock]
	return
    }

    method Done {sock args} {
	# args is either (n),
	#             or (n errormessage)

	set busy 0
	close $sock
	$self Complete $args
	return
    }

    method Complete {alist} {
	set     cmd $options(-command)
	lappend cmd $self
	foreach a $alist {lappend cmd $a}

	uplevel #0 $cmd
	return

	# 8.5: {*}$options(-command) {*}$alist
    }

    # ### ### ### ######### ######### #########
    ## Data structures

    variable dest
    variable conn
    variable busy

    ##
    # ### ### ### ######### ######### #########
}

# ### ### ### ######### ######### #########
## Ready

package provide transfer::receiver 0.1