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