coconet

package
v0.0.0-...-3b8f57b Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2015 License: GPL-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("connection closed")

ErrClosed indicates that the connection has been closed.

View Source
var ErrNotEstablished = errors.New("connection not established")

ErrNotEstablished indicates that the connection has not been successfully established through a call to Connect yet. It does not indicate whether the failure was permanent or temporary.

View Source
var ErrPeerExists = errors.New("peer already exists in given directory")

ErrPeerExists is an ignorable error that says that this peer has already been registered to this directory.

View Source
var Latency = 100

Functions

func IsTemporary

func IsTemporary(err error) bool

IsTemporary returns true if it is a temporary error.

Types

type BinaryMarshaler

type BinaryMarshaler interface {
	MarshalBinary() (data []byte, err error)
}

Taken from: http://golang.org/pkg/encoding/#BinaryMarshaler All messages passing through our conn must implement their own BinaryMarshaler

type BinaryUnmarshaler

type BinaryUnmarshaler interface {
	UnmarshalBinary(data []byte) error
}

Taken from: http://golang.org/pkg/encoding/#BinaryMarshaler All messages passing through our conn must implement their own BinaryUnmarshaler

type Conn

type Conn interface {
	// Name returns the name of the "to" end of the connectio.
	Name() string

	// PubKey returns the public key associated with the peer.
	PubKey() abstract.Point
	SetPubKey(abstract.Point)

	// Put puts data to the connection, calling the MarshalBinary method as needed.
	Put(data BinaryMarshaler) error
	// Get gets data from the connection, calling the UnmarshalBinary method as needed.
	// It blocks until it successfully receives data or there was a network error.
	// It returns io.EOF if the channel has been closed.
	Get(data BinaryUnmarshaler) error

	// Connect establishes the connection. Before using the Put and Get
	// methods of a Conn, Connect must first be called.
	Connect() error

	// Close closes the connection. Any blocked Put or Get operations will
	// be unblocked and return errors.
	Close()

	// Indicates whether the connection has been closed
	Closed() bool
}

Conn is an abstract bidirectonal connection. It abstracts away the network layer as well as the data-format for communication.

type FaultyHost

type FaultyHost struct {
	Host

	State HostState

	// DeadFor[x]=true is Host muslt play dead when x occurs
	// ex: DeadFor["commit"] is set true if host must fail on commit
	DeadFor map[string]bool
}

func NewFaultyHost

func NewFaultyHost(host Host, state ...HostState) *FaultyHost

func (*FaultyHost) Die

func (fh *FaultyHost) Die()

func (*FaultyHost) IsDead

func (fh *FaultyHost) IsDead() bool

returns true is host has failed (for simulating failures)

func (*FaultyHost) IsDeadFor

func (fh *FaultyHost) IsDeadFor(x string) bool

returns true is host should play dead in a specific case ex: DeadFor["commit"] is set true if host must fail on commit

func (*FaultyHost) SetDeadFor

func (fh *FaultyHost) SetDeadFor(x string, val bool)

ex: DeadFor["commit"] is set true if host must fail on commit

type GoConn

type GoConn struct {
	// contains filtered or unexported fields
}

GoConn implements the Conn interface. It emulates connections by using channels. uses channels for communication.

func NewGoConn

func NewGoConn(dir *GoDirectory, from, to string) (*GoConn, error)

NewGoConn creates a GoConn registered in the given directory with the given hostname. It returns an ignorable ErrPeerExists error if this peer already exists.

func (*GoConn) Close

func (c *GoConn) Close()

Close implements the Conn Close interface.

func (*GoConn) Closed

func (c *GoConn) Closed() bool

func (*GoConn) Connect

func (c *GoConn) Connect() error

Connect implements the Conn Connect interface. For GoConn's it is a no-op.

func (*GoConn) Get

func (c *GoConn) Get(bum BinaryUnmarshaler) error

Get receives data from the sender.

func (*GoConn) Name

func (c *GoConn) Name() string

Name implements the Conn Name interface. It returns the To end of the connection.

func (*GoConn) PubKey

