github.com/annchain/OG@v0.0.9/plugin/og/communicator.go (about)

     1  package og
     2  
     3  import (
     4  	"github.com/annchain/OG/arefactor/common/utilfuncs"
     5  	general_communication "github.com/annchain/OG/communication"
     6  	general_message "github.com/annchain/OG/message"
     7  	"github.com/annchain/OG/ogcore/communication"
     8  	"github.com/annchain/OG/ogcore/message"
     9  )
    10  
    11  type ProxyOgPeerCommunicator struct {
    12  	OgMessageAdapter OgMessageAdapter // either TrustfulBftAdapter or PlainBftAdapter
    13  	GeneralOutgoing  general_communication.GeneralPeerCommunicatorOutgoing
    14  	pipe             chan *communication.OgMessageEvent
    15  }
    16  
    17  func (p *ProxyOgPeerCommunicator) Broadcast(msg message.OgMessage) {
    18  	ogMessage, err := p.OgMessageAdapter.AdaptOgMessage(msg)
    19  	utilfuncs.PanicIfError(err, "Adapter for annsensus should never fail")
    20  	p.GeneralOutgoing.Broadcast(ogMessage)
    21  }
    22  
    23  func (p *ProxyOgPeerCommunicator) InitDefault() {
    24  	p.pipe = make(chan *communication.OgMessageEvent)
    25  }
    26  
    27  func (p ProxyOgPeerCommunicator) Multicast(msg message.OgMessage, peers []communication.OgPeer) {
    28  	if p.pipe == nil {
    29  		panic("not initialized.")
    30  	}
    31  
    32  	ogMessage, err := p.OgMessageAdapter.AdaptOgMessage(msg)
    33  	utilfuncs.PanicIfError(err, "Adapter for annsensus should never fail")
    34  
    35  	ogPeers := make([]general_message.GeneralPeer, len(peers))
    36  	for i, peer := range peers {
    37  		adaptedValue, err := p.OgMessageAdapter.AdaptOgPeer(&peer)
    38  		utilfuncs.PanicIfError(err, "Adapter for annsensus peer should never fail")
    39  		ogPeers[i] = adaptedValue
    40  	}
    41  	p.GeneralOutgoing.Multicast(ogMessage, ogPeers)
    42  }
    43  
    44  func (p ProxyOgPeerCommunicator) Unicast(msg message.OgMessage, peer *communication.OgPeer) {
    45  	if p.pipe == nil {
    46  		panic("not initialized.")
    47  	}
    48  
    49  	ogMessage, err := p.OgMessageAdapter.AdaptOgMessage(msg)
    50  	utilfuncs.PanicIfError(err, "Adapter for annsensus should never fail")
    51  	ogPeer, err := p.OgMessageAdapter.AdaptOgPeer(peer)
    52  	utilfuncs.PanicIfError(err, "Adapter for annsensus peer should never fail")
    53  	p.GeneralOutgoing.Unicast(ogMessage, ogPeer)
    54  }
    55  
    56  func (p ProxyOgPeerCommunicator) GetPipeIn() chan *communication.OgMessageEvent {
    57  	if p.pipe == nil {
    58  		panic("not initialized.")
    59  	}
    60  
    61  	return p.pipe
    62  }
    63  
    64  func (p ProxyOgPeerCommunicator) GetPipeOut() chan *communication.OgMessageEvent {
    65  	if p.pipe == nil {
    66  		panic("not initialized.")
    67  	}
    68  
    69  	return p.pipe
    70  }