forked from scionproto/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
129 lines (115 loc) · 3.95 KB
/
events.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
// Copyright 2018 ETH Zurich
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package base
import (
"net"
"sync"
"github.com/scionproto/scion/go/lib/addr"
"github.com/scionproto/scion/go/sig/siginfo"
)
type (
NetworkChangedCb func(NetworkChangedParams)
RemoteHealthChangedCb func(RemoteHealthChangedParams)
SigChangedCb func(SigChangedParams)
)
// NetworkChangedParams contains the parameters that are passed along with a NetworkChanged event.
type NetworkChangedParams struct {
// RemoteIA is the remote IA for which network information changed.
RemoteIA addr.IA
// IpNet contains the network prefix that was added/removed.
IpNet net.IPNet
// Healthy is true if the remote IA can be reached.
Healthy bool
// Added is true if the prefix was added, false otherwise.
Added bool
}
// SigChangedParams contains the parameters that are passed along with a SigChanged event.
type SigChangedParams struct {
// RemoteIA is the IA for which SIG information changed.
RemoteIA addr.IA
// Id is the id of the SIG.
Id siginfo.SigIdType
// Host is the host address of the SIG.
Host addr.HostAddr
// CtrlPort is the port on which the SIG accepts control traffic.
CtrlPort int
// EncapPort is the port on which the SIG accepts encapsulated SIG-SIG frames.
EncapPort int
// Static is true, if this SIG was statically configured and false if it was discovered
// dynamically.
Static bool
// Added is true if the SIG was added, false otherwise.
Added bool
}
// RemoteHealthChangedParams contains the parameters that are passed along with a
// RemoteHealthChanged event.
type RemoteHealthChangedParams struct {
// RemoteIA is the IA for which the reachability status changed.
RemoteIA addr.IA
// Nets contains all network prefixes the remote IA announced.
Nets []*net.IPNet
// Healthy is true if the remote IA is reachable, false otherwise.
Healthy bool
}
// EventCallbacks can be used by a listener to register for certain events by setting the
// corresponding function pointer in the struct. Note, that the callback MUST NOT BLOCK. Long
// running or potentially blocking operations should be executed in a separate go-routine.
type EventCallbacks struct {
// NetworkChanged is called when a remote network was added or removed from the configuration.
NetworkChanged NetworkChangedCb
// SigChanged is called when a remote SIG is was added or removed from the configuration.
// TODO(shitz): This event does not get generated yet.
SigChanged SigChangedCb
// RemoteHealthChanged is called when the reachability status of a remote AS changed.
RemoteHealthChanged RemoteHealthChangedCb
}
var lock sync.RWMutex
var listeners = make(map[string]EventCallbacks)
func AddEventListener(listenerName string, cbs EventCallbacks) {
lock.Lock()
defer lock.Unlock()
listeners[listenerName] = cbs
}
func RemoveEventListener(listenerName string) {
lock.Lock()
defer lock.Unlock()
delete(listeners, listenerName)
}
func NetworkChanged(params NetworkChangedParams) {
lock.RLock()
defer lock.RUnlock()
for _, cbs := range listeners {
if cbs.NetworkChanged != nil {
cbs.NetworkChanged(params)
}
}
}
func RemoteHealthChanged(params RemoteHealthChangedParams) {
lock.RLock()
defer lock.RUnlock()
for _, cbs := range listeners {
if cbs.RemoteHealthChanged != nil {
cbs.RemoteHealthChanged(params)
}
}
}
func SigChanged(params SigChangedParams) {
lock.RLock()
defer lock.RUnlock()
for _, cbs := range listeners {
if cbs.SigChanged != nil {
cbs.SigChanged(params)
}
}
}