github.com/annchain/OG@v0.0.9/debug/dep/dummy.go (about)

     1  package dep
     2  
     3  import (
     4  	"github.com/annchain/OG/common"
     5  	"github.com/annchain/OG/common/crypto"
     6  	"github.com/annchain/OG/message"
     7  	"github.com/sirupsen/logrus"
     8  )
     9  
    10  // LocalGeneralPeerCommunicator is the place for change GeneralMessage locally.
    11  type LocalGeneralPeerCommunicator struct {
    12  	Myid    int
    13  	PeerIns []chan *message.GeneralMessageEvent
    14  	pipe    chan *message.GeneralMessageEvent //pipeIn is the receiver of the outside messages
    15  	me      message.GeneralPeer
    16  }
    17  
    18  func (d *LocalGeneralPeerCommunicator) Broadcast(msg message.GeneralMessage) {
    19  	outMsg := &message.GeneralMessageEvent{
    20  		Message: msg,
    21  		Sender:  d.me,
    22  	}
    23  	for i, peerChan := range d.PeerIns {
    24  		go func(peerChan chan *message.GeneralMessageEvent, i int) {
    25  			logrus.WithFields(logrus.Fields{
    26  				"from":    d.me.Id,
    27  				"to":      i,
    28  				"content": msg.String(),
    29  				"type":    msg.GetType()}).Trace("Sending message")
    30  
    31  			//<- ffchan.NewTimeoutSenderShort(d.PeerIns[peer.Id], outMsg, "bft").C
    32  			peerChan <- outMsg
    33  		}(peerChan, i)
    34  	}
    35  }
    36  
    37  func (d *LocalGeneralPeerCommunicator) HandleIncomingMessage(msgEvent *message.GeneralMessageEvent) {
    38  	d.pipe <- msgEvent
    39  }
    40  
    41  func NewLocalGeneralPeerCommunicator(myid int, incoming chan *message.GeneralMessageEvent, peers []chan *message.GeneralMessageEvent) *LocalGeneralPeerCommunicator {
    42  	d := &LocalGeneralPeerCommunicator{
    43  		PeerIns: peers,
    44  		Myid:    myid,
    45  		pipe:    incoming,
    46  		me: message.GeneralPeer{
    47  			Id:             myid,
    48  			PublicKey:      crypto.PublicKey{},
    49  			Address:        common.Address{},
    50  			PublicKeyBytes: nil,
    51  		},
    52  	}
    53  	return d
    54  }
    55  
    56  func (d *LocalGeneralPeerCommunicator) Multicast(msg message.GeneralMessage, peers []message.GeneralPeer) {
    57  	for _, peer := range peers {
    58  		go func(peer message.GeneralPeer) {
    59  			logrus.WithFields(logrus.Fields{
    60  				"from":    d.me.Id,
    61  				"to":      peer.Id,
    62  				"content": msg.String(),
    63  				"type":    msg.GetType()}).Trace("Sending message")
    64  			outMsg := &message.GeneralMessageEvent{
    65  				Message: msg,
    66  				Sender:  d.me,
    67  			}
    68  			//<- ffchan.NewTimeoutSenderShort(d.PeerIns[peer.Id], outMsg, "bft").C
    69  			d.PeerIns[peer.Id] <- outMsg
    70  		}(peer)
    71  
    72  	}
    73  }
    74  
    75  func (d *LocalGeneralPeerCommunicator) Unicast(msg message.GeneralMessage, peer message.GeneralPeer) {
    76  	go func() {
    77  		logrus.WithFields(logrus.Fields{
    78  			"from":    d.me.Id,
    79  			"to":      peer.Id,
    80  			"content": msg.String(),
    81  			"type":    msg.GetType()}).Trace("Sending message")
    82  		outMsg := &message.GeneralMessageEvent{
    83  			Message: msg,
    84  			Sender:  d.me,
    85  		}
    86  		//<- ffchan.NewTimeoutSenderShort(d.PeerIns[peer.Id], outMsg, "lgp").C
    87  		d.PeerIns[peer.Id] <- outMsg
    88  	}()
    89  }
    90  
    91  func (d *LocalGeneralPeerCommunicator) GetPipeIn() chan *message.GeneralMessageEvent {
    92  	return d.pipe
    93  }
    94  
    95  func (d *LocalGeneralPeerCommunicator) GetPipeOut() chan *message.GeneralMessageEvent {
    96  	return d.pipe
    97  }