-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
83 lines (64 loc) · 1.53 KB
/
main.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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"time"
"github.com/evgeny-s/go-distributed-storage/p2p"
)
func OnPeer(peer p2p.Peer) error {
peer.Close()
// fmt.Println("doing some logic with the peer outside of TCPTransport")
return nil
}
func makeServer(listenAddr string, nodes ...string) *FileServer {
tcpTransportOpts := p2p.TCPTransportOps{
ListenAddr: listenAddr,
HandshakeFunc: p2p.NOPHandshakeFunc,
Decoder: p2p.DefaultDecoder{},
}
tcpTransport := p2p.NewTCPTransport(tcpTransportOpts)
fileServerOpts := FileServerOpts{
EncKey: newEncryptionKey(),
StorageRoot: listenAddr + "_network",
PathTransformFunc: CASPathTransformFunc,
Transport: tcpTransport,
BootstrapNodes: nodes,
}
s := NewFileServer(fileServerOpts)
tcpTransport.OnPeer = s.OnPeer
return s
}
func main() {
s1 := makeServer(":3001", "")
s2 := makeServer(":4000", ":3001")
s3 := makeServer(":5001", ":3001", ":4000")
go func() {
log.Fatal(s1.Start())
}()
time.Sleep(time.Millisecond * 500)
go func() {
log.Fatal(s2.Start())
}()
time.Sleep(2 * time.Second)
go s3.Start()
time.Sleep(2 * time.Second)
for i := 0; i < 20; i++ {
key := fmt.Sprintf("picture_%d.jpg", i)
data := bytes.NewReader([]byte("my big data file here!"))
s3.Store(key, data)
if err := s3.store.Delete(s3.ID, key); err != nil {
log.Fatal(err)
}
r, err := s3.Get(key)
if err != nil {
log.Fatal(err)
}
b, err := ioutil.ReadAll(r)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
}