-
Notifications
You must be signed in to change notification settings - Fork 30
Add DeviceData and eventHandlers.js #14
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
Open
liberty-rowland
wants to merge
3
commits into
nimiq:master
Choose a base branch
from
liberty-rowland:DeviceData
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| node_modules | ||
| .idea | ||
| .DS_Store | ||
| eventHandlers.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /** | ||
| * 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 creating a corresponding | ||
| * PoolAgent. Good time to perform validation or mutation of message data. | ||
| * @param {Object} msg - The full register message. This is not a copy; any | ||
|
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.
|
||
| * 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.beforeRegister = function beforeRegister(msg, connectionPool) { } | ||
|
|
||
| /** | ||
| * 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.onRegister = async function onRegister(agent, connectionPool) { } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
|
|
@@ -37,6 +37,30 @@ class PoolAgent extends Nimiq.Observable { | |
| /** @type {Nimiq.Timers} */ | ||
| this._timers = new Nimiq.Timers(); | ||
| this._timers.resetTimeout('connection-timeout', () => this._onError(), this._pool.config.connectionTimeout); | ||
|
|
||
| // Public interface | ||
| Object.defineProperties(this, { | ||
|
Contributor
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. We use ES6 getters in our codebase instead. |
||
| /** @type {object} */ | ||
| deviceData: { | ||
| enumerable: true, | ||
| get: () => this._deviceData | ||
| }, | ||
| /** @type {number} */ | ||
| deviceId: { | ||
| enumerable: true, | ||
| get: () => this._deviceId | ||
| }, | ||
| /** @type {PoolAgent.Mode} */ | ||
| mode: { | ||
| enumerable: true, | ||
| get: () => this._mode | ||
| }, | ||
| /** @type {boolean} */ | ||
| isRegistered: { | ||
| enumerable: true, | ||
| get: () => this._registered | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -46,7 +70,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 +128,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 +157,22 @@ class PoolAgent extends Nimiq.Observable { | |
| return; | ||
| } | ||
|
|
||
| try { | ||
| this._pool.eventHandlers.beforeRegister(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 +197,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.onRegister(this, this._pool.connectionPool); | ||
| Nimiq.Log.i(PoolAgent, `REGISTER ${this._address.toUserFriendlyAddress()}, current balance: ${await this._pool.getUserBalance(this._userId)}`); | ||
| } | ||
|
|
||
|
|
@@ -449,11 +482,13 @@ 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(); | ||
| } | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
The description is misleading. The
PoolAgentwas already created at this time (because it is created before the first register arrives). There is also no reason why thePoolAgentinstance is not a param on this function.My suggestions would be
a) add
PoolAgentparam tobeforeRegisterb) rename
beforeRegistertoonRegisterMessagec) fix the description to state that it is fired before the message is processed by the PoolAgent
d) also rename
onRegistertoonRegistrationCompletedto make clear that it is fired after the registration.