net.go 2.48 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"
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
11 12 13 14 15 16
)

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

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

	// protocol multiplexing
	muxer *mux.Muxer

	// peer connection multiplexing
	swarm *swarm.Swarm

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

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

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

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

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

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

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

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

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

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

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

95
// GetBandwidthTotals returns the total amount of bandwidth transferred
96 97 98
func (n *IpfsNetwork) GetBandwidthTotals() (in uint64, out uint64) {
	return n.muxer.GetBandwidthTotals()
}