diff --git a/center.js b/center.js index 9fee37f..62c9925 100644 --- a/center.js +++ b/center.js @@ -8,8 +8,8 @@ const { centerOptions } = require("./cliOptions"); const crypto = require("crypto"); const { MetricManager } = require("./metrics/metricManager"); -const logger = createBaseLogger(); const options = commandLineArgs(centerOptions); +const logger = createBaseLogger(options); if (options.help) { const usage = commandLineUsage([ @@ -52,10 +52,10 @@ function startInterval(session, sessionLogger, rxMetrics) { sendTimer.setInterval( async () => { if (sent >= options.messagecount) { - // sessionLogger.info(`Finished sending messages success:${success}, failed:${failed}, idling...`); + sessionLogger.info(`Finished sending messages success:${success}, failed:${failed}, idling...`); sendTimer.clearInterval(); } else if (inFlight < options.window) { - // sessionLogger.info(`Sending message ${sent + 1}/${options.messagecount}`); + sessionLogger.info(`Sending message ${sent + 1}/${options.messagecount}`); session.deliver_sm( { source_addr: options.source, @@ -65,7 +65,7 @@ function startInterval(session, sessionLogger, rxMetrics) { function (pdu) { inFlight--; if (pdu.command_status === 0) { - // sessionLogger.info(`Received response with id ${pdu.message_id}`); + sessionLogger.info(`Received response with id ${pdu.message_id}`); success++; } else { sessionLogger.warn(`Message failed with id ${pdu.message_id}`); @@ -98,7 +98,7 @@ const server = smpp.createServer( }, function (session) { const id = sessionid++; - const sessionLogger = createSessionLogger(id); + const sessionLogger = createSessionLogger(options, id); const rxMetrics = metricManager.AddMetrics(`Session-${id}-RX`); const txMetrics = metricManager.AddMetrics(`Session-${id}-TX`); @@ -109,7 +109,7 @@ const server = smpp.createServer( startInterval(session, sessionLogger); } else { sessionLogger.warn( - `Client tried to connect with incorrect login ('${pdu.system_id}' '${pdu.password}'` + `Client tried to connect with incorrect login ('${pdu.system_id}' '${pdu.password}')` ); pdu.response({ command_status: smpp.ESME_RBINDFAIL, @@ -124,6 +124,9 @@ const server = smpp.createServer( if (!options.dr) { // sessionLogger.info("Replying to incoming submit_sm"); rxMetrics.AddEvent(); + // setTimeout(() => { + // session.send(pdu.response()); + // }, 200); session.send(pdu.response()); return; } @@ -133,7 +136,7 @@ const server = smpp.createServer( let smppid = messageid++; if (options.randid) { - smppid = crypto.randomBytes(12).toString("hex"); + smppid = crypto.randomBytes(8).toString("hex"); } response.message_id = smppid.toString(16); @@ -187,41 +190,3 @@ server.on("error", function (err) { server.listen(options.port); logger.info(`SMPP Server listening on ${options.port}`); -// { -// url: `smpp://${options.host}:${options.port}`, -// auto_enquire_link_period: 10000, -// debug: options.debug, -// }, -// function () { -// logger.info( -// `Connected, sending bind_transciever with systemId '${options.systemid}' and password '${options.password}'...` -// ); -// session.bind_transceiver( -// { -// system_id: options.systemid, -// password: options.password, -// }, -// function (pdu) { -// if (pdu.command_status === 0) { -// logger.info( -// `Successfully bound, sending ${options.messagecount} messages '${options.source}'->'${options.destination}' ('${options.message}')` -// ); -// startInterval(session); - -// session.on("deliver_sm", function (pdu) { -// logger.info("Got deliver_sm, replying..."); -// session.send(pdu.response()); -// }); -// session.on("close", function () { -// logger.error(`Session closed`); -// process.exit(1); -// }); -// session.on("error", function (err) { -// logger.error(`Fatal error ${err}`); -// process.exit(1); -// }); -// } -// } -// ); -// } -// ); diff --git a/cliOptions.js b/cliOptions.js index 79c2c96..ef54500 100644 --- a/cliOptions.js +++ b/cliOptions.js @@ -50,6 +50,12 @@ const clientOptions = [ defaultOption: "smpp debug message", }, { name: "debug", type: Boolean, description: "Display all traffic to and from the client; Debug mode." }, + { name: "logs", type: Boolean, description: "Write logs (to stdout), defaults to true." }, + { + name: "bars", + type: Boolean, + description: "Display TX and RX bars. Can be used with logs (although it will make a mess)." + }, ]; const centerOptions = [ @@ -115,6 +121,12 @@ const centerOptions = [ defaultOption: "smpp debug message", }, { name: "debug", type: Boolean, description: "Display all traffic to and from the center; Debug mode." }, + { name: "logs", type: Boolean, description: "Write logs (to stdout), defaults to true." }, + { + name: "bars", + type: Boolean, + description: "Display TX and RX bars. Can be used with logs (although it will make a mess)." + }, ]; module.exports = { clientOptions, centerOptions }; diff --git a/client.js b/client.js index 1cac220..7722a15 100644 --- a/client.js +++ b/client.js @@ -128,7 +128,7 @@ for (let i = 0; i < options.sessions; i++) { setTimeout(() => { session.send(pdu.response()); txMetrics.AddEvent(); - }, 2000); + }, 200); // session.send(pdu.response()); // txMetrics.AddEvent(); }); diff --git a/logger.js b/logger.js index ccb3dba..d909f6e 100644 --- a/logger.js +++ b/logger.js @@ -7,17 +7,69 @@ const defaultFormat = printf(({ level, message, timestamp }) => { const sessionFormat = printf(({ level, message, label, timestamp }) => { return `${timestamp} [Session ${label}] ${level}: ${message}`; }); -function createBaseLogger() { - return createLogger({ +function createBaseLogger(options) { + const logger = createLogger({ format: combine(format.colorize({ all: true }), timestamp(), defaultFormat), transports: [new transports.Console()], }); + + const oldInfo = logger.info; + const oldWarn = logger.info; + const oldError = logger.error; + logger.info = function (input) { + if (!shouldLog(options)) { + return; + } + oldInfo(input); + }; + logger.error = function (input) { + if (!shouldLog(options)) { + return; + } + oldError(input); + }; + logger.warn = function (input) { + if (!shouldLog(options)) { + return; + } + oldWarn(input); + }; + + return logger; } -function createSessionLogger(ilabel) { - return createLogger({ +function createSessionLogger(options, ilabel) { + const logger = createLogger({ format: combine(label({ label: ilabel }), format.colorize({ all: true }), timestamp(), sessionFormat), transports: [new transports.Console()], }); + + const oldInfo = logger.info; + const oldWarn = logger.info; + const oldError = logger.error; + logger.info = function (input) { + if (!shouldLog(options)) { + return; + } + oldInfo(input); + }; + logger.error = function (input) { + if (!shouldLog(options)) { + return; + } + oldError(input); + }; + logger.warn = function (input) { + if (!shouldLog(options)) { + return; + } + oldWarn(input); + }; + + return logger; +} + +function shouldLog(options) { + return options.logs || !options.bars; } module.exports = { createBaseLogger, createSessionLogger }; diff --git a/metrics/metrics.js b/metrics/metrics.js index 5b99b31..0de11ad 100644 --- a/metrics/metrics.js +++ b/metrics/metrics.js @@ -5,6 +5,7 @@ class Metric { this.multibar = multibar; this.bar = multibar.create(0, 0); this.bar.update(0, { name: barName }); + this.bar.total = 1000; this.buffer = new CircularBuffer(bufferSize); this.maxRate = 0; if (refresh) { @@ -29,10 +30,10 @@ class Metric { UpdateBar() { const eps = this.GetRate(); - if (eps > this.maxRate) { - this.bar.total = eps; - this.maxRate = eps; - } + // if (eps > this.maxRate) { + // this.bar.total = eps; + // this.maxRate = eps; + // } this.bar.update(eps); } }