refactor: need to crease a base protocol package to solve import cycle
This commit is contained in:
parent
ef86db2bd0
commit
54f0a53f77
|
@ -0,0 +1,18 @@
|
|||
package base
|
||||
|
||||
import (
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
type IncomingMessage interface {
|
||||
HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error
|
||||
SetIncomingMessage(msg IncomingMessage)
|
||||
msgpack.CustomDecoder
|
||||
}
|
||||
|
||||
type IncomingMessageTyped interface {
|
||||
DecodeMessage(dec *msgpack.Decoder) error
|
||||
IncomingMessage
|
||||
}
|
|
@ -1,7 +1,11 @@
|
|||
package protocol
|
||||
package base
|
||||
|
||||
import "github.com/vmihailenco/msgpack/v5"
|
||||
|
||||
var (
|
||||
_ EncodeableMessage = (*EncodeableMessageImpl)(nil)
|
||||
)
|
||||
|
||||
type EncodeableMessage interface {
|
||||
ToMessage() (message []byte, err error)
|
||||
msgpack.CustomEncoder
|
|
@ -1,4 +1,4 @@
|
|||
package protocol
|
||||
package base
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@ -9,20 +9,10 @@ import (
|
|||
"net/url"
|
||||
)
|
||||
|
||||
var (
|
||||
_ EncodeableMessage = (*EncodeableMessageImpl)(nil)
|
||||
)
|
||||
var _ msgpack.CustomDecoder = (*IncomingMessageImpl)(nil)
|
||||
var _ IncomingMessage = (*IncomingMessageImpl)(nil)
|
||||
|
||||
type IncomingMessage interface {
|
||||
HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error
|
||||
SetIncomingMessage(msg IncomingMessage)
|
||||
msgpack.CustomDecoder
|
||||
}
|
||||
|
||||
type IncomingMessageTyped interface {
|
||||
DecodeMessage(dec *msgpack.Decoder) error
|
||||
IncomingMessage
|
||||
}
|
||||
type IncomingMessageHandler func(node interfaces.Node, peer *net.Peer, u *url.URL, verifyId bool) error
|
||||
|
||||
type IncomingMessageImpl struct {
|
||||
kind types.ProtocolMethod
|
||||
|
@ -43,9 +33,6 @@ func (i *IncomingMessageImpl) SetIncomingMessage(msg IncomingMessage) {
|
|||
*i = interface{}(msg).(IncomingMessageImpl)
|
||||
}
|
||||
|
||||
var _ msgpack.CustomDecoder = (*IncomingMessageImpl)(nil)
|
||||
var _ IncomingMessage = (*IncomingMessageImpl)(nil)
|
||||
|
||||
func (i *IncomingMessageImpl) GetKind() types.ProtocolMethod {
|
||||
return i.kind
|
||||
}
|
||||
|
@ -89,8 +76,6 @@ func NewIncomingMessageTyped(kind types.ProtocolMethod, data msgpack.RawMessage)
|
|||
return &IncomingMessageTypedImpl{*known}
|
||||
}
|
||||
|
||||
type IncomingMessageHandler func(node interfaces.Node, peer *net.Peer, u *url.URL, verifyId bool) error
|
||||
|
||||
func (i *IncomingMessageImpl) DecodeMsgpack(dec *msgpack.Decoder) error {
|
||||
if i.known {
|
||||
if msgTyped, ok := interface{}(i).(IncomingMessageTyped); ok {
|
|
@ -0,0 +1,5 @@
|
|||
package base
|
||||
|
||||
type SignedIncomingMessage interface {
|
||||
IncomingMessage
|
||||
}
|
|
@ -4,6 +4,7 @@ import (
|
|||
"errors"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
@ -11,8 +12,8 @@ import (
|
|||
type HandshakeOpen struct {
|
||||
challenge []byte
|
||||
networkId string
|
||||
IncomingMessageTypedImpl
|
||||
IncomingMessageHandler
|
||||
base.IncomingMessageTypedImpl
|
||||
base.IncomingMessageHandler
|
||||
}
|
||||
|
||||
func (m HandshakeOpen) Challenge() []byte {
|
||||
|
@ -23,7 +24,7 @@ func (m HandshakeOpen) NetworkId() string {
|
|||
return m.networkId
|
||||
}
|
||||
|
||||
var _ EncodeableMessage = (*HandshakeOpen)(nil)
|
||||
var _ base.EncodeableMessage = (*HandshakeOpen)(nil)
|
||||
var (
|
||||
errInvalidChallenge = errors.New("Invalid challenge")
|
||||
)
|
||||
|
|
|
@ -4,20 +4,21 @@ import (
|
|||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
||||
"github.com/emirpasic/gods/sets/hashset"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
"go.uber.org/zap"
|
||||
"log"
|
||||
)
|
||||
|
||||
var _ IncomingMessageTyped = (*HashQuery)(nil)
|
||||
var _ base.IncomingMessageTyped = (*HashQuery)(nil)
|
||||
|
||||
type HashQuery struct {
|
||||
hash *encoding.Multihash
|
||||
kinds []int
|
||||
|
||||
IncomingMessageTypedImpl
|
||||
IncomingMessageHandler
|
||||
base.IncomingMessageTypedImpl
|
||||
base.IncomingMessageHandler
|
||||
}
|
||||
|
||||
func (h HashQuery) Hash() *encoding.Multihash {
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package protocol
|
||||
|
||||
import (
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||
"sync"
|
||||
)
|
||||
|
@ -10,35 +12,35 @@ var (
|
|||
)
|
||||
|
||||
var (
|
||||
_ IncomingMessage = (*IncomingMessageImpl)(nil)
|
||||
_ base.IncomingMessage = (*base.IncomingMessageImpl)(nil)
|
||||
)
|
||||
|
||||
func init() {
|
||||
messageTypes = sync.Map{}
|
||||
|
||||
// Register factory functions instead of instances
|
||||
RegisterMessageType(types.ProtocolMethodHandshakeOpen, func() IncomingMessage {
|
||||
RegisterMessageType(types.ProtocolMethodHandshakeOpen, func() base.IncomingMessage {
|
||||
return NewHandshakeOpen([]byte{}, "")
|
||||
})
|
||||
RegisterMessageType(types.ProtocolMethodSignedMessage, func() IncomingMessage {
|
||||
return NewSignedMessage()
|
||||
RegisterMessageType(types.ProtocolMethodSignedMessage, func() base.IncomingMessage {
|
||||
return signed.NewSignedMessage()
|
||||
})
|
||||
}
|
||||
|
||||
func RegisterMessageType(messageType types.ProtocolMethod, factoryFunc func() IncomingMessage) {
|
||||
func RegisterMessageType(messageType types.ProtocolMethod, factoryFunc func() base.IncomingMessage) {
|
||||
if factoryFunc == nil {
|
||||
panic("factoryFunc cannot be nil")
|
||||
}
|
||||
messageTypes.Store(messageType, factoryFunc)
|
||||
}
|
||||
|
||||
func GetMessageType(kind types.ProtocolMethod) (IncomingMessage, bool) {
|
||||
func GetMessageType(kind types.ProtocolMethod) (base.IncomingMessage, bool) {
|
||||
value, ok := messageTypes.Load(kind)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
factoryFunc, ok := value.(func() IncomingMessage)
|
||||
factoryFunc, ok := value.(func() base.IncomingMessage)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
|
|
@ -4,20 +4,20 @@ import (
|
|||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
var (
|
||||
_ protocol.IncomingMessageTyped = (*AnnouncePeers)(nil)
|
||||
_ base.IncomingMessageTyped = (*AnnouncePeers)(nil)
|
||||
)
|
||||
|
||||
type AnnouncePeers struct {
|
||||
connected bool
|
||||
peer *encoding.NodeId
|
||||
connectionUris []*url.URL
|
||||
protocol.IncomingMessageTypedImpl
|
||||
base.IncomingMessageTypedImpl
|
||||
}
|
||||
|
||||
func NewAnnouncePeers() *AnnouncePeers {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package signed
|
||||
|
||||
import (
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||
"sync"
|
||||
)
|
||||
|
@ -10,47 +10,43 @@ var (
|
|||
messageTypes sync.Map
|
||||
)
|
||||
|
||||
var (
|
||||
_ IncomingMessage = (*IncomingMessageImpl)(nil)
|
||||
)
|
||||
|
||||
type IncomingMessage interface {
|
||||
protocol.IncomingMessage
|
||||
}
|
||||
|
||||
type IncomingMessageImpl struct {
|
||||
protocol.IncomingMessageImpl
|
||||
message []byte
|
||||
}
|
||||
|
||||
func init() {
|
||||
messageTypes = sync.Map{}
|
||||
|
||||
RegisterMessageType(types.ProtocolMethodHandshakeDone, func() IncomingMessage {
|
||||
RegisterMessageType(types.ProtocolMethodHandshakeDone, func() base.SignedIncomingMessage {
|
||||
return NewHandshakeDone()
|
||||
})
|
||||
RegisterMessageType(types.ProtocolMethodAnnouncePeers, func() IncomingMessage {
|
||||
RegisterMessageType(types.ProtocolMethodAnnouncePeers, func() base.SignedIncomingMessage {
|
||||
return NewAnnouncePeers()
|
||||
})
|
||||
}
|
||||
|
||||
func RegisterMessageType(messageType types.ProtocolMethod, factoryFunc func() IncomingMessage) {
|
||||
func RegisterMessageType(messageType types.ProtocolMethod, factoryFunc func() base.SignedIncomingMessage) {
|
||||
if factoryFunc == nil {
|
||||
panic("factoryFunc cannot be nil")
|
||||
}
|
||||
messageTypes.Store(messageType, factoryFunc)
|
||||
}
|
||||
|
||||
func GetMessageType(kind types.ProtocolMethod) (protocol.IncomingMessage, bool) {
|
||||
func GetMessageType(kind types.ProtocolMethod) (base.SignedIncomingMessage, bool) {
|
||||
value, ok := messageTypes.Load(kind)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
factoryFunc, ok := value.(func() IncomingMessage)
|
||||
factoryFunc, ok := value.(func() base.SignedIncomingMessage)
|
||||
if !ok {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return factoryFunc(), true
|
||||
}
|
||||
|
||||
var (
|
||||
_ base.SignedIncomingMessage = (*IncomingMessageImpl)(nil)
|
||||
)
|
||||
|
||||
type IncomingMessageImpl struct {
|
||||
base.IncomingMessageImpl
|
||||
message []byte
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package protocol
|
||||
package signed
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
|
@ -6,14 +6,14 @@ import (
|
|||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
)
|
||||
|
||||
var (
|
||||
_ IncomingMessageTyped = (*SignedMessage)(nil)
|
||||
_ msgpack.CustomDecoder = (*signedMessagePayoad)(nil)
|
||||
_ base.IncomingMessageTyped = (*SignedMessage)(nil)
|
||||
_ msgpack.CustomDecoder = (*signedMessagePayoad)(nil)
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -24,7 +24,7 @@ type SignedMessage struct {
|
|||
nodeId *encoding.NodeId
|
||||
signature []byte
|
||||
message []byte
|
||||
IncomingMessageTypedImpl
|
||||
base.IncomingMessageTypedImpl
|
||||
}
|
||||
|
||||
type signedMessagePayoad struct {
|
||||
|
@ -62,7 +62,7 @@ func (s *SignedMessage) HandleMessage(node interfaces.Node, peer *net.Peer, veri
|
|||
return err
|
||||
}
|
||||
|
||||
if msgHandler, valid := signed.GetMessageType(types.ProtocolMethod(payload.kind)); valid {
|
||||
if msgHandler, valid := GetMessageType(types.ProtocolMethod(payload.kind)); valid {
|
||||
msgHandler.SetIncomingMessage(s)
|
||||
err := msgpack.Unmarshal(payload.message, &msgHandler)
|
||||
if err != nil {
|
|
@ -6,6 +6,7 @@ import (
|
|||
"git.lumeweb.com/LumeWeb/libs5-go/encoding"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/storage"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/types"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
||||
|
@ -14,7 +15,7 @@ import (
|
|||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var _ IncomingMessageTyped = (*StorageLocation)(nil)
|
||||
var _ base.IncomingMessageTyped = (*StorageLocation)(nil)
|
||||
|
||||
type StorageLocation struct {
|
||||
raw []byte
|
||||
|
@ -25,8 +26,8 @@ type StorageLocation struct {
|
|||
publicKey []byte
|
||||
signature []byte
|
||||
|
||||
IncomingMessageTypedImpl
|
||||
IncomingMessageHandler
|
||||
base.IncomingMessageTypedImpl
|
||||
base.IncomingMessageHandler
|
||||
}
|
||||
|
||||
func (s *StorageLocation) DecodeMessage(dec *msgpack.Decoder) error {
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/net"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/protocol/base"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/structs"
|
||||
"git.lumeweb.com/LumeWeb/libs5-go/utils"
|
||||
"github.com/vmihailenco/msgpack/v5"
|
||||
|
@ -238,7 +239,7 @@ func (p *P2PImpl) OnNewPeerListen(peer *net.Peer, verifyId bool) {
|
|||
})
|
||||
|
||||
(*peer).ListenForMessages(func(message []byte) error {
|
||||
imsg := protocol.NewIncomingMessageUnknown()
|
||||
imsg := base.NewIncomingMessageUnknown()
|
||||
|
||||
err := msgpack.Unmarshal(message, imsg)
|
||||
if err != nil {
|
||||
|
|
Loading…
Reference in New Issue