From 0a9fe206d59876e40f36159642af08d95a355154 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Majdand=C5=BEi=C4=87?= Date: Thu, 9 Nov 2023 10:13:22 +0100 Subject: [PATCH] Implement functionality --- client.js | 67 +++++++++++++++++++++++++++++++++++++++----------- package.json | 1 + pnpm-lock.yaml | 7 ++++++ 3 files changed, 60 insertions(+), 15 deletions(-) diff --git a/client.js b/client.js index 409de63..e170e5b 100644 --- a/client.js +++ b/client.js @@ -3,6 +3,7 @@ const commandLineArgs = require("command-line-args"); const commandLineUsage = require("command-line-usage"); const { createLogger, format, transports } = require("winston"); const { combine, timestamp, label, printf } = format; +const NanoTimer = require("nanotimer"); const myFormat = printf(({ level, message, timestamp }) => { return `${timestamp} ${level}: ${message}`; @@ -19,6 +20,15 @@ function verifyExists(value, err) { process.exit(0); } } +function verifyDefaults(options) { + for (const optionDefinition of optionDefinitions) { + if (optionDefinition.defaultOption) { + if (!options[optionDefinition.name]) { + options[optionDefinition.name] = optionDefinition.defaultOption; + } + } + } +} const optionDefinitions = [ { name: "help", type: Boolean, description: "Display this usage guide." }, @@ -32,6 +42,12 @@ const optionDefinitions = [ description: "Number of messages to send; Optional, defaults to 1.", defaultOption: 1, }, + { + name: "mps", + type: Number, + description: "Number of messages to send per second", + defaultOption: 999999, + }, { name: "source", type: String, @@ -72,12 +88,16 @@ if (options.help) { process.exit(0); } +verifyDefaults(options); verifyExists(options.host, "Host can not be undefined or empty! (--host)"); verifyExists(options.port, "Port can not be undefined or empty! (--port)"); verifyExists(options.systemId, "SystemID can not be undefined or empty! (--systemId)"); verifyExists(options.password, "Password can not be undefined or empty! (--password)"); -let message_id = 0; +let sent = 0; +let success = 0; +let failed = 0; +const sendTimer = new NanoTimer(); logger.info(`Connecting to ${options.host}:${options.port}...`); const session = smpp.connect( { @@ -99,20 +119,35 @@ const session = smpp.connect( logger.info( `Successfully bound, sending ${options.messageCount} messages '${options.source}'->'${options.destination}' ('${options.message}')` ); - // session.submit_sm( - // { - // source_addr: "smpp_test_1", - // destination_addr: "123123123123", - // short_message: "Hello!", - // data_coding: 0xc0, - // }, - // function (pdu) { - // if (pdu.command_status === 0) { - // console.log(pdu.message_id); - // message_id = pdu.message_id; - // } - // } - // ); + sendTimer.setInterval( + () => { + if (sent >= options.messageCount) { + logger.info("Finished sending messages, idling..."); + sendTimer.clearInterval(); + } else { + logger.info(`Sending message ${sent + 1}/${options.messageCount}`); + session.submit_sm( + { + source_addr: options.source, + destination_addr: options.destination, + short_message: options.message, + }, + function (pdu) { + if (pdu.command_status === 0) { + logger.info(`Received response with id ${pdu.message_id}`); + success++; + } else { + logger.warn(`Message failed with id ${pdu.message_id}`); + failed++; + } + } + ); + sent++; + } + }, + "", + `1000/${options.mps} ms` + ); } } ); @@ -123,3 +158,5 @@ session.on("deliver_sm", function (pdu) { console.log("Got deliver_sm"); session.send(pdu.response()); }); + +// Proccess sigint messages diff --git a/package.json b/package.json index a662f2d..db90c36 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ "dependencies": { "command-line-args": "^5.2.1", "command-line-usage": "^7.0.1", + "nanotimer": "^0.3.15", "smpp": "0.6.0-rc.4", "winston": "^3.11.0" } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 17ac75e..1f9580a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -11,6 +11,9 @@ dependencies: command-line-usage: specifier: ^7.0.1 version: 7.0.1 + nanotimer: + specifier: ^0.3.15 + version: 0.3.15 smpp: specifier: 0.6.0-rc.4 version: 0.6.0-rc.4 @@ -226,6 +229,10 @@ packages: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} dev: false + /nanotimer@0.3.15: + resolution: {integrity: sha512-xj8HcwceqeRbfSuwNIzYhdbyZu3zoiHX3y2cyVB/cLn0RzVCI8ZZVQLZELEUMG2tYEsjqbCLb3b4q1lDC7ENnA==} + dev: false + /one-time@1.0.0: resolution: {integrity: sha512-5DXOiRKwuSEcQ/l0kGCF6Q3jcADFv5tSmRaJck/OqkVFcOzutB134KRSfF0xDrL39MNnqxbHBbUUcjZIhTgb2g==} dependencies: