libs5-go/protocol/signed/signed.go

53 lines
1.1 KiB
Go
Raw Normal View History

2024-01-06 11:33:46 +00:00
package signed
import (
"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
)
2024-01-09 17:49:16 +00:00
func Init() {
2024-01-06 11:33:46 +00:00
messageTypes = sync.Map{}
RegisterMessageType(int(types.ProtocolMethodHandshakeDone), func() base.SignedIncomingMessage {
2024-01-06 11:33:46 +00:00
return NewHandshakeDone()
})
RegisterMessageType(int(types.ProtocolMethodAnnouncePeers), func() base.SignedIncomingMessage {
2024-01-06 17:51:38 +00:00
return NewAnnouncePeers()
})
2024-01-06 11:33:46 +00:00
}
func RegisterMessageType(messageType int, factoryFunc func() base.SignedIncomingMessage) {
2024-01-06 11:33:46 +00:00
if factoryFunc == nil {
panic("factoryFunc cannot be nil")
}
messageTypes.Store(int(messageType), factoryFunc)
2024-01-06 11:33:46 +00:00
}
func GetMessageType(kind int) (base.SignedIncomingMessage, bool) {
2024-01-06 11:33:46 +00:00
value, ok := messageTypes.Load(kind)
if !ok {
return nil, false
}
factoryFunc, ok := value.(func() base.SignedIncomingMessage)
2024-01-06 11:33:46 +00:00
if !ok {
return nil, false
}
return factoryFunc(), true
}
var (
_ base.SignedIncomingMessage = (*IncomingMessageImpl)(nil)
)
type IncomingMessageImpl struct {
base.IncomingMessageImpl
message []byte
}