github.com/aergoio/aergo@v1.3.1/p2p/p2putil/pipelistener.go (about)

     1  package p2putil
     2  
     3  // MultiListener can contain multiple unit listeners and toss events
     4  type MultiListener struct {
     5  	ls []PipeEventListener
     6  }
     7  
     8  func NewMultiListener(ls ...PipeEventListener) *MultiListener {
     9  	return &MultiListener{ls: ls}
    10  }
    11  
    12  func (ml *MultiListener) AppendListener(l PipeEventListener) {
    13  	ml.ls = append(ml.ls, l)
    14  }
    15  func (ml *MultiListener) OnIn(element interface{}) {
    16  	for _, l := range ml.ls {
    17  		l.OnIn(element)
    18  	}
    19  }
    20  
    21  func (ml *MultiListener) OnDrop(element interface{}) {
    22  	for _, l := range ml.ls {
    23  		l.OnDrop(element)
    24  	}
    25  }
    26  
    27  func (ml *MultiListener) OnOut(element interface{}) {
    28  	for _, l := range ml.ls {
    29  		l.OnOut(element)
    30  	}
    31  }
    32  
    33  // StatListener make summation
    34  type StatListener struct {
    35  	incnt      uint64
    36  	outcnt     uint64
    37  	dropcnt    uint64
    38  	consecdrop uint64
    39  }
    40  
    41  func NewStatLister() *StatListener {
    42  	return &StatListener{}
    43  }
    44  
    45  func (l *StatListener) OnIn(element interface{}) {
    46  	l.incnt++
    47  }
    48  
    49  func (l *StatListener) OnDrop(element interface{}) {
    50  	l.dropcnt++
    51  	l.consecdrop++
    52  }
    53  
    54  func (l *StatListener) OnOut(element interface{}) {
    55  	l.outcnt++
    56  	l.consecdrop = 0
    57  }