jspurefix
Version:
pure node js fix engine
168 lines • 6.71 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SessionLauncher = void 0;
const path = require("path");
const config_1 = require("../config");
const session_container_1 = require("./session-container");
const di_tokens_1 = require("./di-tokens");
const defaultLoggerFactory = new config_1.JsFixWinstonLogFactory(config_1.WinstonLogger.consoleOptions('info'));
class SessionLauncher {
constructor(initiatorConfig = null, acceptorConfig = null, loggerFactory = defaultLoggerFactory) {
this.loggerFactory = loggerFactory;
this.root = '../../';
this.sessionContainer = new session_container_1.SessionContainer();
this.logger = this.loggerFactory.logger('launcher');
this.initiatorConfig = initiatorConfig ? this.loadConfig(initiatorConfig) : null;
this.acceptorConfig = acceptorConfig ? this.loadConfig(acceptorConfig) : null;
}
empty() {
return __awaiter(this, void 0, void 0, function* () {
return yield new Promise((resolve, reject) => {
try {
setImmediate(() => {
this.logger.info('resolving an empty promise');
resolve(null);
});
}
catch (e) {
reject(e);
}
});
});
}
getAcceptor(sessionContainer) {
return __awaiter(this, void 0, void 0, function* () {
if (sessionContainer.isRegistered(di_tokens_1.DITokens.FixEntity)) {
const entity = sessionContainer.resolve(di_tokens_1.DITokens.FixEntity);
return entity.start();
}
else {
return this.empty();
}
});
}
getInitiator(sessionContainer) {
return __awaiter(this, void 0, void 0, function* () {
if (sessionContainer.isRegistered(di_tokens_1.DITokens.FixEntity)) {
const entity = sessionContainer.resolve(di_tokens_1.DITokens.FixEntity);
return entity.start();
}
else {
return this.empty();
}
});
}
makeFactory(config) {
return null;
}
run() {
return __awaiter(this, void 0, void 0, function* () {
return yield new Promise((resolve, reject) => {
const logger = this.logger;
logger.info('launching ..');
this.setup().then(() => {
logger.info('.. done');
resolve(true);
}).catch((e) => {
logger.error(e);
reject(e);
});
});
});
}
exec() {
this.run().then(() => {
console.log('finished.');
}).catch(e => {
console.error(e);
});
}
isAscii(description) {
return this.sessionContainer.isAscii(description);
}
isInitiator(description) {
return this.sessionContainer.isInitiator(description);
}
registerApplication(_) {
this.logger.info('bypass register via DI');
}
makeSystem(description) {
var _a, _b, _c, _d;
return __awaiter(this, void 0, void 0, function* () {
const name = (_b = (_a = description.application) === null || _a === void 0 ? void 0 : _a.name) !== null && _b !== void 0 ? _b : 'na';
const protocol = (_d = (_c = description.application) === null || _c === void 0 ? void 0 : _c.protocol) !== null && _d !== void 0 ? _d : 'ascii';
this.logger.info(`creating app ${name} [protocol ${protocol}]`);
return yield this.sessionContainer.makeSystem(description);
});
}
register(container) {
const config = container.resolve(di_tokens_1.DITokens.IJsFixConfig);
const factory = this.makeFactory(config);
if (!factory) {
this.registerApplication(container);
}
else {
if (factory.makeSession) {
container.register(di_tokens_1.DITokens.FixSession, {
useFactory: () => factory.makeSession(config)
});
}
}
}
makeClient() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.initiatorConfig)
return;
const sessionContainer = yield this.makeSystem(this.initiatorConfig);
this.register(sessionContainer);
this.logger.info('create initiator');
return yield this.getInitiator(sessionContainer);
});
}
makeServer() {
return __awaiter(this, void 0, void 0, function* () {
if (!this.acceptorConfig)
return;
const sessionContainer = yield this.makeSystem(this.acceptorConfig);
this.register(sessionContainer);
this.logger.info('create acceptor');
return yield this.getAcceptor(sessionContainer);
});
}
serverOrEmpty() {
return __awaiter(this, void 0, void 0, function* () {
return this.acceptorConfig ? this.makeServer() : this.empty();
});
}
clientOrEmpty() {
return __awaiter(this, void 0, void 0, function* () {
return this.initiatorConfig ? this.makeClient() : this.empty();
});
}
setup() {
return __awaiter(this, void 0, void 0, function* () {
this.sessionContainer.registerGlobal(this.loggerFactory);
const server = this.serverOrEmpty();
const client = this.clientOrEmpty();
this.logger.info('launching ....');
return yield Promise.all([server, client]);
});
}
loadConfig(config) {
if (typeof config === 'string') {
return require(path.join(this.root, config));
}
return config;
}
}
exports.SessionLauncher = SessionLauncher;
//# sourceMappingURL=session-launcher.js.map