Skip to content
This repository was archived by the owner on Aug 29, 2023. It is now read-only.

multiple listeners, more tests, understand multiaddr on the listen function #3

Merged
merged 1 commit into from
Mar 4, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
{
"name": "libp2p-tcp",
"version": "0.1.2",
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the abstract-connection interface",
"description": "Node.js implementation of the TCP module that libp2p uses, which implements the interface-connection and interface-transport interfaces",
"main": "src/index.js",
"scripts": {
"test": "node tests/test-ac.js"
"test:compliance:connection": "node tests/connection.js",
"test:compliance:transport": "node tests/transport.js",
"test:specific": "mocha tests/*-test.js",
"test": "npm run test:specific",
"test-2": "npm run test:specific && npm run test:compliance:transport && npm run test:compliance:connection",
"lint": "standard"
},
"pre-commit": [],
"pre-commit": [
"lint",
"test"
],
"repository": {
"type": "git",
"url": "https://github.com/diasdavid/js-libp2p-tcp.git"
Expand All @@ -21,10 +29,14 @@
},
"homepage": "https://github.com/diasdavid/js-libp2p-tcp",
"devDependencies": {
"abstract-connection": "0.0.1",
"abstract-transport": "^0.1.0",
"pre-commit": "^1.1.1",
"standard": "^5.2.2",
"chai": "^3.5.0",
"interface-connection": "0.0.3",
"interface-transport": "^0.1.1",
"istanbul": "^0.4.2",
"mocha": "^2.4.5",
"multiaddr": "^1.1.1",
"pre-commit": "^1.1.2",
"standard": "^6.0.7",
"tape": "^4.2.0"
},
"dependencies": {
Expand Down
63 changes: 57 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,61 @@
var tcp = require('net')
const debug = require('debug')
const log = debug('libp2p:tcp')
const tcp = require('net')

exports = module.exports
exports = module.exports = TCP

exports.dial = function (multiaddr, options) {
options.ready = options.ready || function noop () {}
return tcp.connect(multiaddr.toOptions(), options.ready)
function TCP () {
if (!(this instanceof TCP)) {
return new TCP()
}

const listeners = []

this.dial = function (multiaddr, options) {
if (!options) {
options = {}
}
options.ready = options.ready || function noop () {}
return tcp.connect(multiaddr.toOptions(), options.ready)
}

this.createListener = (multiaddrs, options, handler, callback) => {
if (typeof options === 'function') {
callback = handler
handler = options
options = {}
}

if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
}

var count = 0

multiaddrs.forEach((m) => {
const listener = tcp.createServer(handler)
listener.listen(m.toOptions(), () => {
log('listening on: ', m.toString())
if (++count === multiaddrs.length) {
callback()
}
})
listeners.push(listener)
})
}

this.close = (callback) => {
if (listeners.length === 0) {
throw new Error('there are no listeners')
}
var count = 0
listeners.forEach((listener) => {
listener.close(() => {
if (++count === listeners.length) {
callback()
}
})
})
}
}

exports.createListener = tcp.createServer
File renamed without changes.
57 changes: 57 additions & 0 deletions tests/libp2p-tcp-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* eslint-env mocha */

const expect = require('chai').expect
const TCPlibp2p = require('../src')
const net = require('net')
const multiaddr = require('multiaddr')

describe('libp2p-tcp', function () {
this.timeout(10000)
var tcp

it('create', (done) => {
tcp = new TCPlibp2p()
expect(tcp).to.exist
done()
})

it('listen', (done) => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
tcp.createListener(mh, (socket) => {
expect(socket).to.exist
socket.end()
tcp.close(() => {
done()
})
}, () => {
const socket = net.connect({ host: '127.0.0.1', port: 9090 })
socket.end()
})
})

it('dial', (done) => {
const server = net.createServer((socket) => {
expect(socket).to.exist
socket.end()
server.close(done)
})

server.listen(9090, () => {
const mh = multiaddr('/ip4/127.0.0.1/tcp/9090')
const socket = tcp.dial(mh)
socket.end()
})
})

it('listen on several', (done) => {
const mh1 = multiaddr('/ip4/127.0.0.1/tcp/9090')
const mh2 = multiaddr('/ip4/127.0.0.1/tcp/9091')
const tcp = new TCPlibp2p()

tcp.createListener([mh1, mh2], (socket) => {}, () => {
tcp.close(done)
})
})

it.skip('listen on IPv6', (done) => {})
})
2 changes: 1 addition & 1 deletion tests/test-ac.js → tests/transport.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var tape = require('tape')
var tests = require('abstract-transport/tests')
var tests = require('interface-transport/tests')
var conn = require('../src')

var common = {
Expand Down