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