2024-01-06 11:33:46 +00:00
|
|
|
package signed
|
|
|
|
|
|
|
|
import (
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
messageTypes sync.Map
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
_ IncomingMessage = (*IncomingMessageImpl)(nil)
|
|
|
|
)
|
|
|
|
|
|
|
|
type IncomingMessage interface {
|
|
|
|
protocol.IncomingMessage
|
|
|
|
}
|
|
|
|
|
|
|
|
type IncomingMessageImpl struct {
|
|
|
|
protocol.IncomingMessageImpl
|
|
|
|
message []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
messageTypes = sync.Map{}
|
|
|
|
|
|
|
|
RegisterMessageType(types.ProtocolMethodHandshakeDone, func() IncomingMessage {
|
|
|
|
return NewHandshakeDone()
|
|
|
|
})
|
2024-01-06 17:51:38 +00:00
|
|
|
RegisterMessageType(types.ProtocolMethodAnnouncePeers, func() IncomingMessage {
|
|
|
|
return NewAnnouncePeers()
|
|
|
|
})
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterMessageType(messageType types.ProtocolMethod, factoryFunc func() IncomingMessage) {
|
|
|
|
if factoryFunc == nil {
|
|
|
|
panic("factoryFunc cannot be nil")
|
|
|
|
}
|
|
|
|
messageTypes.Store(messageType, factoryFunc)
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetMessageType(kind types.ProtocolMethod) (protocol.IncomingMessage, bool) {
|
|
|
|
value, ok := messageTypes.Load(kind)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
factoryFunc, ok := value.(func() IncomingMessage)
|
|
|
|
if !ok {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
|
|
|
return factoryFunc(), true
|
|
|
|
}
|