feat: working on spawning commands
This commit is contained in:
parent
1e5f515a25
commit
47b2191335
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -1042,7 +1042,6 @@ dependencies = [
|
|||||||
"rmp-serde",
|
"rmp-serde",
|
||||||
"serde",
|
"serde",
|
||||||
"sparse-05-common",
|
"sparse-05-common",
|
||||||
"sparse-05-server",
|
|
||||||
"structopt",
|
"structopt",
|
||||||
"tokio",
|
"tokio",
|
||||||
]
|
]
|
||||||
|
|||||||
@ -14,6 +14,5 @@ raw_tty = "0.1.0"
|
|||||||
rmp-serde = "1.1.2"
|
rmp-serde = "1.1.2"
|
||||||
serde = { version = "1.0.188", features = ["derive"] }
|
serde = { version = "1.0.188", features = ["derive"] }
|
||||||
sparse-05-common = { version = "0.1.0", path = "../sparse-05-common" }
|
sparse-05-common = { version = "0.1.0", path = "../sparse-05-common" }
|
||||||
sparse-05-server = { version = "0.5.0", path = "../sparse-05-server" }
|
|
||||||
structopt = { version = "0.3.26", features = ["paw"] }
|
structopt = { version = "0.3.26", features = ["paw"] }
|
||||||
tokio = { version = "1.32.0", features = ["io-std", "net", "fs", "macros", "rt"] }
|
tokio = { version = "1.32.0", features = ["io-std", "net", "fs", "macros", "rt"] }
|
||||||
|
|||||||
@ -11,6 +11,9 @@ pub fn print_help(arg: Option<SparseCommands>) {
|
|||||||
Some(SparseCommands::SysInfo) => println!(
|
Some(SparseCommands::SysInfo) => println!(
|
||||||
"Prints system information from the system you are connecting to"
|
"Prints system information from the system you are connecting to"
|
||||||
),
|
),
|
||||||
|
Some(SparseCommands::Cd { .. }) => println!(
|
||||||
|
"Changes the current working directory you are in"
|
||||||
|
),
|
||||||
None => println!(
|
None => println!(
|
||||||
"\n{}{}\n\
|
"\n{}{}\n\
|
||||||
\n\
|
\n\
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
use std::{
|
use std::{
|
||||||
io::{self, Read, Write},
|
io::{self, Read, Write},
|
||||||
os::fd::AsRawFd,
|
os::fd::AsRawFd,
|
||||||
|
path::PathBuf,
|
||||||
sync::Arc,
|
sync::Arc,
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -16,6 +17,9 @@ pub enum SparseCommands {
|
|||||||
SysInfo,
|
SysInfo,
|
||||||
#[structopt(name = "#exit")]
|
#[structopt(name = "#exit")]
|
||||||
Exit,
|
Exit,
|
||||||
|
Cd {
|
||||||
|
folder: PathBuf,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! libc_try {
|
macro_rules! libc_try {
|
||||||
@ -45,6 +49,10 @@ fn convert_termios_raw(attrs: &mut libc::termios) -> anyhow::Result<()> {
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn run_command(connection: Arc<Connection>) -> anyhow::Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub(super) async fn shell(
|
pub(super) async fn shell(
|
||||||
connection: Arc<Connection>,
|
connection: Arc<Connection>,
|
||||||
mut capabilities: Capabilities,
|
mut capabilities: Capabilities,
|
||||||
|
|||||||
@ -12,7 +12,7 @@ pub mod messages {
|
|||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub enum Command {
|
pub enum Command {
|
||||||
RunCommand(OsString),
|
RunCommand(String),
|
||||||
SendStdin(Vec<u8>, u64),
|
SendStdin(Vec<u8>, u64),
|
||||||
|
|
||||||
Cd(PathBuf),
|
Cd(PathBuf),
|
||||||
@ -62,8 +62,9 @@ pub mod messages {
|
|||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
pub enum Response {
|
pub enum Response {
|
||||||
AckRunCommand(u64),
|
AckRunCommand(u64),
|
||||||
|
SendStderr(Vec<u8>, u64, u64),
|
||||||
SendStdout(Vec<u8>, u64, u64),
|
SendStdout(Vec<u8>, u64, u64),
|
||||||
CommandDone(u64),
|
CommandDone(u64, u32),
|
||||||
|
|
||||||
CdDone,
|
CdDone,
|
||||||
LsResults(Vec<DirEntry>),
|
LsResults(Vec<DirEntry>),
|
||||||
|
|||||||
@ -229,9 +229,10 @@ where
|
|||||||
use packets::*;
|
use packets::*;
|
||||||
|
|
||||||
let mut commands = HashMap::new();
|
let mut commands = HashMap::new();
|
||||||
let mut uploaded_files = HashMap::new();
|
/*let mut uploaded_files = HashMap::new();
|
||||||
let mut downloaded_files = HashMap::new();
|
let mut downloaded_files = HashMap::new();*/
|
||||||
|
|
||||||
|
std::thread::scope(|s| -> anyhow::Result<()> {
|
||||||
loop {
|
loop {
|
||||||
let msg = packet_handler.recv()?;
|
let msg = packet_handler.recv()?;
|
||||||
let pkt = msg.pkt();
|
let pkt = msg.pkt();
|
||||||
@ -248,7 +249,17 @@ where
|
|||||||
};
|
};
|
||||||
|
|
||||||
match data {
|
match data {
|
||||||
Command::RunCommand(_) => {}
|
Command::RunCommand(comm) => {
|
||||||
|
let handler = match command::spawn_command(&s, comm, &conninfo) {
|
||||||
|
Ok(handler) => handler,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("error spawning command: {e:?}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
commands.insert(handler.id, handler);
|
||||||
|
}
|
||||||
Command::SendStdin(_, _) => {}
|
Command::SendStdin(_, _) => {}
|
||||||
|
|
||||||
Command::Cd(_) => {}
|
Command::Cd(_) => {}
|
||||||
@ -269,6 +280,8 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
})?;
|
||||||
|
|
||||||
close();
|
close();
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,29 @@
|
|||||||
|
use std::{
|
||||||
|
sync::{
|
||||||
|
atomic::AtomicU64,
|
||||||
|
mpsc::{channel, Sender},
|
||||||
|
},
|
||||||
|
thread::Scope,
|
||||||
|
};
|
||||||
|
|
||||||
|
use super::ConnectionInformation;
|
||||||
|
|
||||||
|
const CURRENT_COMMAND_ID: AtomicU64 = AtomicU64::new(0);
|
||||||
|
|
||||||
|
pub(super) struct CommandHandler {
|
||||||
|
pub id: u64,
|
||||||
|
data_sender: Sender<Vec<u8>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(super) fn spawn_command<'a, 'b: 'a>(
|
||||||
|
s: &'a Scope<'a, 'b>,
|
||||||
|
command: String,
|
||||||
|
conninfo: &'a ConnectionInformation,
|
||||||
|
) -> anyhow::Result<CommandHandler> {
|
||||||
|
let (data_sender, data_receiver) = channel();
|
||||||
|
|
||||||
|
Ok(CommandHandler {
|
||||||
|
id: CURRENT_COMMAND_ID.fetch_add(1, std::sync::atomic::Ordering::Relaxed),
|
||||||
|
data_sender,
|
||||||
|
})
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user