forked from docker-archive/classicswarm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
203 lines (167 loc) · 3.36 KB
/
message.go
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package libswarm
import (
"github.com/dmcgowan/libchan"
"fmt"
"io"
)
type Message struct {
Verb
Args []string
Ret Sender
Att io.ReadWriteCloser
}
type Sender interface {
Send(msg *Message) (Receiver, error)
Close() error
}
type Receiver interface {
Receive(mode int) (*Message, error)
}
type internalMessage struct {
Verb
Args []string
Ret libchan.Sender
Att io.ReadWriteCloser
}
type senderWrapper struct {
libchan.Sender
}
func WrapSender(s libchan.Sender) Sender {
return &senderWrapper{s}
}
func (s *senderWrapper) Send(msg *Message) (Receiver, error) {
var rcvr Receiver = NopReceiver{}
imsg := &internalMessage{
Verb: msg.Verb,
Args: msg.Args,
}
if msg.Ret != nil {
thisEnd, otherEnd, err := s.Sender.CreateNestedReceiver()
if err != nil {
return nil, err
}
imsg.Ret = otherEnd
if RetPipe.Equals(msg.Ret) {
rcvr = &receiverWrapper{thisEnd}
} else {
go Copy(msg.Ret, &receiverWrapper{thisEnd})
}
}
if msg.Att != nil {
byteStream, err := s.Sender.CreateByteStream()
if err != nil {
return nil, err
}
imsg.Att = byteStream
go func() {
io.Copy(byteStream, msg.Att)
byteStream.Close()
}()
go func() {
io.Copy(msg.Att, byteStream)
msg.Att.Close()
}()
}
return rcvr, s.Sender.Send(imsg)
}
type receiverWrapper struct {
libchan.Receiver
}
func WrapReceiver(r libchan.Receiver) Receiver {
return &receiverWrapper{r}
}
func (r *receiverWrapper) Receive(mode int) (*Message, error) {
imsg := &internalMessage{}
if err := r.Receiver.Receive(imsg); err != nil {
return nil, err
}
var ret Sender
if imsg.Ret == nil {
ret = NopSender{}
} else {
ret = &senderWrapper{imsg.Ret}
}
if mode&Ret == 0 {
if err := ret.Close(); err != nil {
return nil, err
}
}
msg := &Message{
Verb: imsg.Verb,
Args: imsg.Args,
Ret: ret,
Att: imsg.Att,
}
return msg, nil
}
func Pipe() (*receiverWrapper, *senderWrapper) {
r, s := libchan.Pipe()
return &receiverWrapper{r}, &senderWrapper{s}
}
func Copy(dst Sender, src Receiver) (int, error) {
var n int
for {
msg, err := src.Receive(Ret)
if err == io.EOF {
return n, nil
}
if err != nil {
return n, err
}
if _, err := dst.Send(msg); err != nil {
return n, err
}
n++
}
}
type Handler func(msg *Message) error
func (h Handler) Send(msg *Message) (Receiver, error) {
var ret Receiver
if RetPipe.Equals(msg.Ret) {
ret, msg.Ret = Pipe()
}
go func() {
if msg.Ret == nil {
msg.Ret = NopSender{}
}
h(msg)
msg.Ret.Close()
}()
return ret, nil
}
func (h Handler) Close() error {
return fmt.Errorf("can't close a Handler")
}
func Repeater(payload *Message) Sender {
return Handler(func(msg *Message) error {
msg.Ret.Send(payload)
return nil
})
}
var notImplementedMsg = &Message{Verb: Error, Args: []string{"not implemented"}}
var NotImplemented = Repeater(notImplementedMsg)
const (
Ret int = 1 << iota
// FIXME: use an `Att` flag to auto-close attachments by default
)
type retPipe struct {
NopSender
}
var RetPipe = retPipe{}
func (r retPipe) Equals(val Sender) bool {
if rval, ok := val.(retPipe); ok {
return rval == r
}
return false
}
type NopSender struct{}
func (s NopSender) Send(msg *Message) (Receiver, error) {
return NopReceiver{}, nil
}
func (s NopSender) Close() error {
return nil
}
type NopReceiver struct{}
func (r NopReceiver) Receive(mode int) (*Message, error) {
return nil, io.EOF
}