Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-rowland committed May 21, 2018
1 parent f131604 commit 1409c95
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 36 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ Run `node index.js --config=[CONFIG_FILE]`. See `[server|service|payout].sample.
The pool server supports custom behaviors through the optional `eventHandlers.js` file. Code written here
will be run every time a specific event occurs. The currently supported events are:

* **beforeRegister** - Called on a *server* configuration when a register message is received, before
setting up the `PoolAgent`.
* **onRegister** - Called on a *server* configuration after a `PoolAgent` has successfully been registered.
* **onRegisterMessage** - Called on a *server* configuration when a register message is received, before
setting `PoolAgent` has processed it.
* **onRegistrationCompleted** - Called on a *server* configuration after a `PoolAgent` has successfully been registered.

See `eventHandlers.sample.js` for the template. To use, copy to a new file: `eventHandlers.js`, and add your
own logic inside the defined callbacks. The pool server will automatically include this file if it exists.
Expand Down
7 changes: 4 additions & 3 deletions eventHandlers.sample.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
*/

/**
* Fired when a REGISTER message is received, before creating a corresponding
* 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,
Expand All @@ -14,7 +15,7 @@
* if registration should not continue.
* @returns {void}
*/
module.exports.beforeRegister = function beforeRegister(msg, connectionPool) { }
module.exports.onRegisterMessage = function onRegisterMessage(agent, msg, connectionPool) { }

/**
* Fired when a new PoolAgent is registered to the PoolServer.
Expand All @@ -23,4 +24,4 @@ module.exports.beforeRegister = function beforeRegister(msg, connectionPool) { }
* logged in as 'pool_server'.
* @returns {void}
*/
module.exports.onRegister = async function onRegister(agent, connectionPool) { }
module.exports.onRegistrationCompleted = async function onRegistrationCompleted(agent, connectionPool) { }
49 changes: 23 additions & 26 deletions src/PoolAgent.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,6 @@ 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, {
/** @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
}
});
}

/**
Expand Down Expand Up @@ -158,7 +134,7 @@ class PoolAgent extends Nimiq.Observable {
}

try {
this._pool.eventHandlers.beforeRegister(msg, this._pool.connectionPool);
this._pool.eventHandlers.onRegisterMessage(this, msg, this._pool.connectionPool);
} catch (e) {
this._sendError(e.message);
return;
Expand Down Expand Up @@ -204,7 +180,7 @@ class PoolAgent extends Nimiq.Observable {
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);
this._pool.eventHandlers.onRegistrationCompleted(this, this._pool.connectionPool);
Nimiq.Log.i(PoolAgent, `REGISTER ${this._address.toUserFriendlyAddress()}, current balance: ${await this._pool.getUserBalance(this._userId)}`);
}

Expand Down Expand Up @@ -492,6 +468,27 @@ class PoolAgent extends Nimiq.Observable {
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';
Expand Down
8 changes: 4 additions & 4 deletions src/PoolServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ const Helper = require('./Helper.js');

/**
* @typedef EventHandlers
* @property {function} beforeRegister
* @property {function} onRegister
* @property {function} onRegisterMessage
* @property {function} onRegistrationCompleted
*/

class PoolServer extends Nimiq.Observable {
Expand Down Expand Up @@ -38,8 +38,8 @@ class PoolServer extends Nimiq.Observable {

/** @type {EventHandlers} */
this.eventHandlers = Object.assign({
beforeRegister: () => { },
onRegister: async () => { }
onRegisterMessage: () => { },
onRegistrationCompleted: async () => { }
}, eventHandlers);

/** @type {PoolConfig} */
Expand Down

0 comments on commit 1409c95

Please sign in to comment.