-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandler.go
247 lines (193 loc) · 6.59 KB
/
handler.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package main
import (
"bytes"
"encoding/binary"
"fmt"
"io"
"log"
"time"
"github.com/pc-1827/distributed-file-system/p2p"
)
func (s *FileServer) handleMessage(from string, msg *Message) error {
switch v := msg.Payload.(type) {
case MessageStoreFile:
return s.handleMessageStoreFile(from, v)
case MessageGetFile:
return s.handleMessageGetFile(from, v)
case MessageDeleteFile:
return s.handleMessageDeleteFile(v)
case MessageAddFile:
return s.handleMessageAddFile(from, v)
}
return nil
}
type Response struct {
Metadata Metadata
FileSize int64
}
// Checks if the requested file is present or not. If present reads the file,
// and writes the encrypted binary data to the peer message was sent from.
func (s *FileServer) handleMessageGetFile(from string, msg MessageGetFile) error {
fmt.Printf("[%s] MSG.CID: (%s)\n", s.Transport.Addr(), msg.CID)
if !s.store.Has(msg.CID) {
return fmt.Errorf("[%s] file (%s) is not present in the disk", s.Transport.Addr(), msg.CID)
}
metadata, err := s.store.readMetadata(msg.CID)
if err != nil {
return fmt.Errorf("error reading metadata: %s", err)
}
fmt.Printf("[%s] got file part (%d) from the disk, serving over the network (%s)\n", s.Transport.Addr(), metadata.Part, msg.CID)
fileSize, r, err := s.store.Read(msg.CID)
if err != nil {
return err
}
if rc, ok := r.(io.ReadCloser); ok {
defer rc.Close()
}
peer, ok := s.peers[from]
if !ok {
return fmt.Errorf("peer (%s) is not in peer map", from)
}
if !(network.Split) {
keySize := int64(len(metadata.FileName))
peer.Send([]byte{p2p.IncomingStream})
binary.Write(peer, binary.LittleEndian, keySize)
binary.Write(peer, binary.LittleEndian, fileSize)
n, err := io.Copy(peer, bytes.NewReader([]byte(metadata.FileName)))
if err != nil {
return err
}
fmt.Printf("[%s] written %d bytes of key over the network to %s\n", s.Transport.Addr(), n, from)
time.Sleep(5 * time.Millisecond)
n, err = io.Copy(peer, r)
if err != nil {
return err
}
fmt.Printf("[%s] sent file %d bytes over the network to %s\n", s.Transport.Addr(), n, from)
} else {
partNumber := int64(metadata.Part)
// Send partNumber and fileSize using binary.Write
peer.Send([]byte{p2p.IncomingStream})
binary.Write(peer, binary.LittleEndian, partNumber) // Send partNumber
binary.Write(peer, binary.LittleEndian, fileSize) // Send fileSize
// Send the actual file data
n, err := io.Copy(peer, r)
if err != nil {
return err
}
fmt.Printf("[%s] sent file part %d (%d bytes) over the network to %s\n", s.Transport.Addr(), metadata.Part, n, from)
}
return nil
}
// func (s *FileServer) handleMessageGetFile(from string, msg MessageGetFile) error {
// fmt.Printf("[%s] MSG.CID: (%s)\n", s.Transport.Addr(), msg.CID)
// if !s.store.Has(msg.CID) {
// return fmt.Errorf("[%s] file (%s) is not present in the disk", s.Transport.Addr(), msg.CID)
// }
// metadata, err := s.store.readMetadata(msg.CID)
// if err != nil {
// return fmt.Errorf("error reading metadata: %s", err)
// }
// fmt.Printf("[%s] got file part (%d) from the disk, serving over the network (%s)\n", s.Transport.Addr(), metadata.Part, msg.CID)
// fileSize, r, err := s.store.Read(msg.CID)
// if err != nil {
// return err
// }
// keySize := int64(len(metadata.FileName))
// if rc, ok := r.(io.ReadCloser); ok {
// defer rc.Close()
// }
// fmt.Printf("From message: %s\n", from)
// peer, ok := s.peers[from]
// if !ok {
// return fmt.Errorf("peer (%s) is not in peer map", from)
// }
// time.Sleep(100 * time.Millisecond)
// if !(network.Split) {
// peer.Send([]byte{p2p.IncomingStream})
// binary.Write(peer, binary.LittleEndian, fileSize)
// n, err := io.Copy(peer, r)
// if err != nil {
// return err
// }
// fmt.Printf("[%s] sent file %d bytes over the network to %s\n", s.Transport.Addr(), n, from)
// peer.Send([]byte{p2p.IncomingStream})
// binary.Write(peer, binary.LittleEndian, keySize)
// n, err = io.Copy(peer, bytes.NewReader([]byte(metadata.FileName)))
// if err != nil {
// return err
// }
// fmt.Printf("[%s] written %d bytes of key over the network to %s\n", s.Transport.Addr(), n, from)
// } else {
// fmt.Println("PartNumber sent:", metadata.Part)
// partNumber := int64(metadata.Part)
// peer.Send([]byte{p2p.IncomingStream})
// binary.Write(peer, binary.LittleEndian, partNumber)
// time.Sleep(time.Millisecond * 500)
// io.Copy(peer, bytes.NewReader([]byte(string(metadata.Part))))
// fmt.Printf("[%s] sent file part %d over the network to %s\n", s.Transport.Addr(), metadata.Part, from)
// peer.Send([]byte{p2p.IncomingStream})
// binary.Write(peer, binary.LittleEndian, fileSize)
// n, err := io.Copy(peer, r)
// if err != nil {
// return err
// }
// fmt.Printf("[%s] written %d bytes over the network to %s\n", s.Transport.Addr(), n, from)
// }
// return nil
// }
// Gets the peer in the received message and writes the data received to
// the local disk.
func (s *FileServer) handleMessageStoreFile(from string, msg MessageStoreFile) error {
peer, ok := s.peers[from]
if !ok {
return fmt.Errorf("peer (%s) could not be found in the peer list", from)
}
fmt.Println(msg)
n, err := s.store.Write(msg.CID, msg.Key, io.LimitReader(peer, msg.Size), msg.Part, msg.TotalParts)
if err != nil {
return err
}
fmt.Printf("[%s] written part (%d) %d bytes to the disk\n", s.Transport.Addr(), msg.Part, n)
peer.CloseStream()
return nil
}
// Finds the file to delete from the received message and then calls the
// store.Delete function to remove from the local disk
func (s *FileServer) handleMessageDeleteFile(msg MessageDeleteFile) error {
if err := s.store.Delete(msg.CID); err != nil {
return fmt.Errorf("[%s] Failed to delete file (%s) from the disk: ", s.Transport.Addr(), msg.CID)
}
fmt.Printf("[%s] Deleting from the disk\n", s.Transport.Addr())
return nil
}
func (s *FileServer) handleMessageAddFile(from string, msg MessageAddFile) error {
if len(msg.Addr) == 0 {
network.Nodes = msg.Network.Nodes
return nil
}
network = msg.Network
peer, ok := s.peers[from]
if !ok {
return fmt.Errorf("peer not found")
}
fmt.Printf("Peer: %s\n", peer.LocalAddr().String())
peersMap := msg.Network.Nodes
fmt.Printf("PeerMapStr: %s\n", peersMap)
// Dial every peer in the map except the sender and the new peer
for _, addr := range peersMap {
if addr == msg.LocalAddr || addr == msg.Addr {
continue
}
if len(addr) == 0 {
continue
}
fmt.Printf("Address to be dialed %s\n", addr)
go func(addr string) {
if _, err := s.Transport.Dial(addr); err != nil {
log.Printf("Failed to dial %s: %s\n", addr, err)
}
}(addr)
}
return nil
}