net.go 3.06 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7
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"
8
	ctxc "github.com/jbenet/go-ipfs/util/ctxcloser"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
9

10
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
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 16 17
)

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

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

	// protocol multiplexing
	muxer *mux.Muxer

	// peer connection multiplexing
	swarm *swarm.Swarm

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

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

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

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

47 48 49
	in.AddCloserChild(in.swarm)
	in.AddCloserChild(in.muxer)

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

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

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

// DialPeer attempts to establish a connection to a given peer
60
func (n *IpfsNetwork) DialPeer(p peer.Peer) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
61 62 63 64 65
	_, err := n.swarm.Dial(p)
	return err
}

// ClosePeer connection to peer
66
func (n *IpfsNetwork) ClosePeer(p peer.Peer) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
67 68 69 70
	return n.swarm.CloseConnection(p)
}

// IsConnected returns whether a connection to given peer exists.
71 72
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
73 74 75 76 77 78 79 80 81 82 83 84 85
}

// 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
86
func (n *IpfsNetwork) SendMessage(m msg.NetMessage) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
87 88 89 90
	n.swarm.Outgoing <- m
	return nil
}

91 92
// GetPeerList returns the networks list of connected peers
func (n *IpfsNetwork) GetPeerList() []peer.Peer {
93
	return n.swarm.GetPeerList()
Jeromy's avatar
Jeromy committed
94
}
95

96
// GetBandwidthTotals returns the total amount of bandwidth transferred
97 98 99
func (n *IpfsNetwork) GetBandwidthTotals() (in uint64, out uint64) {
	return n.muxer.GetBandwidthTotals()
}
100 101 102 103 104 105 106 107 108 109 110 111

// 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()
}