-
Notifications
You must be signed in to change notification settings - Fork 210
/
service.go
94 lines (79 loc) · 2.58 KB
/
service.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
// Package service defines basic interfaces to for protocols to consume p2p functionality.
package service
import (
"github.com/spacemeshos/go-spacemesh/p2p/p2pcrypto"
"github.com/spacemeshos/go-spacemesh/priorityq"
"net"
)
// MessageValidation is a gossip message validation event.
type MessageValidation struct {
sender p2pcrypto.PublicKey
msg []byte
prot string
}
// Message returns the message as bytes
func (mv MessageValidation) Message() []byte {
return mv.msg
}
// Sender returns the public key of the sender of this message. (might not be the author)
func (mv MessageValidation) Sender() p2pcrypto.PublicKey {
return mv.sender
}
// Protocol is the protocol this message is targeted to.
func (mv MessageValidation) Protocol() string {
return mv.prot
}
// P2PMetadata is a generic metadata interface
type P2PMetadata struct {
FromAddress net.Addr
// add here more fields that are needed by protocols
}
// NewMessageValidation creates a message validation struct to pass to the protocol.
func NewMessageValidation(sender p2pcrypto.PublicKey, msg []byte, prot string) MessageValidation {
return MessageValidation{sender, msg, prot}
}
// DirectMessage is an interface that represents a simple direct message structure
type DirectMessage interface {
Metadata() P2PMetadata
Sender() p2pcrypto.PublicKey
Bytes() []byte
}
// GossipMessage is an interface that represents a simple gossip message structure
type GossipMessage interface {
Sender() p2pcrypto.PublicKey
Bytes() []byte
ValidationCompletedChan() chan MessageValidation
ReportValidation(protocol string)
}
// Service is an interface that represents a networking service (ideally p2p) that we can use to send messages or listen to incoming messages
type Service interface {
Start() error
RegisterGossipProtocol(protocol string, prio priorityq.Priority) chan GossipMessage
RegisterDirectProtocol(protocol string) chan DirectMessage
SubscribePeerEvents() (new chan p2pcrypto.PublicKey, del chan p2pcrypto.PublicKey)
Broadcast(protocol string, payload []byte) error
Shutdown()
}
// Data is a wrapper around a message that can hold either raw bytes message or a req-res wrapper.
type Data interface {
Bytes() []byte
}
// DataBytes is a byte array payload wrapper.
type DataBytes struct {
Payload []byte
}
// DataMsgWrapper is a req-res payload wrapper
type DataMsgWrapper struct {
Req bool
MsgType uint32
ReqID uint64
Payload []byte
}
// Bytes returns the message as bytes
func (m DataBytes) Bytes() []byte {
return m.Payload
}
// Bytes returns the message as bytes
func (m DataMsgWrapper) Bytes() []byte {
return m.Payload
}