Skip to content
This repository has been archived by the owner on Apr 29, 2020. It is now read-only.

Commit

Permalink
level up libp2p functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
daviddias committed May 29, 2016
1 parent e24218d commit 822ed46
Show file tree
Hide file tree
Showing 7 changed files with 640 additions and 106 deletions.
15 changes: 9 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,19 @@
"homepage": "https://github.com/ipfs/js-libp2p-ipfs#readme",
"devDependencies": {
"aegir": "^3.1.0",
"bl": "^1.1.2",
"chai": "^3.5.0",
"pre-commit": "^1.1.3"
"pre-commit": "^1.1.3",
"run-parallel": "^1.1.6"
},
"dependencies": {
"libp2p-spdy": "^0.6.1",
"libp2p-swarm": "^0.19.0",
"libp2p-tcp": "^0.6.0",
"libp2p-websockets": "^0.6.0",
"mafmt": "^2.1.0",
"libp2p-swarm": "^0.19.4",
"libp2p-tcp": "^0.6.1",
"libp2p-websockets": "^0.6.1",
"mafmt": "^2.1.1",
"multiaddr": "^2.0.2",
"peer-book": "^0.3.0",
"peer-id": "^0.7.0",
"peer-info": "^0.7.0",
"run-parallel": "^1.1.6"
Expand All @@ -62,4 +65,4 @@
"Stephen Whitmore <stephen.whitmore@gmail.com>",
"dignifiedquire <dignifiedquire@gmail.com>"
]
}
}
188 changes: 173 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,199 @@
'use strict'

const Swarm = require('libp2p-swarm')
const Peer = require('peer-info')
const PeerId = require('peer-id')
const PeerInfo = require('peer-info')
const PeerBook = require('peer-book')
const TCP = require('libp2p-tcp')
// const UTP = require('libp2p-utp')
const WS = require('libp2p-websockets')
const spdy = require('libp2p-spdy')
const multiaddr = require('multiaddr')
const mafmt = require('mafmt')
const EE = require('events').EventEmitter

exports = module.exports

exports.Node = function Node (peerInfo) {
const OFFLINE_ERROR_MESSAGE = 'The libp2p node is not started yet'
const IPFS_CODE = 421

exports.Node = function Node (pInfo, pBook) {
if (!(this instanceof Node)) {
return new Node(peerInfo)
return new Node(pInfo, pBook)
}

if (!pInfo) {
pInfo = new PeerInfo()
pInfo.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))
}
if (!peerInfo) {
peerInfo = new Peer()
peerInfo.multiaddr.add(multiaddr('/ip4/0.0.0.0/tcp/0'))

if (!pBook) {
pBook = new PeerBook()
}

this.peerInfo = pInfo
this.peerBook = pBook

// Swarm
this.swarm = new Swarm(peerInfo)
this.swarm.transport.add('ws', new WS())
this.swarm.transport.add('tcp', new TCP())
this.swarm = new Swarm(pInfo)
this.swarm.connection.addStreamMuxer(spdy)
this.swarm.connection.reuse()

this.start = (done) => {
this.swarm.listen(done)
this.swarm.on('peer-mux-established', (peerInfo) => {
this.peerBook.put(peerInfo)
})

this.swarm.on('peer-mux-closed', (peerInfo) => {
this.peerBook.removeByB58String(peerInfo.id.toB58String())
})

let isOnline = false

this.start = (callback) => {
const ws = new WS()
const tcp = new TCP()

// Do not activate the dialer if no listener is going to be present
if (ws.filter(this.peerInfo.multiaddrs).length > 0) {
this.swarm.transport.add('ws', new WS())
}
if (tcp.filter(this.peerInfo.multiaddrs).length > 0) {
this.swarm.transport.add('tcp', new TCP())
}

this.swarm.listen((err) => {
if (err) {
return callback(err)
}
isOnline = true
callback()
})
}

this.stop = (callback) => {
isOnline = false
this.swarm.close(callback)
}

this.dialById = (id, protocol, callback) => {
if (typeof protocol === 'function') {
callback = protocol
protocol = undefined
}

if (!isOnline) {
return callback(new Error(OFFLINE_ERROR_MESSAGE))
}
// NOTE, these dialById only works if a previous dial
// was made until we have PeerRouting
// TODO support PeerRouting when it is Ready
callback(new Error('not implemented yet'))
}

this.dialByMultiaddr = (maddr, protocol, callback) => {
if (typeof protocol === 'function') {
callback = protocol
protocol = undefined
}

if (!isOnline) {
return callback(new Error(OFFLINE_ERROR_MESSAGE))
}

if (typeof maddr === 'string') {
maddr = multiaddr(maddr)
}

if (!mafmt.IPFS.matches(maddr.toString())) {
return callback(new Error('multiaddr not valid'))
}

const ipfsIdB58String = maddr.stringTuples().filter((tuple) => {
if (tuple[0] === IPFS_CODE) {
return true
}
})[0][1]

let peer
try {
peer = this.peerBook.getByB58String(ipfsIdB58String)
} catch (err) {
peer = new PeerInfo(PeerId.createFromB58String(ipfsIdB58String))
}

peer.multiaddr.add(maddr)
this.dialByPeerInfo(peer, protocol, callback)
}

this.dialByPeerInfo = (peer, protocol, callback) => {
if (typeof protocol === 'function') {
callback = protocol
protocol = undefined
}
if (!isOnline) {
return callback(new Error(OFFLINE_ERROR_MESSAGE))
}

this.swarm.dial(peer, protocol, (err, conn) => {
if (err) {
return callback(err)
}
this.peerBook.put(peer)
callback(null, conn)
})
}

this.hangUpById = (id, callback) => {
callback(new Error('not implemented yet'))
// TODO
}

this.hangUpByMultiaddr = (maddr, callback) => {
if (!isOnline) {
return callback(new Error(OFFLINE_ERROR_MESSAGE))
}

if (typeof maddr === 'string') {
maddr = multiaddr(maddr)
}

if (!mafmt.IPFS.matches(maddr.toString())) {
return callback(new Error('multiaddr not valid'))
}

const ipfsIdB58String = maddr.stringTuples().filter((tuple) => {
if (tuple[0] === IPFS_CODE) {
return true
}
})[0][1]

try {
const pi = this.peerBook.getByB58String(ipfsIdB58String)
this.hangUpByPeerInfo(pi, callback)
} catch (err) {
// already disconnected
callback()
}
}

this.hangUpByPeerInfo = (peer, callback) => {
if (!isOnline) {
return callback(new Error(OFFLINE_ERROR_MESSAGE))
}

this.peerBook.removeByB58String(peer.id.toB58String())
this.swarm.hangUp(peer, callback)
}

this.handle = (protocol, handler) => {
return this.swarm.handle(protocol, handler)
}

this.unhandle = (protocol) => {
return this.swarm.unhandle(protocol)
}

this.discovery = new EE()
this.routing = null
this.records = null

this.dial = () => {
throw new Error('THIS WILL BE EQUIVALENT TO THE ROUTED HOST FEATURE, IT WILL FIGURE OUT EVERYTHING :D')
}
}
85 changes: 0 additions & 85 deletions test/index.spec.js

This file was deleted.

Loading

0 comments on commit 822ed46

Please sign in to comment.