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