github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/p2p/net/swarm/swarm_net.go (about) 1 package swarm 2 3 import ( 4 "fmt" 5 6 peer "github.com/ipfs/go-ipfs/p2p/peer" 7 8 metrics "github.com/ipfs/go-ipfs/metrics" 9 inet "github.com/ipfs/go-ipfs/p2p/net" 10 11 ma "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multiaddr" 12 "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess" 13 context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context" 14 ) 15 16 // Network implements the inet.Network interface. 17 // It is simply a swarm, with a few different functions 18 // to implement inet.Network. 19 type Network Swarm 20 21 // NewNetwork constructs a new network and starts listening on given addresses. 22 func NewNetwork(ctx context.Context, listen []ma.Multiaddr, local peer.ID, 23 peers peer.Peerstore, bwc metrics.Reporter) (*Network, error) { 24 25 s, err := NewSwarm(ctx, listen, local, peers, bwc) 26 if err != nil { 27 return nil, err 28 } 29 30 return (*Network)(s), nil 31 } 32 33 // DialPeer attempts to establish a connection to a given peer. 34 // Respects the context. 35 func (n *Network) DialPeer(ctx context.Context, p peer.ID) (inet.Conn, error) { 36 log.Debugf("[%s] network dialing peer [%s]", n.local, p) 37 sc, err := n.Swarm().Dial(ctx, p) 38 if err != nil { 39 return nil, err 40 } 41 42 log.Debugf("network for %s finished dialing %s", n.local, p) 43 return inet.Conn(sc), nil 44 } 45 46 // Process returns the network's Process 47 func (n *Network) Process() goprocess.Process { 48 return n.proc 49 } 50 51 // Swarm returns the network's peerstream.Swarm 52 func (n *Network) Swarm() *Swarm { 53 return (*Swarm)(n) 54 } 55 56 // LocalPeer the network's LocalPeer 57 func (n *Network) LocalPeer() peer.ID { 58 return n.Swarm().LocalPeer() 59 } 60 61 // Peers returns the known peer IDs from the Peerstore 62 func (n *Network) Peers() []peer.ID { 63 return n.Swarm().Peers() 64 } 65 66 // Peers returns the Peerstore, which tracks known peers 67 func (n *Network) Peerstore() peer.Peerstore { 68 return n.Swarm().peers 69 } 70 71 // Conns returns the connected peers 72 func (n *Network) Conns() []inet.Conn { 73 conns1 := n.Swarm().Connections() 74 out := make([]inet.Conn, len(conns1)) 75 for i, c := range conns1 { 76 out[i] = inet.Conn(c) 77 } 78 return out 79 } 80 81 // ConnsToPeer returns the connections in this Netowrk for given peer. 82 func (n *Network) ConnsToPeer(p peer.ID) []inet.Conn { 83 conns1 := n.Swarm().ConnectionsToPeer(p) 84 out := make([]inet.Conn, len(conns1)) 85 for i, c := range conns1 { 86 out[i] = inet.Conn(c) 87 } 88 return out 89 } 90 91 // ClosePeer connection to peer 92 func (n *Network) ClosePeer(p peer.ID) error { 93 return n.Swarm().CloseConnection(p) 94 } 95 96 // close is the real teardown function 97 func (n *Network) close() error { 98 return n.Swarm().Close() 99 } 100 101 // Close calls the ContextCloser func 102 func (n *Network) Close() error { 103 return n.Swarm().proc.Close() 104 } 105 106 // Listen tells the network to start listening on given multiaddrs. 107 func (n *Network) Listen(addrs ...ma.Multiaddr) error { 108 return n.Swarm().Listen(addrs...) 109 } 110 111 // ListenAddresses returns a list of addresses at which this network listens. 112 func (n *Network) ListenAddresses() []ma.Multiaddr { 113 return n.Swarm().ListenAddresses() 114 } 115 116 // InterfaceListenAddresses returns a list of addresses at which this network 117 // listens. It expands "any interface" addresses (/ip4/0.0.0.0, /ip6/::) to 118 // use the known local interfaces. 119 func (n *Network) InterfaceListenAddresses() ([]ma.Multiaddr, error) { 120 return n.Swarm().InterfaceListenAddresses() 121 } 122 123 // Connectedness returns a state signaling connection capabilities 124 // For now only returns Connected || NotConnected. Expand into more later. 125 func (n *Network) Connectedness(p peer.ID) inet.Connectedness { 126 c := n.Swarm().ConnectionsToPeer(p) 127 if c != nil && len(c) > 0 { 128 return inet.Connected 129 } 130 return inet.NotConnected 131 } 132 133 // NewStream returns a new stream to given peer p. 134 // If there is no connection to p, attempts to create one. 135 func (n *Network) NewStream(p peer.ID) (inet.Stream, error) { 136 log.Debugf("[%s] network opening stream to peer [%s]", n.local, p) 137 s, err := n.Swarm().NewStreamWithPeer(p) 138 if err != nil { 139 return nil, err 140 } 141 142 return inet.Stream(s), nil 143 } 144 145 // SetHandler sets the protocol handler on the Network's Muxer. 146 // This operation is threadsafe. 147 func (n *Network) SetStreamHandler(h inet.StreamHandler) { 148 n.Swarm().SetStreamHandler(h) 149 } 150 151 // SetConnHandler sets the conn handler on the Network. 152 // This operation is threadsafe. 153 func (n *Network) SetConnHandler(h inet.ConnHandler) { 154 n.Swarm().SetConnHandler(func(c *Conn) { 155 h(inet.Conn(c)) 156 }) 157 } 158 159 // String returns a string representation of Network. 160 func (n *Network) String() string { 161 return fmt.Sprintf("<Network %s>", n.LocalPeer()) 162 } 163 164 // Notify signs up Notifiee to receive signals when events happen 165 func (n *Network) Notify(f inet.Notifiee) { 166 n.Swarm().Notify(f) 167 } 168 169 // StopNotify unregisters Notifiee fromr receiving signals 170 func (n *Network) StopNotify(f inet.Notifiee) { 171 n.Swarm().StopNotify(f) 172 }