-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·153 lines (139 loc) · 4.03 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main
import (
"./client"
"./datastruc"
"./server"
"bufio"
"crypto/ecdsa"
"crypto/elliptic"
crand "crypto/rand"
"encoding/gob"
"fmt"
"log"
"net"
"os"
"time"
)
// Get preferred outbound ip of this machine
func GetOutboundIP() net.IP {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
log.Fatal(err)
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP
}
func ReadAllIps(fn string) []string {
fmt.Println("read all ips")
file, err := os.Open(fn)
if err != nil {
log.Panic("error")
}
res := make([]string, 0)
scanner := bufio.NewScanner(file)
for scanner.Scan() {
// fmt.Println(scanner.Text())
res = append(res, scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
return res
}
func DetermineId(allips []string, localip string) int {
for i, ip := range allips {
if ip == localip {
return i
}
}
return -1
}
func RecordClientKeys(clk *client.ClienKeys) {
file, err := os.OpenFile("clientkeys", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0666)
if err != nil {
fmt.Println("create clientkey file error")
}
enc := gob.NewEncoder(file)
err = enc.Encode(clk.GetSerialize())
if err != nil {
fmt.Println("write config to file error")
}
}
func ReadClientKeys(fn string) client.ClienKeys {
ck := client.ClienKeys{}
ck.GetDeserializeFromFile(fn)
return ck
}
func main() {
phaselen := 30
var tmp string
tmp = "main"
if tmp == "clientkey" {
// ***************************************generate client keys and save
ck := client.ClienKeys{}
ck.Clienprivks = make(map[int]string)
ck.Clientpubkstrs = make(map[int]string)
for i := 0; i < 20000; i++ {
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), crand.Reader)
if err != nil {
log.Fatalln(err)
}
ck.Clienprivks[i] = datastruc.EncodePrivate(privateKey)
pubkey := &privateKey.PublicKey
ck.Clientpubkstrs[i] = datastruc.EncodePublic(pubkey)
}
RecordClientKeys(&ck)
// ***************************************generate client keys and save
} else if tmp == "main" {
fmt.Println("add account_hash_generation_time, backup")
fmt.Println("Get the cluster IPs from IpAddr.txt")
fmt.Println("Get client keys from clientkeys")
localip := GetOutboundIP().String()
fmt.Println("local ip: ", localip)
allips := ReadAllIps("IpAddr.txt")
fmt.Println("all ips: ")
for _, x := range allips {
fmt.Println(x)
}
localid := DetermineId(allips, localip)
fmt.Println("local id is", localid, "\n")
clientnumber := 1
instanceoneachserver := 1
initialserver := 4
lateserver := 0 // mechanism1
totalserver := initialserver + lateserver
// read client pubkeys
ck := ReadClientKeys("clientkeys")
if localid < initialserver {
// invoke two server
for i := 0; i < instanceoneachserver; i++ {
instanceid := i + instanceoneachserver*localid
theserver := server.CreateServer(instanceid, localip, ck.Clientpubkstrs, allips[0:initialserver], instanceoneachserver)
go theserver.Start()
fmt.Println("server", instanceid, "starts")
}
} else if localid >= initialserver && localid < totalserver {
for i := 0; i < instanceoneachserver; i++ {
instanceid := i + instanceoneachserver*localid
theserver := server.CreateLateServer(instanceid, localip, ck.Clientpubkstrs, allips[0:initialserver], instanceoneachserver)
starttime := phaselen*(localid-initialserver+1) + phaselen
go theserver.LateStart(ck.Clientpubkstrs, starttime) // new nodes join serially
fmt.Println("server", instanceid, "starts, it is a late server, will start at", starttime, "s")
}
} else {
//invoke clients
for i := 0; i < clientnumber; i++ {
privatekey := datastruc.DecodePrivate(ck.Clienprivks[i])
theclient := client.CreateClient(i, totalserver*instanceoneachserver, privatekey, allips[0:totalserver], instanceoneachserver)
go theclient.Run()
fmt.Println("the ", i, "client starts")
}
}
time.Sleep(time.Second * time.Duration(5+5*phaselen))
//time.Sleep(time.Second * 30)
} else {
fmt.Printf("tmp is ", tmp)
}
fmt.Println("main thread completes")
}