func (c *GoConn) PubKey() abstract.Point

PubKey returns the public key of the connection.

func (*GoConn) Put

func (c *GoConn) Put(data BinaryMarshaler) error

Put puts data on the connection.

func (*GoConn) SetPubKey

func (c *GoConn) SetPubKey(pk abstract.Point)

SetPubKey sets the public key of the connection.

type GoDirectory

type GoDirectory struct {
	// protects accesses to channel and nameToPeer
	sync.RWMutex
	// contains filtered or unexported fields
}

GoDirectory is a testing structure for the goConn. It allows us to simulate tcp network connections locally (and is easily adaptable for network connections). A single directory should be shared between all goConns's that are operating in the same network 'space'. If they are on the same tree they should share a directory.

func NewGoDirectory

func NewGoDirectory() *GoDirectory

NewGoDirectory creates a new directory for registering GoConns.

func (*GoDirectory) Close

func (d *GoDirectory) Close()

type GoHost

type GoHost struct {
	PeerLock sync.RWMutex

	Ready map[string]bool
	// Peers asking to join overall tree structure of nodes
	// via connection to current host node
	PendingPeers map[string]bool

	Pubkey abstract.Point // own public key
	// contains filtered or unexported fields
}

GoHost is an implementation of the Host interface, that uses GoConns as its underlying connection type.

func NewGoHost

func NewGoHost(hostname string, dir *GoDirectory) *GoHost

NewGoHost creates a new GoHost with the given hostname, and registers it in the given directory.

func (*GoHost) AddChildren

func (h *GoHost) AddChildren(view int, cs ...string)

AddChildren adds children to the specified view.

func (*GoHost) AddParent

func (h *GoHost) AddParent(view int, c string)

AddParent adds a parent node to the specified view.

func (*GoHost) AddPeerToHostlist

func (h *GoHost) AddPeerToHostlist(view int, name string)

func (*GoHost) AddPeerToPending

func (h *GoHost) AddPeerToPending(p string)

func (*GoHost) AddPeers

func (h *GoHost) AddPeers(cs ...string)

AddPeers adds the list of Peers to the host.

func (*GoHost) AddPendingPeer

func (h *GoHost) AddPendingPeer(view int, name string) error

func (*GoHost) Children

func (h *GoHost) Children(view int) map[string]Conn

Children returns the children in the specified view.

func (*GoHost) Close

func (h *GoHost) Close()

Close closes the connections.

func (*GoHost) Closed

func (h *GoHost) Closed() bool

func (*GoHost) Connect

func (h *GoHost) Connect(view int) error

Connect connects to the parent of the host. For GoHosts this is a noop.

func (*GoHost) ConnectTo

func (h *GoHost) ConnectTo(parent string) error

func (*GoHost) Get

func (h *GoHost) Get() chan NetworkMessg

Get returns two channels. One of messages that are received, and another of errors associated with each message.

func (*GoHost) GetDirectory

func (h *GoHost) GetDirectory() *GoDirectory

GetDirectory returns the underlying directory used for GoHosts.

func (*GoHost) HostListOn

func (h *GoHost) HostListOn(view int) []string

func (*GoHost) IsChild

func (h *GoHost) IsChild(view int, peer string) bool

IsChild returns true if the peer is a Child for the specified view.

func (*GoHost) IsParent

func (h *GoHost) IsParent(view int, peer string) bool

IsParent returns true if the peer is the Parent of the specifired view.

func (*GoHost) IsRoot

func (h *GoHost) IsRoot(view int) bool

IsRoot returns true if this Host is the root of the specified view.

func (*GoHost) Listen

func (h *GoHost) Listen() error

It shares the public keys and names of the hosts.

func (*GoHost) NChildren

func (h *GoHost) NChildren(view int) int

NChildren returns the number of children specified by the given view.

func (*GoHost) Name

func (h *GoHost) Name() string

Name returns the hostname of the Host.

func (*GoHost) NewView

func (h *GoHost) NewView(view int, parent string, children []string, hoslist []string)

NewView creates a new view with the given view number, parent, and children.

