network

package
v0.0.0-...-a69b080 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2016 License: MIT Imports: 24 Imported by: 0

Documentation

Overview

Package network takes care of networking for degdb.

Index

Constants

This section is empty.

Variables

View Source
var (
	IndexTemplate = template.Must(template.New("").Parse(staticBox.MustString("common/index.html")))
	ErrorTemplate = template.Must(template.New("").Parse(staticBox.MustString("common/error.html")))
)
View Source
var (
	ErrNoRecipients = errors.New("no recipients for the message were found")
)
View Source
var Timeout = errors.New("request timed-out")

Functions

This section is empty.

Types

type Closable

type Closable interface {
	Close() error
}

type Conn

type Conn struct {
	Peer   *protocol.Peer
	Closed bool

	net.Conn
	// contains filtered or unexported fields
}

Conn is a net.Conn with extensions.

func (*Conn) Close

func (c *Conn) Close() error

Close closes the connection and sets Closed to true.

func (*Conn) PrettyID

func (c *Conn) PrettyID() string

PrettyID returns a terminal colored format of the connection ID.

func (*Conn) Request

func (c *Conn) Request(m *protocol.Message) (*protocol.Message, error)

Request sends a message on a connection and waits for a response. Returns error network.Timeout if no response in 10 seconds.

func (*Conn) RespondTo

func (c *Conn) RespondTo(to *protocol.Message, resp *protocol.Message) error

RespondTo sends `resp` as a response to the request `to`.

func (*Conn) Send

func (c *Conn) Send(m *protocol.Message) error

Send a message on the specified connection. Consider Request.

type Server

type Server struct {
	IP   string
	Port int

	Serving bool

	HTTP *http.Server

	Peers map[string]*Conn

	*log.Logger
	// contains filtered or unexported fields
}

Server handles all network traffic.

func NewServer

func NewServer(logger *log.Logger, port int) (*Server, error)

NewServer creates a new server with routing information. If log is nil, stdout is used.

func (*Server) Broadcast

func (s *Server) Broadcast(hash *uint64, msg *protocol.Message) error

Broadcast sends a message to all peers with that have the hash in their keyspace. If there is no peer that can receive the message, ErrNoRecipients is returned.

func (*Server) Connect

func (s *Server) Connect(addr string) error

Connect to another server. `addr` should be in the format "google.com:80".

func (*Server) HTTPHandle

func (s *Server) HTTPHandle(route string, handler http.Handler)

func (*Server) HTTPHandleFunc

func (s *Server) HTTPHandleFunc(route string, handler func(w http.ResponseWriter, r *http.Request))

func (*Server) Handle

func (s *Server) Handle(typ string, f protocolHandler)

Handle registers a handler for a specific protobuf message type.

func (*Server) Listen

func (s *Server) Listen() error

Listen for incoming connections on the specified port.

func (*Server) ListenWait

func (s *Server) ListenWait()

ListenWait waits for the server to start accepting connections before returning.

func (*Server) LocalID

func (s *Server) LocalID() string

LocalID returns the local machines ID.

func (*Server) LocalKeyspace

func (s *Server) LocalKeyspace() *protocol.Keyspace

LocalKeyspace returns the keyspace that the local node represents.

func (*Server) LocalPeer

func (s *Server) LocalPeer() *protocol.Peer

LocalPeer returns a peer object of the current server.

func (*Server) MinimumCoveringPeers

func (s *Server) MinimumCoveringPeers() []*Conn

MinimumCoveringPeers returns a set of peers that minimizes overlap. This is similar to the Set Covering Problem and is NP-hard. This is a greedy algorithm. While the keyspace is not entirely covered, scan through all peers and pick the peer that will add the most to the set while still having the start in the selected set. TODO(wiz): Make this more optimal. TODO(wiz): achieve n-redundancy

func (*Server) NewConn

func (s *Server) NewConn(c net.Conn) *Conn

NewConn creates a new Conn with the specified net.Conn.

func (*Server) Stop

func (s *Server) Stop()

Stop closes all connections and cleans up.

Directories

Path Synopsis
Package customhttp is a reimplementation of http.FileServer that supports custom HTML templates for errors and directory listings.
Package customhttp is a reimplementation of http.FileServer that supports custom HTML templates for errors and directory listings.
Package ip returns the current IP address by contacting a number of public servers.
Package ip returns the current IP address by contacting a number of public servers.

Jump to

Keyboard shortcuts

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