-
Notifications
You must be signed in to change notification settings - Fork 2
/
peer.go
69 lines (57 loc) · 1.21 KB
/
peer.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
package raft
import (
"fmt"
"net"
)
type peer struct {
Host string
Port int
}
func (p *peer) String() string {
return fmt.Sprintf("%s:%d", p.Host, p.Port)
}
func (p *peer) Equals(other peer) bool {
return p.Host == other.Host && p.Port == other.Port
}
func (p *peer) SendMessage(msg message) error {
raddr, err := net.ResolveUDPAddr("udp", p.String())
if err != nil {
return err
}
conn, err := net.DialUDP("udp", nil, raddr)
if err != nil {
return err
}
defer conn.Close()
_, err = conn.Write(msg.Json())
return err
}
type Peers []peer
func (peers *Peers) Contains(peer peer) bool {
for _, p := range *peers {
if p.Host == peer.Host && p.Port == peer.Port {
return true
}
}
return false
}
func (peers *Peers) PrintInfo() {
fmt.Println("Connected Peers:")
for _, p := range *peers {
fmt.Printf("%s: %d\n", p.Host, p.Port)
}
}
func (peers *Peers) BroadcastMessage(msg message) (err error) {
failedPeers := make([]int, 0)
for i, p := range *peers {
err = p.SendMessage(msg)
if err != nil {
fmt.Printf("Sending message to peer %s failed: %v\n", p.String(), err)
failedPeers = append(failedPeers, i)
}
}
return err
}
func (peers *Peers) Quorum() int {
return (len(*peers)+1)/2 + 1
}