-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp.go
More file actions
60 lines (56 loc) · 1.44 KB
/
http.go
File metadata and controls
60 lines (56 loc) · 1.44 KB
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
package main
import (
"crypto/rand"
"fmt"
"log"
"math/big"
"net/http"
"net/rpc"
)
// Waiting for node access
func (rf *Raft) getRequest(writer http.ResponseWriter, request *http.Request) {
request.ParseForm()
//http://localhost:8080/req?message=ohmygod
if len(request.Form["message"]) > 0 && rf.currentLeader != "-1" {
message := request.Form["message"][0]
m := new(Message)
m.MsgID = getRandom()
m.Msg = message
//After receiving the message, forward it directly to the leader
fmt.Println("HTTP supervised the message, ready to send to the leader, message ID:", m.MsgID)
port := (nodeTable[rf.currentLeader])
rp, err := rpc.DialHTTP("tcp", "127.0.0.1"+":"+port)
if err != nil {
log.Panic(err)
}
b := false
err = rp.Call("Raft.LeaderReceiveMessage", m, &b)
if err != nil {
log.Panic(err)
}
fmt.Println("Whether the message has been sent to the leader:", b)
writer.Write([]byte("ok!!!"))
}
}
func (rf *Raft) httpListen() {
//Create a GetRequest () callback method
http.HandleFunc("/req", rf.getRequest)
fmt.Println("Monitoring", httpPort, "port")
if err := http.ListenAndServe(":"+httpPort, nil); err != nil {
fmt.Println(err)
return
}
}
// Return a ten -digit random number, as a message IDGIT
func getRandom() int {
x := big.NewInt(10000000000)
for {
result, err := rand.Int(rand.Reader, x)
if err != nil {
log.Panic(err)
}
if result.Int64() > 1000000000 {
return int(result.Int64())
}
}
}