network

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 19, 2015 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNoPeers = errors.New("no peers")

	// hard-coded addresses used when bootstrapping
	BootstrapPeers = []Address{
		"23.239.14.98:9988",
	}
)

Functions

func Ping

func Ping(addr Address) bool

Ping returns whether an Address is reachable and responds correctly to the ping request -- in other words, whether it is a potential peer.

Types

type Address

type Address string

An Address contains the information needed to contact a peer over TCP.

func (Address) Call

func (na Address) Call(name string, fn func(net.Conn) error) error

Call establishes a TCP connection to the Address, calls the provided function on it, and closes the connection.

func (*Address) RPC

func (na *Address) RPC(name string, arg, resp interface{}) error

RPC performs a Remote Procedure Call by sending the procedure name and encoded argument, and decoding the response into the supplied object. 'resp' must be a pointer. If arg is nil, no object is sent. If 'resp' is nil, no response is read.

type TCPServer

type TCPServer struct {
	net.Listener

	// used to protect addressbook and handlerMap
	sync.RWMutex
	// contains filtered or unexported fields
}

A TCPServer sends and receives messages. It also maintains an address book of peers to broadcast to and make requests of.

func NewTCPServer

func NewTCPServer(addr string) (tcps *TCPServer, err error)

NewTCPServer creates a TCPServer that listens on the specified address.

func (*TCPServer) AddPeer

func (tcps *TCPServer) AddPeer(addr Address) error

AddPeer safely adds a peer to the address book.

func (*TCPServer) Address

func (tcps *TCPServer) Address() Address

Address returns the Address of the server.

func (*TCPServer) AddressBook

func (tcps *TCPServer) AddressBook() []Address

AddressBook returns the server's address book as a slice.

func (*TCPServer) Bootstrap

func (tcps *TCPServer) Bootstrap() (err error)

Bootstrap discovers the external IP of the TCPServer, requests peers from the initial peer list, and announces itself to those peers.

func (*TCPServer) Broadcast

func (tcps *TCPServer) Broadcast(name string, arg, resp interface{})

Broadcast calls the RPC on each peer in the address book.

func (*TCPServer) RandomPeer

func (tcps *TCPServer) RandomPeer() Address

RandomPeer selects and returns a random peer from the address book.

func (*TCPServer) RegisterRPC

func (tcps *TCPServer) RegisterRPC(name string, fn interface{}) error

RegisterRPC registers a function as an RPC handler for a given identifier. The function must be one of four possible types:

func(net.Conn) error
func(Type) (Type, error)
func(Type) error
func() (Type, error)

To call an RPC, use Address.RPC, supplying the same identifier given to RegisterRPC. Identifiers should always use PascalCase.

func (*TCPServer) RemovePeer

func (tcps *TCPServer) RemovePeer(addr Address) error

Remove safely removes a peer from the address book.

Jump to

Keyboard shortcuts

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