-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
127 lines (102 loc) · 2.79 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
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"bufio"
"flag"
"fmt"
"net"
"os"
"strconv"
"strings"
"time"
)
func main() {
port := flag.String("port", ":12345", "rpc raft port")
mport := flag.String("mport", ":9091", "server listen port")
cluster := flag.String("cluster", "127.0.0.1:9091", "comma sep")
id := flag.Int("id", 1001, "node ID")
flag.Parse()
fmt.Printf("clusters %v \n", *cluster)
clusters := strings.Split(*cluster, ",")
ns := make(map[int]*node)
for k, v := range clusters {
ns[k] = newNode(v)
}
raft := &Raft{}
raft.me = *id
raft.nodes = ns
PORT := *mport
l, err := net.Listen("tcp", PORT)
if err != nil {
fmt.Println(err)
return
}
defer l.Close()
raft.rpc(*port)
time.Sleep(1 * time.Second)
raft.start()
time.Sleep(1 * time.Second)
for {
c, err := l.Accept()
if err != nil {
fmt.Println(err)
continue
}
netData, err := bufio.NewReader(c).ReadString('\n')
if err != nil {
fmt.Println(err)
continue
}
msg := strings.TrimSpace(string(netData))
clientMsgArr := strings.Split(msg, "$")
opt, clientMsg, clientName := clientMsgArr[0], clientMsgArr[1], clientMsgArr[2]
var idx int
t := time.Now()
if opt == "W" {
idx = WriteEntry(raft, clientName, clientMsg)
if idx == -1 {
myMsg := t.Format(time.RFC3339) + " :: error: server is not a Leader" + "\n"
c.Write([]byte(myMsg))
} else {
myMsg := t.Format(time.RFC3339) + " :: " + fmt.Sprintf("stored at log index %d", idx) + "\n"
c.Write([]byte(myMsg))
}
} else if opt == "R" {
idx, _ = strconv.Atoi(clientMsg)
logMsg := ReadEntry(raft, idx)
myMsg := t.Format(time.RFC3339) + " :: " + fmt.Sprintf("log at index %d is %s", idx, logMsg) + "\n"
c.Write([]byte(myMsg))
} else if opt == "GET" { // get all entries by that particular client
logMsgs := GetEntries(raft, clientName)
myMsg := t.Format(time.RFC3339) + " :: " + fmt.Sprintf("log entries by %s are %s", clientName, logMsgs) + "\n"
c.Write([]byte(myMsg))
} else if opt == "GETLOG" {
logMsgs := GetAllEntries(raft)
myMsg := t.Format(time.RFC3339) + " :: " + fmt.Sprintf("all log entries : %s", logMsgs) + "\n"
c.Write([]byte(myMsg))
} else if opt == "STOP" {
// save the log
logArr := GetAllEntriesArray(raft)
if len(logArr) > 0 {
filename := "log-id-" + strconv.Itoa(*id) + "-" + t.Format(time.RFC3339) + ".txt"
f, err := os.Create(filename)
if err != nil {
fmt.Println("error: cannot create " + filename)
}
defer f.Close()
for _, log := range logArr {
_, err := f.WriteString(log + "\n")
if err != nil {
fmt.Println("error: cannot Write " + log)
}
}
}
fmt.Println("Stopping TCP server!")
myMsg := "server stopped" + "\n"
c.Write([]byte(myMsg))
return
} else {
myMsg := "error :: wrong command"
c.Write([]byte(myMsg))
}
}
}