1
1
mirror of https://github.com/leon-ai/leon.git synced 2024-12-25 01:31:47 +03:00

refactor(server): TCP client to singleton

This commit is contained in:
louistiti 2022-11-12 22:31:49 +08:00
parent c53feba8f5
commit ea76739d88
No known key found for this signature in database
GPG Key ID: 7ECA3DD523793FE6
7 changed files with 39 additions and 31 deletions

View File

@ -29,6 +29,7 @@ import infoPlugin from '@/core/http-server/api/info'
import downloadsPlugin from '@/core/http-server/api/downloads'
import { LogHelper } from '@/helpers/log-helper'
import { DateHelper } from '@/helpers/date-helper'
import { TCP_CLIENT } from '@/core/tcp-client'
const server = {}
@ -216,10 +217,10 @@ server.handleOnConnection = (socket) => {
const provider = await addProvider(socket.id)
// Check whether the TCP client is connected to the TCP server
if (global.tcpClient.isConnected) {
if (TCP_CLIENT.isConnected) {
socket.emit('ready')
} else {
global.tcpClient.ee.on('connected', () => {
TCP_CLIENT.ee.on('connected', () => {
socket.emit('ready')
})
}

View File

@ -7,6 +7,7 @@ import fs from 'node:fs'
import { LogHelper } from '@/helpers/log-helper'
import { StringHelper } from '@/helpers/string-helper'
import { TCP_CLIENT } from '@/core/tcp-client'
class Ner {
constructor(ner) {
@ -99,13 +100,10 @@ class Ner {
resolve(spacyEntities)
}
global.tcpClient.ee.removeAllListeners()
global.tcpClient.ee.on(
'spacy-entities-received',
spacyEntitiesReceivedHandler
)
TCP_CLIENT.ee.removeAllListeners()
TCP_CLIENT.ee.on('spacy-entities-received', spacyEntitiesReceivedHandler)
global.tcpClient.emit('get-spacy-entities', utterance)
TCP_CLIENT.emit('get-spacy-entities', utterance)
})
}

View File

@ -11,18 +11,12 @@ import kill from 'tree-kill'
import { langs } from '@@/core/langs.json'
import { version } from '@@/package.json'
import {
HAS_LOGGER,
IS_TESTING_ENV,
TCP_SERVER_BIN_PATH,
TCP_SERVER_HOST,
TCP_SERVER_PORT
} from '@/constants'
import { HAS_LOGGER, IS_TESTING_ENV, TCP_SERVER_BIN_PATH } from '@/constants'
import Ner from '@/core/ner'
import { LogHelper } from '@/helpers/log-helper'
import { StringHelper } from '@/helpers/string-helper'
import { LangHelper } from '@/helpers/lang-helper'
import TCPClient from '@/core/tcp-client'
import { TCP_CLIENT } from '@/core/tcp-client'
import Conversation from '@/core/conversation'
const defaultNluResultObj = {
@ -227,10 +221,9 @@ class Nlu {
shell: true
})
global.tcpClient = new TCPClient(TCP_SERVER_HOST, TCP_SERVER_PORT)
global.tcpClient.ee.removeListener('connected', connectedHandler)
global.tcpClient.ee.on('connected', connectedHandler)
TCP_CLIENT.init()
TCP_CLIENT.ee.removeListener('connected', connectedHandler)
TCP_CLIENT.ee.on('connected', connectedHandler)
})
return {}

View File

@ -1,7 +1,11 @@
import Net from 'node:net'
import { EventEmitter } from 'node:events'
import { IS_PRODUCTION_ENV } from '@/constants'
import {
IS_PRODUCTION_ENV,
TCP_SERVER_HOST,
TCP_SERVER_PORT
} from '@/constants'
import { LogHelper } from '@/helpers/log-helper'
import { OSHelper, OSTypes } from '@/helpers/os-helper'
@ -10,22 +14,30 @@ const INTERVAL = IS_PRODUCTION_ENV ? 3000 : 500
// Number of retries to connect to the TCP server
const RETRIES_NB = IS_PRODUCTION_ENV ? 8 : 30
export default class TCPClient {
class TCPClient {
private static instance: TCPClient
private readonly host: string
private readonly port: number
private reconnectCounter = 0
private tcpSocket = new Net.Socket()
public ee = new EventEmitter()
public status = this.tcpSocket.readyState
public isConnected = false
constructor(host: string, port: number) {
if (!TCPClient.instance) {
LogHelper.title('TCP Client')
LogHelper.success('New instance')
TCPClient.instance = this
}
this.host = host
this.port = port
LogHelper.title('TCP Client')
LogHelper.success('New instance')
this.tcpSocket.on('connect', () => {
LogHelper.title('TCP Client')
LogHelper.success(
@ -81,7 +93,9 @@ export default class TCPClient {
LogHelper.title('TCP Client')
LogHelper.success('Disconnected from the TCP server')
})
}
public init(): void {
setTimeout(() => {
this.connect()
}, INTERVAL)
@ -103,3 +117,8 @@ export default class TCPClient {
})
}
}
export const TCP_CLIENT = new TCPClient(
String(TCP_SERVER_HOST),
TCP_SERVER_PORT
)

View File

@ -18,7 +18,6 @@ export class LoaderHelper {
private static spinner = new Spinner('\x1b[95m%s\x1b[0m\r').setSpinnerString(
18
)
private static interval: NodeJS.Timer | undefined
/**

View File

@ -1,14 +1,12 @@
import { spawn } from 'node:child_process'
import {
TCP_SERVER_HOST,
TCP_SERVER_PORT,
IS_DEVELOPMENT_ENV,
LANG as LEON_LANG,
TCP_SERVER_BIN_PATH
} from '@/constants'
import { LangHelper } from '@/helpers/lang-helper'
import TCPClient from '@/core/tcp-client'
import { TCP_CLIENT } from '@/core/tcp-client'
import server from '@/core/http-server/server'
;(async (): Promise<void> => {
process.title = 'leon'
@ -23,7 +21,7 @@ import server from '@/core/http-server/server'
)
// Start the TCP client
global.tcpClient = new TCPClient(String(TCP_SERVER_HOST), TCP_SERVER_PORT)
TCP_CLIENT.init()
// Start the core server
await server.init()

View File

@ -16,7 +16,7 @@
"Au revoir ! Prenez soin de vous.",
"Au revoir, merci de prendre soin de vous-même.",
"Bye ! Profitez de votre temps à bon escient !",
"A la prochaine !"
"À la prochaine !"
]
}
}