libs5-go/protocol/registry_query.go

70 lines
1.3 KiB
Go
Raw Normal View History

2024-01-10 11:21:03 +00:00
package protocol
import (
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5"
)
var _ base.IncomingMessage = (*RegistryQuery)(nil)
2024-01-10 11:21:03 +00:00
var _ base.EncodeableMessage = (*RegistryQuery)(nil)
type RegistryQuery struct {
pk []byte
base.HandshakeRequirement
2024-01-10 11:21:03 +00:00
}
func NewEmptyRegistryQuery() *RegistryQuery {
rq := &RegistryQuery{}
rq.SetRequiresHandshake(true)
return rq
2024-01-10 11:21:03 +00:00
}
func NewRegistryQuery(pk []byte) *RegistryQuery {
return &RegistryQuery{pk: pk}
}
func (s *RegistryQuery) EncodeMsgpack(enc *msgpack.Encoder) error {
err := enc.EncodeInt(int64(types.ProtocolMethodRegistryQuery))
if err != nil {
return err
}
err = enc.EncodeBytes(s.pk)
if err != nil {
return err
}
return nil
}
func (s *RegistryQuery) DecodeMessage(dec *msgpack.Decoder, message base.IncomingMessageData) error {
2024-01-10 11:21:03 +00:00
pk, err := dec.DecodeBytes()
if err != nil {
return err
}
s.pk = pk
return nil
}
func (s *RegistryQuery) HandleMessage(message base.IncomingMessageData) error {
node := message.Node
peer := message.Peer
2024-01-10 11:21:03 +00:00
sre, err := node.Services().Registry().Get(s.pk)
if err != nil {
return err
}
if sre != nil {
err := peer.SendMessage(MarshalSignedRegistryEntry(sre))
if err != nil {
return err
}
}
return nil
}