Simulator first commit
This commit is contained in:
2
node_modules/reconnect-core/.npmignore
generated
vendored
Normal file
2
node_modules/reconnect-core/.npmignore
generated
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
*.sw*
|
8
node_modules/reconnect-core/.travis.yml
generated
vendored
Normal file
8
node_modules/reconnect-core/.travis.yml
generated
vendored
Normal file
@ -0,0 +1,8 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "0.10"
|
||||
- "0.12"
|
||||
- 2
|
||||
- 3
|
||||
- 4
|
||||
- 5
|
22
node_modules/reconnect-core/History.md
generated
vendored
Normal file
22
node_modules/reconnect-core/History.md
generated
vendored
Normal file
@ -0,0 +1,22 @@
|
||||
|
||||
0.2.1 / 2014-10-06
|
||||
==================
|
||||
|
||||
* only react to connect once
|
||||
|
||||
0.2.0 / 2014-07-19
|
||||
==================
|
||||
|
||||
* add history
|
||||
* Setting failAfter option directly instead of on backoff module
|
||||
* Emit fail event
|
||||
|
||||
0.1.0 / 2014-07-05
|
||||
==================
|
||||
|
||||
* docs
|
||||
* add passing test for context in createConnection
|
||||
* Using emitter as 'this' on 'createConnection' call
|
||||
* fix redundant "disconnect" event on disconnect()
|
||||
* add make test
|
||||
* properly defaults opts if only onConnect is supplied
|
6
node_modules/reconnect-core/Makefile
generated
vendored
Normal file
6
node_modules/reconnect-core/Makefile
generated
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
test:
|
||||
@node_modules/.bin/tape test/*.js
|
||||
|
||||
.PHONY: test
|
||||
|
135
node_modules/reconnect-core/README.md
generated
vendored
Normal file
135
node_modules/reconnect-core/README.md
generated
vendored
Normal file
@ -0,0 +1,135 @@
|
||||
# reconnect-core
|
||||
|
||||
Generic stream reconnection module.
|
||||
|
||||
[](http://travis-ci.org/juliangruber/reconnect-core)
|
||||
|
||||
[](https://ci.testling.com/juliangruber/reconnect-core)
|
||||
|
||||
## Usage
|
||||
|
||||
Create a `reconnect` instance that keeps reconnecting over tcp:
|
||||
|
||||
```js
|
||||
var inject = require('reconnect-core');
|
||||
var net = require('net');
|
||||
|
||||
// build you own reconnect module
|
||||
var reconnect = inject(function () {
|
||||
// arguments are what you passed to .connect
|
||||
// this is the reconnect instance
|
||||
return net.connect.apply(null, arguments);
|
||||
});
|
||||
|
||||
var re = reconnect({
|
||||
// all options are optional
|
||||
initialDelay: 1e3,
|
||||
maxDelay: 30e3,
|
||||
strategy: 'fibonacci', // available: fibonacci, exponential, or a custom backoff instance (see below)
|
||||
failAfter: Infinity,
|
||||
randomisationFactor: 0,
|
||||
immediate: false
|
||||
}, function (stream) {
|
||||
// stream = the stream you should consume
|
||||
})
|
||||
.on('connect', function (con) {
|
||||
// con = underlying connection
|
||||
})
|
||||
.on('reconnect', function (n, delay) {
|
||||
// n = current number of reconnect
|
||||
// delay = delay used before reconnect
|
||||
})
|
||||
.on('disconnect', function (err) {
|
||||
// err = possible error
|
||||
})
|
||||
.on('error', function (err) {
|
||||
// never forget
|
||||
})
|
||||
.connect(port)
|
||||
|
||||
// disconnect
|
||||
re.disconnect();
|
||||
|
||||
// ...or prevent reconnecting
|
||||
re.reconnect = false;
|
||||
|
||||
// reset the internal backoff timer
|
||||
re.reset();
|
||||
```
|
||||
|
||||
## Strategies
|
||||
|
||||
reconnect utilises the [backoff](https://github.com/MathieuTurcotte/node-backoff) library to control backoff behaviour.
|
||||
There are 2 options for choosing a strategy for your reconnect instance, pass one of the following to the `strategy` key when creating your instance:
|
||||
* Pass the string "fibonacci" or "exponential" to utilise these built-in backoff strategies, options passed to your reconnect instance will also be passed to these strategies.
|
||||
* Pass a Backoff instance, this allows you to customise your backoff strategy by implementing a [Backoff Strategy](https://github.com/MathieuTurcotte/node-backoff#interface-backoffstrategy).
|
||||
|
||||
An example using a custom strategy:
|
||||
```js
|
||||
var inject = require('reconnect-core');
|
||||
var backoff = require('backoff');
|
||||
var net = require('net');
|
||||
|
||||
// build you own reconnect module
|
||||
var reconnect = inject(function () {
|
||||
// arguments are what you passed to .connect
|
||||
// this is the reconnect instance
|
||||
return net.connect.apply(null, arguments);
|
||||
});
|
||||
|
||||
// Reconnect every 10 seconds
|
||||
var myStrategy = {
|
||||
next: function() { return 10e3; },
|
||||
reset: function() { }
|
||||
}
|
||||
|
||||
var re = reconnect({
|
||||
strategy: new backoff.Backoff(myStrategy),
|
||||
failAfter: Infinity,
|
||||
immediate: false
|
||||
}, function (stream) {
|
||||
// stream = the stream you should consume
|
||||
})
|
||||
```
|
||||
|
||||
## Available implementations
|
||||
|
||||
* tcp: [reconnect-net](https://github.com/juliangruber/reconnect-net)
|
||||
* engine.io/websockets: [reconnect-engine](https://github.com/juliangruber/reconnect-engine)
|
||||
|
||||
## Installation
|
||||
|
||||
With [npm](https://npmjs.org) do:
|
||||
|
||||
```bash
|
||||
npm install reconnect-core
|
||||
```
|
||||
|
||||
## Kudos
|
||||
|
||||
This has been refactored out of [dominictarr](https://github.com/dominictarr)'s
|
||||
[reconnect](https://github.com/dominictarr/reconnect) module.
|
||||
|
||||
## License
|
||||
|
||||
(MIT)
|
||||
|
||||
Copyright (c) 2013 Julian Gruber <julian@juliangruber.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
this software and associated documentation files (the "Software"), to deal in
|
||||
the Software without restriction, including without limitation the rights to
|
||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
|
||||
of the Software, and to permit persons to whom the Software is furnished to do
|
||||
so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
133
node_modules/reconnect-core/index.js
generated
vendored
Normal file
133
node_modules/reconnect-core/index.js
generated
vendored
Normal file
@ -0,0 +1,133 @@
|
||||
var EventEmitter = require('events').EventEmitter
|
||||
var backoff = require('backoff')
|
||||
|
||||
module.exports =
|
||||
function (createConnection) {
|
||||
return function (opts, onConnect) {
|
||||
onConnect = 'function' == typeof opts ? opts : onConnect
|
||||
opts = 'object' == typeof opts ? opts : {initialDelay: 1e3, maxDelay: 30e3}
|
||||
if(!onConnect)
|
||||
onConnect = opts.onConnect
|
||||
|
||||
var emitter = new EventEmitter()
|
||||
emitter.connected = false
|
||||
emitter.reconnect = true
|
||||
|
||||
if(onConnect)
|
||||
//use "connection" to match core (net) api.
|
||||
emitter.on('connection', onConnect)
|
||||
|
||||
var backoffStrategy = opts.strategy || opts.type
|
||||
var backoffMethod
|
||||
if (typeof backoffStrategy == 'string')
|
||||
backoffMethod = backoff[backoffStrategy](opts)
|
||||
else
|
||||
backoffMethod = backoffStrategy || backoff.fibonacci(opts)
|
||||
|
||||
if(opts.failAfter)
|
||||
backoffMethod.failAfter(opts.failAfter);
|
||||
|
||||
backoffMethod.on('backoff', function (n, d, e) {
|
||||
emitter.emit('backoff', n, d, e)
|
||||
})
|
||||
backoffMethod.on('fail', function (e) {
|
||||
emitter.disconnect()
|
||||
emitter.emit('fail', e)
|
||||
})
|
||||
|
||||
var args
|
||||
function attempt (n, delay) {
|
||||
if(emitter.connected) return
|
||||
if(!emitter.reconnect) return
|
||||
|
||||
emitter.emit('reconnect', n, delay)
|
||||
var con = createConnection.apply(emitter, args)
|
||||
emitter._connection = con
|
||||
|
||||
function onError (err) {
|
||||
con.removeListener('error', onError)
|
||||
emitter.emit('error', err)
|
||||
onDisconnect(err)
|
||||
}
|
||||
|
||||
function onDisconnect (err) {
|
||||
emitter.connected = false
|
||||
con.removeListener('close', onDisconnect)
|
||||
con.removeListener('end' , onDisconnect)
|
||||
|
||||
//hack to make http not crash.
|
||||
//HTTP IS THE WORST PROTOCOL.
|
||||
if(con.constructor.name == 'Request')
|
||||
con.on('error', function () {})
|
||||
|
||||
//emit disconnect before checking reconnect, so user has a chance to decide not to.
|
||||
emitter.emit('disconnect', err)
|
||||
|
||||
if(!emitter.reconnect) return
|
||||
try { backoffMethod.backoff() } catch (_) { }
|
||||
}
|
||||
|
||||
con
|
||||
.on('error', onError)
|
||||
.on('close', onDisconnect)
|
||||
.on('end' , onDisconnect)
|
||||
|
||||
if(opts.immediate || con.constructor.name == 'Request') {
|
||||
emitter.connected = true
|
||||
emitter.emit('connect', con)
|
||||
emitter.emit('connection', con)
|
||||
con.once('data', function () {
|
||||
//this is the only way to know for sure that data is coming...
|
||||
backoffMethod.reset()
|
||||
})
|
||||
} else {
|
||||
con
|
||||
.once('connect', function () {
|
||||
backoffMethod.reset()
|
||||
emitter.connected = true
|
||||
if(onConnect)
|
||||
con.removeListener('connect', onConnect)
|
||||
emitter.emit('connect', con)
|
||||
//also support net style 'connection' method.
|
||||
emitter.emit('connection', con)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
emitter.connect =
|
||||
emitter.listen = function () {
|
||||
this.reconnect = true
|
||||
if(emitter.connected) return
|
||||
backoffMethod.reset()
|
||||
backoffMethod.on('ready', attempt)
|
||||
if (!args) {
|
||||
var len = arguments.length;
|
||||
args = new Array(len);
|
||||
for (var i = 0; i < len; i++) {
|
||||
args[i] = arguments[i];
|
||||
}
|
||||
}
|
||||
attempt(0, 0)
|
||||
return emitter
|
||||
}
|
||||
|
||||
//force reconnection
|
||||
|
||||
emitter.disconnect = function () {
|
||||
this.reconnect = false
|
||||
|
||||
if(emitter._connection)
|
||||
emitter._connection.end()
|
||||
|
||||
return emitter
|
||||
}
|
||||
|
||||
emitter.reset = function () {
|
||||
backoffMethod.reset()
|
||||
attempt(0, 0)
|
||||
}
|
||||
|
||||
return emitter
|
||||
}
|
||||
|
||||
}
|
74
node_modules/reconnect-core/package.json
generated
vendored
Normal file
74
node_modules/reconnect-core/package.json
generated
vendored
Normal file
@ -0,0 +1,74 @@
|
||||
{
|
||||
"_from": "reconnect-core@^1.3.0",
|
||||
"_id": "reconnect-core@1.3.0",
|
||||
"_inBundle": false,
|
||||
"_integrity": "sha1-+65SkZp4d9hE4yRtAaLyZwHIM8g=",
|
||||
"_location": "/reconnect-core",
|
||||
"_phantomChildren": {},
|
||||
"_requested": {
|
||||
"type": "range",
|
||||
"registry": true,
|
||||
"raw": "reconnect-core@^1.3.0",
|
||||
"name": "reconnect-core",
|
||||
"escapedName": "reconnect-core",
|
||||
"rawSpec": "^1.3.0",
|
||||
"saveSpec": null,
|
||||
"fetchSpec": "^1.3.0"
|
||||
},
|
||||
"_requiredBy": [
|
||||
"/"
|
||||
],
|
||||
"_resolved": "https://registry.npmjs.org/reconnect-core/-/reconnect-core-1.3.0.tgz",
|
||||
"_shasum": "fbae52919a7877d844e3246d01a2f26701c833c8",
|
||||
"_spec": "reconnect-core@^1.3.0",
|
||||
"_where": "/home/sergiu/linx-audio-simulator",
|
||||
"author": {
|
||||
"name": "Julian Gruber",
|
||||
"email": "mail@juliangruber.com",
|
||||
"url": "http://juliangruber.com"
|
||||
},
|
||||
"bugs": {
|
||||
"url": "https://github.com/juliangruber/reconnect-core/issues"
|
||||
},
|
||||
"bundleDependencies": false,
|
||||
"dependencies": {
|
||||
"backoff": "~2.5.0"
|
||||
},
|
||||
"deprecated": false,
|
||||
"description": "Generic stream reconnection module.",
|
||||
"devDependencies": {
|
||||
"tape": "^4.6.0"
|
||||
},
|
||||
"homepage": "https://github.com/juliangruber/reconnect-core",
|
||||
"keywords": [
|
||||
"reconnect",
|
||||
"core"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "index.js",
|
||||
"name": "reconnect-core",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/juliangruber/reconnect-core.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "make test"
|
||||
},
|
||||
"testling": {
|
||||
"files": "test/*.js",
|
||||
"browsers": [
|
||||
"ie/8..latest",
|
||||
"firefox/17..latest",
|
||||
"firefox/nightly",
|
||||
"chrome/22..latest",
|
||||
"chrome/canary",
|
||||
"opera/12..latest",
|
||||
"opera/next",
|
||||
"safari/5.1..latest",
|
||||
"ipad/6.0..latest",
|
||||
"iphone/6.0..latest",
|
||||
"android-browser/4.2..latest"
|
||||
]
|
||||
},
|
||||
"version": "1.3.0"
|
||||
}
|
34
node_modules/reconnect-core/test/attempts.js
generated
vendored
Normal file
34
node_modules/reconnect-core/test/attempts.js
generated
vendored
Normal file
@ -0,0 +1,34 @@
|
||||
var inject = require('..')
|
||||
var Stream = require('stream')
|
||||
var test = require('tape')
|
||||
|
||||
var reconnect = inject(function () {
|
||||
var s = new Stream
|
||||
process.nextTick(function () {
|
||||
s.emit('error', new Error('oh noes!'))
|
||||
})
|
||||
return s
|
||||
})
|
||||
|
||||
test('attempts', function (t) {
|
||||
t.plan(21)
|
||||
|
||||
var fail = function () { t.fail() }
|
||||
var reconnector = reconnect({initialDelay: 10}, fail)
|
||||
|
||||
reconnector.on('error', function (err) {
|
||||
t.equal(err.message, 'oh noes!', 'correct error message')
|
||||
})
|
||||
|
||||
var onReconnect = function (n, delay) {
|
||||
t.equal(reconnector.connected, false)
|
||||
t.equal(reconnector.reconnect, true)
|
||||
if (n > 4) reconnector.reconnect = false
|
||||
}
|
||||
|
||||
reconnector
|
||||
.on('connect', fail)
|
||||
.on('reconnect', onReconnect)
|
||||
.connect()
|
||||
})
|
||||
|
58
node_modules/reconnect-core/test/connection.js
generated
vendored
Normal file
58
node_modules/reconnect-core/test/connection.js
generated
vendored
Normal file
@ -0,0 +1,58 @@
|
||||
var inject = require('..')
|
||||
var test = require('tape')
|
||||
var Stream = require('stream')
|
||||
var Emitter = require('events').EventEmitter
|
||||
var noop = function () {}
|
||||
|
||||
test('connection', function (t) {
|
||||
t.plan(4) // reconnect and connect
|
||||
|
||||
var ee = new Emitter
|
||||
|
||||
var reconnect = inject(function () {
|
||||
var s = new Stream
|
||||
ee.on('start server', function () {
|
||||
s.emit('connect')
|
||||
s.emit('close')
|
||||
})
|
||||
s.end = noop
|
||||
return s
|
||||
})
|
||||
|
||||
var reconnector = reconnect({initialDelay: 10}, function (stream) {
|
||||
reconnector.reconnect = false
|
||||
reconnector.disconnect()
|
||||
})
|
||||
|
||||
var connected = false
|
||||
var onConnect = function () {
|
||||
connected = true
|
||||
t.ok(reconnector.connected)
|
||||
}
|
||||
|
||||
var onReconnect = function (n) {
|
||||
t.notOk(connected)
|
||||
t.notOk(reconnector.connected)
|
||||
t.ok(reconnector.reconnect)
|
||||
if (n < 4) process.nextTick(function () {
|
||||
ee.emit('start server')
|
||||
})
|
||||
}
|
||||
|
||||
reconnector
|
||||
.on('connect', onConnect)
|
||||
.on('reconnect', onReconnect)
|
||||
.connect()
|
||||
})
|
||||
|
||||
test('this arg', function (t) {
|
||||
var thisArg
|
||||
var reconnect = inject(function () {
|
||||
thisArg = this
|
||||
return new Stream
|
||||
})
|
||||
var reconnector = reconnect()
|
||||
reconnector.connect()
|
||||
t.equal(thisArg, reconnector)
|
||||
t.end()
|
||||
})
|
33
node_modules/reconnect-core/test/disconnect.js
generated
vendored
Normal file
33
node_modules/reconnect-core/test/disconnect.js
generated
vendored
Normal file
@ -0,0 +1,33 @@
|
||||
var inject = require('..')
|
||||
var test = require('tape')
|
||||
var Stream = require('stream')
|
||||
|
||||
test('disconnect', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
var timeout
|
||||
var reconnect = inject(function () {
|
||||
reconnector.reconnect = false
|
||||
reconnector.disconnect()
|
||||
|
||||
timeout = setTimeout(function() {
|
||||
t.fail('client did not disconnect')
|
||||
}, 500)
|
||||
|
||||
var s = new Stream
|
||||
process.nextTick(function () {
|
||||
s.emit('end')
|
||||
})
|
||||
return s
|
||||
})
|
||||
|
||||
var reconnector = reconnect({initialDelay: 10})
|
||||
|
||||
reconnector.on('disconnect', function() {
|
||||
clearTimeout(timeout)
|
||||
t.ok(true, 'disconnected')
|
||||
})
|
||||
|
||||
reconnector.connect()
|
||||
})
|
||||
|
26
node_modules/reconnect-core/test/onconnect-listener.js
generated
vendored
Normal file
26
node_modules/reconnect-core/test/onconnect-listener.js
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
var inject = require('..')
|
||||
var test = require('tape')
|
||||
var Stream = require('stream')
|
||||
var Emitter = require('events').EventEmitter
|
||||
var noop = function () {}
|
||||
|
||||
var ee = new Emitter
|
||||
|
||||
var reconnect = inject(function () {
|
||||
var s = new Stream
|
||||
process.nextTick(function () {
|
||||
s.emit('connect')
|
||||
s.emit('close')
|
||||
})
|
||||
s.end = noop
|
||||
return s
|
||||
})
|
||||
|
||||
test('on connect listener', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
reconnect().on('connect', function () {
|
||||
t.ok(true)
|
||||
this.reconnect = false
|
||||
}).connect()
|
||||
})
|
51
node_modules/reconnect-core/test/reconnection.js
generated
vendored
Normal file
51
node_modules/reconnect-core/test/reconnection.js
generated
vendored
Normal file
@ -0,0 +1,51 @@
|
||||
var inject = require('..')
|
||||
var test = require('tape')
|
||||
var Stream = require('stream')
|
||||
|
||||
test('reconnect', function (t) {
|
||||
t.plan(18)
|
||||
|
||||
var start = false
|
||||
var times = 0
|
||||
var reconnect = inject(function () {
|
||||
var s = new Stream
|
||||
|
||||
if (start) {
|
||||
t.ok(true, 'start server')
|
||||
if (++times >= 2) {
|
||||
reconnector.reconnect = false
|
||||
}
|
||||
process.nextTick(function () {
|
||||
s.emit('connect')
|
||||
});
|
||||
}
|
||||
|
||||
process.nextTick(function () {
|
||||
s.emit('end')
|
||||
})
|
||||
|
||||
return s
|
||||
})
|
||||
|
||||
var reconnector = reconnect({initialDelay: 10}, function () {
|
||||
t.ok(true, 'reconnect cb fired')
|
||||
})
|
||||
|
||||
function onConnect () {
|
||||
t.ok(reconnector.connected, 'client connected')
|
||||
}
|
||||
|
||||
function onReconnect (n) {
|
||||
t.ok(true, 'reconnect ' + n)
|
||||
t.notOk(reconnector.connected, 'not connected')
|
||||
t.ok(reconnector.reconnect, 'will reconnect')
|
||||
if (n == 1) start = true;
|
||||
}
|
||||
|
||||
reconnector
|
||||
.on('connect', onConnect)
|
||||
.on('reconnect', onReconnect)
|
||||
.connect()
|
||||
})
|
||||
|
||||
|
14
node_modules/reconnect-core/test/reset.js
generated
vendored
Normal file
14
node_modules/reconnect-core/test/reset.js
generated
vendored
Normal file
@ -0,0 +1,14 @@
|
||||
|
||||
var inject = require('..')
|
||||
var test = require('tape')
|
||||
var Stream = require('stream')
|
||||
|
||||
var reconnect = inject(function () {
|
||||
return new Stream
|
||||
})
|
||||
|
||||
test('reset', function (t) {
|
||||
// TODO real test
|
||||
reconnect().reset()
|
||||
t.end()
|
||||
})
|
Reference in New Issue
Block a user