From 0b6361e27a2221a07978be0f99f3f12c18f3b2f4 Mon Sep 17 00:00:00 2001 From: David Dias Date: Fri, 4 Mar 2016 18:58:01 +0000 Subject: [PATCH] multiple listeners, more tests, understand multiaddr on the listen function --- package.json | 26 ++++++++---- src/index.js | 63 ++++++++++++++++++++++++++--- tests/{test-as.js => connection.js} | 0 tests/libp2p-tcp-test.js | 57 ++++++++++++++++++++++++++ tests/{test-ac.js => transport.js} | 2 +- 5 files changed, 134 insertions(+), 14 deletions(-) rename tests/{test-as.js => connection.js} (100%) create mode 100644 tests/libp2p-tcp-test.js rename tests/{test-ac.js => transport.js} (79%) diff --git a/package.json b/package.json index 2d7eb7e..25e3173 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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": { diff --git a/src/index.js b/src/index.js index 8cda113..1ab4463 100644 --- a/src/index.js +++ b/src/index.js @@ -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 diff --git a/tests/test-as.js b/tests/connection.js similarity index 100% rename from tests/test-as.js rename to tests/connection.js diff --git a/tests/libp2p-tcp-test.js b/tests/libp2p-tcp-test.js new file mode 100644 index 0000000..ac4a252 --- /dev/null +++ b/tests/libp2p-tcp-test.js @@ -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) => {}) +}) diff --git a/tests/test-ac.js b/tests/transport.js similarity index 79% rename from tests/test-ac.js rename to tests/transport.js index ce82bf1..13f1a1d 100644 --- a/tests/test-ac.js +++ b/tests/transport.js @@ -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 = {