refactor: add kind to interface

This commit is contained in:
Derrick Hammer 2024-01-09 12:49:16 -05:00
parent 1458cbe1d9
commit 799be312e1
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 8 additions and 4 deletions

View File

@ -14,6 +14,7 @@ type IncomingMessage interface {
IncomingMessage() IncomingMessage
Self() IncomingMessage
SetSelf(self IncomingMessage)
Kind() int
msgpack.CustomDecoder
}

View File

@ -126,6 +126,7 @@ func (h *HashQuery) HandleMessage(node interfaces.Node, peer net.Peer, verifyId
var peers *hashset.Set
hashString, err := h.hash.ToString()
node.Logger().Debug("HashQuery", zap.Any("hashString", hashString))
if err != nil {
return err
}
@ -147,7 +148,7 @@ func (h *HashQuery) HandleMessage(node interfaces.Node, peer net.Peer, verifyId
for _, val := range node.Services().P2P().Peers().Values() {
peerVal := val.(net.Peer)
if !peerVal.Id().Equals(peer.Id()) {
err := peerVal.SendMessage(h.IncomingMessageImpl.Original())
err := peerVal.SendMessage(h.IncomingMessage().Original())
if err != nil {
node.Logger().Error("Failed to send message", zap.Error(err))
}

View File

@ -15,7 +15,7 @@ var (
_ base.IncomingMessage = (*base.IncomingMessageImpl)(nil)
)
func init() {
func Init() {
messageTypes = sync.Map{}
// Register factory functions instead of instances

View File

@ -10,7 +10,7 @@ var (
messageTypes sync.Map
)
func init() {
func Init() {
messageTypes = sync.Map{}
RegisterMessageType(int(types.ProtocolMethodHandshakeDone), func() base.SignedIncomingMessage {

View File

@ -9,6 +9,7 @@ import (
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
"git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5"
"go.uber.org/zap"
"io"
)
@ -85,7 +86,8 @@ func (s *SignedMessage) HandleMessage(node interfaces.Node, peer net.Peer, verif
return err
}
if msgHandler, valid := GetMessageType(types.ProtocolMethod(payload.kind)); valid {
if msgHandler, valid := GetMessageType(payload.kind); valid {
node.Logger().Debug("SignedMessage", zap.Any("type", types.ProtocolMethodMap[types.ProtocolMethod(payload.kind)]))
msgHandler.SetIncomingMessage(s)
msgHandler.SetSelf(msgHandler)
err := msgpack.Unmarshal(payload.message, &msgHandler)