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

A few BUGFIXES and new tests to cover those scenarios #54

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
33b18f3
A few BUGFIXES and new tests to cover those scenarios
michaelfakhri Dec 13, 2016
d4ea43e
some of the requested changes
michaelfakhri Dec 14, 2016
1ed3727
lint error
michaelfakhri Dec 14, 2016
f11a78d
A few BUGFIXES and new tests to cover those scenarios
michaelfakhri Dec 13, 2016
e5592fd
some of the requested changes
michaelfakhri Dec 14, 2016
c391214
lint error
michaelfakhri Dec 14, 2016
a39f3d9
sample test
michaelfakhri Dec 16, 2016
c11a2e1
New test and multiple sig server handling
michaelfakhri Jan 15, 2017
39fcefc
New test and multipl sig server
michaelfakhri Jan 15, 2017
4c322a4
include merge of test-dial.js - missed it first time
michaelfakhri Jan 15, 2017
ddecc91
temp shrink-wrap for testing
michaelfakhri Jan 15, 2017
e053355
revert to older simple-peer to avoid onSuccess error
michaelfakhri Jan 15, 2017
4304663
chore: update deps and fix simple-peer to 6.1.0
daviddias Jan 16, 2017
f0afad6
chore: update contributors
daviddias Jan 16, 2017
75dcddf
chore: release version v0.7.1
daviddias Jan 16, 2017
8d69cf8
chore(package): update mafmt to version 2.1.3
greenkeeperio-bot Jan 16, 2017
d82b431
chore(package): update mafmt to version 2.1.4
greenkeeperio-bot Jan 16, 2017
c264d1a
chore: release version v0.7.2
daviddias Jan 16, 2017
5cf4c78
A few BUGFIXES and new tests to cover those scenarios
michaelfakhri Dec 13, 2016
c1b8cc8
avoid stopping sig server to avoid callback err
michaelfakhri Jan 17, 2017
8773c73
Merge branch 'master' into master
michaelfakhri Jan 17, 2017
33a302a
remove extra line added during rebase
michaelfakhri Jan 17, 2017
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
50 changes: 40 additions & 10 deletions gulpfile.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
'use strict'

const gulp = require('gulp')
const parallel = require('async/parallel')

const sigServer = require('./src/signalling')

let sigS
let sigS, sigS2

gulp.task('test:node:before', boot)
gulp.task('test:node:after', stop)
Expand All @@ -15,19 +17,47 @@ function boot (done) {
port: 15555,
host: '127.0.0.1'
}
const options2 = {
port: 15556,
host: '127.0.0.1'
}

sigServer.start(options, (err, server) => {
if (err) {
throw err
}
sigS = server
console.log('signalling on:', server.info.uri)
done()
})
parallel([
first,
second
], done)
function first () {
sigServer.start(options, (err, server) => {
if (err) {
throw err
}
sigS = server
console.log('signalling on:', server.info.uri)
})
}
function second () {
sigServer.start(options2, (err, server) => {
if (err) {
throw err
}
sigS2 = server
console.log('signalling 2 on:', server.info.uri)
done()
})
}
}

function stop (done) {
sigS.stop(done)
parallel([
first,
second
], done)
function first (done) {
sigS.stop(() => done())
}
function second (done) {
sigS2.stop(() => done())
}
}

require('aegir/gulp')(gulp)
54 changes: 26 additions & 28 deletions src/webrtc-star/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ function WebRTCStar () {
return new WebRTCStar()
}

let maSelf
const listeners = {}
this.discovery = new EE()

Expand All @@ -36,12 +35,14 @@ function WebRTCStar () {
options = {}
}

if (!callback) {
callback = function noop () {}
}
callback = callback || function () {}

const intentId = (~~(Math.random() * 1e9)).toString(36) + Date.now()
const sioClient = listeners[Object.keys(listeners)[0]].io
const keys = Object.keys(listeners)
.filter((key) => key.startsWith(ma.toString().substring(0, ma.toString().lastIndexOf('/'))))
const listener = listeners[keys[0]]
if (!listener) return callback(new Error('signalling server not connected'))
const sioClient = listener.io

