net.go 2.64 KB
Newer Older
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
1 2 3 4 5 6 7 8 9 10
package net

import (
	"errors"

	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"

11
	context "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/go.net/context"
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 26 27 28 29 30 31

	// protocol multiplexing
	muxer *mux.Muxer

	// peer connection multiplexing
	swarm *swarm.Swarm

	// network context
	ctx    context.Context
	cancel context.CancelFunc
}

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

	ctx, cancel := context.WithCancel(ctx)

	in := &IpfsNetwork{
		local:  local,
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
39
		muxer:  mux.NewMuxer(ctx, *pmap),
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
40 41 42 43
		ctx:    ctx,
		cancel: cancel,
	}

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

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

Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
54 55 56 57 58 59 60
	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
61
func (n *IpfsNetwork) DialPeer(p peer.Peer) error {
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
62 63 64 65 66
	_, err := n.swarm.Dial(p)
	return err
}

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

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

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

// Close terminates all network operation
func (n *IpfsNetwork) Close() error {
	if n.cancel == nil {
		return errors.New("Network already closed.")
	}

	n.swarm.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
99
	n.muxer.Close()
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
100 101 102 103 104

	n.cancel()
	n.cancel = nil
	return nil
}
Jeromy's avatar
Jeromy committed
105

106 107
// GetPeerList returns the networks list of connected peers
func (n *IpfsNetwork) GetPeerList() []peer.Peer {
108
	return n.swarm.GetPeerList()
Jeromy's avatar
Jeromy committed
109
}
110

111
// GetBandwidthTotals returns the total amount of bandwidth transferred
112 113 114
func (n *IpfsNetwork) GetBandwidthTotals() (in uint64, out uint64) {
	return n.muxer.GetBandwidthTotals()
}