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

     1  package subproto
     2  
     3  import (
     4  	"github.com/aergoio/aergo-lib/log"
     5  	"github.com/aergoio/aergo/p2p/p2pcommon"
     6  )
     7  
     8  // func(msg *types.P2PMessage)
     9  // BaseMsgHandler contains common attributes of MessageHandler
    10  type BaseMsgHandler struct {
    11  	protocol p2pcommon.SubProtocol
    12  
    13  	pm p2pcommon.PeerManager
    14  	sm p2pcommon.SyncManager
    15  
    16  	peer  p2pcommon.RemotePeer
    17  	actor p2pcommon.ActorService
    18  
    19  	logger    *log.Logger
    20  
    21  	advice []p2pcommon.HandlerAdvice
    22  	advSize int
    23  }
    24  
    25  func (bh *BaseMsgHandler) CheckAuth(msg p2pcommon.Message, msgBody p2pcommon.MessageBody) error {
    26  	// check permissions
    27  	// or etc...
    28  
    29  	return nil
    30  }
    31  
    32  func (bh *BaseMsgHandler) AddAdvice(advice p2pcommon.HandlerAdvice) {
    33  	bh.advice = append(bh.advice, advice)
    34  	bh.advSize = len(bh.advice)
    35  }
    36  
    37  func (bh *BaseMsgHandler) PreHandle() {
    38  	for i := bh.advSize-1 ; i>=0; i-- {
    39  		bh.advice[i].PreHandle()
    40  	}
    41  }
    42  
    43  func (bh *BaseMsgHandler) PostHandle(msg p2pcommon.Message, msgBody p2pcommon.MessageBody) {
    44  	for i := 0 ; i<bh.advSize; i++ {
    45  		bh.advice[i].PostHandle(msg, msgBody)
    46  	}
    47  }
    48  
    49  // asyncHelper is
    50  type asyncHelper struct {
    51  	w chan int
    52  }
    53  
    54  func newAsyncHelper() asyncHelper {
    55  	h := asyncHelper{w: make(chan int,1)}
    56  	h.w <- 1
    57  	return h
    58  }
    59  
    60  func (th *asyncHelper) issue() bool {
    61  	// temporarily disable exclusive locking
    62  	return true
    63  	//select {
    64  	//case <-th.w:
    65  	//	return true
    66  	//default:
    67  	//	return false
    68  	//}
    69  }
    70  func (th *asyncHelper) release() {
    71  	// temporarily disable exclusive locking
    72  	//th.w <- 1
    73  }