Skip to content

Commit

Permalink
update _doOpen, _doClose, error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
fanatid committed Feb 16, 2015
1 parent d283da2 commit 28b38b5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var createError = require('errno').create
* | +-- ChainRequestError
* | +-- ConnectionTimeout
* | +-- ElectrumJSError
* | +-- GetHeaderError
* | +-- GetTxError
* | +-- IdleTimeout
* | +-- NotConnectedError
Expand Down
13 changes: 11 additions & 2 deletions src/network/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ Chain.prototype._doOpen = function () {
return self.emit('error', new errors.NotImplementedError(errMsg))
}

if (self.readyState !== self.CLOSED) {
return
}

self._setReadyState(self.CONNECTING)

self._autoReconnect = true
Expand Down Expand Up @@ -164,6 +168,10 @@ Chain.prototype._doOpen = function () {
* @see {@link Network#_doClose}
*/
Chain.prototype._doClose = function () {
if (this.readyState === this.CLOSING || this.readyState === this.CLOSED) {
return
}

this._setReadyState(this.CLOSING)

this._ws.onopen = null
Expand Down Expand Up @@ -198,7 +206,7 @@ Chain.prototype._updateIdleTimeout = function () {
self._idleTimeout = setTimeout(function () {
if (self.readyState === self.OPEN) {
self._ws.close()
self.emit('error', new errors.IdleTimeout('Chain: WebSocket'))
self.emit('error', new errors.IdleTimeout('Chain: WebSocket timeout'))
}

}, 25000)
Expand Down Expand Up @@ -333,7 +341,8 @@ Chain.prototype.getHeader = function (height) {
}

if (response.height !== height) {
throw new errors.GetHeaderError()
var errMsg = 'Chain: requested - ' + height + ', got - ' + response.height
throw new errors.GetHeaderError(errMsg)
}

yatc.verify('ChainHeader', response)
Expand Down
15 changes: 12 additions & 3 deletions src/network/electrumjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ function ElectrumJS(opts) {

self._socket.on('connect_error', function () {
self._setReadyState(self.CLOSED)
self.emit('error', new errors.ConnectionTimeout('ElectrumJS'))
self.emit('error', new errors.ConnectionTimeout('ElectrumJS: connect_error'))
})

self._socket.on('connect_timeout', function () {
self._setReadyState(self.CLOSED)
self.emit('error', new errors.ConnectionTimeout('ElectrumJS'))
self.emit('error', new errors.ConnectionTimeout('ElectrumJS: connect_timeout'))
})

self._socket.on('disconnect', function (reason) {
Expand Down Expand Up @@ -150,6 +150,10 @@ inherits(ElectrumJS, Network)
* @see {@link Network#_doOpen}
*/
ElectrumJS.prototype._doOpen = function () {
if (this.readyState !== this.CLOSED) {
return
}

this._setReadyState(this.CONNECTING)
this._socket.connect()
}
Expand All @@ -160,6 +164,10 @@ ElectrumJS.prototype._doOpen = function () {
* @see {@link Network#_doClose}
*/
ElectrumJS.prototype._doClose = function () {
if (this.readyState === this.CLOSING || this.readyState === this.CLOSED) {
return
}

this._setReadyState(this.CLOSING)
this._socket.disconnect()
}
Expand Down Expand Up @@ -248,7 +256,8 @@ ElectrumJS.prototype.getHeader = function (height) {
}

if (response.block_height !== height) {
throw new errors.GetHeaderError()
var errMsg = 'Chain: requested - ' + height + ', got - ' + response.block_height
throw new errors.GetHeaderError(errMsg)
}

yatc.verify('ElectrumHeader', response)
Expand Down

0 comments on commit 28b38b5

Please sign in to comment.