const spOptions = {
initiator: true,
Expand All @@ -58,7 +59,7 @@ function WebRTCStar () {
channel.on('signal', function (signal) {
sioClient.emit('ss-handshake', {
intentId: intentId,
srcMultiaddr: maSelf.toString(),
srcMultiaddr: listener.ma.toString(),
dstMultiaddr: ma.toString(),
signal: signal
})
Expand Down Expand Up @@ -113,14 +114,12 @@ function WebRTCStar () {
const listener = new EE()

listener.listen = (ma, callback) => {
if (!callback) {
callback = function noop () {}
}
maSelf = ma
callback = callback || function () {}

const sioUrl = 'http://' + ma.toString().split('/')[3] + ':' + ma.toString().split('/')[5]

listener.io = io.connect(sioUrl, sioOptions)
listener.ma = ma

listener.io.on('connect_error', callback)
listener.io.on('error', (err) => {
Expand All @@ -135,8 +134,25 @@ function WebRTCStar () {
listener.io.on('ws-handshake', incommingDial)
listener.io.on('ws-peer', peerDiscovered.bind(this))
listener.emit('listening')
listeners[ma.toString()] = listener
callback()
})
listener.close = (callback) => {
callback = callback || function () {}

listener.io.emit('ss-leave', ma.toString())
setTimeout(() => {
listener.io.disconnect()
delete listeners[ma.toString()]
listener.emit('close')
callback()
}, 100)
}
listener.getAddrs = (callback) => {
setImmediate(() => {
callback(null, [ma])
})
}

function incommingDial (offer) {
if (offer.answer || offer.err) {
Expand Down Expand Up @@ -172,24 +188,6 @@ function WebRTCStar () {
}
}

listener.close = (callback) => {
if (!callback) {
callback = function noop () {}
}
listener.io.emit('ss-leave')
setTimeout(() => {
listener.emit('close')
callback()
}, 100)
}

listener.getAddrs = (callback) => {
process.nextTick(() => {
callback(null, [maSelf])
})
}

listeners[multiaddr.toString()] = listener
return listener
}

Expand Down
105 changes: 105 additions & 0 deletions test/webrtc-star/test-dial.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,108 @@ describe('dial', () => {
// TODO IPv6 not supported yet
})
})
describe('complex dial scenarios', () => {
let ws1, ws2, ws3
const ma1a = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooA1')
const ma1b = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooA2')
const ma1c = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15556/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooA3')
const ma2 = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15555/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooB1')
const ma3 = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.1/tcp/15556/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSoooC1')
before((done) => {
series([
first,
second,
third,
fourth,
fifth
], done)

function first (next) {
ws1 = new WebRTCStar()

const listener = ws1.createListener((conn) => {
pull(conn, conn)
})

// close immediately
listener.listen(ma1a, () => {
listener.close(next)
})
}

function second (next) {
const listener = ws1.createListener((conn) => {
pull(conn, conn)
})

listener.listen(ma1b, next)
}
function third (next) {
const listener = ws1.createListener((conn) => {
pull(conn, conn)
})

listener.listen(ma1c, next)
}

function fourth (next) {
ws2 = new WebRTCStar()

const listener = ws2.createListener((conn) => {
pull(conn, conn)
})
listener.listen(ma2, next)
}
function fifth (next) {
ws3 = new WebRTCStar()

const listener = ws3.createListener((conn) => {
pull(conn, conn)
})
listener.listen(ma3, next)
}
})

it('dial closed listener should error', (done) => {
ws2.dial(ma1a, (err, conn) => {
expect(err).to.exist
done()
})
})

it('dial after first listener is closed and its signalling connection disconnected', (done) => {
ws1.dial(ma2, (err, conn) => {
expect(err).to.not.exist

const data = new Buffer('some data')

pull(
pull.values([data]),
conn,
pull.collect((err, values) => {
expect(err).to.not.exist
expect(values).to.be.eql([data])
done()
})
)
})
})
it('dial a second node on a different signaling server', (done) => {
ws1.dial(ma3, (err, conn) => {
expect(err).to.not.exist

const data = new Buffer('some data')

pull(
pull.values([data]),
conn,
pull.collect((err, values) => {
expect(err).to.not.exist
expect(values).to.be.eql([data])
done()
})
)
})
})
})

10 changes: 10 additions & 0 deletions test/webrtc-star/test-listen.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@ describe('listen', () => {
})
})

it('listen on offline signalling server should error', (done) => {
let maOfflineSigServer = multiaddr('/libp2p-webrtc-star/ip4/127.0.0.0/tcp/15555/ws/ipfs/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSooooA')
const listener = ws.createListener((conn) => {})
listener.listen(maOfflineSigServer, (err) => {
expect(err).to.exist
listener.once('close', done)
listener.close()
})
})

it.skip('close listener with connections, through timeout', (done) => {
// TODO ? Should this apply ?
})
Expand Down