libs5-go/protocol/registry_entry.go

57 lines
1.2 KiB
Go
Raw Normal View History

2024-01-10 11:21:03 +00:00
package protocol
import (
"git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5"
)
2024-01-30 03:25:21 +00:00
var _ IncomingMessage = (*RegistryEntryRequest)(nil)
var _ EncodeableMessage = (*RegistryEntryRequest)(nil)
2024-01-10 11:21:03 +00:00
type RegistryEntryRequest struct {
sre SignedRegistryEntry
2024-01-30 03:25:21 +00:00
HandshakeRequirement
2024-01-10 11:21:03 +00:00
}
func NewEmptyRegistryEntryRequest() *RegistryEntryRequest {
rer := &RegistryEntryRequest{}
rer.SetRequiresHandshake(true)
return rer
2024-01-10 11:21:03 +00:00
}
func NewRegistryEntryRequest(sre SignedRegistryEntry) *RegistryEntryRequest {
2024-01-10 11:21:03 +00:00
return &RegistryEntryRequest{sre: sre}
}
func (s *RegistryEntryRequest) EncodeMsgpack(enc *msgpack.Encoder) error {
err := enc.EncodeInt(int64(types.RecordTypeRegistryEntry))
if err != nil {
return err
}
err = enc.EncodeBytes(MarshalSignedRegistryEntry(s.sre))
if err != nil {
return err
}
return nil
}
2024-01-30 03:25:21 +00:00
func (s *RegistryEntryRequest) DecodeMessage(dec *msgpack.Decoder, message IncomingMessageData) error {
sre, err := UnmarshalSignedRegistryEntry(message.Data)
2024-01-10 11:21:03 +00:00
if err != nil {
return err
}
s.sre = sre
return nil
}
2024-01-30 03:25:21 +00:00
func (s *RegistryEntryRequest) HandleMessage(message IncomingMessageData) error {
peer := message.Peer
services := message.Services
return services.Registry().Set(s.sre, false, peer)
2024-01-10 11:21:03 +00:00
}