github.com/annchain/OG@v0.0.9/engine_test/dummy_go_test.go (about) 1 package engine_test 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) HandleIncomingMessage(msgEvent *message.GeneralMessageEvent) { 19 d.pipe <- msgEvent 20 } 21 22 func NewLocalGeneralPeerCommunicator(myid int, incoming chan *message.GeneralMessageEvent, peers []chan *message.GeneralMessageEvent) *LocalGeneralPeerCommunicator { 23 d := &LocalGeneralPeerCommunicator{ 24 PeerIns: peers, 25 Myid: myid, 26 pipe: incoming, 27 me: message.GeneralPeer{ 28 Id: myid, 29 PublicKey: crypto.PublicKey{}, 30 Address: common.Address{}, 31 PublicKeyBytes: nil, 32 }, 33 } 34 return d 35 } 36 37 func (d *LocalGeneralPeerCommunicator) Broadcast(msg message.GeneralMessage) { 38 outMsg := &message.GeneralMessageEvent{ 39 Message: msg, 40 Sender: d.me, 41 } 42 for i, peerChan := range d.PeerIns { 43 go func(peerChan chan *message.GeneralMessageEvent, i int) { 44 logrus.WithFields(logrus.Fields{ 45 "from": d.me.Id, 46 "to": i, 47 "content": msg.String(), 48 "type": msg.GetType()}).Trace("Sending message") 49 50 //<- ffchan.NewTimeoutSenderShort(d.PeerIns[peer.Id], outMsg, "bft").C 51 peerChan <- outMsg 52 }(peerChan, i) 53 } 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 func (d *LocalGeneralPeerCommunicator) Unicast(msg message.GeneralMessage, peer message.GeneralPeer) { 75 go func() { 76 logrus.WithFields(logrus.Fields{ 77 "from": d.me.Id, 78 "to": peer.Id, 79 "content": msg.String(), 80 "type": msg.GetType()}).Trace("Sending message") 81 outMsg := &message.GeneralMessageEvent{ 82 Message: msg, 83 Sender: d.me, 84 } 85 //<-ffchan.NewTimeoutSenderShort(d.PeerIns[peer.Id], outMsg, "lgp").C 86 d.PeerIns[peer.Id] <- outMsg 87 }() 88 } 89 90 func (d *LocalGeneralPeerCommunicator) GetPipeIn() chan *message.GeneralMessageEvent { 91 return d.pipe 92 } 93 94 func (d *LocalGeneralPeerCommunicator) GetPipeOut() chan *message.GeneralMessageEvent { 95 return d.pipe 96 }