-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.go
More file actions
61 lines (50 loc) · 986 Bytes
/
server.go
File metadata and controls
61 lines (50 loc) · 986 Bytes
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
package main
import (
"DistributedFileStorage/p2p"
"fmt"
"log"
)
type FileServerOpts struct {
StorageRoot string
PathTransformFunc PathTransformFunc
Transport p2p.Transport
}
type FileServer struct {
FileServerOpts
store *Store
quitch chan struct{}
}
func NewFileServer(opts FileServerOpts) *FileServer {
storeOpts := StoreOpts{
Root: opts.StorageRoot,
PathTransformFunc: opts.PathTransformFunc,
}
return &FileServer{
FileServerOpts: opts,
store: NewStore(storeOpts),
quitch: make(chan struct{}),
}
}
func (s *FileServer) Stop() {
close(s.quitch)
}
func (s *FileServer) loop() {
defer func() {
log.Println("file server stopped due to user quit action")
}
for {
select {
case msg := <-s.Transport.Consume():
fmt.Println(msg)
case <-s.quitch:
return
}
}
}
func (s *FileServer) Start() error {
if err := s.Transport.ListenAndAccept(); err != nil {
return err
}
s.loop()
return nil
}