Implement windowsleep
This commit is contained in:
83
client.js
83
client.js
@@ -8,7 +8,6 @@ const NanoTimer = require("nanotimer");
|
|||||||
const myFormat = printf(({ level, message, timestamp }) => {
|
const myFormat = printf(({ level, message, timestamp }) => {
|
||||||
return `${timestamp} ${level}: ${message}`;
|
return `${timestamp} ${level}: ${message}`;
|
||||||
});
|
});
|
||||||
|
|
||||||
const logger = createLogger({
|
const logger = createLogger({
|
||||||
format: combine(format.colorize(), timestamp(), myFormat),
|
format: combine(format.colorize(), timestamp(), myFormat),
|
||||||
transports: [new transports.Console()],
|
transports: [new transports.Console()],
|
||||||
@@ -34,14 +33,28 @@ const optionDefinitions = [
|
|||||||
{ name: "help", type: Boolean, description: "Display this usage guide." },
|
{ name: "help", type: Boolean, description: "Display this usage guide." },
|
||||||
{ name: "host", alias: "h", type: String, description: "The host (IP) to connect to." },
|
{ name: "host", alias: "h", type: String, description: "The host (IP) to connect to." },
|
||||||
{ name: "port", alias: "p", type: Number, description: "The port to connect to." },
|
{ name: "port", alias: "p", type: Number, description: "The port to connect to." },
|
||||||
{ name: "systemId", alias: "s", type: String, description: "SMPP related login info." },
|
{ name: "systemid", alias: "s", type: String, description: "SMPP related login info." },
|
||||||
{ name: "password", alias: "w", type: String, description: "SMPP related login info." },
|
{ name: "password", alias: "w", type: String, description: "SMPP related login info." },
|
||||||
{
|
{
|
||||||
name: "messageCount",
|
name: "messagecount",
|
||||||
type: Number,
|
type: Number,
|
||||||
description: "Number of messages to send; Optional, defaults to 1.",
|
description: "Number of messages to send; Optional, defaults to 1.",
|
||||||
defaultOption: 1,
|
defaultOption: 1,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "window",
|
||||||
|
type: Number,
|
||||||
|
description:
|
||||||
|
"Defines the amount of messages that are allowed to be 'in flight'. The client no longer waits for a response before sending the next message for up to <window> messages. Defaults to 100.",
|
||||||
|
defaultOption: 100,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "windowsleep",
|
||||||
|
type: Number,
|
||||||
|
description:
|
||||||
|
"Defines the amount time (in ms) waited between retrying in the case of full window. Defaults to 100.",
|
||||||
|
defaultOption: 100,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "mps",
|
name: "mps",
|
||||||
type: Number,
|
type: Number,
|
||||||
@@ -91,41 +104,23 @@ if (options.help) {
|
|||||||
verifyDefaults(options);
|
verifyDefaults(options);
|
||||||
verifyExists(options.host, "Host can not be undefined or empty! (--host)");
|
verifyExists(options.host, "Host can not be undefined or empty! (--host)");
|
||||||
verifyExists(options.port, "Port can not be undefined or empty! (--port)");
|
verifyExists(options.port, "Port can not be undefined or empty! (--port)");
|
||||||
verifyExists(options.systemId, "SystemID can not be undefined or empty! (--systemId)");
|
verifyExists(options.systemid, "SystemID can not be undefined or empty! (--systemid)");
|
||||||
verifyExists(options.password, "Password can not be undefined or empty! (--password)");
|
verifyExists(options.password, "Password can not be undefined or empty! (--password)");
|
||||||
|
|
||||||
|
let inFlight = 0;
|
||||||
let sent = 0;
|
let sent = 0;
|
||||||
let success = 0;
|
let success = 0;
|
||||||
let failed = 0;
|
let failed = 0;
|
||||||
const sendTimer = new NanoTimer();
|
const sendTimer = new NanoTimer();
|
||||||
logger.info(`Connecting to ${options.host}:${options.port}...`);
|
|
||||||
const session = smpp.connect(
|
function startInterval(session) {
|
||||||
{
|
|
||||||
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}')`
|
|
||||||
);
|
|
||||||
sendTimer.setInterval(
|
sendTimer.setInterval(
|
||||||
() => {
|
async () => {
|
||||||
if (sent >= options.messageCount) {
|
if (sent >= options.messagecount) {
|
||||||
logger.info("Finished sending messages, idling...");
|
logger.info("Finished sending messages, idling...");
|
||||||
sendTimer.clearInterval();
|
sendTimer.clearInterval();
|
||||||
} else {
|
} else if (inFlight < options.window) {
|
||||||
logger.info(`Sending message ${sent + 1}/${options.messageCount}`);
|
logger.info(`Sending message ${sent + 1}/${options.messagecount}`);
|
||||||
session.submit_sm(
|
session.submit_sm(
|
||||||
{
|
{
|
||||||
source_addr: options.source,
|
source_addr: options.source,
|
||||||
@@ -133,6 +128,7 @@ const session = smpp.connect(
|
|||||||
short_message: options.message,
|
short_message: options.message,
|
||||||
},
|
},
|
||||||
function (pdu) {
|
function (pdu) {
|
||||||
|
inFlight--;
|
||||||
if (pdu.command_status === 0) {
|
if (pdu.command_status === 0) {
|
||||||
logger.info(`Received response with id ${pdu.message_id}`);
|
logger.info(`Received response with id ${pdu.message_id}`);
|
||||||
success++;
|
success++;
|
||||||
@@ -143,11 +139,40 @@ const session = smpp.connect(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
sent++;
|
sent++;
|
||||||
|
inFlight++;
|
||||||
|
} else {
|
||||||
|
logger.warn(`${inFlight}/${options.window} messages pending, waiting for a reply before sending more`);
|
||||||
|
sendTimer.clearInterval();
|
||||||
|
setTimeout(() => startInterval(session), options.windowsleep);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"",
|
"",
|
||||||
`${1 / options.mps} s`
|
`${1 / options.mps} s`
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
logger.info(`Connecting to ${options.host}:${options.port}...`);
|
||||||
|
const session = smpp.connect(
|
||||||
|
{
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
Reference in New Issue
Block a user