-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
87 lines (76 loc) · 1.93 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
package main
import (
"flag"
"fmt"
"strconv"
"strings"
"github.com/science-engineering-art/kademlia-grpc/core"
"github.com/science-engineering-art/kademlia-grpc/structs"
"github.com/science-engineering-art/kademlia-grpc/utils"
"gopkg.in/readline.v1"
)
var fullNode core.FullNode
var grpcServerAddress string
func main() {
// Init CLI for using Full Node Methods
rl, err := readline.New("> ")
if err != nil {
panic(err)
}
defer rl.Close()
for {
line, err := rl.Readline()
if err != nil { // io.EOF, readline.ErrInterrupt
break
}
input := strings.Split(line, " ")
switch input[0] {
case "node":
if len(input) != 4 {
utils.DisplayHelp()
continue
}
port, _ := strconv.Atoi(input[1])
bPort, _ := strconv.Atoi(input[2])
isB, _ := strconv.ParseBool(input[3])
flag.Parse()
storage := structs.NewStorage()
ip := utils.GetIpFromHost()
grpcServerAddress = ip + ":" + strconv.FormatInt(int64(port), 10)
fullNode = *core.NewFullNode(ip, port, bPort, storage, isB)
go fullNode.CreateGRPCServer(grpcServerAddress)
//fmt.Println("Node running at:", ip, ":", port)
case "store":
if len(input) != 3 {
utils.DisplayHelp()
continue
}
key := input[1]
data := []byte(input[2])
id, err := fullNode.StoreValue(key, &data)
if err != nil {
fmt.Println(err.Error())
}
fmt.Println("Stored with ID: ", id)
case "get":
if len(input) != 4 {
utils.DisplayHelp()
continue
}
key := input[1]
start, _ := strconv.ParseInt(input[2], 10, 32)
end, _ := strconv.ParseInt(input[3], 10, 32)
value, err := fullNode.GetValue(key, int64(start), int64(end))
if err != nil {
//fmt.Println(err.Error())
}
if value == nil || (value != nil && len(value) == 0) {
//fmt.Println("There is no value in the network for that key")
} else {
fmt.Println("The retrived value is:", string(value))
}
case "dht":
fullNode.PrintRoutingTable()
}
}
}