Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .imptest
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"modelId": "qryDFGlK1tUd" /* B */,
"devices": [
"2370797838a609ee" /* E */
"233f4aafaf952dee" /* G */
],
"agentFile": "tests/agent.nut",
"deviceFile": "MessageManager.class.nut",
"deviceFile": "MessageManager.lib.nut",
"stopOnFailure": false,
"timeout": 30,
"tests": [
Expand Down
24 changes: 13 additions & 11 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
The MIT License (MIT)
MIT License

Copyright (c) 2016 Electric Imp
Copyright 2016-2017 Electric Imp

SPDX-License-Identifier: MIT

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -9,13 +11,13 @@ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
145 changes: 125 additions & 20 deletions MessageManager.class.nut → MessageManager.lib.nut
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
// Copyright (c) 2016-2017 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
// MIT License
//
// Copyright 2016-2017 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.


// Default configuration values
const MM_DEFAULT_DEBUG = 0;
Expand Down Expand Up @@ -38,7 +59,7 @@ const MM_HANDLER_NAME_ON_TIMEOUT = "onTimeout";

class MessageManager {

static VERSION = "1.0.2";
static VERSION = "2.1.0";

// Queue of messages that are pending for acknowledgement
_sentQueue = null;
Expand Down Expand Up @@ -109,9 +130,21 @@ class MessageManager {
// Global handler to be called when a message is acknowledged
_onAck = null;

// Global handler to be called prior to the onAck handler(s) being called
_beforeOnAck = null;

// Global handler to be called when a message is replied
_onReply = null;

// Global handler to be called prior to the onReply handler(s) being called
_beforeOnReply = null;

// Global handler to be called when any data message is received
_beforeOnData = null;

// Global handler to be called prior to any message being replied
_beforeReply = null;

// User defined callback to generate next message id
_nextIdGenerator = null;

Expand Down Expand Up @@ -157,6 +190,9 @@ class MessageManager {
// Handler to be called when the message is replied
_onReply = null;

// Flag indicating that the message was acknowledged
_acked = null;

// Data message constructor
// Constructor is not going to be called from the user code
//
Expand All @@ -177,6 +213,7 @@ class MessageManager {
"created": time()
};
this.tries = 0;
this._acked = false;
this.metadata = metadata;
this._timeout = timeout;
this._nextRetry = 0;
Expand Down Expand Up @@ -302,8 +339,11 @@ class MessageManager {
_cm.onConnect(_onConnect.bindenv(this));
_cm.onDisconnect(_onDisconnect.bindenv(this));

// Make sure we are connected and the onConnect callback is triggered
_cm.connect();
// On device side make sure we are connected and the
// onConnect callback is triggered to notify the agent
if (!_isAgent()) {
_cm.connect();
}
}
}

Expand Down Expand Up @@ -442,6 +482,19 @@ class MessageManager {
_onAck = handler;
}

// Sets the handler to be called before caling the onAck handler(s)
//
// Parameters:
// handler The handler to be called. It has signature:
// handler(message)
// Paremeters:
// message The message that was acked
//
// Returns: Nothing
function beforeOnAck(handler) {
_beforeOnAck = handler;
}

// Sets the handler to be called when the message is replied
//
// Parameters:
Expand All @@ -455,6 +508,44 @@ class MessageManager {
_onReply = handler;
}

// Sets the handler to be called before calling the onReply handler(s)
//
// Parameters:
// handler The handler to be called. It has signature:
// handler(message, response), where
// message The message that received a reply
// response Response received as reply
//
// Returns: Nothing
function beforeOnReply(handler) {
_beforeOnReply = handler;
}

// Sets the handler to be called before calling "on" handlers
//
// Parameters:
// handler The handler to be called. It has signature:
// handler(name, payload), where
// name The message name
// response The message payload
//
// Returns: Nothing
function beforeOnData(handler) {
_beforeOnData = handler;
}

// Sets the handler to be called before calling a reply is sent
//
// Parameters:
// handler The handler to be called. It has signature:
// handler(replyPayload), where
// replyPayload The payload of the reply to be sent
//
// Returns: Nothing
function beforeReply(handler) {
_beforeReply = handler;
}

// Returns the overall number of pending messages
// (either waiting for acknowledgement or hanging in the retry queue)
//
Expand Down Expand Up @@ -549,25 +640,27 @@ class MessageManager {
// Clean up the timer
_queueTimer = null;

local t = time();
local now = time();
local drop = true;

// Process timed out messages from the sent (waiting for ack) queue
foreach (id, msg in _sentQueue) {
local timeout = msg._timeout ? msg._timeout : _msgTimeout;
if (t - msg._sent > timeout) {
if (now - msg._sent > timeout) {
local wait = function(duration = null) {
local delay = duration != null ? duration : timeout;
msg._timeout = t - msg._sent + delay;
msg._timeout = now - msg._sent + delay;
drop = false;
}.bindenv(this);

local fail = function() {
_callOnFail(msg, MM_ERR_USER_CALLED_FAIL);
}.bindenv(this);

_isFunc(msg._onTimeout) && msg._onTimeout(msg, wait, fail);
_isFunc(_onTimeout) && _onTimeout(msg, wait, fail);
if (!msg._acked) {
_isFunc(msg._onTimeout) && msg._onTimeout(msg, wait, fail);
_isFunc(_onTimeout) && _onTimeout(msg, wait, fail);
}

if (drop) {
delete _sentQueue[id];
Expand All @@ -589,10 +682,10 @@ class MessageManager {
//
// Returns: Nothing
function _processRetryQueue() {
local t = time();
local now = time();
// Process retry message queue
foreach (id, msg in _retryQueue) {
if (t >= msg._nextRetry) {
if (now >= msg._nextRetry) {
_retry(msg);
}
}
Expand Down Expand Up @@ -742,17 +835,23 @@ class MessageManager {
local handlerFound = false;
local error = 0;

_isFunc(_beforeOnData) && _beforeOnData(name, payload);

if (name in _on) {
local handler = _on[name];
if (_isFunc(handler)) {
handlerFound = true;
handler(payload, function/*reply*/(data = null) {
replied = true;
error = _partner.send(MM_MESSAGE_TYPE_REPLY, {
"id" : payload["id"],
"data" : data
});
});

local replyPayload = {
"id" : payload["id"],
"data" : data
}

_isFunc(_beforeReply) && _beforeReply(replyPayload);
error = _partner.send(MM_MESSAGE_TYPE_REPLY, replyPayload);
}.bindenv(this));
}
}

Expand Down Expand Up @@ -787,11 +886,16 @@ class MessageManager {
if (id in _sentQueue) {
local msg = _sentQueue[id];

_isFunc(_beforeOnAck) && msg._beforeOnAck(msg);
_isFunc(msg._onAck) && msg._onAck(msg);
_isFunc(_onAck) && _onAck(msg);

// Delete the acked message from the queue
delete _sentQueue[id];
// Delete the acked message from the queue if there is no _onReply handler set (either global or message-specific)
if (!_isFunc(msg._onReply) && !_isFunc(_onReply)) {
delete _sentQueue[id]
} else {
msg._acked = true;
}
}
}

Expand Down Expand Up @@ -857,6 +961,7 @@ class MessageManager {
_isFunc(_onAck) && _onAck(msg);

// Then call the global handlers
_isFunc(_beforeOnReply) && _beforeOnReply(msg, payload);
_isFunc(msg._onReply) && msg._onReply(msg, payload["data"]);
_isFunc(_onReply) && _onReply(msg, payload["data"]);

Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ MessageManager is framework for asynchronous bidirectional agent to device commu

The library uses [ConnectionManager](https://github.com/electricimp/ConnectionManager) on the device side to receive notifications of connection and disconnection events, and to monitor connection status (ie. so that no attempt it made to send messages when the device is disconnected).

**To add this library to your project, add** `#require "messagemanager.class.nut:1.0.2"` **to the top of your agent and device code.**
**To add this library to your project, add** `#require "MessageManager.lib.nut:2.0.0"` **to the top of your agent and device code.**

**Note** MessageManager is designed to run over reliable (ie. TCP/TLS) connections. Retries only occur in the case of dropped connections or lost packets, or if called manually from [beforeSend()](#mmanager_before_send) or [beforeRetry()](#mmanager_before_retry).

Expand Down Expand Up @@ -291,7 +291,7 @@ Sets a message-local version of the [MessageManager.onReply()](#mmanager_on_repl
// Device code

#require "ConnectionManager.class.nut:1.0.2"
#require "MessageManager.class.nut:1.0.2"
#require "MessageManager.lib.nut:2.0.0"

local cm = ConnectionManager({
"blinkupBehavior": ConnectionManager.BLINK_ALWAYS,
Expand Down Expand Up @@ -334,7 +334,7 @@ sendData();
```squirrel
// Agent code

#require "MessageManager.class.nut:1.0.2"
#require "MessageManager.lib.nut:2.0.0"

local mm = MessageManager();

Expand Down
27 changes: 24 additions & 3 deletions tests/Basic.device.test.nut
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
// Copyright (c) 2017 Electric Imp
// This file is licensed under the MIT License
// http://opensource.org/licenses/MIT
// MIT License
//
// Copyright 2016-2017 Electric Imp
//
// SPDX-License-Identifier: MIT
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
// EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
// OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.


class BasicTestCase extends ImpTestCase {

Expand Down
Loading