Refactored codebase to Strategy instead of plugin
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
pub mod command;
|
||||
pub mod execution;
|
||||
pub mod plugin;
|
||||
pub mod strategy;
|
||||
|
||||
use crate::{
|
||||
engine::plugin::StrategyEngine,
|
||||
engine::strategy::StrategyEngine,
|
||||
store::{accounts::AccountList, config::Config},
|
||||
terminal::TerminalServer,
|
||||
};
|
||||
|
||||
@@ -15,11 +15,11 @@ use tokio::{
|
||||
|
||||
use crate::{
|
||||
engine::Engine,
|
||||
store::{plugin::Plugin, pulse_plugin},
|
||||
store::{strategy::StrategyChild, pulse_plugin},
|
||||
};
|
||||
|
||||
pub struct StrategyEngine {
|
||||
pub strategy: Arc<Plugin<StrategyEngineMessage, StrategyMessage, StrategyManifest>>,
|
||||
pub strategy: Arc<StrategyChild>,
|
||||
pub engine: Weak<Engine>,
|
||||
|
||||
pub ws: WebSocket,
|
||||
@@ -37,7 +37,7 @@ impl StrategyEngine {
|
||||
)?;
|
||||
|
||||
Ok(Self {
|
||||
strategy: Arc::new(Plugin::new(strategy, strategy_manifest)),
|
||||
strategy: Arc::new(StrategyChild::new(strategy, strategy_manifest)),
|
||||
engine: Weak::new(),
|
||||
ws: hypercore::mainnet_ws(),
|
||||
subscriptions: Mutex::new(HashSet::new()),
|
||||
@@ -2,7 +2,7 @@ use std::path::PathBuf;
|
||||
|
||||
pub mod accounts;
|
||||
pub mod config;
|
||||
pub mod plugin;
|
||||
pub mod strategy;
|
||||
|
||||
pub fn home_dir() -> tokio::io::Result<PathBuf> {
|
||||
std::env::home_dir().ok_or_else(|| {
|
||||
|
||||
@@ -1,33 +1,29 @@
|
||||
use std::marker::PhantomData;
|
||||
|
||||
use pulse_sdk::map_postcard_err;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use pulse_sdk::{
|
||||
map_postcard_err,
|
||||
strategy::{StrategyEngineMessage, StrategyManifest, StrategyMessage},
|
||||
};
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt},
|
||||
process::{Child, ChildStdout},
|
||||
sync::Mutex,
|
||||
};
|
||||
#[derive(Debug)]
|
||||
pub struct Plugin<S: Serialize, R: for<'de> Deserialize<'de>, M: for<'de> Deserialize<'de>> {
|
||||
pub manifest: Mutex<M>,
|
||||
pub struct StrategyChild {
|
||||
pub manifest: Mutex<StrategyManifest>,
|
||||
pub stdout: Mutex<ChildStdout>,
|
||||
pub process: Mutex<Child>,
|
||||
|
||||
pub _p: (PhantomData<S>, PhantomData<R>),
|
||||
}
|
||||
|
||||
impl<S: Serialize, R: for<'de> Deserialize<'de>, M: for<'de> Deserialize<'de>> Plugin<S, R, M> {
|
||||
pub fn new(mut child: Child, manifest: M) -> Self {
|
||||
impl StrategyChild {
|
||||
pub fn new(mut child: Child, manifest: StrategyManifest) -> Self {
|
||||
Self {
|
||||
stdout: Mutex::new(child.stdout.take().expect("Failed to obtain child stdout")),
|
||||
process: Mutex::new(child),
|
||||
manifest: Mutex::new(manifest),
|
||||
|
||||
_p: (PhantomData, PhantomData),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn recv(&self) -> tokio::io::Result<Option<R>> {
|
||||
pub async fn recv(&self) -> tokio::io::Result<Option<StrategyMessage>> {
|
||||
let mut stdout = self.stdout.lock().await;
|
||||
|
||||
let mut len_buf = [0u8; size_of::<usize>()];
|
||||
@@ -46,7 +42,7 @@ impl<S: Serialize, R: for<'de> Deserialize<'de>, M: for<'de> Deserialize<'de>> P
|
||||
Ok(Some(map_postcard_err(postcard::from_bytes(&buffer))?))
|
||||
}
|
||||
|
||||
pub async fn send(&self, msg: &S) -> tokio::io::Result<()> {
|
||||
pub async fn send(&self, msg: &StrategyEngineMessage) -> tokio::io::Result<()> {
|
||||
self.send_raw(&map_postcard_err(postcard::to_allocvec(msg))?)
|
||||
.await
|
||||
}
|
||||
@@ -62,7 +58,11 @@ impl<S: Serialize, R: for<'de> Deserialize<'de>, M: for<'de> Deserialize<'de>> P
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn reload(&self, mut child: Child, manifest: M) -> tokio::io::Result<()> {
|
||||
pub async fn reload(
|
||||
&self,
|
||||
mut child: Child,
|
||||
manifest: StrategyManifest,
|
||||
) -> tokio::io::Result<()> {
|
||||
let mut process = self.process.lock().await;
|
||||
|
||||
process.kill().await?;
|
||||
Reference in New Issue
Block a user