Skip to content

A full-featured download manager.

feat: main init engine client

Dr_rOotf887878b9e7e
diff --git a/src/main/Application.js b/src/main/Application.js
index b10a2312..4f99a14e 100644
--- a/src/main/Application.js
+++ b/src/main/Application.js
@@ -9,6 +9,7 @@ import logger from './core/Logger'
import ConfigManager from './core/ConfigManager'
import { setupLocaleManager } from '@/ui/Locale'
import Engine from './core/Engine'
+import EngineClient from './core/EngineClient'
import AutoLaunchManager from './core/AutoLaunchManager'
import UpdateManager from './core/UpdateManager'
import EnergyManager from './core/EnergyManager'
@@ -48,6 +49,8 @@ export default class Application extends EventEmitter {
})
this.startEngine()
+ this.initEngineClient()
+
this.trayManager = new TrayManager()
this.dockManager = new DockManager({
@@ -88,6 +91,15 @@ export default class Application extends EventEmitter {
}
}
+ initEngineClient () {
+ const port = this.configManager.getSystemConfig('rpc-listen-port')
+ const secret = this.configManager.getSystemConfig('rpc-secret')
+ this.engineClient = new EngineClient({
+ port,
+ secret
+ })
+ }
+
initWindowManager () {
this.windowManager = new WindowManager({
userConfig: this.configManager.getUserConfig()
diff --git a/src/main/core/EngineClient.js b/src/main/core/EngineClient.js
new file mode 100644
index 00000000..af578ceb
--- /dev/null
+++ b/src/main/core/EngineClient.js
@@ -0,0 +1,64 @@
+'use strict'
+
+import Aria2 from 'aria2'
+
+import logger from './Logger'
+import {
+ compactUndefined,
+ formatOptionsForEngine
+} from '@shared/utils'
+import {
+ ENGINE_RPC_HOST,
+ ENGINE_RPC_PORT,
+ EMPTY_STRING
+} from '@shared/constants'
+
+const defaults = {
+ host: ENGINE_RPC_HOST,
+ port: ENGINE_RPC_PORT,
+ secret: EMPTY_STRING
+}
+
+export default class EngineClient {
+ static instance = null
+ static client = null
+
+ constructor (options = {}) {
+ this.options = {
+ ...defaults,
+ ...options
+ }
+
+ this.init()
+ }
+
+ init () {
+ this.connect()
+ }
+
+ connect () {
+ logger.info('[Motrix] main engine client connect', this.options)
+ const { host, port, secret } = this.options
+ this.client = new Aria2({
+ host,
+ port,
+ secret
+ })
+ }
+
+ changeGlobalOption (options) {
+ console.log('EngineClient.changeGlobalOption===>', options)
+ const args = formatOptionsForEngine(options)
+
+ return this.client.call('changeGlobalOption', args)
+ }
+
+ shutdown (options = {}) {
+ const { force = false } = options
+ const { secret } = this.options
+
+ const method = force ? 'forceShutdown' : 'shutdown'
+ const args = compactUndefined([secret])
+ return this.client.call(method, ...args)
+ }
+}