mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-04-25 18:42:15 +00:00
connection reuse test
This commit is contained in:
parent
a80a5fc32b
commit
a5b2524873
13
README.md
13
README.md
@ -13,4 +13,17 @@ Ref link (still a WiP) - https://github.com/diasdavid/specs/blob/protocol-spec/p
|
|||||||
|
|
||||||
# Usage
|
# Usage
|
||||||
|
|
||||||
|
## API calls
|
||||||
|
|
||||||
|
.openStream
|
||||||
|
.registerHandle
|
||||||
|
|
||||||
|
## Events emmited
|
||||||
|
|
||||||
|
.on('error')
|
||||||
|
|
||||||
|
.on('connection')
|
||||||
|
.on('connection-unknown')
|
||||||
|
|
||||||
|
.on('stream')
|
||||||
|
.on('stream-unknown')
|
||||||
|
@ -14,11 +14,11 @@ i.on('thenews', function (news) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
b.on('error', function (err) {
|
b.on('error', function (err) {
|
||||||
if (err) return
|
console.log(err)
|
||||||
})
|
})
|
||||||
|
|
||||||
b.listen()
|
b.listen()
|
||||||
|
|
||||||
b.registerHandle('/ipfs/sparkles/1.2.3', function (stream) {
|
b.registerHandle('/ipfs/sparkles/1.2.3', function (err, stream) {
|
||||||
console.log('woop got a stream')
|
console.log('woop got a stream')
|
||||||
})
|
})
|
40
src/swarm.js
40
src/swarm.js
@ -26,26 +26,19 @@ function Swarm () {
|
|||||||
// set the listener
|
// set the listener
|
||||||
|
|
||||||
self.listen = function (port, ready) {
|
self.listen = function (port, ready) {
|
||||||
if (!ready) {
|
if (!ready) { ready = function noop () {} }
|
||||||
ready = function noop () {}
|
|
||||||
}
|
|
||||||
if (typeof port === 'function') {
|
if (typeof port === 'function') {
|
||||||
ready = port
|
ready = port
|
||||||
} else if (port) {
|
} else if (port) { self.port = port }
|
||||||
self.port = port
|
|
||||||
}
|
|
||||||
|
|
||||||
self.listener = tcp.createServer(function (socket) {
|
self.listener = tcp.createServer(function (socket) {
|
||||||
socket.on('error', errorEmit)
|
errorUp(self, socket)
|
||||||
var ms = new Select()
|
var ms = new Select()
|
||||||
ms.handle(socket)
|
ms.handle(socket)
|
||||||
ms.addHandler('/spdy/3.1.0', function (ds) {
|
ms.addHandler('/spdy/3.1.0', function (ds) {
|
||||||
log.info('Negotiated spdy with incoming socket')
|
log.info('Negotiated spdy with incoming socket')
|
||||||
|
|
||||||
var conn = spdy.connection.create(ds, {
|
var conn = spdy.connection.create(ds, { protocol: 'spdy', isServer: true })
|
||||||
protocol: 'spdy',
|
|
||||||
isServer: true
|
|
||||||
})
|
|
||||||
|
|
||||||
conn.start(3.1)
|
conn.start(3.1)
|
||||||
|
|
||||||
@ -53,14 +46,13 @@ function Swarm () {
|
|||||||
|
|
||||||
// attach multistream handlers to incoming streams
|
// attach multistream handlers to incoming streams
|
||||||
conn.on('stream', registerHandles)
|
conn.on('stream', registerHandles)
|
||||||
conn.on('error', errorEmit)
|
errorUp(self, conn)
|
||||||
|
|
||||||
// IDENTIFY DOES THAT FOR US
|
// IDENTIFY DOES THAT FOR US
|
||||||
// conn.on('close', function () { delete self.connections[conn.peerId] })
|
// conn.on('close', function () { delete self.connections[conn.peerId] })
|
||||||
})
|
})
|
||||||
}).listen(self.port, ready)
|
}).listen(self.port, ready)
|
||||||
self.listener.on('error', errorEmit)
|
errorUp(self, self.listener)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// interface
|
// interface
|
||||||
@ -76,7 +68,7 @@ function Swarm () {
|
|||||||
|
|
||||||
var tmp = tcp.connect(multiaddr.toOptions(), function () {
|
var tmp = tcp.connect(multiaddr.toOptions(), function () {
|
||||||
socket = tmp
|
socket = tmp
|
||||||
socket.on('error', errorEmit)
|
errorUp(self, socket)
|
||||||
next()
|
next()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -109,7 +101,7 @@ function Swarm () {
|
|||||||
self.connections[peer.id.toB58String()] = conn
|
self.connections[peer.id.toB58String()] = conn
|
||||||
|
|
||||||
conn.on('close', function () { delete self.connections[peer.id.toB58String()] })
|
conn.on('close', function () { delete self.connections[peer.id.toB58String()] })
|
||||||
conn.on('error', errorEmit)
|
errorUp(self, conn)
|
||||||
|
|
||||||
createStream(peer, protocol, cb)
|
createStream(peer, protocol, cb)
|
||||||
})
|
})
|
||||||
@ -121,7 +113,7 @@ function Swarm () {
|
|||||||
var conn = self.connections[peer.id.toB58String()]
|
var conn = self.connections[peer.id.toB58String()]
|
||||||
conn.request({path: '/', method: 'GET'}, function (err, stream) {
|
conn.request({path: '/', method: 'GET'}, function (err, stream) {
|
||||||
if (err) { return cb(err) }
|
if (err) { return cb(err) }
|
||||||
stream.on('error', errorEmit)
|
errorUp(self, stream)
|
||||||
|
|
||||||
// negotiate desired protocol
|
// negotiate desired protocol
|
||||||
var msi = new Interactive()
|
var msi = new Interactive()
|
||||||
@ -135,11 +127,11 @@ function Swarm () {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self.registerHandle = function (protocol, handleFunc) {
|
self.registerHandler = function (protocol, handlerFunc) {
|
||||||
if (self.handles[protocol]) {
|
if (self.handles[protocol]) {
|
||||||
throw new Error('Handle for protocol already exists', protocol)
|
return handlerFunc(new Error('Handle for protocol already exists', protocol))
|
||||||
}
|
}
|
||||||
self.handles.push({ protocol: protocol, func: handleFunc })
|
self.handles.push({ protocol: protocol, func: handlerFunc })
|
||||||
log.info('Registered handler for protocol:', protocol)
|
log.info('Registered handler for protocol:', protocol)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -161,7 +153,7 @@ function Swarm () {
|
|||||||
|
|
||||||
function registerHandles (stream) {
|
function registerHandles (stream) {
|
||||||
log.info('Registering protocol handlers on new stream')
|
log.info('Registering protocol handlers on new stream')
|
||||||
stream.on('error', errorEmit)
|
errorUp(self, stream)
|
||||||
var msH = new Select()
|
var msH = new Select()
|
||||||
msH.handle(stream)
|
msH.handle(stream)
|
||||||
self.handles.forEach(function (handle) {
|
self.handles.forEach(function (handle) {
|
||||||
@ -169,8 +161,12 @@ function Swarm () {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function errorEmit (err) { self.emit('error', err) }
|
}
|
||||||
|
|
||||||
|
function errorUp (self, emitter) {
|
||||||
|
emitter.on('error', function (err) {
|
||||||
|
self.emit('error', err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function Counter (target, callback) {
|
function Counter (target, callback) {
|
||||||
|
@ -32,34 +32,21 @@ beforeEach(function (done) {
|
|||||||
peerB = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmB.port)])
|
peerB = new Peer(Id.create(), [multiaddr('/ip4/127.0.0.1/tcp/' + swarmB.port)])
|
||||||
c.hit()
|
c.hit()
|
||||||
})
|
})
|
||||||
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach({ timeout: 5000 }, function (done) {
|
afterEach(function (done) {
|
||||||
var c = new Counter(4, done)
|
swarmA.closeListener()
|
||||||
swarmA.closeConns(function () {
|
swarmB.closeListener()
|
||||||
c.hit()
|
done()
|
||||||
swarmA.closeListener(function () {
|
|
||||||
console.log('AAA CLOSE')
|
|
||||||
c.hit()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
swarmB.closeConns(function () {
|
|
||||||
console.log('bb')
|
|
||||||
c.hit()
|
|
||||||
swarmB.closeListener(function () {
|
|
||||||
console.log('BBB CLOSE')
|
|
||||||
c.hit()
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
experiment('BASE', function () {
|
experiment('BASE', function () {
|
||||||
test('Open a stream', {timeout: false}, function (done) {
|
test('Open a stream', function (done) {
|
||||||
var protocol = '/sparkles/3.3.3'
|
var protocol = '/sparkles/3.3.3'
|
||||||
var c = new Counter(2, done)
|
var c = new Counter(2, done)
|
||||||
|
|
||||||
swarmB.registerHandle(protocol, function (stream) {
|
swarmB.registerHandler(protocol, function (stream) {
|
||||||
c.hit()
|
c.hit()
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -69,17 +56,21 @@ experiment('BASE', function () {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test('Reuse stream (from dialer)', {timeout: false}, function (done) {
|
test('Reuse connection (from dialer)', function (done) {
|
||||||
var protocol = '/sparkles/3.3.3'
|
var protocol = '/sparkles/3.3.3'
|
||||||
var c = new Counter(2, done)
|
|
||||||
|
|
||||||
swarmB.registerHandle(protocol, function (stream) {
|
swarmB.registerHandler(protocol, function (err, stream) {
|
||||||
c.hit()
|
expect(err).to.not.be.instanceof(Error)
|
||||||
})
|
})
|
||||||
|
|
||||||
swarmA.openStream(peerB, protocol, function (err, stream) {
|
swarmA.openStream(peerB, protocol, function (err, stream) {
|
||||||
expect(err).to.not.be.instanceof(Error)
|
expect(err).to.not.be.instanceof(Error)
|
||||||
c.hit()
|
|
||||||
|
swarmA.openStream(peerB, protocol, function (err, stream) {
|
||||||
|
expect(err).to.not.be.instanceof(Error)
|
||||||
|
expect(swarmA.connections.length === 1)
|
||||||
|
done()
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -99,3 +90,8 @@ function Counter (target, callback) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkErr (err) {
|
||||||
|
console.log('err')
|
||||||
|
expect(err).to.be.instanceof(Error)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user