Skip to content

Commit

Permalink
Merge 930625e into cbf4a3a
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jul 13, 2017
2 parents cbf4a3a + 930625e commit 730a4b2
Show file tree
Hide file tree
Showing 9 changed files with 335 additions and 242 deletions.
15 changes: 3 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
[![Coverage Status](https://coveralls.io/repos/github/ipfs/js-ipfs-bitswap/badge.svg?branch=master)](https://coveralls.io/github/ipfs/js-ipfs-bitswap?branch=master)
[![Travis CI](https://travis-ci.org/ipfs/js-ipfs-bitswap.svg?branch=master)](https://travis-ci.org/ipfs/js-ipfs-bitswap)
[![Circle CI](https://circleci.com/gh/ipfs/js-ipfs-bitswap.svg?style=svg)](https://circleci.com/gh/ipfs/js-ipfs-bitswap)
[![Dependency Status](https://david-dm.org/ipfs/js-ipfs-bitswap.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-bitswap) [![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
[![Dependency Status](https://david-dm.org/ipfs/js-ipfs-bitswap.svg?style=flat-square)](https://david-dm.org/ipfs/js-ipfs-bitswap)
[![js-standard-style](https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/feross/standard)
![](https://img.shields.io/badge/npm-%3E%3D3.0.0-orange.svg?style=flat-square)
![](https://img.shields.io/badge/Node.js-%3E%3D4.0.0-orange.svg?style=flat-square)

Expand All @@ -33,13 +34,7 @@
> npm install ipfs-bitswap
```

### Use in Node.js

```js
const Bitswap = require('ipfs-bitswap')
```

### Use in a browser with browserify, webpack or any other bundler
### Use in Node.js or in the browser with browserify, webpack or any other bundler

```js
const Bitswap = require('ipfs-bitswap')
Expand All @@ -55,10 +50,6 @@ Loading this module through a script tag will make the `IpfsBitswap` object avai
<script src="https://unpkg.com/ipfs-bitswap/dist/index.js"></script>
```

## Usage

See https://ipfs.github.io/js-ipfs-bitswap

## API

See https://ipfs.github.io/js-ipfs-bitswap
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,4 @@
"greenkeeperio-bot <support@greenkeeper.io>",
"npmcdn-to-unpkg-bot <npmcdn-to-unpkg-bot@users.noreply.github.com>"
]
}
}
125 changes: 57 additions & 68 deletions src/components/network/index.js → src/components/network.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
'use strict'

const debug = require('debug')
const lp = require('pull-length-prefixed')
const pull = require('pull-stream')
const setImmediate = require('async/setImmediate')
const waterfall = require('async/waterfall')
const each = require('async/each')

const Message = require('../../types/message')
const CONSTANTS = require('../../constants')
const Message = require('../types/message')
const CONSTANTS = require('../constants')
const debug = require('debug')
const log = debug('bitswap:network')
log.error = debug('bitswap:network:error')

const BITSWAP100 = '/ipfs/bitswap/1.0.0'
const BITSWAP110 = '/ipfs/bitswap/1.1.0'

class Network {
constructor (libp2p, peerBook, bitswap, b100Only) {
constructor (libp2p, bitswap, options) {
options = options || {}
this.libp2p = libp2p
this.peerBook = peerBook
this.bitswap = bitswap
this.b100Only = b100Only || false
this.b100Only = options.b100Only || false

// increase event listener max
this._running = false

// TODO: move this up to libp2p-swarm
this.libp2p.swarm.setMaxListeners(CONSTANTS.maxListeners)
}

Expand All @@ -33,36 +35,31 @@ class Network {

this._onConnection = this._onConnection.bind(this)
this.libp2p.handle(BITSWAP100, this._onConnection)
if (!this.b100Only) {
this.libp2p.handle(BITSWAP110, this._onConnection)
}
if (!this.b100Only) { this.libp2p.handle(BITSWAP110, this._onConnection) }

this.libp2p.on('peer:connect', this._onPeerConnect)
this.libp2p.on('peer:disconnect', this._onPeerDisconnect)

// All existing connections are like new ones for us
const pKeys = Object.keys(this.peerBook.getAll())
pKeys.forEach((k) => this._onPeerConnect(this.peerBook.get(k)))
this.libp2p.peerBook.getAllArray().filter((peer) => peer.isConnected())
.forEach((peer) => this._onPeerConnect((peer)))
}

stop () {
this._running = false

this.libp2p.unhandle(BITSWAP100)
if (!this.b100Only) {
this.libp2p.unhandle(BITSWAP110)
}
if (!this.b100Only) { this.libp2p.unhandle(BITSWAP110) }

this.libp2p.removeListener('peer:connect', this._onPeerConnect)
this.libp2p.removeListener('peer:disconnect', this._onPeerDisconnect)
}

// Handles both types of bitswap messgages
_onConnection (protocol, conn) {
if (!this._running) {
return
}
if (!this._running) { return }
log('incomming new bitswap connection: %s', protocol)

pull(
conn,
lp.decode(),
Expand All @@ -86,85 +83,77 @@ class Network {
}

_onPeerConnect (peerInfo) {
if (!this._running) {
return
}
if (!this._running) { return }

this.bitswap._onPeerConnected(peerInfo.id)
}

_onPeerDisconnect (peerInfo) {
if (!this._running) {
return
}
if (!this._running) { return }

this.bitswap._onPeerDisconnected(peerInfo.id)
}

// Connect to the given peer
connectTo (peerId, callback) {
const done = (err) => setImmediate(() => callback(err))
findProviders (cid, maxProviders, callback) {
// TODO
// consider if we want to trickleDown maxProviders, currently this is
// not an exposed option:
// https://github.com/libp2p/js-libp2p-kad-dht/blob/master/src/index.js#L416
this.libp2p.contentRouting.findProviders(cid, CONSTANTS.providerRequestTimeout, callback)
}

if (!this._running) {
return done(new Error('No running network'))
}
findAndConnect (cid, maxProviders, callback) {
waterfall([
(cb) => this.findProviders(cid, maxProviders, cb),
(provs, cb) => each(provs, (p, cb) => this.connectTo(p, cb))
], callback)
}

// NOTE: For now, all this does is ensure that we are
// connected. Once we have Peer Routing, we will be able
// to find the Peer
if (this.libp2p.swarm.muxedConns[peerId.toB58String()]) {
done()
} else {
done(new Error('Could not connect to peer with peerId:', peerId.toB58String()))
}
provide (cid, callback) {
this.libp2p.contentRouting.provide(cid, callback)
}

// Connect to the given peer
// Send the given msg (instance of Message) to the given peer
sendMessage (peerId, msg, callback) {
if (!this._running) {
return callback(new Error('No running network'))
}
sendMessage (peer, msg, callback) {
if (!this._running) { return callback(new Error(`network isn't running`)) }

const stringId = peerId.toB58String()
const stringId = peer.toB58String() ? peer.toB58String() : peer.id.toB58String()
log('sendMessage to %s', stringId, msg)
let peerInfo
try {
peerInfo = this.peerBook.get(stringId)
} catch (err) {
return callback(err)
}

this._dialPeer(peerInfo, (err, conn, protocol) => {
if (err) {
return callback(err)
}
this._dialPeer(peer, (err, conn, protocol) => {
if (err) { return callback(err) }

let serialized
switch (protocol) {
case BITSWAP100:
serialized = msg.serializeToBitswap100()
break
case BITSWAP110:
serialized = msg.serializeToBitswap110()
break
default:
return callback(new Error('Unkown protocol: ' + protocol))
case BITSWAP100: serialized = msg.serializeToBitswap100(); break
case BITSWAP110: serialized = msg.serializeToBitswap110(); break
default: return callback(new Error('Unkown protocol: ' + protocol))
}
// TODO: why doesn't the error get propageted back??
writeMessage(conn, serialized, (err) => {
if (err) {
log(err)
}
if (err) { log(err) }
})
callback()
})
}

_dialPeer (peerInfo, callback) {
// TODO: if this method is just to call libp2p.dial, better call libp2p dial
connectTo (peer, callback) {
if (!this._running) { return callback(new Error(`network isn't running`)) }

this.libp2p.dial(peer, callback)
}

// Dial to the peer and try to use the most recent Bitswap
_dialPeer (peer, callback) {
// dialByPeerInfo throws if no network is there
try {
// Attempt Bitswap 1.1.0
this.libp2p.dial(peerInfo, BITSWAP110, (err, conn) => {
this.libp2p.dial(peer, BITSWAP110, (err, conn) => {
if (err) {
// Attempt Bitswap 1.0.0
this.libp2p.dial(peerInfo, BITSWAP100, (err, conn) => {
this.libp2p.dial(peer, BITSWAP100, (err, conn) => {
if (err) {
return callback(err)
}
Expand Down
1 change: 1 addition & 0 deletions src/components/want-manager/msg-queue.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ module.exports = class MsgQueue {
log.error('cant connect to peer %s: %s', this.peerId.toB58String(), err.message)
return
}

log('sending message')
this.network.sendMessage(this.peerId, msg, (err) => {
if (err) {
Expand Down
Loading

0 comments on commit 730a4b2

Please sign in to comment.