-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
116 lines (88 loc) · 2.16 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
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
package main
import (
"bytes"
"fmt"
"io/ioutil"
"log"
"time"
"github.com/amanbhardwaj12072003/Distributed_File_Storage/p2p"
)
func makeServer(listenAddr string, nodes ...string) *FileServer {
tcpTransportOpts := p2p.TCPTransportOpts{
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() {
server_1 := makeServer(":3000", "")
server_2 := makeServer(":3001", "")
server_3 := makeServer(":3002", ":3000", ":3001")
go func() { log.Fatal(server_1.Start()) }()
time.Sleep(2 * time.Second)
go func() { log.Fatal(server_2.Start()) }()
time.Sleep(2 * time.Second)
go server_3.Start()
time.Sleep(2 * time.Second)
for i := 0; i < 10; i++ {
key := fmt.Sprintf("cowpicture_%d.jpg", i)
// key := "cowpicture.jpg"
data := bytes.NewReader([]byte("my big data file here!"))
server_3.Store(key, data)
if err := server_3.store.Delete(server_3.ID, key); err != nil {
log.Fatal(err)
}
r, err := server_3.Get(key)
if err != nil {
log.Fatal(err)
}
b, err := ioutil.ReadAll(r)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
}
// */
/*
func main() {
server_1 := makeServer(":3000", "")
server_2 := makeServer(":4000", ":3000")
go func() {
log.Fatal(server_1.Start())
}()
time.Sleep(1 * time.Second)
go server_2.Start()
time.Sleep(1 * time.Second)
for i := 0; i < 10; i++ {
key := fmt.Sprintf("catspicture_%d.jpg", i)
// key := "catspicture.jpg"
data := bytes.NewReader([]byte("my big data file here!"))
server_2.Store(key, data)
if err := server_2.store.Delete(server_2.ID, key); err != nil {
log.Fatal(err)
}
r, err := server_2.Get(key)
if err != nil {
log.Fatal(err)
}
b, err := ioutil.ReadAll(r)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(b))
}
}
*/