interface.go 2.39 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3
package net

import (
4
	"github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
5
	conn "github.com/jbenet/go-ipfs/net/conn"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
6 7
	msg "github.com/jbenet/go-ipfs/net/message"
	mux "github.com/jbenet/go-ipfs/net/mux"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
	srv "github.com/jbenet/go-ipfs/net/service"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	peer "github.com/jbenet/go-ipfs/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
10
	ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
11 12

	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
13 14 15 16
)

// Network is the interface IPFS uses for connecting to the world.
type Network interface {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
17
	ctxc.ContextCloser
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
18 19 20 21 22 23

	// Listen handles incoming connections on given Multiaddr.
	// Listen(*ma.Muliaddr) error
	// TODO: for now, only listen on addrs in local peer when initializing.

	// DialPeer attempts to establish a connection to a given peer
24
	DialPeer(context.Context, peer.Peer) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
25 26

	// ClosePeer connection to peer
27
	ClosePeer(peer.Peer) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
28 29

	// IsConnected returns whether a connection to given peer exists.
30
	IsConnected(peer.Peer) (bool, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
31 32 33 34

	// GetProtocols returns the protocols registered in the network.
	GetProtocols() *mux.ProtocolMap

35
	// GetPeerList returns the list of peers currently connected in this network.
36
	GetPeerList() []peer.Peer
37

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
38 39 40
	// GetConnections returns the list of connections currently open in this network.
	GetConnections() []conn.Conn

41 42 43 44
	// GetBandwidthTotals returns the total number of bytes passed through
	// the network since it was instantiated
	GetBandwidthTotals() (uint64, uint64)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
45
	// SendMessage sends given Message out
46
	SendMessage(msg.NetMessage) error
47 48 49 50 51 52 53 54

	// ListenAddresses returns a list of addresses at which this network listens.
	ListenAddresses() []ma.Multiaddr

	// InterfaceListenAddresses returns a list of addresses at which this network
	// listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to
	// use the known local interfaces.
	InterfaceListenAddresses() ([]ma.Multiaddr, error)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55
}
56 57

// Sender interface for network services.
58
type Sender srv.Sender
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
59 60 61

// Handler interface for network services.
type Handler srv.Handler
62 63 64

// Service interface for network resources.
type Service srv.Service
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
65

Jeromy's avatar
Jeromy committed
66
// Dialer represents a service that can dial out to peers
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67
// (this is usually just a Network, but other services may not need the whole
Jeromy's avatar
Jeromy committed
68
// stack, and thus it becomes easier to mock)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
69 70 71
type Dialer interface {

	// DialPeer attempts to establish a connection to a given peer
72
	DialPeer(context.Context, peer.Peer) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73
}