refactor: use interfaces

This commit is contained in:
Derrick Hammer 2024-01-07 03:58:22 -05:00
parent 4678d406fc
commit 51d76a2d95
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
7 changed files with 19 additions and 18 deletions

View File

@ -2,8 +2,8 @@ package protocol
import ( import (
"errors" "errors"
"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/node"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
) )
@ -55,7 +55,7 @@ func (m HandshakeOpen) EncodeMsgpack(enc *msgpack.Encoder) error {
return nil return nil
} }
func (m *HandshakeOpen) HandleMessage(node *node.NodeImpl, peer *net.Peer, verifyId bool) error { func (m *HandshakeOpen) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
return nil return nil
} }

View File

@ -2,8 +2,8 @@ package protocol
import ( 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/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go/node"
"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"
@ -47,7 +47,7 @@ func (h *HashQuery) DecodeMessage(dec *msgpack.Decoder) error {
return nil return nil
} }
func (h *HashQuery) HandleMessage(node *libs5_go.NodeImpl, peer *net.Peer, verifyId bool) error { func (h *HashQuery) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
mapLocations, err := node.GetCachedStorageLocations(h.hash, h.kinds) mapLocations, err := node.GetCachedStorageLocations(h.hash, h.kinds)
if err != nil { if err != nil {
log.Printf("Error getting cached storage locations: %v", err) log.Printf("Error getting cached storage locations: %v", err)
@ -71,14 +71,14 @@ func (h *HashQuery) HandleMessage(node *libs5_go.NodeImpl, peer *net.Peer, verif
return err return err
} }
sortedNodeId, err := score[0].ToString() sortedNodeId, err := (*score[0]).ToString()
if err != nil { if err != nil {
return err return err
} }
entry, exists := mapLocations[sortedNodeId] entry, exists := mapLocations[sortedNodeId]
if exists { if exists {
err := (*peer).SendMessage(entry.ProviderMessage) err := (*peer).SendMessage(entry.ProviderMessage())
if err != nil { if err != nil {
return err return err
} }

View File

@ -15,7 +15,7 @@ var (
) )
type IncomingMessage interface { type IncomingMessage interface {
HandleMessage(node *interfaces.Node, peer *net.Peer, verifyId bool) error HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error
SetIncomingMessage(msg IncomingMessage) SetIncomingMessage(msg IncomingMessage)
msgpack.CustomDecoder msgpack.CustomDecoder
} }
@ -55,7 +55,7 @@ func (i *IncomingMessageImpl) ToMessage() (message []byte, err error) {
return msgpack.Marshal(i) return msgpack.Marshal(i)
} }
func (i *IncomingMessageImpl) HandleMessage(node *interfaces.Node, peer *net.Peer, verifyId bool) error { func (i *IncomingMessageImpl) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
panic("child class should implement this method") panic("child class should implement this method")
} }

View File

@ -2,8 +2,8 @@ package signed
import ( 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/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/protocol" "git.lumeweb.com/LumeWeb/libs5-go/protocol"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
"net/url" "net/url"
@ -60,7 +60,7 @@ func (a *AnnouncePeers) DecodeMessage(dec *msgpack.Decoder) error {
return nil return nil
} }
func (a AnnouncePeers) HandleMessage(node *libs5_go.NodeImpl, peer *net.Peer, verifyId bool) error { func (a AnnouncePeers) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
if len(a.connectionUris) > 0 { if len(a.connectionUris) > 0 {
firstUrl := a.connectionUris[0] firstUrl := a.connectionUris[0]
uri := new(url.URL) uri := new(url.URL)

View File

@ -3,8 +3,8 @@ package signed
import ( import (
"bytes" "bytes"
"errors" "errors"
"git.lumeweb.com/LumeWeb/libs5-go/interfaces"
"git.lumeweb.com/LumeWeb/libs5-go/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/protocol" "git.lumeweb.com/LumeWeb/libs5-go/protocol"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
) )
@ -20,8 +20,8 @@ func NewHandshakeDone() *HandshakeDone {
return &HandshakeDone{HandshakeOpen: *protocol.NewHandshakeOpen(nil, ""), supportedFeatures: -1} return &HandshakeDone{HandshakeOpen: *protocol.NewHandshakeOpen(nil, ""), supportedFeatures: -1}
} }
func (h HandshakeDone) HandleMessage(node *libs5_go.NodeImpl, peer *net.Peer, verifyId bool) error { func (h HandshakeDone) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
if !(*node).IsStarted() { if !node.IsStarted() {
err := (*peer).End() err := (*peer).End()
if err != nil { if err != nil {
return nil return nil

View File

@ -4,8 +4,8 @@ import (
"crypto/ed25519" "crypto/ed25519"
"errors" "errors"
"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/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
libs5_go "git.lumeweb.com/LumeWeb/libs5-go/node"
"git.lumeweb.com/LumeWeb/libs5-go/protocol/signed" "git.lumeweb.com/LumeWeb/libs5-go/protocol/signed"
"git.lumeweb.com/LumeWeb/libs5-go/types" "git.lumeweb.com/LumeWeb/libs5-go/types"
"github.com/vmihailenco/msgpack/v5" "github.com/vmihailenco/msgpack/v5"
@ -54,7 +54,7 @@ func NewSignedMessage() *SignedMessage {
return &SignedMessage{} return &SignedMessage{}
} }
func (s *SignedMessage) HandleMessage(node *libs5_go.NodeImpl, peer *net.Peer, verifyId bool) error { func (s *SignedMessage) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
var payload signedMessagePayoad var payload signedMessagePayoad
err := msgpack.Unmarshal(s.message, &payload) err := msgpack.Unmarshal(s.message, &payload)

View File

@ -4,8 +4,9 @@ import (
"crypto/ed25519" "crypto/ed25519"
"fmt" "fmt"
"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/net" "git.lumeweb.com/LumeWeb/libs5-go/net"
"git.lumeweb.com/LumeWeb/libs5-go/node" nodep "git.lumeweb.com/LumeWeb/libs5-go/node"
"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"
"github.com/emirpasic/gods/sets/hashset" "github.com/emirpasic/gods/sets/hashset"
@ -39,7 +40,7 @@ func (s *StorageLocation) DecodeMessage(dec *msgpack.Decoder) error {
return nil return nil
} }
func (s *StorageLocation) HandleMessage(node *node.NodeImpl, peer *net.Peer, verifyId bool) error { func (s *StorageLocation) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
hash := encoding.NewMultihash(s.raw[1:34]) // Replace NewMultihash with appropriate function hash := encoding.NewMultihash(s.raw[1:34]) // Replace NewMultihash with appropriate function
fmt.Println("Hash:", hash) fmt.Println("Hash:", hash)
@ -73,7 +74,7 @@ func (s *StorageLocation) HandleMessage(node *node.NodeImpl, peer *net.Peer, ver
nodeId := encoding.NewNodeId(publicKey) nodeId := encoding.NewNodeId(publicKey)
// Assuming `node` is an instance of your NodeImpl structure // Assuming `node` is an instance of your NodeImpl structure
err := node.AddStorageLocation(hash, nodeId, node.NewStorageLocation(int(typeOfData), parts, int64(expiry)), s.raw, node.Config()) // Implement AddStorageLocation err := node.AddStorageLocation(hash, nodeId, nodep.NewStorageLocation(int(typeOfData), parts, int64(expiry)), s.raw, node.Config()) // Implement AddStorageLocation
if err != nil { if err != nil {
return fmt.Errorf("Failed to add storage location: %s", err) return fmt.Errorf("Failed to add storage location: %s", err)