-
Notifications
You must be signed in to change notification settings - Fork 8
/
receiver.go
197 lines (163 loc) · 4.85 KB
/
receiver.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
package transfer
import (
"context"
"errors"
"fmt"
"io"
"net"
"github.com/fabiodcorreia/catch-my-file/pkg/clog"
"github.com/fabiodcorreia/catch-my-file/pkg/file"
"github.com/fabiodcorreia/catch-my-file/pkg/network"
"github.com/fabiodcorreia/catch-my-file/pkg/transfer/internal/protocol"
)
// Done is channel used to singal the termination of the service.
type Done chan<- interface{}
type Receiver struct {
port int
store *TransferStore
}
// NewReceiver will create a new Receiver server that will wait for
// connections on the specified port.
func NewReceiver(port int, store *TransferStore) *Receiver {
return &Receiver{
port: port,
store: store,
}
}
// Run will start the receiver starting the listener to receive requests.
func (rv *Receiver) Run(ctx context.Context, done Done) error {
listener, lErr := net.Listen(network.Type, fmt.Sprintf(":%d", rv.port))
if lErr != nil {
close(done)
return fmt.Errorf("receiver run listen error: %v", lErr)
}
go waitForRequests(ctx, listener, done, rv.store)
go watchdog(ctx, listener)
return nil
}
// watchdog will wait until the context gets cancelled and after
// that it will stop the listener.
func watchdog(ctx context.Context, listener net.Listener) {
<-ctx.Done()
if err := listener.Close(); err != nil {
clog.Error(err)
}
}
// waitForRequests will wait for new connections from senders and for each
// connection will handle handle the request.
func waitForRequests(ctx context.Context, listener net.Listener, done Done, store *TransferStore) {
for {
conn, err := listener.Accept()
if err != nil {
switch {
case errors.Is(err, net.ErrClosed):
clog.Info("Receiver server is closed")
default:
clog.Error(err)
}
close(done)
return
}
go handleRequest(ctx, conn, store)
}
}
// handleRequest will receive a new connection, add the transfer to the store,
// wait for the confirmation or rejection and if accepted start receiving and
// storing the file.
func handleRequest(ctx context.Context, conn net.Conn, store *TransferStore) {
defer conn.Close()
id, err := reqDecisionAndWait(ctx, store, conn, conn.RemoteAddr())
if err != nil {
clog.Error(err)
return
}
trans := store.Get(id)
clog.Info("writing decision to sender: %v", trans.Status)
if err = protocol.WriteDecision(trans.Status == Accepted, conn); err != nil {
trans.SetError(err)
store.Update(id, trans)
clog.Error(err)
return
}
if trans.Status != Accepted {
clog.Info("transfer rejected %d", id)
return //It was rejected just end the work
}
// The stored file should get updated with the path where to store the file
w, err := file.Open(trans.LocalFilePath, file.OPEN_WRITE)
if err != nil {
clog.Error(err)
return
}
defer func() {
if cErr := w.Close(); cErr != nil {
clog.Error(cErr)
}
}()
clog.Info("receiving file from sender and store at: %s", trans.LocalFilePath)
rcvSize, err := file.Stream(ctx, conn, w, func(transferred int) {
store.UpdateProgress(id, float64(transferred)/float64(trans.FileSize))
})
if err != nil {
trans.SetError(err)
store.Update(id, trans)
clog.Error(err)
return
}
trans.Status = Verifying
store.Update(id, trans)
verifyFile(ctx, trans, rcvSize, id, store)
}
func verifyFile(ctx context.Context, t *Transfer, rcvSize, id int, store *TransferStore) {
f, err := file.Open(t.LocalFilePath, file.OPEN_READ)
if err != nil {
clog.Error(err)
}
defer func() {
if cErr := f.Close(); cErr != nil {
clog.Error(err)
}
}()
if err = verifyTransfer(ctx, t, rcvSize, f); err != nil {
t.SetError(err)
} else {
t.Status = Completed
}
store.Update(id, t)
}
// reqDecisionAndWait will add the transfer to the store and wait for confirmation
// by the user or a cancelation of the context.
func reqDecisionAndWait(ctx context.Context, store *TransferStore, r io.Reader, addr net.Addr) (int, error) {
var rm protocol.RequestMessage
if err := protocol.ReadRequestMessage(&rm, r); err != nil {
clog.Error(err)
return -1, err
}
id, wait := store.AddToWait(NewTransfer(rm.FileName, rm.Checksum, rm.Hostname, rm.FileSize, addr, Download))
clog.Info("waiting for trans: %d", id)
select {
case <-wait:
return id, nil
case <-ctx.Done():
clog.Info("waiting interrupted: %d", id)
return -1, ctx.Err()
}
}
// verifyTransfer will verify is the amount of data transferred matches with
// the amount received and will check with the checkshum also match.
func verifyTransfer(ctx context.Context, trans *Transfer, rcvSize int, in io.Reader) error {
if trans == nil {
return fmt.Errorf("receiver verify transfer error: transfer is nil")
}
if int64(rcvSize) != trans.FileSize {
return fmt.Errorf("receiver verify transfer error: data size don't match")
}
check, err := file.Checksum(ctx, in)
if err != nil {
return err
}
if check != trans.FileChecksum {
return fmt.Errorf("receiver verify transfer error: checksum doesn't match")
}
return nil
}