interface.go 2.22 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 6
	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
7
	srv "github.com/jbenet/go-ipfs/net/service"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
8
	peer "github.com/jbenet/go-ipfs/peer"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9
	ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
10 11

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

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

	// 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
23
	DialPeer(context.Context, peer.Peer) error
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
24 25

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

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

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

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

37 38 39 40
	// 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
41
	// SendMessage sends given Message out
42
	SendMessage(msg.NetMessage) error
43 44 45 46 47 48 49 50

	// 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
51
}
52 53

// Sender interface for network services.
54
type Sender srv.Sender
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56 57

// Handler interface for network services.
type Handler srv.Handler
58 59 60

// Service interface for network resources.
type Service srv.Service
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61 62 63 64 65 66 67

// Dialer service that can dial to peers
// (this is usually just a Network, but other services may not need the whole
// thing, and thus it becomes easier to mock)
type Dialer interface {

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