2024-01-06 11:33:46 +00:00
|
|
|
package signed
|
|
|
|
|
|
|
|
import (
|
2024-01-29 04:39:40 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
2024-01-30 03:25:21 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
2024-01-06 11:33:46 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
2024-01-29 04:39:40 +00:00
|
|
|
"github.com/vmihailenco/msgpack/v5"
|
2024-01-06 11:33:46 +00:00
|
|
|
)
|
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
type IncomingMessageDataSigned struct {
|
2024-01-30 03:25:21 +00:00
|
|
|
protocol.IncomingMessageData
|
2024-01-29 04:39:40 +00:00
|
|
|
NodeId *encoding.NodeId
|
|
|
|
}
|
|
|
|
|
|
|
|
type IncomingMessageSigned interface {
|
|
|
|
HandleMessage(message IncomingMessageDataSigned) error
|
|
|
|
DecodeMessage(dec *msgpack.Decoder, message IncomingMessageDataSigned) error
|
2024-01-30 03:25:21 +00:00
|
|
|
protocol.HandshakeRequirer
|
2024-01-29 04:39:40 +00:00
|
|
|
}
|
|
|
|
|
2024-01-06 11:33:46 +00:00
|
|
|
var (
|
2024-01-29 04:39:40 +00:00
|
|
|
messageTypes map[int]func() IncomingMessageSigned
|
2024-01-06 11:33:46 +00:00
|
|
|
)
|
|
|
|
|
2024-01-29 06:10:04 +00:00
|
|
|
func RegisterSignedProtocols() {
|
2024-01-29 04:39:40 +00:00
|
|
|
messageTypes = make(map[int]func() IncomingMessageSigned)
|
2024-01-06 11:33:46 +00:00
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
RegisterMessageType(int(types.ProtocolMethodHandshakeDone), func() IncomingMessageSigned {
|
2024-01-06 11:33:46 +00:00
|
|
|
return NewHandshakeDone()
|
|
|
|
})
|
2024-01-29 04:39:40 +00:00
|
|
|
RegisterMessageType(int(types.ProtocolMethodAnnouncePeers), func() IncomingMessageSigned {
|
2024-01-06 17:51:38 +00:00
|
|
|
return NewAnnouncePeers()
|
|
|
|
})
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
func RegisterMessageType(messageType int, factoryFunc func() IncomingMessageSigned) {
|
2024-01-06 11:33:46 +00:00
|
|
|
if factoryFunc == nil {
|
|
|
|
panic("factoryFunc cannot be nil")
|
|
|
|
}
|
2024-01-09 18:57:35 +00:00
|
|
|
messageTypes[messageType] = factoryFunc
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 04:39:40 +00:00
|
|
|
func GetMessageType(kind int) (IncomingMessageSigned, bool) {
|
2024-01-09 18:57:35 +00:00
|
|
|
value, ok := messageTypes[kind]
|
2024-01-06 11:33:46 +00:00
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2024-01-09 18:57:35 +00:00
|
|
|
return value(), true
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|