-
Notifications
You must be signed in to change notification settings - Fork 1
/
pbft.go
94 lines (81 loc) · 2.27 KB
/
pbft.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
package main
import (
"fmt"
"io"
"net/http"
"os"
)
type nodeInfo struct {
id string
path string
writer http.ResponseWriter
}
var nodeTable = make(map[string]string)
func (node *nodeInfo) request(writer http.ResponseWriter, request *http.Request) {
request.ParseForm()
if len(request.Form["warTime"]) > 0 {
node.writer = writer
fmt.Println("", request.Form["warTime"][0])
node.broadcast(request.Form["warTime"][0], "/prePrepare")
}
}
func (node *nodeInfo) broadcast(msg string, path string) {
fmt.Println("", path)
for nodeId, url := range nodeTable {
if nodeId == node.id {
continue
}
http.Get("http://" + url + path + "?warTime=" + msg + "&nodeId=" + node.id)
}
}
func (node *nodeInfo) prePrepare(writer http.ResponseWriter, request *http.Request) {
request.ParseForm()
fmt.Println("", request.Form["warTime"][0])
if len(request.Form["warTime"]) > 0 {
node.broadcast(request.Form["warTime"][0], "/prepare")
}
}
func (node *nodeInfo) prepare(writer http.ResponseWriter, request *http.Request) {
request.ParseForm()
fmt.Println("", request.Form["warTime"][0])
if len(request.Form["warTime"]) > 2/3*len(nodeTable) {
node.authentication(request)
}
}
var authenticationNodeMap = make(map[string]string)
var authenticationSuceess = false
func (node *nodeInfo) authentication(request *http.Request) {
if !authenticationSuceess {
if len(request.Form["nodeId"]) > 0 {
authenticationNodeMap[request.Form["nodeId"][0]] = "OK"
if len(authenticationNodeMap) > len(nodeTable)/3 {
authenticationSuceess = true
node.broadcast(request.Form["warTime"][0], "/commit")
}
}
}
}
func (node *nodeInfo) commit(writer http.ResponseWriter, request *http.Request) {
if writer != nil {
fmt.Println("")
io.WriteString(node.writer, "ok")
}
}
func main() {
userId := os.Args[1]
fmt.Println(userId)
nodeTable = map[string]string{
"Apple": "localhost:1111",
"MS": "localhost:1112",
"Google": "localhost:1113",
"IBM": "localhost:1114",
}
node := nodeInfo{id: userId, path: nodeTable[userId]}
http.HandleFunc("/req", node.request)
http.HandleFunc("/prePrepare", node.prePrepare)
http.HandleFunc("/prepare", node.prepare)
http.HandleFunc("/commit", node.commit)
if err := http.ListenAndServe(node.path, nil); err != nil {
fmt.Println(err)
}
}