-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathmessager.go
73 lines (63 loc) · 1.28 KB
/
messager.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
package paxos
import (
"fmt"
"net/rpc"
"strconv"
)
type PrepareMsg struct {
ProposeID float32
}
type PromiseMsg struct {
AcceptorAddr string
ProposeID float32
Success bool
AccepedID float32
AccepedValue interface{}
}
type AcceptMsg struct {
ProposeID float32
AcceptorAddr string
Value interface{}
}
type AcceptedMsg struct {
ProposeID float32
AcceptorAddr string
Success bool
}
type EmptyMsg struct{}
func callRpc(peerAddr, roleService, method string, arg interface{}, reply interface{}) error {
c, err := rpc.Dial("tcp", peerAddr)
if err != nil {
return err
}
defer c.Close()
err = c.Call(roleService+"."+method, arg, reply)
if err != nil {
return err
}
return nil
}
func generateNumber(me int, number float32) float32 {
var strNum string
if number == 0 {
strNum = fmt.Sprintf("1.%d", me)
n, err := strconv.ParseFloat(strNum, 32)
if err != nil {
panic("error parse num")
}
return float32(n)
}
i := int(number) + 1 // 暂时不考虑float的精度问题
strNum = fmt.Sprintf("%d.%d", i, me)
n, err := strconv.ParseFloat(strNum, 32)
if err != nil {
panic("error parse num")
}
return float32(n)
}
var logLevel = 1
func logPrint(format string, a ...interface{}) {
if logLevel == 1 {
fmt.Printf(format+"\n", a...)
}
}