From ac26de397f81efe9b53ef04229072e5a457e72b2 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Fri, 1 Feb 2019 07:08:00 +0000 Subject: [PATCH] send ping message to new peers Signed-off-by: Leo Chen --- api/service/discovery/service.go | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/api/service/discovery/service.go b/api/service/discovery/service.go index 3310bb021..b240dc72f 100644 --- a/api/service/discovery/service.go +++ b/api/service/discovery/service.go @@ -2,10 +2,10 @@ package discovery import ( "context" - "io" "sync" "github.com/ethereum/go-ethereum/log" + proto_discovery "github.com/harmony-one/harmony/api/proto/discovery" "github.com/harmony-one/harmony/internal/utils" "github.com/harmony-one/harmony/p2p" @@ -26,8 +26,6 @@ type Service struct { Host p2p.Host DHT *libp2pdht.IpfsDHT Rendezvous string - reader io.Reader - writer io.Writer ctx context.Context } @@ -75,9 +73,11 @@ func (s *Service) StartService() { if peer.ID == s.Host.GetP2PHost().ID() { continue } - log.Debug("Found peer", "adding peer", peer) + log.Debug("Found Peer", "peer", peer.ID, "addr", peer.Addrs) p := p2p.Peer{PeerID: peer.ID, Addrs: peer.Addrs} s.Host.AddPeer(&p) + // TODO: stop ping if pinged before + s.pingPeer(p) } select {} @@ -116,3 +116,11 @@ func (s *Service) Init() error { return nil } + +func (s *Service) pingPeer(peer p2p.Peer) { + ping := proto_discovery.NewPingMessage(s.Host.GetSelfPeer()) + buffer := ping.ConstructPingMessage() + log.Debug("Sending Ping Message to", "peer", peer) + s.Host.SendMessage(peer, buffer) + log.Debug("Sent Ping Message to", "peer", peer) +}