net.go 3.29 KB
Newer Older
Jeromy's avatar
Jeromy committed
1
// package net provides an interface for ipfs to interact with the network through
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
2 3 4 5 6 7 8
package net

import (
	msg "github.com/jbenet/go-ipfs/net/message"
	mux "github.com/jbenet/go-ipfs/net/mux"
	swarm "github.com/jbenet/go-ipfs/net/swarm"
	peer "github.com/jbenet/go-ipfs/peer"
9
	util "github.com/jbenet/go-ipfs/util"
10
	ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11

12
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
13
	ma "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
14 15 16 17 18 19
)

// IpfsNetwork implements the Network interface,
type IpfsNetwork struct {

	// local peer
20
	local peer.Peer
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
21 22 23 24 25 26 27

	// protocol multiplexing
	muxer *mux.Muxer

	// peer connection multiplexing
	swarm *swarm.Swarm

28 29
	// network context closer
	ctxc.ContextCloser
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
30 31 32
}

// NewIpfsNetwork is the structure that implements the network interface
33
func NewIpfsNetwork(ctx context.Context, listen []ma.Multiaddr, local peer.Peer,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
34
	peers peer.Peerstore, pmap *mux.ProtocolMap) (*IpfsNetwork, error) {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
35 36

	in := &IpfsNetwork{
37 38 39
		local:         local,
		muxer:         mux.NewMuxer(ctx, *pmap),
		ContextCloser: ctxc.NewContextCloser(ctx, nil),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41
	}

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
42
	var err error
43
	in.swarm, err = swarm.NewSwarm(ctx, listen, local, peers)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
44
	if err != nil {
45
		in.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
46 47 48
		return nil, err
	}

49 50 51
	in.AddCloserChild(in.swarm)
	in.AddCloserChild(in.muxer)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
52 53 54
	// remember to wire components together.
	in.muxer.Pipe.ConnectTo(in.swarm.Pipe)

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
55 56 57 58 59 60
	return in, nil
}

// Listen handles incoming connections on given Multiaddr.
// func (n *IpfsNetwork) Listen(*ma.Muliaddr) error {}

61 62
// DialPeer attempts to establish a connection to a given peer.
// Respects the context.
63
func (n *IpfsNetwork) DialPeer(ctx context.Context, p peer.Peer) error {
64
	err := util.ContextDo(ctx, func() error {
65 66 67
		_, err := n.swarm.Dial(p)
		return err
	})
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
68 69 70 71
	return err
}

// ClosePeer connection to peer
72
func (n *IpfsNetwork) ClosePeer(p peer.Peer) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
73 74 75 76
	return n.swarm.CloseConnection(p)
}

// IsConnected returns whether a connection to given peer exists.
77 78
func (n *IpfsNetwork) IsConnected(p peer.Peer) (bool, error) {
	return n.swarm.GetConnection(p.ID()) != nil, nil
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
79 80 81 82 83 84 85 86 87 88 89 90 91
}

// GetProtocols returns the protocols registered in the network.
func (n *IpfsNetwork) GetProtocols() *mux.ProtocolMap {
	// copy over because this map should be read only.
	pmap := mux.ProtocolMap{}
	for id, proto := range n.muxer.Protocols {
		pmap[id] = proto
	}
	return &pmap
}

// SendMessage sends given Message out
92
func (n *IpfsNetwork) SendMessage(m msg.NetMessage) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
93 94 95 96
	n.swarm.Outgoing <- m
	return nil
}

97 98
// GetPeerList returns the networks list of connected peers
func (n *IpfsNetwork) GetPeerList() []peer.Peer {
99
	return n.swarm.GetPeerList()
Jeromy's avatar
Jeromy committed
100
}
101

102
// GetBandwidthTotals returns the total amount of bandwidth transferred
103 104 105
func (n *IpfsNetwork) GetBandwidthTotals() (in uint64, out uint64) {
	return n.muxer.GetBandwidthTotals()
}
106 107 108 109 110 111 112 113 114 115 116 117

// ListenAddresses returns a list of addresses at which this network listens.
func (n *IpfsNetwork) ListenAddresses() []ma.Multiaddr {
	return n.swarm.ListenAddresses()
}

// 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.
func (n *IpfsNetwork) InterfaceListenAddresses() ([]ma.Multiaddr, error) {
	return n.swarm.InterfaceListenAddresses()
}