func (sm simDirectMessage) Bytes() []byte {
	return sm.msg.Bytes()
}
Bytes is the message's binary data in byte array format.
package service
import (
	"errors"
	"io"
	"net"
	"sync"
	"time"
	"github.com/libonomy/libonomy-p2p/log"
	"github.com/libonomy/libonomy-p2p/p2p/node"
	"github.com/libonomy/libonomy-p2p/p2p/p2pcrypto"
	"github.com/libonomy/libonomy-p2p/priorityq"
	"github.com/libonomy/libonomy-p2p/rand"
)
type Simulator struct {
	io.Closer
	mutex                 sync.RWMutex
	protocolDirectHandler map[p2pcrypto.PublicKey]map[string]chan DirectMessage  
	protocolGossipHandler map[p2pcrypto.PublicKey]map[string]chan GossipMessage  
	nodes                 map[p2pcrypto.PublicKey]*Node
	subLock      sync.Mutex
	newPeersSubs []chan p2pcrypto.PublicKey
	delPeersSubs []chan p2pcrypto.PublicKey
}
var _ Service = new(Node)
type Node struct {
	sim *Simulator
	*node.Info
	sndDelay      uint32
	rcvDelay      uint32
	randBehaviour bool
}
func NewSimulator() *Simulator {
	s := &Simulator{
		protocolDirectHandler: make(map[p2pcrypto.PublicKey]map[string]chan DirectMessage),
		protocolGossipHandler: make(map[p2pcrypto.PublicKey]map[string]chan GossipMessage),
		nodes:                 make(map[p2pcrypto.PublicKey]*Node),
	}
	return s
}
func (s *Simulator) SubscribeToPeerEvents(myid p2pcrypto.Key) (chan p2pcrypto.PublicKey, chan p2pcrypto.PublicKey) {
	s.mutex.RLock()
	var keys []p2pcrypto.PublicKey
	for _, nd := range s.nodes {
		if nd.PublicKey() == myid {
			continue
		}
		keys = append(keys, nd.PublicKey())
	}
	s.mutex.RUnlock()
	newp := make(chan p2pcrypto.PublicKey, len(keys)+1)
	delp := make(chan p2pcrypto.PublicKey, len(keys)+1)
	s.subLock.Lock()
	s.newPeersSubs = append(s.newPeersSubs, newp)
	s.delPeersSubs = append(s.delPeersSubs, delp)
	s.subLock.Unlock()
	for _, x := range keys {
		newp <- x
	}
	return newp, delp
}
func (s *Simulator) publishNewPeer(peer p2pcrypto.PublicKey) {
	s.subLock.Lock()
	for _, ch := range s.newPeersSubs {
		log.Info("publish on chan with len %v, cap %v", len(ch), cap(ch))
		ch <- peer
	}
	s.subLock.Unlock()
}
func (s *Simulator) publishDelPeer(peer p2pcrypto.PublicKey) {
	s.subLock.Lock()
	for _, ch := range s.delPeersSubs {
		ch <- peer
	}
	s.subLock.Unlock()
}
func (s *Simulator) createdNode(n *Node) {
	s.mutex.Lock()
	s.protocolDirectHandler[n.PublicKey()] = make(map[string]chan DirectMessage)
	s.protocolGossipHandler[n.PublicKey()] = make(map[string]chan GossipMessage)
	s.nodes[n.PublicKey()] = n
	s.mutex.Unlock()
	s.publishNewPeer(n.PublicKey())
}
func (s *Simulator) NewFaulty(isRandBehaviour bool, maxBroadcastDelaySec uint32, maxReceiveDelaySec uint32) *Node {
	n := s.NewNode()
	n.randBehaviour = isRandBehaviour
	n.sndDelay = maxBroadcastDelaySec
	n.rcvDelay = maxReceiveDelaySec
	return n
}
func (s *Simulator) NewNode() *Node {
	n := node.GenerateRandomNodeData()
	sn := &Node{
		sim:  s,
		Info: n,
	}
	s.createdNode(sn)
	return sn
}
func (s *Simulator) NewNodeFrom(n *node.Info) *Node {
	sn := &Node{
		sim:  s,
		Info: n,
	}
	s.createdNode(sn)
	return sn
}
type simDirectMessage struct {
	metadata P2PMetadata
	msg      Data
	sender   p2pcrypto.PublicKey
}
func (sm simDirectMessage) Metadata() P2PMetadata {
	return sm.metadata
}
func (sm simDirectMessage) Data() Data {
	return sm.msg
}