Documentation
¶
Index ¶
- Constants
- type Peer
- type PeerStore
- func (ps *PeerStore) Add(p *Peer)
- func (ps *PeerStore) Addrs() []string
- func (ps *PeerStore) Broadcast(push msg.Push)
- func (ps *PeerStore) ConnectionHandler(c net.Conn)
- func (ps *PeerStore) Get(addr string) *Peer
- func (ps *PeerStore) GetRandom() *Peer
- func (ps *PeerStore) MaintainConnections(wg *sync.WaitGroup)
- func (ps *PeerStore) PeerInfoHandler(res *msg.Response)
- func (ps *PeerStore) Remove(addr string)
- func (ps *PeerStore) RemoveRandom()
- func (ps *PeerStore) SetDefaultPushHandler(ph PushHandler)
- func (ps *PeerStore) SetDefaultRequestHandler(rh RequestHandler)
- func (ps *PeerStore) Size() int
- type PushHandler
- type RequestHandler
- type ResponseHandler
Constants ¶
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 ¶
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 (*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 ¶
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 ¶
NewPeerStore returns an initialized peerstore.
func (*PeerStore) Add ¶
Add synchronously adds the given peer to the peerstore
func (*PeerStore) Addrs ¶
Addrs returns the list of addresses of the peers in the peerstore in the form <IP addr>:<port>
func (*PeerStore) Broadcast ¶
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 ¶
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 ¶
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 ¶
GetRandom synchronously retreives a random peer from the peerstore Returns nil if the PeerStore is empty
func (*PeerStore) MaintainConnections ¶
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 ¶
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 ¶
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().
type PushHandler ¶
PushHandler is any function that handles a push message.
type RequestHandler ¶
RequestHandler is any function that returns a response to a request.
Source Files
¶
- peer.go
- peerstore.go