-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
208 lines (172 loc) · 4.07 KB
/
server.go
File metadata and controls
208 lines (172 loc) · 4.07 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
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
package main
import (
"bytes"
"dist-store/p2p"
"encoding/gob"
"errors"
"io"
"log/slog"
"sync"
)
type ServerOpts struct {
ListenAddr string
StorageRoot string
PathTransformFunc PathTransformFunc
Transport p2p.Transport
BootstrapNodes []string
}
type Server struct {
ServerOpts
store *Store
quitch chan struct{}
peerLock sync.Mutex
peer map[string]p2p.Peer
}
func (s *Server) Stop() {
close(s.quitch)
}
func (s *Server) loop() {
//TODO: have a defer func to do clean up
for {
select {
case msg := <-s.Transport.Consume():
var p Payload
slog.Debug("RPC", "msg", msg)
if err := gob.NewDecoder(bytes.NewReader(msg.Payload)).Decode(&p); err != nil {
slog.Error("Error decoding Payload", err)
return
}
if err := s.handlePayload(&p, msg.From.String()); err != nil {
slog.Error("Error handling payload", err)
}
case <-s.quitch:
return
}
}
}
func (s *Server) handlePayload(p *Payload, from string) error {
//get the peer for stream logic
peer, ok := s.peer[from]
if !ok {
return errors.New("Unknown peer with addr" + from)
}
switch p.Action {
case Save:
slog.Debug("Started to receive payload stream from ", "peer", from, "size", p.Size)
s.StoreData(p.Key, io.LimitReader(peer, p.Size))
peer.StopStream()
slog.Debug("Stopped to receive payload stream from ", "peer", from)
case Delete:
//TODO: delete a file
default:
slog.Warn("Unknown Request with ", "action", p.Action)
}
return nil
}
func (s *Server) bootStrapNetwork() error {
for _, addr := range s.BootstrapNodes {
//return if no bootstrap nodes configured
if len(addr) == 0 {
continue
}
go func(addr string) {
if err := s.Transport.Dial(addr); err != nil {
slog.Error("Error dialing addr", err)
}
}(addr)
}
return nil
}
func (s *Server) Broadcast(p *Payload) error {
peers := []io.Writer{}
buf := new(bytes.Buffer)
for _, peer := range s.peer {
if peer.IsOutbound() {
peers = append(peers, peer)
}
}
if len(peers) == 0 {
return nil
}
//using the multi writer
mw := io.MultiWriter(peers...)
//broad cast the payload
mw.Write([]byte{p2p.NonStreamingMode})
err := gob.NewEncoder(buf).Encode(p)
if err != nil {
slog.Warn("Error Encoding Payload", err)
return err
}
n, err := mw.Write(buf.Bytes())
slog.Info("Broadcasted to peers", "source", len(buf.Bytes()), "sent", n)
return err
}
func (s *Server) StoreData(key string, r io.Reader) error {
buf := new(bytes.Buffer)
tee := io.TeeReader(r, buf)
//store the key to the current node
if err := s.store.Write(key, tee); err != nil {
return err
}
p := Payload{
Action: Save,
Key: key,
Size: int64(buf.Len()),
}
slog.Debug("created payload", "payload", p)
//share stuff with other nodes
err := s.Broadcast(&p)
if err != nil {
slog.Error("Error sending payload", err)
}
for _, peer := range s.peer {
if peer.IsOutbound() {
slog.Debug("Started writing to peers", "data", buf.Bytes())
_, err := peer.Write([]byte{p2p.StreamingMode})
if err != nil {
slog.Error("Error Writing Stream Mode", err)
return err
}
n, err := peer.Write(buf.Bytes())
if err != nil {
slog.Debug("err", err)
return err
}
slog.Info("Wrote to peers", "bytes", n)
}
}
return err
}
func (s *Server) OnPeer(p p2p.Peer) error {
s.peerLock.Lock()
defer s.peerLock.Unlock()
s.peer[p.RemoteAddr().String()] = p
slog.Info("New Peer Added", "Addr", p.RemoteAddr().String())
return nil
}
// starts the server with the transport specifies in server opts
func (s *Server) Start() error {
if err := s.Transport.ListenAndAccept(); err != nil {
return err
}
s.bootStrapNetwork()
//this blocks when the Start is called
s.loop()
return nil
}
func NewServer(opts ServerOpts) *Server {
storeOpts := StorageOpts{
PathTransformFunc: opts.PathTransformFunc,
RootDir: opts.StorageRoot,
}
return &Server{
ServerOpts: opts,
store: NewStore(storeOpts),
quitch: make(chan struct{}),
peer: make(map[string]p2p.Peer),
}
}
// // for gob initialization
// func init() {
// gob.Register(StoreDataPayload{})
// }