UNPKG

@sotatech/nest-quickfix

Version:

A powerful NestJS implementation of the FIX (Financial Information eXchange) protocol. Provides high-performance, reliable messaging for financial trading applications with built-in session management, message validation, and recovery mechanisms.

101 lines 4.66 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; var SessionManager_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.SessionManager = void 0; const common_1 = require("@nestjs/common"); const room_manager_1 = require("../services/room.manager"); const session_1 = require("./session"); const fields_1 = require("../fields"); const tokens_constant_1 = require("../constants/tokens.constant"); let SessionManager = SessionManager_1 = class SessionManager { constructor(maxSessions = 0, roomManager) { this.maxSessions = maxSessions; this.roomManager = roomManager; this.logger = new common_1.Logger(SessionManager_1.name); this.sessions = new Map(); this.DUPLICATE_SESSION_LOGOUT_REASON = 'Duplicate session detected - logging out existing session'; } createSession(config, socket) { const sessionId = `${config.senderCompId}->${config.targetCompId}`; if (this.maxSessions && this.sessions.size >= this.maxSessions) { throw new Error('Maximum number of sessions reached'); } const session = new session_1.Session(config, socket, this.roomManager, this); this.sessions.set(sessionId, session); return session; } getSession(senderCompId, targetCompId) { const sessionId = `${senderCompId}->${targetCompId}`; return this.sessions.get(sessionId); } getSessionFromLogon(logon) { const senderCompId = logon.getField(fields_1.Fields.SenderCompID); const targetCompId = logon.getField(fields_1.Fields.TargetCompID); return this.getSession(senderCompId, targetCompId); } removeSession(sessionId) { const session = this.sessions.get(sessionId); if (session) { this.roomManager.leaveAllRooms(sessionId); this.sessions.delete(sessionId); this.logger.debug(`Session ${sessionId} removed`); } } async closeAll() { const closePromises = Array.from(this.sessions.values()).map((session) => session.logout()); await Promise.all(closePromises); this.sessions.clear(); this.logger.debug('All sessions closed'); } getSessionCount() { return this.sessions.size; } getAllSessions() { return Array.from(this.sessions.values()); } getSessionById(sessionId) { const session = this.sessions.get(sessionId); if (!session) { this.logger.warn(`Session not found: ${sessionId}`); } return session; } async registerSession(session) { const sessionId = session.getSessionId(); if (this.sessions.has(sessionId)) { this.logger.warn(`Session ${sessionId} already exists, logging out old session...`); const oldSession = this.sessions.get(sessionId); try { await oldSession.logout(this.DUPLICATE_SESSION_LOGOUT_REASON); } catch (error) { this.logger.error(`Error logging out old session ${sessionId}:`, error); } } if (this.maxSessions && this.sessions.size >= this.maxSessions) { throw new Error(`Maximum number of sessions (${this.maxSessions}) reached`); } this.sessions.set(sessionId, session); this.logger.debug(`Session ${sessionId} registered successfully`); } }; exports.SessionManager = SessionManager; exports.SessionManager = SessionManager = SessionManager_1 = __decorate([ (0, common_1.Injectable)(), __param(0, (0, common_1.Optional)()), __param(0, (0, common_1.Inject)(tokens_constant_1.MAX_SESSIONS)), __metadata("design:paramtypes", [Number, room_manager_1.RoomManager]) ], SessionManager); //# sourceMappingURL=session.manager.js.map