-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
76 lines (64 loc) · 1.7 KB
/
Copy pathserver.go
File metadata and controls
76 lines (64 loc) · 1.7 KB
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
package main
import (
"bytes"
"encoding/gob"
"fmt"
"github.com/girish/storage/p2p"
"github.com/girish/storage/store"
)
type FileServerOpts struct {
StorageRoot string //StorageRoot is the folder on hard drive where this node will save files
Transport p2p.Transport
Store *store.Store
}
// DataMessage is wire protocol payload!
type DataMessage struct {
Key string
Data []byte
}
type FileServer struct {
FileServerOpts
quitCh chan struct{}
}
func NewFileServer(opts FileServerOpts) *FileServer {
return &FileServer{
FileServerOpts: opts,
quitCh: make(chan struct{}),
}
}
// Start boots up the transport layer and begins processing messages
func (s *FileServer) Start() error {
fmt.Printf("Starting File Server... (Storage Root: %s)\n", s.StorageRoot)
if err := s.Transport.ListenAndAccept(); err != nil {
return err
}
s.loop()
return nil
}
func (s *FileServer) loop() {
for {
select {
case rpc := <-s.Transport.Consume():
s.handleMessage(rpc)
case <-s.quitCh:
fmt.Println("File Server shutting down")
return
}
}
}
func (s *FileServer) handleMessage(rpc p2p.RPC) {
//1 Decode the binary network payload back into Structured DataMessage
var msg DataMessage
if err := gob.NewDecoder(bytes.NewReader(rpc.Payload)).Decode(&msg); err != nil {
fmt.Printf("Failed to decode network payload: %s\n", err)
return
}
fmt.Printf("FileServer received command to store file: '%s' (%d bytes)\n", msg.Key, len(msg.Data))
//2 Write the file to disk using CAS store engine
err := s.Store.WriteStream(msg.Key, bytes.NewReader(msg.Data))
if err != nil {
fmt.Printf("Error Storing file to disk: %s\n", err)
return
}
fmt.Println("File successfully saved from the network!")
}