Skip to content

Commit

Permalink
feat(cmd-socketio-server): support multiple BLP in single server
Browse files Browse the repository at this point in the history
- Add support for starting Cactus CMD SocketIO server with multiple Bussiness Logic Plugins (BLP).
  All required TransactionManagement request should be routed to the correct BLP.
- Add optional onListening callback when starting the cactus cmd socketio server, to simplify tests.
- Fix some bugs and hard-coded BLP Ids in TransactionManagement.
- Add new unit tests to check newly added functionalities.

Closes #2102

Depends on #2030

Signed-off-by: Michal Bajer <michal.bajer@fujitsu.com>
  • Loading branch information
outSH authored and petermetz committed Nov 29, 2022
1 parent 1b352c2 commit 0f67085
Show file tree
Hide file tree
Showing 12 changed files with 533 additions and 54 deletions.
3 changes: 2 additions & 1 deletion packages/cactus-cmd-socketio-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@
"@types/escape-html": "1.0.1",
"@types/http-errors": "1.6.3",
"@types/morgan": "1.9.1",
"@types/shelljs": "^0.8.11",
"@types/shelljs": "0.8.11",
"http-terminator": "3.2.0",
"ts-node": "8.9.1"
},
"publishConfig": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@

import { BusinessLogicPlugin } from "./BusinessLogicPlugin";

let _blp: BusinessLogicPlugin | null = null;
// Singleton of BLPs
let _blpMapping = new Map<string, BusinessLogicPlugin>();

export function getTargetBLPInstance(
businessLogicID: string,
): BusinessLogicPlugin | null {
return _blp;
return _blpMapping.get(businessLogicID) ?? null;
}

export function setTargetBLPInstance(
businessLogicID: string,
blp: BusinessLogicPlugin,
) {
_blp = blp;
}
_blpMapping.set(businessLogicID, blp);
}

export function deleteTargetBLPInstance(
businessLogicID: string,
) {
return _blpMapping.delete(businessLogicID);
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ export class BusinessLogicBase implements BusinessLogicPlugin {
return null;
}

// This function is optional in setup with single BLP,
// that's why value true is returned (to indicate that given Tx always belong to this BLP)
hasTxIDInTransactions(txID: string): boolean {
// NOTE: This method implements the BisinessLogcPlugin operation(* Override by subclass)
return false;
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,17 @@ export type BLPConfig = {
plugin: BusinessLogicPlugin;
};

export function startCactusSocketIOServer(blp?: BLPConfig) {
export function startCactusSocketIOServer(blp?: BLPConfig | BLPConfig[], onListening?: () => void) {
if (blp) {
logger.info("Using BLP with id =", blp.id);
setTargetBLPInstance(blp.id, blp.plugin);
// Support both single and multiple BLPs
if(!Array.isArray(blp)) {
blp = [blp];
}

blp.forEach((cfg) => {
logger.info("Using BLP with id =", cfg.id);
setTargetBLPInstance(cfg.id, cfg.plugin);
});
}

runExpressApp();
Expand Down Expand Up @@ -85,15 +92,16 @@ export function startCactusSocketIOServer(blp?: BLPConfig) {
/**
* Event listener for HTTP server "listening" event.
*/

function onListening(): void {
const addr = server.address();
if (addr) {
const bind =
typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debugModule("Listening on " + bind);
} else {
throw new Error("Could not get server address!");
if (!onListening) {
onListening = () => {
const addr = server.address();
if (addr) {
const bind =
typeof addr === "string" ? "pipe " + addr : "port " + addr.port;
debugModule("Listening on " + bind);
} else {
throw new Error("Could not get server address!");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@
import { Request } from "express";
import { BusinessLogicPlugin } from "../business-logic-plugin/BusinessLogicPlugin";
import { BLPRegistry } from "./util/BLPRegistry";
import { LPInfoHolder } from "./util/LPInfoHolder";
import { json2str } from "../verifier/DriverCommon";
import { Verifier } from "../verifier/Verifier";
import { IVerifierEventListener, LedgerEvent } from "../verifier/LedgerPlugin";
import { getTargetBLPInstance } from "../business-logic-plugin/BLP_config";
import { ConfigUtil } from "./util/ConfigUtil";

const fs = require("fs");
const path = require("path");
const config: any = ConfigUtil.getConfig();
import { getLogger } from "log4js";
const moduleName = "TransactionManagement";
Expand Down Expand Up @@ -117,22 +112,20 @@ export class TransactionManagement implements IVerifierEventListener {

// object judgment
let result: {} = {};
if (businessLogicID === "h40Q9eMD") {
const blp = getTargetBLPInstance(businessLogicID);
if (blp === null) {
logger.warn(
`##startBusinessLogic(): not found BusinessLogicPlugin. businessLogicID: ${businessLogicID}`,
);
return undefined;
}
const blp = getTargetBLPInstance(businessLogicID);
if (blp === null) {
logger.warn(
`##startBusinessLogic(): not found BusinessLogicPlugin. businessLogicID: ${businessLogicID}`,
);
return undefined;
}

logger.debug("created instance");
logger.debug("created instance");

// Set business logic config
logger.debug(`meterParams = ${req.body.meterParams}`);
result = blp.setConfig(req.body.meterParams);
logger.debug("set business logic config");
}
// Set business logic config
logger.debug(`meterParams = ${req.body.meterParams}`);
result = blp.setConfig(req.body.meterParams);
logger.debug("set business logic config");

return result;
}
Expand Down Expand Up @@ -271,12 +264,12 @@ export class TransactionManagement implements IVerifierEventListener {
continue;
}

// if (blp.hasTxIDInTransactions(txID)) {
logger.debug(
`####getBLPInstanceFromTxID(): found!, businessLogicID: ${businessLogicID}`,
);
return blp;
// }
if (blp.hasTxIDInTransactions(txID)) {
logger.debug(
`####getBLPInstanceFromTxID(): found!, businessLogicID: ${businessLogicID}`,
);
return blp;
}
}

// not found.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import {
LogLevelDesc,
} from "@hyperledger/cactus-common";

const logLevel: LogLevelDesc = "error";
const logLevel: LogLevelDesc = "info";
const log: Logger = LoggerProvider.getOrCreate({
label: "test-cmd-socketio-verifier",
level: logLevel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import {
LogLevelDesc,
} from "@hyperledger/cactus-common";

const logLevel: LogLevelDesc = "error";
const logLevel: LogLevelDesc = "info";
const log: Logger = LoggerProvider.getOrCreate({
label: "test-cmd-socketio-verifier",
level: logLevel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
LogLevelDesc,
} from "@hyperledger/cactus-common";

const logLevel: LogLevelDesc = "error";
const logLevel: LogLevelDesc = "info";
const log: Logger = LoggerProvider.getOrCreate({
label: "test-cmd-socketio-verifier",
level: logLevel,
Expand Down
Loading

0 comments on commit 0f67085

Please sign in to comment.