-
Notifications
You must be signed in to change notification settings - Fork 2
/
monitor.go
155 lines (140 loc) · 3.61 KB
/
monitor.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
154
155
package peer
import (
"fmt"
"math"
"net"
"strconv"
"strings"
"github.com/golang/glog"
)
type PeerState uint8
func (ps PeerState) String() string {
if ps == PeerUp {
return "UP"
}
return "DOWN"
}
const (
PeerUp PeerState = iota + 1
PeerDown
)
type MonitorMessage struct {
RemotePeer string
PeerState PeerState
}
type Monitor interface {
// GetPeerState(string) PeerState
GetMonitorCh() chan *MonitorMessage
Stop()
}
var _ Monitor = &monitor{}
type monitor struct {
stop chan struct{}
monitorCh chan *MonitorMessage
monitorChActive bool
}
func (m *monitor) Stop() {
glog.Infof("Closing monitor...")
m.stop <- struct{}{}
<-m.stop
glog.Infof("monitor closed ..")
}
func (m *monitor) GetMonitorCh() chan *MonitorMessage {
m.monitorChActive = true
return m.monitorCh
}
func (m *monitor) manager(la string, port int, rPeers ...string) {
peers := make(map[string]Peer)
peersStateCh := make(chan *SessionState)
for _, p := range rPeers {
rp, err := NewPeer(la, port, p, peersStateCh)
if err != nil {
glog.Errorf("failed creating remote peer %s with error: %+v", p, err)
continue
}
peers[rp.GetRemoteAddr()] = rp
rp.Start()
}
for {
select {
case <-m.stop:
for _, p := range peers {
glog.Infof("Closing keepalive session with %s", p.GetRemoteAddr())
p.Stop()
}
close(m.stop)
return
case msg := <-peersStateCh:
glog.Infof("Peer: %s state changed to %s", msg.RemotePeer, msg.PeerState)
}
}
}
// SetupMonitorForRemotePeer sets up a keep alive mechanism between a local and remote peers,
// returned Monitor interface allows get a state of a particular peer by specifying its id, or get
// a notification channel for changes of a remote peer states.
func SetupMonitorForRemotePeer(la string, port int, rPeers ...string) (Monitor, error) {
// If number of remote peers is 0, return an error as nothing to do
if len(rPeers) == 0 {
return nil, fmt.Errorf("no remote peers specified")
}
// Validate the port to use for listening of Keepalive messages
if port >= math.MaxUint16 {
return nil, fmt.Errorf("invalid value %d for the port", port)
}
// Validate the list of remote peers IPs to be a valid IP addresses and
// to belong to the same address family, ipv4 or ipv6. Mixed remote peers are not supported
_, _, isIPv4, err := validateIPAddress(la)
if err != nil {
return nil, err
}
for _, rp := range rPeers {
_, _, ipv4, err := validateIPAddress(rp)
if err != nil {
return nil, err
}
if !isIPv4 && ipv4 {
return nil, fmt.Errorf("remote peer %s and local peer %s belong to different address families, it is not supported", la, rp)
}
}
m := &monitor{
stop: make(chan struct{}),
monitorCh: make(chan *MonitorMessage),
monitorChActive: false,
}
go m.manager(la, port, rPeers...)
return m, nil
}
func validateIPAddress(route string) (string, int, bool, error) {
// Validating route
p := strings.Split(route, "/")
var prfx string
var prfxLength string
switch len(p) {
case 1:
prfx = p[0]
case 2:
prfx = p[0]
prfxLength = p[1]
default:
return "", 0, false, fmt.Errorf("invalid format")
}
addr := net.ParseIP(prfx)
if addr.To16() == nil && addr.To4() == nil {
return "", 0, false, fmt.Errorf("invalid format of a prefix %s", prfx)
}
isIPv4 := false
if addr.To4() != nil {
isIPv4 = true
}
if prfxLength == "" {
prfxLength = "0"
}
l, err := strconv.Atoi(prfxLength)
if err != nil {
return "", 0, false, fmt.Errorf("invalid prefix length")
}
if (l > 128 && !isIPv4) || (l > 32 && isIPv4) {
return "", 0, false, fmt.Errorf("invalid prefix length %d value for prefix %s", l, prfx)
}
return prfx, l, isIPv4, nil
}