peer

package
v0.0.0-...-c1f1bd4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// DefaultPort is the TCP port hosts will communicate over if none is
	// provided
	DefaultPort = 8000
	// DefaultIP is the IP address new hosts will use if none if provided
	DefaultIP = "127.0.0.1"
	// DefaultRequestTimeout is the default amount of time we will wait for a
	// peer to return a response to a request
	DefaultRequestTimeout = time.Second * 10
	// MessageWaitTime is the amount of time the dispatcher should wait before
	// attempting to read from the connection again when no data was received
	MessageWaitTime = time.Second * 2
	// MaxPeers is the maximum number of peers we can be connected to at a time
	MaxPeers = 50
	// PeerSearchWaitTime is the amount of time the maintainConnections goroutine
	// will wait before checking if we can connect to more peers when is sees that
	// our PeerStore is full.
	PeerSearchWaitTime = time.Second * 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Peer

type Peer struct {
	Connection net.Conn
	Store      *PeerStore
	ListenAddr string
	// contains filtered or unexported fields
}

Peer represents a remote peer we are connected to.

func Connect

func Connect(address string, ps *PeerStore) (*Peer, error)

Connect attempts to establish a connection with a peer given its listen address (in the form <IP address>:<TCP port>). If successful returns the peer, otherwise returns error. Once the connection is established the peer will be added to the given PeerStore and returned.

func New

func New(c net.Conn, ps *PeerStore, listenAddr string) *Peer

New returns a new Peer

func (*Peer) Dispatch

func (p *Peer) Dispatch()

Dispatch listens on this peer's Connection and passes received messages to the appropriate message handlers.

func (*Peer) Push

func (p *Peer) Push(push msg.Push) error

Push sends a push message to this peer. Returns an error if the push message could not be written.

func (*Peer) Request

func (p *Peer) Request(req msg.Request, rh ResponseHandler) error

Request sends the given request over this peer's Connection and registers the given response handler to be called when the response arrives at the dispatcher. implementations. Returns error if request could not be written.

func (*Peer) SetPushHandler

func (p *Peer) SetPushHandler(ph PushHandler)

SetPushHandler will add the given push handler to this peer. The push handler must be set for this peer to handle received push messages and is NOT set by default.

func (*Peer) SetRequestHandler

func (p *Peer) SetRequestHandler(rh RequestHandler)

SetRequestHandler will add the given request handler to this peer. The request handler must be set for this peer to handle received request messages and is NOT set by default.

type PeerStore

type PeerStore struct {
	ListenAddr string
	// contains filtered or unexported fields
}

PeerStore is a thread-safe container for all the peers we are currently connected to. It maps remote peer listen addresses to Peer objects.

func NewPeerStore

func NewPeerStore(la string) *PeerStore

NewPeerStore returns an initialized peerstore.

func (*PeerStore) Add

func (ps *PeerStore) Add(p *Peer)

Add synchronously adds the given peer to the peerstore

func (*PeerStore) Addrs

func (ps *PeerStore) Addrs() []string

Addrs returns the list of addresses of the peers in the peerstore in the form <IP addr>:<port>

func (*PeerStore) Broadcast

func (ps *PeerStore) Broadcast(push msg.Push)

Broadcast sends the given push message to all peers in the PeerStore at the time this function is called. Note that if we fail to write the push message to a peer the failure is ignored. Generally this is okay, because push messages sent via Broadcast() should be propagated by other peers.

func (*PeerStore) ConnectionHandler

func (ps *PeerStore) ConnectionHandler(c net.Conn)

ConnectionHandler is called when a new connection is opened with us by a remote peer. It will create a dispatcher and message handlers to handle retrieving messages over the new connection and sending them to App.

func (*PeerStore) Get

func (ps *PeerStore) Get(addr string) *Peer

Get synchronously retreives the peer with the given id from the peerstore Returns nil if there is no peer with the given id

func (*PeerStore) GetRandom

func (ps *PeerStore) GetRandom() *Peer

GetRandom synchronously retreives a random peer from the peerstore Returns nil if the PeerStore is empty

func (*PeerStore) MaintainConnections

func (ps *PeerStore) MaintainConnections(wg *sync.WaitGroup)

MaintainConnections will infinitely attempt to maintain as close to MaxPeers connections as possible by requesting PeerInfo from peers in the PeerStore and establishing connections with newly discovered peers. NOTE: this should be called only once and should be run as a goroutine.

func (*PeerStore) PeerInfoHandler

func (ps *PeerStore) PeerInfoHandler(res *msg.Response)

PeerInfoHandler will handle the response to a PeerInfo request by attempting to establish connections with all new peers in the given response Resource.

func (*PeerStore) Remove

func (ps *PeerStore) Remove(addr string)

Remove synchronously removes the given peer from the peerstore

func (*PeerStore) RemoveRandom

func (ps *PeerStore) RemoveRandom()

RemoveRandom removes a random peer from the given PeerStore

func (*PeerStore) SetDefaultPushHandler

func (ps *PeerStore) SetDefaultPushHandler(ph PushHandler)

SetDefaultPushHandler will ensure that all new peers created who's PushHandlers have not been set will use the given request handler by default until it is overridden by the call to SetPushHandler().

func (*PeerStore) SetDefaultRequestHandler

func (ps *PeerStore) SetDefaultRequestHandler(rh RequestHandler)

SetDefaultRequestHandler will ensure that all new peers created who's RequestHandlers have not been set will use the given request handler by default until it is overridden by the call to SetRequestHandler().

func (*PeerStore) Size

func (ps *PeerStore) Size() int

Size synchornously returns the number of peers in the PeerStore

type PushHandler

type PushHandler func(*msg.Push)

PushHandler is any function that handles a push message.

type RequestHandler

type RequestHandler func(*msg.Request) msg.Response

RequestHandler is any function that returns a response to a request.

type ResponseHandler

type ResponseHandler func(*msg.Response)

ResponseHandler is any function that handles a response to a request.

Source Files

  • peer.go
  • peerstore.go

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL