-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
61 lines (47 loc) · 1.18 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
package main
import (
"fmt"
"os"
"time"
"peer/config"
"peer/handlers"
"peer/network"
"peer/utils"
)
func main() {
cfg := config.ParseFlags()
time.Sleep(time.Duration(cfg.InitialDelay) * time.Second)
me, err := os.Hostname()
if err != nil {
fmt.Println("Error getting hostname:", err)
os.Exit(1)
}
peers, err := utils.GetPeers(cfg.HostsFile)
if err != nil {
fmt.Println("Error reading peers:", err)
os.Exit(1)
}
peerNameToId := utils.GetPeerNameToIdMapping(peers)
peerIdToName := utils.GetPeerIdToNameMapping(peers)
leaderName, err := utils.GetLeader(peers)
if err != nil {
fmt.Println("Error determining leader:", err)
os.Exit(1)
}
p := network.NewPeer(me, leaderName, peerNameToId, peerIdToName, peers)
if cfg.CrashDelay > 0 {
go func() {
time.Sleep(time.Duration(cfg.CrashDelay) * time.Second)
fmt.Printf("{peer_id:%d, view_id: %d, leader: %d, message:\"crashing\"}\n",
p.PeerNameToId[me], p.ViewId.Get(), p.PeerNameToId[p.Leader.Get()])
os.Exit(1)
}()
}
p.Start()
// Handle Test Case 4 simulation
if cfg.Crash {
p.SimulateTestCase4()
}
messageHandler := handlers.NewMessageHandler(p)
messageHandler.HandleMessages(cfg.Crash)
}