domotz-remote-pawn
Version:
Domotz Agent
107 lines (97 loc) • 4.44 kB
JavaScript
/** This file is part of Domotz Agent.
* Copyright (C) 2016 Domotz Ltd
*
* Domotz Agent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Domotz Agent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>.
**/
/**
* Created by Georgi Tsatsev <gtsatsev@domotz.com> on 26/07/16.
*
*/
const messageSizeLimit = Math.pow(2, 20);
var tcpRequest = function (message, resourceLocator) {
var tcp = resourceLocator.net;
var cmdUtils = resourceLocator.utils.command;
var payload = message.payload;
var host = payload.host;
var port = payload.port;
var msg = payload.message;
var timeout = payload.timeout;
var encoding = payload.encoding;
var keepAlive = payload.await_timeout || payload.keepAlive;
var entireMessage = '';
var myConsole = resourceLocator.log.decorateLogs();
return {
execute: function (callback) {
var id;
var connection = tcp.createConnection(port, host, function () {
connection.write(msg, encoding);
myConsole.verbose('Message %s with encoding: %s, sent to %s:%d', msg.toString(encoding), encoding, host, port);
});
connection.on('close', function () {
myConsole.debug('TCP connection to host %s closed', host);
});
connection.on('data', function (msg) {
if (keepAlive === true) {
var msgLength = msg.length;
var entireMessageLength = entireMessage.length;
myConsole.debug('Received %d bytes', msgLength);
if (entireMessageLength + msgLength < messageSizeLimit) {
entireMessage += msg;
myConsole.debug('Keep alive, await more messages');
} else {
myConsole.error('Message overflow! Entire message becomes %d bytes.', entireMessageLength);
callback(
cmdUtils.getResponse(
null,
'Message limit of ' +
messageSizeLimit +
' bytes was exceeded with ' +
(entireMessageLength + msgLength - messageSizeLimit) +
' bytes.'
)
);
clearTimeout(id);
connection.destroy();
}
} else {
myConsole.debug('Received %d bytes', msg.length);
myConsole.verbose('Message: %s', msg.toString(encoding));
callback(cmdUtils.getResponse(msg.toString(encoding), null));
clearTimeout(id);
connection.destroy();
}
});
connection.on('error', function (error) {
myConsole.error('TCP error - Error: {}' + error.message);
myConsole.error(error.stack);
callback(cmdUtils.getResponse(null, error));
connection.destroy();
});
id = setTimeout(function () {
if (keepAlive === true) {
myConsole.debug('Timeout expired');
callback(cmdUtils.getResponse(entireMessage.toString(encoding), null));
} else {
myConsole.debug('TIMEOUT - Closed TCP connection after ' + timeout + ' milliseconds');
callback(cmdUtils.getResponse(null, 'Timeout'));
}
connection.destroy();
}, timeout);
},
describe: function () {
return message.correlationId + ' - tcpRequest - ' + host + ':' + port + ' - ' + message.length + ' packets';
},
};
};
module.exports.command = tcpRequest;