func (*GoHost) NewViewFromPrev

func (h *GoHost) NewViewFromPrev(view int, parent string)

func (*GoHost) Parent

func (h *GoHost) Parent(view int) string

func (*GoHost) Peers

func (h *GoHost) Peers() map[string]Conn

Peers returns the list of Peers as a mapping from hostname to Conn

func (*GoHost) Pending

func (h *GoHost) Pending() map[string]bool

func (*GoHost) Pool

func (h *GoHost) Pool() *sync.Pool

Pool returns the underlying pool of objects for creating new BinaryUnmarshalers, when Getting from network connections.

func (*GoHost) PubKey

func (h *GoHost) PubKey() abstract.Point

PubKey returns the public key of the Host.

func (*GoHost) PutDown

func (h *GoHost) PutDown(ctx context.Context, view int, data []BinaryMarshaler) error

PutDown sends messages to its children on the given view, potentially timing out.

func (*GoHost) PutTo

func (h *GoHost) PutTo(ctx context.Context, host string, data BinaryMarshaler) error

func (*GoHost) PutUp

func (h *GoHost) PutUp(ctx context.Context, view int, data BinaryMarshaler) error

PutUp sends a message to the parent on the given view, potentially timing out.

func (*GoHost) RemovePeer

func (h *GoHost) RemovePeer(view int, name string) bool

func (*GoHost) RemovePeerFromHostlist

func (h *GoHost) RemovePeerFromHostlist(view int, name string)

func (*GoHost) RemovePendingPeer

func (h *GoHost) RemovePendingPeer(peer string)

func (*GoHost) SetHostList

func (h *GoHost) SetHostList(view int, hostlist []string)

func (*GoHost) SetPool

func (h *GoHost) SetPool(p *sync.Pool)

SetPool sets the pool of underlying objects for creating new BinaryUnmarshalers, when Getting from network connections.

func (*GoHost) SetPubKey

func (h *GoHost) SetPubKey(pk abstract.Point)

SetPubKey sets the publick key of the Host.

func (*GoHost) SetSuite

func (h *GoHost) SetSuite(s abstract.Suite)

SetSuite sets the crypto suite which this Host is using.

func (*GoHost) Views

func (h *GoHost) Views() *Views

type Host

type Host interface {
	// Name returns the name of this host.
	Name() string

	Parent(view int) string

	// Peers returns a mapping from peer names to Connections.
	Peers() map[string]Conn

	// AddPeers adds new peers to the host, but does not make it either child or parent.
	AddPeers(hostnames ...string)

	// NewView creates a NewView to operate on.
	// It creates a view with the given view number, which corresponds to the tree
	// with the specified parent and children.
	NewView(view int, parent string, children []string, hostlist []string)

	NewViewFromPrev(view int, parent string)

	// Returns map of this host's views
	Views() *Views

	// AddParent adds a parent to the view specified.
	// Used for building a view incrementally.
	AddParent(view int, hostname string)
	// AddChildren adds children to the view specified.
	// Used for building a view incrementally.
	AddChildren(view int, hostname ...string)

	// NChildren returns the number of children for the given view.
	NChildren(view int) int
	// Children returns the children for a given view.
	Children(view int) map[string]Conn
	// Returns list of hosts available on a specific view
	HostListOn(view int) []string
	// Set the hoslist on a specific view
	SetHostList(view int, hostlist []string)

	// IsRoot returns true if this host is the root for the given view.
	IsRoot(view int) bool
	// IsParent returns true if the given peer is the parent for a specified view.
	IsParent(view int, peer string) bool
	// IsChild returns true if the given peer is the child for a specified view.
	IsChild(view int, peer string) bool

	// PutUp puts the given data up to the parent in the specified view.
	// The context is used to timeout the request.
	PutUp(ctx context.Context, view int, data BinaryMarshaler) error
	// PutDown puts the given data down to the children in the specified view.
	// The context is used to timeout the request.
	PutDown(ctx context.Context, view int, data []BinaryMarshaler) error

	PutTo(ctx context.Context, host string, data BinaryMarshaler) error

	// Get returns a channel on which all received messages will be put.
	// It always returns a reference to the same channel.
	// Multiple listeners will receive disjoint sets of messages.
	// When receiving from the channels always recieve from both the network
	// messages channel as well as the error channel.
	Get() chan NetworkMessg

	// Connect connects to the parent in the given view.
	Connect(view int) error
	ConnectTo(host string) error
	// Listen listens for incoming connections.
	Listen() error

	// Close closes all the connections in the Host.
	Close()

	// SetSuite sets the suite to use for the Host.
	SetSuite(abstract.Suite)
	// PubKey returns the public key of the Host.
	PubKey() abstract.Point
	// SetPubKey sets the public key of the Host.
	SetPubKey(abstract.Point)

	// Pool is a pool of BinaryUnmarshallers to use when generating NetworkMessg's.
	Pool() *sync.Pool
	// SetPool sets the pool of the Host.
	SetPool(*sync.Pool)

	// Functions to allow group evolution
	AddPeerToPending(h string)
	AddPendingPeer(view int, name string) error
	RemovePeer(view int, name string) bool
	Pending() map[string]bool

	AddPeerToHostlist(view int, name string)
	RemovePeerFromHostlist(view int, name string)
}

