tendermint/p2p
Jae Kwon 6d6f061f19 Support nil pointers for Binary.
If the thing does not already have a typebyte declared,
a fake one will be given (0x01).
A TypeByte of 0x00 is reserved for nil things.
No nil-dogs.
2015-04-12 17:46:16 -07:00
..
upnp Package import path change 2015-04-01 17:30:16 -07:00
README.md Changed config to confer, TOML format. 2015-01-08 16:40:23 -08:00
addrbook.go Package import path change 2015-04-01 17:30:16 -07:00
addrbook_test.go addrbook cleanup 2014-07-10 02:19:50 -07:00
connection.go Support nil pointers for Binary. 2015-04-12 17:46:16 -07:00
listener.go various changes. removed debora temporarily 2015-04-03 16:15:52 -07:00
log.go Package import path change 2015-04-01 17:30:16 -07:00
netaddress.go P2P docs 2014-12-23 19:31:24 -08:00
peer.go Package import path change 2015-04-01 17:30:16 -07:00
peer_set.go BlockchainReactor syncs first before ConsensusReactor. 2015-03-25 11:50:28 -07:00
peer_set_test.go Package import path change 2015-04-01 17:30:16 -07:00
pex_reactor.go Some renames and small fixes. 2015-04-08 12:30:49 -07:00
switch.go Intermediate... working on debora 2015-04-08 11:35:17 -07:00
switch_test.go various changes. removed debora temporarily 2015-04-03 16:15:52 -07:00
util.go cleanup 2014-07-09 14:27:32 -07:00

README.md

tendermint/p2p

tendermint/p2p provides an abstraction around peer-to-peer communication.

Peer/MConnection/Channel

Each peer has one MConnection (multiplex connection) instance.

multiplex noun a system or signal involving simultaneous transmission of several messages along a single channel of communication.

Each MConnection handles message transmission on multiple abstract communication Channels. Each channel has a globally unique byte id. The byte id and the relative priorities of each Channel are configured upon initialization of the connection.

There are two methods for sending messages:

func (m MConnection) Send(chId byte, msg interface{}) bool {}
func (m MConnection) TrySend(chId byte, msg interface{}) bool {}

Send(chId, msg) is a blocking call that waits until msg is successfully queued for the channel with the given id byte chId. The message msg is serialized using the tendermint/binary submodule's WriteBinary() reflection routine.

TrySend(chId, msg) is a nonblocking call that returns false if the channel's queue is full.

Send() and TrySend() are also exposed for each Peer.

Switch/Reactor

The Switch handles peer connections and exposes an API to receive incoming messages on Reactors. Each Reactor is responsible for handling incoming messages of one or more Channels. So while sending outgoing messages is typically performed on the peer, incoming messages are received on the reactor.

// Declare a MyReactor reactor that handles messages on MyChannelId.
type MyReactor struct{}

func (reactor MyReactor) GetChannels() []*ChannelDescriptor {
    return []*ChannelDescriptor{ChannelDescriptor{Id:MyChannelId, Priority: 1}}
}

func (reactor MyReactor) Receive(chId byte, peer *Peer, msgBytes []byte) {
    r, n, err := bytes.NewBuffer(msgBytes), new(int64), new(error)
    msgString := ReadString(r, n, err)
    fmt.Println(msgString)
}

// Other Reactor methods omitted for brevity
...

switch := NewSwitch([]Reactor{MyReactor{}})

...

// Send a random message to all outbound connections
for _, peer := range switch.Peers().List() {
    if peer.IsOutbound() {
        peer.Send(MyChannelId, "Here's a random message")
    }
}

PexReactor/AddrBook

A PEXReactor reactor implementation is provided to automate peer discovery.

book := p2p.NewAddrBook(config.App.GetString("AddrBookFile"))
pexReactor := p2p.NewPEXReactor(book)
...
switch := NewSwitch([]Reactor{pexReactor, myReactor, ...})