/
events.go
51 lines (43 loc) · 1.58 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
package gossip
import (
"github.com/iotaledger/hive.go/autopeering/peer"
"github.com/iotaledger/hive.go/events"
)
// Events defines all the events related to the gossip protocol.
type Events struct {
// Fired when a new message was received via the gossip protocol.
MessageReceived *events.Event
}
// NeighborsEvents is a collection of events specific for a particular neighbors group, e.g "manual" or "auto".
type NeighborsEvents struct {
// Fired when an attempt to build a connection to a neighbor has failed.
ConnectionFailed *events.Event
// Fired when a neighbor connection has been established.
NeighborAdded *events.Event
// Fired when a neighbor has been removed.
NeighborRemoved *events.Event
}
// NewNeighborsEvents returns a new instance of NeighborsEvents.
func NewNeighborsEvents() NeighborsEvents {
return NeighborsEvents{
ConnectionFailed: events.NewEvent(peerAndErrorCaller),
NeighborAdded: events.NewEvent(neighborCaller),
NeighborRemoved: events.NewEvent(neighborCaller),
}
}
// MessageReceivedEvent holds data about a message received event.
type MessageReceivedEvent struct {
// The raw message.
Data []byte
// The sender of the message.
Peer *peer.Peer
}
func peerAndErrorCaller(handler interface{}, params ...interface{}) {
handler.(func(*peer.Peer, error))(params[0].(*peer.Peer), params[1].(error))
}
func neighborCaller(handler interface{}, params ...interface{}) {
handler.(func(*Neighbor))(params[0].(*Neighbor))
}
func messageReceived(handler interface{}, params ...interface{}) {
handler.(func(*MessageReceivedEvent))(params[0].(*MessageReceivedEvent))
}