Host is an abstract node on the Host tree. Representing this tree, it has the ability to PutUp and PutDown to its parent and children respectively. It also can Get from all its connections. It is up to the caller to de-multiplex the messages sent back from Get.

A Host also implements multiple views of this tree. Each view is a unique tree (parent, children) configuration. Applications using Hosts can specify which view to use for operations.

type HostState

type HostState int
const (
	ALIVE HostState = iota
	DEAD
)
const DefaultState HostState = ALIVE

type NetworkMessg

type NetworkMessg struct {
	Data BinaryUnmarshaler // data
	From string            // name of server data came from
	Err  error
}

func (*NetworkMessg) MarshalBinary

func (nm *NetworkMessg) MarshalBinary() ([]byte, error)

func (*NetworkMessg) UnmarshalBinary

func (nm *NetworkMessg) UnmarshalBinary(data []byte) error

type StringMarshaler

type StringMarshaler string

StringMarshaler is a wrapper type to allow strings to be marshalled and unmarshalled.

func (*StringMarshaler) MarshalBinary

func (s *StringMarshaler) MarshalBinary() ([]byte, error)

MarshalBinary implements the BinaryMarshaler interface for the StringMarshaler.

func (*StringMarshaler) UnmarshalBinary

func (s *StringMarshaler) UnmarshalBinary(b []byte) error

UnmarshalBinary implements the BinaryUnmarshaler interface for the StringMarshaler.

type TCPConn

type TCPConn struct {
	// contains filtered or unexported fields
}

TCPConn is an implementation of the Conn interface for TCP network connections.

func NewTCPConn

func NewTCPConn(hostname string) *TCPConn

NewTCPConn takes a hostname and creates TCPConn. Before calling Get or Put Connect must first be called to establish the connection.

func NewTCPConnFromNet

func NewTCPConnFromNet(conn net.Conn) *TCPConn

NewTCPConnFromNet wraps a net.Conn creating a new TCPConn using conn as the underlying connection. After creating a TCPConn in this fashion, it might be necessary to call SetName, in order to give it an understandable name.

func (*TCPConn) Close

func (tc *TCPConn) Close()

Close closes the connection.

func (*TCPConn) Closed

func (tc *TCPConn) Closed() bool

func (*TCPConn) Connect

func (tc *TCPConn) Connect() error

Connect connects to the endpoint specified.

func (*TCPConn) Get

func (tc *TCPConn) Get(bum BinaryUnmarshaler) error

Get gets data from the connection. Returns io.EOF on an irrecoveralbe error. Returns given error if it is Temporary.

func (*TCPConn) Name

func (tc *TCPConn) Name() string

Name returns the name of the connection.

func (*TCPConn) PubKey

func (tc *TCPConn) PubKey() abstract.Point

PubKey returns the public key of this peer.

