diff --git a/protocol/base/base.go b/protocol/base/base.go new file mode 100644 index 0000000..bfc4b87 --- /dev/null +++ b/protocol/base/base.go @@ -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 +} diff --git a/protocol/encodeable_message.go b/protocol/base/encodeable_message.go similarity index 85% rename from protocol/encodeable_message.go rename to protocol/base/encodeable_message.go index 7f0e281..5fdf84e 100644 --- a/protocol/encodeable_message.go +++ b/protocol/base/encodeable_message.go @@ -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 diff --git a/protocol/incoming_message.go b/protocol/base/incoming_message.go similarity index 86% rename from protocol/incoming_message.go rename to protocol/base/incoming_message.go index 01f82b7..8f2372e 100644 --- a/protocol/incoming_message.go +++ b/protocol/base/incoming_message.go @@ -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 { diff --git a/protocol/base/signed.go b/protocol/base/signed.go new file mode 100644 index 0000000..9013e86 --- /dev/null +++ b/protocol/base/signed.go @@ -0,0 +1,5 @@ +package base + +type SignedIncomingMessage interface { + IncomingMessage +} diff --git a/protocol/handshake_open.go b/protocol/handshake_open.go index 8cae126..bcab306 100644 --- a/protocol/handshake_open.go +++ b/protocol/handshake_open.go @@ -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") ) diff --git a/protocol/hash_query.go b/protocol/hash_query.go index b4d958c..0ce55ab 100644 --- a/protocol/hash_query.go +++ b/protocol/hash_query.go @@ -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 { diff --git a/protocol/message.go b/protocol/message.go index c1204a3..754a79c 100644 --- a/protocol/message.go +++ b/protocol/message.go @@ -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 } diff --git a/protocol/signed/accounce_peers.go b/protocol/signed/accounce_peers.go index 468ff27..832d047 100644 --- a/protocol/signed/accounce_peers.go +++ b/protocol/signed/accounce_peers.go @@ -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 { diff --git a/protocol/signed/signed.go b/protocol/signed/signed.go index 382131c..21600c9 100644 --- a/protocol/signed/signed.go +++ b/protocol/signed/signed.go @@ -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 +} diff --git a/protocol/signed_message.go b/protocol/signed/signed_message.go similarity index 85% rename from protocol/signed_message.go rename to protocol/signed/signed_message.go index 17c6970..975ca82 100644 --- a/protocol/signed_message.go +++ b/protocol/signed/signed_message.go @@ -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 { diff --git a/protocol/storage_location.go b/protocol/storage_location.go index 85dd68f..3e5bc1f 100644 --- a/protocol/storage_location.go +++ b/protocol/storage_location.go @@ -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 { diff --git a/service/p2p.go b/service/p2p.go index 75182c2..2ef90f0 100644 --- a/service/p2p.go +++ b/service/p2p.go @@ -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 {