github.com/aergoio/aergo@v1.3.1/p2p/subproto/ping.go (about)

     1  /**
     2   *  @file
     3   *  @copyright defined in aergo/LICENSE.txt
     4   */
     5  
     6  package subproto
     7  
     8  import (
     9  	"github.com/aergoio/aergo-lib/log"
    10  	"github.com/aergoio/aergo/p2p/p2pcommon"
    11  	"github.com/aergoio/aergo/p2p/p2putil"
    12  	"github.com/aergoio/aergo/types"
    13  )
    14  
    15  type pingRequestHandler struct {
    16  	BaseMsgHandler
    17  }
    18  
    19  var _ p2pcommon.MessageHandler = (*pingRequestHandler)(nil)
    20  
    21  type pingResponseHandler struct {
    22  	BaseMsgHandler
    23  }
    24  
    25  var _ p2pcommon.MessageHandler = (*pingResponseHandler)(nil)
    26  
    27  type goAwayHandler struct {
    28  	BaseMsgHandler
    29  }
    30  
    31  var _ p2pcommon.MessageHandler = (*goAwayHandler)(nil)
    32  
    33  // newPingReqHandler creates handler for PingRequest
    34  func NewPingReqHandler(pm p2pcommon.PeerManager, peer p2pcommon.RemotePeer, logger *log.Logger, actor p2pcommon.ActorService) *pingRequestHandler {
    35  	ph := &pingRequestHandler{BaseMsgHandler: BaseMsgHandler{protocol: p2pcommon.PingRequest, pm: pm, peer: peer, actor: actor, logger: logger}}
    36  	return ph
    37  }
    38  
    39  func (ph *pingRequestHandler) ParsePayload(rawbytes []byte) (p2pcommon.MessageBody, error) {
    40  	return p2putil.UnmarshalAndReturn(rawbytes, &types.Ping{})
    41  }
    42  
    43  func (ph *pingRequestHandler) Handle(msg p2pcommon.Message, msgBody p2pcommon.MessageBody) {
    44  	remotePeer := ph.peer
    45  	pingData := msgBody.(*types.Ping)
    46  	p2putil.DebugLogReceive(ph.logger, ph.protocol, msg.ID().String(), remotePeer, pingData)
    47  	if _, err := types.ParseToBlockID(pingData.GetBestBlockHash()); err != nil {
    48  		ph.logger.Info().Str(p2putil.LogPeerName, remotePeer.Name()).Msg("ping is old format or wrong")
    49  		return
    50  	}
    51  	remotePeer.UpdateLastNotice(pingData.BestBlockHash, pingData.BestHeight)
    52  
    53  	// generate response message
    54  	ph.logger.Debug().Str(p2putil.LogPeerName, remotePeer.Name()).Str(p2putil.LogMsgID, msg.ID().String()).Msg("Sending ping response")
    55  	resp := &types.Pong{}
    56  	remotePeer.SendMessage(remotePeer.MF().NewMsgResponseOrder(msg.ID(), p2pcommon.PingResponse, resp))
    57  }
    58  
    59  // newPingRespHandler creates handler for PingResponse
    60  func NewPingRespHandler(pm p2pcommon.PeerManager, peer p2pcommon.RemotePeer, logger *log.Logger, actor p2pcommon.ActorService) *pingResponseHandler {
    61  	ph := &pingResponseHandler{BaseMsgHandler: BaseMsgHandler{protocol: p2pcommon.PingResponse, pm: pm, peer: peer, actor: actor, logger: logger}}
    62  	return ph
    63  }
    64  
    65  func (ph *pingResponseHandler) ParsePayload(rawbytes []byte) (p2pcommon.MessageBody, error) {
    66  	return p2putil.UnmarshalAndReturn(rawbytes, &types.Pong{})
    67  }
    68  
    69  func (ph *pingResponseHandler) Handle(msg p2pcommon.Message, msgBody p2pcommon.MessageBody) {
    70  	remotePeer := ph.peer
    71  	//data := msgBody.(*types.Pong)
    72  	p2putil.DebugLogReceive(ph.logger, ph.protocol, msg.ID().String(), remotePeer, nil)
    73  	remotePeer.ConsumeRequest(msg.ID())
    74  }
    75  
    76  // newGoAwayHandler creates handler for PingResponse
    77  func NewGoAwayHandler(pm p2pcommon.PeerManager, peer p2pcommon.RemotePeer, logger *log.Logger, actor p2pcommon.ActorService) *goAwayHandler {
    78  	ph := &goAwayHandler{BaseMsgHandler: BaseMsgHandler{protocol: p2pcommon.GoAway, pm: pm, peer: peer, actor: actor, logger: logger}}
    79  	return ph
    80  }
    81  
    82  func (ph *goAwayHandler) ParsePayload(rawbytes []byte) (p2pcommon.MessageBody, error) {
    83  	return p2putil.UnmarshalAndReturn(rawbytes, &types.GoAwayNotice{})
    84  }
    85  
    86  func (ph *goAwayHandler) Handle(msg p2pcommon.Message, msgBody p2pcommon.MessageBody) {
    87  	data := msgBody.(*types.GoAwayNotice)
    88  	p2putil.DebugLogReceive(ph.logger, ph.protocol, msg.ID().String(), ph.peer, data)
    89  
    90  	// TODO: check to remove peer here or not. (the sending peer will disconnect.)
    91  }