func (*TCPConn) Put

func (tc *TCPConn) Put(bm BinaryMarshaler) error

Put puts data to the connection. Returns io.EOF on an irrecoverable error. Returns actual error if it is Temporary.

func (*TCPConn) SetName

func (tc *TCPConn) SetName(name string)

SetName sets the name of the connection.

func (*TCPConn) SetPubKey

func (tc *TCPConn) SetPubKey(pk abstract.Point)

SetPubKey sets the public key.

type TCPHost

type TCPHost struct {
	PeerLock sync.RWMutex

	Ready map[string]bool

	// Peers asking to join overall tree structure of nodes
	// via connection to current host node
	PendingPeers map[string]bool

	Pubkey abstract.Point // own public key
	// contains filtered or unexported fields
}

func NewTCPHost

func NewTCPHost(hostname string) *TCPHost

NewTCPHost creates a new TCPHost with a given hostname.

func (*TCPHost) AddChildren

func (h *TCPHost) AddChildren(view int, cs ...string)

AddChildren adds children to the specified view.

func (*TCPHost) AddParent

func (h *TCPHost) AddParent(view int, c string)

AddParent adds a parent node to the TCPHost, for the given view.

func (*TCPHost) AddPeerToHostlist

func (h *TCPHost) AddPeerToHostlist(view int, name string)

func (*TCPHost) AddPeerToPending

func (h *TCPHost) AddPeerToPending(p string)

func (*TCPHost) AddPeers

func (h *TCPHost) AddPeers(cs ...string)

AddPeers adds the list of Peers.

func (*TCPHost) AddPendingPeer

func (h *TCPHost) AddPendingPeer(view int, name string) error

func (*TCPHost) Children

func (h *TCPHost) Children(view int) map[string]Conn

Children returns a map of childname to Conn for the given view.

func (*TCPHost) Close

func (h *TCPHost) Close()

Close closes all the connections currently open.

func (*TCPHost) Closed

func (h *TCPHost) Closed() bool

func (*TCPHost) Connect

func (h *TCPHost) Connect(view int) error

Connect connects to the parent in the given view. It connects to the parent by establishing a TCPConn. It then sends its name and public key to initialize the connection.

func (*TCPHost) ConnectTo

func (h *TCPHost) ConnectTo(parent string) error

func (*TCPHost) Get

func (h *TCPHost) Get() chan NetworkMessg

Get gets from all of the Peers and sends the responses to a channel of NetworkMessg and errors that it returns.

TODO: each of these goroutines could be spawned when we initally connect to them instead.

func (*TCPHost) HostListOn

func (h *TCPHost) HostListOn(view int) []string

func (*TCPHost) IsChild

func (h *TCPHost) IsChild(view int, peer string) bool

IsChild returns true f the given peer is the child for the specified view.

func (*TCPHost) IsParent

func (h *TCPHost) IsParent(view int, peer string) bool

IsParent returns true if the given peer is the parent for the specified view.

func (*TCPHost) IsRoot

func (h *TCPHost) IsRoot(view int) bool

IsRoot returns true if the TCPHost is the root of it's tree for the given view..

func (*TCPHost) Listen

func (h *TCPHost) Listen() error

Listen listens for incoming TCP connections. It is a non-blocking call that runs in the background. It accepts incoming connections and establishes Peers. When a peer attempts to connect it must send over its name (as a StringMarshaler), as well as its public key. Only after that point can be communicated with.

func (*TCPHost) NChildren

func (h *TCPHost) NChildren(view int) int

NChildren returns the number of children for the specified view.

func (*TCPHost) Name

func (h *TCPHost) Name() string

Name returns the hostname of the TCPHost.

func (*TCPHost) NewView

func (h *TCPHost) NewView(view int, parent string, children []string, hostlist []string)

NewView creates a new view with the given view number, parent and children.

func (*TCPHost) NewViewFromPrev

func (h *TCPHost) NewViewFromPrev(view int, parent string)

func (*TCPHost) Parent

func (h *TCPHost) Parent(view int) string

