2024-01-06 11:33:46 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
2024-01-07 10:12:43 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
|
2024-01-06 11:33:46 +00:00
|
|
|
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2024-01-09 18:57:35 +00:00
|
|
|
messageTypes map[int]func() base.IncomingMessage
|
2024-01-06 11:33:46 +00:00
|
|
|
)
|
|
|
|
|
2024-01-09 17:49:16 +00:00
|
|
|
func Init() {
|
2024-01-09 18:57:35 +00:00
|
|
|
messageTypes = make(map[int]func() base.IncomingMessage)
|
2024-01-06 11:33:46 +00:00
|
|
|
|
|
|
|
// Register factory functions instead of instances
|
2024-01-09 17:47:58 +00:00
|
|
|
RegisterMessageType(int(types.ProtocolMethodHandshakeOpen), func() base.IncomingMessage {
|
2024-01-06 11:33:46 +00:00
|
|
|
return NewHandshakeOpen([]byte{}, "")
|
|
|
|
})
|
2024-01-09 17:47:58 +00:00
|
|
|
RegisterMessageType(int(types.ProtocolMethodHashQuery), func() base.IncomingMessage {
|
2024-01-08 17:42:02 +00:00
|
|
|
return NewHashQuery()
|
|
|
|
})
|
2024-01-09 18:58:11 +00:00
|
|
|
RegisterMessageType(int(types.RecordTypeStorageLocation), func() base.IncomingMessage {
|
|
|
|
return NewStorageLocation()
|
|
|
|
})
|
2024-01-10 11:21:03 +00:00
|
|
|
RegisterMessageType(int(types.RecordTypeRegistryEntry), func() base.IncomingMessage {
|
|
|
|
return NewEmptyRegistryEntryRequest()
|
|
|
|
})
|
|
|
|
RegisterMessageType(int(types.ProtocolMethodRegistryQuery), func() base.IncomingMessage {
|
|
|
|
return NewEmptyRegistryQuery()
|
|
|
|
})
|
2024-01-09 17:47:58 +00:00
|
|
|
RegisterMessageType(int(types.ProtocolMethodSignedMessage), func() base.IncomingMessage {
|
2024-01-07 10:12:43 +00:00
|
|
|
return signed.NewSignedMessage()
|
2024-01-06 11:33:46 +00:00
|
|
|
})
|
2024-01-08 17:42:02 +00:00
|
|
|
|
2024-01-06 11:33:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-09 17:47:58 +00:00
|
|
|
func RegisterMessageType(messageType int, factoryFunc func() base.IncomingMessage) {
|
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-09 17:47:58 +00:00
|
|
|
func GetMessageType(kind int) (base.IncomingMessage, 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
|
|
|
}
|