I think I might be sending the first command over the socket.

This commit is contained in:
Tom Alexander 2022-02-17 21:32:16 -05:00
parent 41d5f980ef
commit d56a7f32b6
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 49 additions and 6 deletions

View File

@ -10,3 +10,5 @@ walkdir = "2.3.2"
sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "sqlite", "migrate" ] } sqlx = { version = "0.5", features = [ "runtime-tokio-rustls", "sqlite", "migrate" ] }
tokio = { version = "1.16.1", features = ["full"] } tokio = { version = "1.16.1", features = ["full"] }
dirs = "4.0.0" dirs = "4.0.0"
serde_json = "1.0.79"
serde = { version = "1.0.136", features= ["derive"] }

View File

@ -8,6 +8,8 @@ use tokio::process::Command;
use walkdir::DirEntry; use walkdir::DirEntry;
use walkdir::WalkDir; use walkdir::WalkDir;
mod mpvctl; mod mpvctl;
use crate::mpvctl::MpvCtl;
use tokio::time::{sleep, Duration};
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -43,6 +45,13 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
launch_mpv().await?; launch_mpv().await?;
sleep(Duration::from_secs(10)).await;
let mut mpvctl = MpvCtl::connect("/tmp/recordwatchsocket").await?;
mpvctl.get_client_name().await?;
println!("done");
Ok(()) Ok(())
} }

View File

@ -1,9 +1,35 @@
use serde::ser::SerializeMap;
use serde::{Deserialize, Serialize, Serializer};
pub struct Command { pub struct Command {
command: Vec<String>, command: Vec<String>,
request_id: Option<u64>,
asynchronous: bool,
} }
impl Command { impl Command {
pub fn new(command: Vec<String>) -> Self { pub fn new(command: Vec<String>, request_id: Option<u64>) -> Self {
Command { command: command } Command {
command,
request_id,
asynchronous: false,
}
}
}
impl Serialize for Command {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut map = serializer.serialize_map(None)?;
map.serialize_entry("command", &self.command)?;
self.request_id
.map(|reqid| map.serialize_entry("request_id", &reqid))
.map_or(Ok(None), |x| x.map(Some))?;
if self.asynchronous {
map.serialize_entry("async", &self.asynchronous)?;
}
map.end()
} }
} }

View File

@ -1,5 +1,8 @@
use std::collections::HashMap; use std::collections::HashMap;
use std::io::Read;
use std::os::unix::prelude::OsStrExt;
use std::{collections::BTreeMap, path::Path}; use std::{collections::BTreeMap, path::Path};
use tokio::io::AsyncWriteExt;
use tokio::net::UnixStream; use tokio::net::UnixStream;
use super::command::Command; use super::command::Command;
@ -26,14 +29,17 @@ impl MpvCtl {
ret ret
} }
async fn send_command(&self, cmd: Command) { async fn send_command(&mut self, cmd: Command) -> Result<(), Box<dyn std::error::Error>> {
// let serialized = serde_json::to_string(&cmd)?;
// let serialized = println!("Sending {}", serialized);
self.socket.write_all(serialized.as_bytes()).await?;
Ok(())
} }
pub async fn get_client_name(&mut self) -> Result<(), Box<dyn std::error::Error>> { pub async fn get_client_name(&mut self) -> Result<(), Box<dyn std::error::Error>> {
let request_id = self.grab_request_id(); let request_id = self.grab_request_id();
let cmd = Command::new(vec!["client_name".to_string()]); let cmd = Command::new(vec!["client_name".to_string()], Some(request_id));
self.send_command(cmd).await?;
Ok(()) Ok(())
} }
} }