func (*TCPHost) Peers

func (h *TCPHost) Peers() map[string]Conn

Peers returns the list of Peers as a mapping from hostname to Conn.

func (*TCPHost) Pending

func (h *TCPHost) Pending() map[string]bool

func (*TCPHost) Pool

func (h *TCPHost) Pool() *sync.Pool

Pool is the underlying pool of BinaryUnmarshallers to use when getting.

func (*TCPHost) PubKey

func (h *TCPHost) PubKey() abstract.Point

PubKey returns the public key of the host.

func (*TCPHost) PutDown

func (h *TCPHost) PutDown(ctx context.Context, view int, data []BinaryMarshaler) error

PutDown sends a message (an interface{} value) up to all children through whatever 'network' interface each child Peer implements.

func (*TCPHost) PutTo

func (h *TCPHost) PutTo(ctx context.Context, host string, data BinaryMarshaler) error

func (*TCPHost) PutUp

func (h *TCPHost) PutUp(ctx context.Context, view int, data BinaryMarshaler) error

PutUp sends a message to the parent in the specified view.

func (*TCPHost) RemovePeer

func (h *TCPHost) RemovePeer(view int, name string) bool

func (*TCPHost) RemovePeerFromHostlist

func (h *TCPHost) RemovePeerFromHostlist(view int, name string)

func (*TCPHost) RemovePendingPeer

func (h *TCPHost) RemovePendingPeer(peer string)

func (*TCPHost) SetHostList

func (h *TCPHost) SetHostList(view int, hostlist []string)

func (*TCPHost) SetPool

func (h *TCPHost) SetPool(p *sync.Pool)

SetPool sets the pool of BinaryUnmarshallers when getting from channels

func (*TCPHost) SetPubKey

func (h *TCPHost) SetPubKey(pk abstract.Point)

SetPubKey sets the public key of the host.

func (*TCPHost) SetSuite

func (h *TCPHost) SetSuite(s abstract.Suite)

SetSuite sets the suite of the TCPHost to use.

func (*TCPHost) Views

func (h *TCPHost) Views() *Views

type View

type View struct {
	sync.RWMutex
	Num      int
	Parent   string
	Children []string

	HostList []string
}

func (*View) AddChildren

func (v *View) AddChildren(children ...string)

func (*View) AddParent

func (v *View) AddParent(parent string)

func (*View) AddPeerToHostlist

func (v *View) AddPeerToHostlist(name string)

func (*View) RemoveChild

func (v *View) RemoveChild(child string) bool

func (*View) RemovePeer

func (v *View) RemovePeer(name string) bool

func (*View) RemovePeerFromHostlist

func (v *View) RemovePeerFromHostlist(name string)

func (*View) SetHostList

func (v *View) SetHostList(hostlist []string)

type Views

type Views struct {
	sync.RWMutex
	Views map[int]*View
}

func NewViews

func NewViews() *Views

func (*Views) AddChildren

func (v *Views) AddChildren(view int, children ...string)

func (*Views) AddParent

func (v *Views) AddParent(view int, parent string)

func (*Views) AddPeerToHostlist

func (vs *Views) AddPeerToHostlist(view int, name string)

func (*Views) Children

func (v *Views) Children(view int) []string

func (*Views) HostList

func (v *Views) HostList(view int) []string

func (*Views) NChildren

func (v *Views) NChildren(view int) int

func (*Views) NewView

func (v *Views) NewView(view int, parent string, children []string, hostlist []string)

func (*Views) NewViewFromPrev

func (v *Views) NewViewFromPrev(view int, parent string)

func (*Views) Parent

func (v *Views) Parent(view int) string

func (*Views) RemoveChild

func (v *Views) RemoveChild(view int, child string)

func (*Views) RemovePeer

func (v *Views) RemovePeer(view int, child string) bool

func (*Views) RemovePeerFromHostlist

func (vs *Views) RemovePeerFromHostlist(view int, name string)

func (*Views) SetHostList

func (v *Views) SetHostList(view int, hostlist []string)

Jump to

Keyboard shortcuts

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