-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refined Topic Alias support. (Implement #1300)
Add automatic topic alias management functionality. - On PUBLISH sending, the client can automatic using/assin Topic Alias (optional). - On PUBLISH receiving, the topic parameter of on message handler is automatically complemented but the packet.topic preserves the original topic. Fix invalid tests.
- Loading branch information
Showing
7 changed files
with
892 additions
and
42 deletions.
There are no files selected for viewing
This file contains 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 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 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,47 @@ | ||
'use strict' | ||
|
||
/** | ||
* Topic Alias receiving manager | ||
* This holds alias to topic map | ||
* @param {Number} [max] - topic alias maximum entries | ||
*/ | ||
function TopicAliasRecv (max) { | ||
if (!(this instanceof TopicAliasRecv)) { | ||
return new TopicAliasRecv(max) | ||
} | ||
this.aliasToTopic = {} | ||
this.max = max | ||
} | ||
|
||
/** | ||
* Insert or update topic - alias entry. | ||
* @param {String} [topic] - topic | ||
* @param {Number} [alias] - topic alias | ||
* @returns {Boolean} - if success return true otherwise false | ||
*/ | ||
TopicAliasRecv.prototype.put = function (topic, alias) { | ||
if (alias === 0 || alias > this.max) { | ||
return false | ||
} | ||
this.aliasToTopic[alias] = topic | ||
this.length = Object.keys(this.aliasToTopic).length | ||
return true | ||
} | ||
|
||
/** | ||
* Get topic by alias | ||
* @param {String} [topic] - topic | ||
* @returns {Number} - if mapped topic exists return topic alias, otherwise return undefined | ||
*/ | ||
TopicAliasRecv.prototype.getTopicByAlias = function (alias) { | ||
return this.aliasToTopic[alias] | ||
} | ||
|
||
/** | ||
* Clear all entries | ||
*/ | ||
TopicAliasRecv.prototype.clear = function () { | ||
this.aliasToTopic = {} | ||
} | ||
|
||
module.exports = TopicAliasRecv |
Oops, something went wrong.