-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add DeviceData and eventHandlers.js #14
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
.idea | ||
.DS_Store | ||
eventHandlers.js |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* NOTE: Don't modify this file! Copy this file to `evenHandlers.js` and it will | ||
* automatically be included in the pool server! | ||
*/ | ||
|
||
/** | ||
* Fired when a REGISTER message is received, before being processed by the | ||
* PoolAgent. Good time to perform validation or mutation of message data. | ||
* @param {PoolAgent} agent - The Agent for the newly registered device. | ||
* @param {Object} msg - The full register message. This is not a copy; any | ||
* mutation will affect the data used to create the PoolAgent. | ||
* @param {mysql.PoolConnection} connectionPool - A MySQL connection pool, | ||
* logged in as 'pool_server'. | ||
* @throws {Error} Should throw an Error with a message to send to the device | ||
* if registration should not continue. | ||
* @returns {void} | ||
*/ | ||
module.exports.onRegisterMessage = function onRegisterMessage(agent, msg, connectionPool) { } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this one be async as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I thought I'd leave it synchronous since I was catching with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, exceptions do work across async/await |
||
|
||
/** | ||
* Fired when a new PoolAgent is registered to the PoolServer. | ||
* @param {PoolAgent} agent - The Agent for the newly registered device. | ||
* @param {mysql.PoolConnection} connectionPool - A MySQL connection pool, | ||
* logged in as 'pool_server'. | ||
* @returns {void} | ||
*/ | ||
module.exports.onRegistrationCompleted = async function onRegistrationCompleted(agent, connectionPool) { } |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ class PoolAgent extends Nimiq.Observable { | |
this._netAddress = netAddress; | ||
|
||
/** @type {PoolAgent.Mode} */ | ||
this.mode = PoolAgent.Mode.UNREGISTERED; | ||
this._mode = PoolAgent.Mode.UNREGISTERED; | ||
|
||
/** @type {number} */ | ||
this._difficulty = this._pool.config.startDifficulty; | ||
|
@@ -46,7 +46,7 @@ class PoolAgent extends Nimiq.Observable { | |
* @param {Nimiq.Hash} accountsHash | ||
*/ | ||
async updateBlock(prevBlock, transactions, prunedAccounts, accountsHash) { | ||
if (this.mode !== PoolAgent.Mode.NANO) return; | ||
if (this._mode !== PoolAgent.Mode.NANO) return; | ||
if (!prevBlock || !transactions || !prunedAccounts || !accountsHash) return; | ||
|
||
this._currentBody = new Nimiq.BlockBody(this._pool.poolAddress, transactions, this._extraData, prunedAccounts); | ||
|
@@ -104,9 +104,9 @@ class PoolAgent extends Nimiq.Observable { | |
|
||
switch (msg.message) { | ||
case PoolAgent.MESSAGE_SHARE: { | ||
if (this.mode === PoolAgent.Mode.NANO) { | ||
if (this._mode === PoolAgent.Mode.NANO) { | ||
await this._onNanoShareMessage(msg); | ||
} else if (this.mode === PoolAgent.Mode.SMART) { | ||
} else if (this._mode === PoolAgent.Mode.SMART) { | ||
await this._onSmartShareMessage(msg); | ||
} | ||
this._sharesSinceReset++; | ||
|
@@ -133,14 +133,22 @@ class PoolAgent extends Nimiq.Observable { | |
return; | ||
} | ||
|
||
try { | ||
this._pool.eventHandlers.onRegisterMessage(this, msg, this._pool.connectionPool); | ||
} catch (e) { | ||
this._sendError(e.message); | ||
return; | ||
} | ||
|
||
this._address = Nimiq.Address.fromUserFriendlyAddress(msg.address); | ||
this._deviceId = msg.deviceId; | ||
this._deviceData = msg.deviceData; | ||
switch (msg.mode) { | ||
case PoolAgent.MODE_SMART: | ||
this.mode = PoolAgent.Mode.SMART; | ||
this._mode = PoolAgent.Mode.SMART; | ||
break; | ||
case PoolAgent.MODE_NANO: | ||
this.mode = PoolAgent.Mode.NANO; | ||
this._mode = PoolAgent.Mode.NANO; | ||
break; | ||
default: | ||
throw new Error('Client did not specify mode'); | ||
|
@@ -165,13 +173,14 @@ class PoolAgent extends Nimiq.Observable { | |
}); | ||
|
||
this._sendSettings(); | ||
if (this.mode === PoolAgent.Mode.NANO) { | ||
if (this._mode === PoolAgent.Mode.NANO) { | ||
this._pool.requestCurrentHead(this); | ||
} | ||
await this.sendBalance(); | ||
this._timers.resetInterval('send-balance', () => this.sendBalance(), 1000 * 60 * 5); | ||
this._timers.resetInterval('send-keep-alive-ping', () => this._ws.ping(), 1000 * 10); | ||
|
||
this._pool.eventHandlers.onRegistrationCompleted(this, this._pool.connectionPool); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing await? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch |
||
Nimiq.Log.i(PoolAgent, `REGISTER ${this._address.toUserFriendlyAddress()}, current balance: ${await this._pool.getUserBalance(this._userId)}`); | ||
} | ||
|
||
|
@@ -449,14 +458,37 @@ class PoolAgent extends Nimiq.Observable { | |
_onClose() { | ||
this._offAll(); | ||
|
||
this._registered = false; | ||
this._timers.clearAll(); | ||
this._pool.removeAgent(this); | ||
} | ||
|
||
_onError() { | ||
this._registered = false; | ||
this._pool.removeAgent(this); | ||
this._ws.close(); | ||
} | ||
|
||
|
||
/** @type {object} */ | ||
get deviceData() { | ||
return this._deviceData; | ||
} | ||
|
||
/** @type {number} */ | ||
get deviceId() { | ||
return this._deviceId; | ||
} | ||
|
||
/** @type {PoolAgent.Mode} */ | ||
get mode() { | ||
return this._mode; | ||
} | ||
|
||
/** @type {boolean} */ | ||
get isRegistered() { | ||
return this._registered; | ||
} | ||
} | ||
PoolAgent.MESSAGE_REGISTER = 'register'; | ||
PoolAgent.MESSAGE_REGISTERED = 'registered'; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
msg
should definitely be a copy