refactor: dont use pointers with interfaces
This commit is contained in:
parent
52b7426a7a
commit
2e9b07c6bd
|
@ -14,8 +14,8 @@ type P2PService interface {
|
||||||
Stop() error
|
Stop() error
|
||||||
Init() error
|
Init() error
|
||||||
ConnectToNode(connectionUris []*url.URL, retried bool) error
|
ConnectToNode(connectionUris []*url.URL, retried bool) error
|
||||||
OnNewPeer(peer *net.Peer, verifyId bool) error
|
OnNewPeer(peer net.Peer, verifyId bool) error
|
||||||
OnNewPeerListen(peer *net.Peer, verifyId bool)
|
OnNewPeerListen(peer net.Peer, verifyId bool)
|
||||||
ReadNodeScore(nodeId *encoding.NodeId) (NodeVotes, error)
|
ReadNodeScore(nodeId *encoding.NodeId) (NodeVotes, error)
|
||||||
GetNodeScore(nodeId *encoding.NodeId) (float64, error)
|
GetNodeScore(nodeId *encoding.NodeId) (float64, error)
|
||||||
SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId, error)
|
SortNodesByScore(nodes []*encoding.NodeId) ([]*encoding.NodeId, error)
|
||||||
|
|
|
@ -51,7 +51,7 @@ func CreateTransportSocket(peerType string, uri *url.URL) (interface{}, error) {
|
||||||
return &t, err
|
return &t, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTransportPeer(peerType string, options *TransportPeerConfig) (*Peer, error) {
|
func CreateTransportPeer(peerType string, options *TransportPeerConfig) (Peer, error) {
|
||||||
factory, ok := transports.Load(peerType)
|
factory, ok := transports.Load(peerType)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, errors.New("no factory registered for type: " + peerType)
|
return nil, errors.New("no factory registered for type: " + peerType)
|
||||||
|
@ -59,5 +59,5 @@ func CreateTransportPeer(peerType string, options *TransportPeerConfig) (*Peer,
|
||||||
|
|
||||||
t, err := factory.(PeerFactory).NewPeer(options)
|
t, err := factory.(PeerFactory).NewPeer(options)
|
||||||
|
|
||||||
return &t, err
|
return t, err
|
||||||
}
|
}
|
||||||
|
|
20
net/peer.go
20
net/peer.go
|
@ -6,6 +6,10 @@ import (
|
||||||
"net/url"
|
"net/url"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
_ Peer = (*BasePeer)(nil)
|
||||||
|
)
|
||||||
|
|
||||||
// EventCallback type for the callback function
|
// EventCallback type for the callback function
|
||||||
type EventCallback func(event []byte) error
|
type EventCallback func(event []byte) error
|
||||||
|
|
||||||
|
@ -43,6 +47,22 @@ type BasePeer struct {
|
||||||
id *encoding.NodeId
|
id *encoding.NodeId
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (b *BasePeer) SendMessage(message []byte) error {
|
||||||
|
panic("must implement in child class")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BasePeer) RenderLocationURI() string {
|
||||||
|
panic("must implement in child class")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BasePeer) ListenForMessages(callback EventCallback, options ListenerOptions) {
|
||||||
|
panic("must implement in child class")
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *BasePeer) End() error {
|
||||||
|
panic("must implement in child class")
|
||||||
|
}
|
||||||
|
|
||||||
func (b *BasePeer) Challenge() []byte {
|
func (b *BasePeer) Challenge() []byte {
|
||||||
return b.challenge
|
return b.challenge
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
|
@ -41,7 +41,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")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,7 @@ func (m HandshakeOpen) EncodeMsgpack(enc *msgpack.Encoder) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *HandshakeOpen) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId bool) error {
|
func (m *HandshakeOpen) HandleMessage(node interfaces.Node, peer net.Peer, verifyId bool) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ func (h *HashQuery) DecodeMessage(dec *msgpack.Decoder) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (h *HashQuery) HandleMessage(node interfaces.Node, 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)
|
||||||
|
@ -79,7 +79,7 @@ func (h *HashQuery) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId
|
||||||
|
|
||||||
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
|
||||||
}
|
}
|
||||||
|
@ -94,13 +94,13 @@ func (h *HashQuery) HandleMessage(node interfaces.Node, peer *net.Peer, verifyId
|
||||||
|
|
||||||
peers = peersVal.(*hashset.Set)
|
peers = peersVal.(*hashset.Set)
|
||||||
|
|
||||||
if exists := peers.Contains((*peer).Id()); !exists {
|
if exists := peers.Contains(peer.Id()); !exists {
|
||||||
peers.Add((*peer).Id())
|
peers.Add(peer.Id())
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, val := range node.Services().P2P().Peers().Values() {
|
for _, val := range node.Services().P2P().Peers().Values() {
|
||||||
peerVal := val.(net.Peer)
|
peerVal := val.(net.Peer)
|
||||||
if !peerVal.Id().Equals((*peer).Id()) {
|
if !peerVal.Id().Equals(peer.Id()) {
|
||||||
err := peerVal.SendMessage(h.IncomingMessageImpl.Original())
|
err := peerVal.SendMessage(h.IncomingMessageImpl.Original())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
node.Logger().Error("Failed to send message", zap.Error(err))
|
node.Logger().Error("Failed to send message", zap.Error(err))
|
||||||
|
|
|
@ -60,7 +60,7 @@ func (a *AnnouncePeers) DecodeMessage(dec *msgpack.Decoder) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a AnnouncePeers) HandleMessage(node interfaces.Node, 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)
|
||||||
|
|
|
@ -31,21 +31,21 @@ func NewHandshakeDone() *HandshakeDone {
|
||||||
return &HandshakeDone{challenge: nil, networkId: "", supportedFeatures: -1}
|
return &HandshakeDone{challenge: nil, networkId: "", supportedFeatures: -1}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h HandshakeDone) HandleMessage(node interfaces.Node, 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
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !bytes.Equal((*peer).Challenge(), h.challenge) {
|
if !bytes.Equal(peer.Challenge(), h.challenge) {
|
||||||
return errors.New("Invalid challenge")
|
return errors.New("Invalid challenge")
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
if !verifyId {
|
if !verifyId {
|
||||||
(*peer).SetId(h)
|
peer.SetId(h)
|
||||||
} else {
|
} else {
|
||||||
if !peer.ID.Equals(pId) {
|
if !peer.ID.Equals(pId) {
|
||||||
return errInvalidChallenge
|
return errInvalidChallenge
|
||||||
|
|
|
@ -54,7 +54,7 @@ func NewSignedMessage() *SignedMessage {
|
||||||
return &SignedMessage{}
|
return &SignedMessage{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SignedMessage) HandleMessage(node interfaces.Node, 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)
|
||||||
|
|
|
@ -41,7 +41,7 @@ func (s *StorageLocation) DecodeMessage(dec *msgpack.Decoder) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (s *StorageLocation) HandleMessage(node interfaces.Node, 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)
|
||||||
|
|
||||||
|
|
|
@ -194,15 +194,14 @@ func (p *P2PImpl) ConnectToNode(connectionUris []*url.URL, retried bool) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
(*peer).SetId(id)
|
peer.SetId(id)
|
||||||
return p.OnNewPeer(peer, true)
|
return p.OnNewPeer(peer, true)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *P2PImpl) OnNewPeer(peer *net.Peer, verifyId bool) error {
|
func (p *P2PImpl) OnNewPeer(peer net.Peer, verifyId bool) error {
|
||||||
challenge := protocol.GenerateChallenge()
|
challenge := protocol.GenerateChallenge()
|
||||||
|
|
||||||
pd := *peer
|
peer.SetChallenge(challenge)
|
||||||
pd.SetChallenge(challenge)
|
|
||||||
|
|
||||||
p.OnNewPeerListen(peer, verifyId)
|
p.OnNewPeerListen(peer, verifyId)
|
||||||
|
|
||||||
|
@ -212,15 +211,15 @@ func (p *P2PImpl) OnNewPeer(peer *net.Peer, verifyId bool) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = pd.SendMessage(handshakeOpenMsg)
|
err = peer.SendMessage(handshakeOpenMsg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (p *P2PImpl) OnNewPeerListen(peer *net.Peer, verifyId bool) {
|
func (p *P2PImpl) OnNewPeerListen(peer net.Peer, verifyId bool) {
|
||||||
onDone := net.CloseCallback(func() {
|
onDone := net.CloseCallback(func() {
|
||||||
peerId, err := (*peer).Id().ToString()
|
peerId, err := peer.Id().ToString()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
p.logger.Error("failed to get peer id", zap.Error(err))
|
p.logger.Error("failed to get peer id", zap.Error(err))
|
||||||
return
|
return
|
||||||
|
@ -236,7 +235,7 @@ func (p *P2PImpl) OnNewPeerListen(peer *net.Peer, verifyId bool) {
|
||||||
p.logger.Error("peer error", zap.Any("args", args))
|
p.logger.Error("peer error", zap.Any("args", args))
|
||||||
})
|
})
|
||||||
|
|
||||||
(*peer).ListenForMessages(func(message []byte) error {
|
peer.ListenForMessages(func(message []byte) error {
|
||||||
imsg := base.NewIncomingMessageUnknown()
|
imsg := base.NewIncomingMessageUnknown()
|
||||||
|
|
||||||
err := msgpack.Unmarshal(message, imsg)
|
err := msgpack.Unmarshal(message, imsg)
|
||||||
|
|
Loading…
Reference in New Issue