-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsijsop_example_test.go
126 lines (98 loc) · 2.54 KB
/
sijsop_example_test.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
package sijsop
import (
"bytes"
"fmt"
"io"
"os"
"sync"
)
type FileTransfer struct {
Name string `json:"name"`
Size int64 `json:"size"`
}
func (ft *FileTransfer) SijsopType() string {
return "file_transfer"
}
func (ft *FileTransfer) New() Message {
return &FileTransfer{}
}
type FileTransferAck struct {
Name string `json:"name"`
}
func (fta *FileTransferAck) SijsopType() string {
return "file_transfer_ack"
}
func (fta *FileTransferAck) New() Message {
return &FileTransferAck{}
}
type RW struct {
io.ReadCloser
io.WriteCloser
}
// This example demonstrates transferring a binary file using the sijsop
// protocol, with the file transferred out-of-band. Left is going to read a
// file and send it to Right, who will acknowledge it.
func Example() {
toRightRead, fromLeftWrite := io.Pipe()
toLeftRead, fromRightWrite := io.Pipe()
protocol := &Definition{}
protocol.Register(&FileTransfer{}, &FileTransferAck{})
wg := sync.WaitGroup{}
wg.Add(2)
// The left side, who will be reading the file and sending it
go func() {
sp := protocol.Wrap(RW{toLeftRead, fromLeftWrite})
f, err := os.Open("sijsop_example_test.go")
if err != nil {
panic("Couldn't read this file")
}
fi, err := f.Stat()
if err != nil {
panic("couldn't stat this file")
}
transfer := &FileTransfer{
Name: "sijsop_example_test.go",
Size: fi.Size(),
}
err = sp.Send(transfer)
if err != nil {
panic("Couldn't send transfer request")
}
// now we copy the file over the stream directly
_, _ = io.Copy(fromLeftWrite, f)
ack := &FileTransferAck{}
err = sp.Unmarshal(ack)
if err != nil {
panic("didn't get the expected ack")
}
// deliberately done here, to ensure we received the ack
wg.Done()
}()
// The right side, who will be receiving it and ack'ing it.
go func() {
sp := protocol.Wrap(RW{toRightRead, fromRightWrite})
// show just receiving next message
msg, err := sp.ReceiveNext()
if err != nil {
panic("couldn't get the file transfer request")
}
size := msg.(*FileTransfer).Size
// Now directly extract the file from the bytestream
r := &io.LimitedReader{toRightRead, size}
buf := &bytes.Buffer{}
_, _ = io.Copy(buf, r)
// and send the ack, transititioning back to JSON
err = sp.Send(&FileTransferAck{msg.(*FileTransfer).Name})
if err != nil {
panic("couldn't send acknowledgement")
}
fileContents := buf.String()
// print out the package declaration to show the file was truly
// transferred.
fmt.Println(fileContents[:15])
wg.Done()
}()
wg.Wait()
// Output:
// package sijsop
}