fix: need to store a reference to the handler to itself so we can access it by the proper type in a parent method

This commit is contained in:
Derrick Hammer 2024-01-07 10:37:42 -05:00
parent a6389eb738
commit a9fb6aedb9
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 17 additions and 2 deletions

View File

@ -9,6 +9,8 @@ import (
type IncomingMessage interface {
HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error
SetIncomingMessage(msg IncomingMessage)
Self() IncomingMessage
SetSelf(self IncomingMessage)
msgpack.CustomDecoder
}

View File

@ -20,6 +20,15 @@ type IncomingMessageImpl struct {
data msgpack.RawMessage
original []byte
known bool
self IncomingMessage
}
func (i *IncomingMessageImpl) Self() IncomingMessage {
return i.self
}
func (i *IncomingMessageImpl) SetSelf(self IncomingMessage) {
i.self = self
}
func (i *IncomingMessageImpl) DecodeMessage(dec *msgpack.Decoder) error {
@ -97,7 +106,7 @@ func NewIncomingMessageTyped(kind types.ProtocolMethod, data msgpack.RawMessage)
func (i *IncomingMessageImpl) DecodeMsgpack(dec *msgpack.Decoder) error {
if i.known {
if msgTyped, ok := interface{}(i).(IncomingMessageTyped); ok {
if msgTyped, ok := interface{}(i.Self()).(IncomingMessageTyped); ok {
return msgTyped.DecodeMessage(dec)
}
return fmt.Errorf("type assertion to IncomingMessageTyped failed")

View File

@ -2,6 +2,7 @@ package service
import (
"errors"
"fmt"
"git.lumeweb.com/LumeWeb/libs5-go/ed25519"
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
@ -15,6 +16,7 @@ import (
bolt "go.etcd.io/bbolt"
"go.uber.org/zap"
"net/url"
"reflect"
"sort"
"time"
)
@ -247,9 +249,11 @@ func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {
handler, ok := protocol.GetMessageType(imsg.GetKind())
if ok {
imsg.SetOriginal(message)
handler.SetIncomingMessage(imsg)
handler.SetSelf(handler)
fmt.Println(reflect.TypeOf(handler))
err := msgpack.Unmarshal(imsg.Data(), handler)
if err != nil {
return err