Go to file
Anton Kaliaev 590efc1040
call saveToFile OnStop
This is better than waiting because while we wait, anything could happen
(crash, timeout of the code who's using addrbook, ...). If we save
immediately, we have much greater chances of success.
2017-04-20 13:36:38 +04:00
upnp initial commit 2015-10-25 18:21:51 -07:00
.gitignore add glide 2017-04-10 22:47:04 +04:00
CHANGELOG.md CHANGELOG and version bump 2017-04-18 23:58:24 -04:00
Dockerfile add Dockerfile 2017-04-20 13:36:37 +04:00
LICENSE Change license to Apache2.0 2015-12-23 14:08:39 -08:00
README.md circle badge 2016-06-21 14:40:24 -04:00
addrbook.go call saveToFile OnStop 2017-04-20 13:36:38 +04:00
addrbook_test.go more tests for AddrBook 2017-03-04 22:44:25 -05:00
config.go test peer 2017-04-12 16:55:17 +04:00
connection.go add a comment for MConnection#CanSend 2017-04-18 12:11:48 +04:00
connection_test.go add a comment for MConnection#CanSend 2017-04-18 12:11:48 +04:00
fuzz.go [fuzz] only one way to set config variables 2017-04-13 11:55:14 +04:00
glide.lock update glide 2017-04-19 00:01:55 -04:00
glide.yaml update glide 2017-04-19 00:01:55 -04:00
ip_range_counter.go Add RemoteAddr and ListenAddr to NodeInfo; Refactor IPRange logic 2015-12-05 09:44:03 -08:00
listener.go dial seeds error handling 2017-03-03 16:42:10 -05:00
listener_test.go MakeConnectedSwitches function 2016-06-26 00:34:23 -04:00
log.go initial commit 2015-10-25 18:21:51 -07:00
netaddress.go [netaddress] panic only when normal run 2017-04-14 14:56:02 +04:00
netaddress_test.go tests for NetAddress 2017-04-14 16:37:07 +04:00
peer.go test peer with no auth enc 2017-04-14 12:43:28 +04:00
peer_set.go Add RemoteAddr and ListenAddr to NodeInfo; Refactor IPRange logic 2015-12-05 09:44:03 -08:00
peer_set_test.go Add RemoteAddr and ListenAddr to NodeInfo; Refactor IPRange logic 2015-12-05 09:44:03 -08:00
peer_test.go refactor MConnection#sendBytes 2017-04-14 14:21:58 +04:00
pex_reactor.go improve ensurePeers routine 2017-04-20 13:36:38 +04:00
pex_reactor_test.go do not create file, just temp dir 2017-04-20 13:36:38 +04:00
secret_connection.go Confirm to go-wire new TypeByte behavior 2015-12-21 12:55:52 -08:00
secret_connection_test.go initial commit 2015-10-25 18:21:51 -07:00
switch.go test ensurePeers goroutine 2017-04-20 13:36:37 +04:00
switch_test.go test peer with no auth enc 2017-04-14 12:43:28 +04:00
types.go close conns on filter; fix order in MakeConnectedSwitch 2017-01-12 20:49:48 -05:00
util.go initial commit 2015-10-25 18:21:51 -07:00
version.go CHANGELOG and version bump 2017-04-18 23:58:24 -04:00

README.md

tendermint/go-p2p

CircleCI

tendermint/go-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/wire 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(addrBookFilePath)
pexReactor := p2p.NewPEXReactor(book)
...
switch := NewSwitch([]Reactor{pexReactor, myReactor, ...})