domotz-remote-pawn
Version:
Domotz Agent
60 lines (57 loc) • 2.3 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 Tommaso Latini <tommaso@domotz.com> on 31/03/16.
*/
var factory = function(dgram) {
return {
createSocket: function() {
return dgram.createSocket('udp4');
},
send: function (socket, buffer, host, port, burst) {
for (var i = 0; i < burst; i++) {
socket.send(buffer, 0, buffer.length, port, host, function () {
console.silly('Message %s sent to %s:%d', buffer.toString(),
host, port);
});
}
console.silly(buffer.length + ' bytes sent ' + i + ' times');
},
decode: function (filter) {
var hexString = filter.replace(/\s+/g, '');
console.silly("Filtering by " + hexString);
return Buffer(hexString, 'hex');
},
logListening: function(socket) {
var address = socket.address();
var ip = address.address, port = address.port;
console.verbose('UDP Socket bound on ' + ip + ":" + port);
},
logClose: function(type) {
console.verbose('UDP %s closed', type);
},
logMessage: function (msg, rinfo) {
console.debug('Received %d bytes from %s:%d', msg.length, rinfo.address, rinfo.port);
console.verbose('Message: %s', msg.toString());
},
logError: function (error) {
console.error('UDP error - Error: {}' + error.message);
console.error(error.stack);
}
};
};
module.exports.factory = factory;