Skip to content

Commit

Permalink
Starting to rewrite darklight_driver to be more clean and hopefully f…
Browse files Browse the repository at this point in the history
…aster.
  • Loading branch information
NathanMcMillan54 committed Nov 4, 2024
1 parent 6418c16 commit b0f8fb4
Show file tree
Hide file tree
Showing 22 changed files with 517 additions and 594 deletions.
207 changes: 102 additions & 105 deletions darklight_driver/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::cns::cns_add;
use crate::streams::{StreamInfo, STREAMS_HANDLER};
use crate::driver::DarkLightDriver;
use crate::streams::{Stream, StreamInfo, StreamsHandler};
use dlwp::id::local_user_id;
use std::fs::File;
use std::io::{BufRead, BufReader};
use std::io::{BufRead, BufReader, Read, Write};
use std::thread::{sleep, spawn};
use std::time::Duration;

pub fn cmd_input_thread() {
pub fn check_cmd_input(driver: &mut DarkLightDriver) {
File::create("/tmp/darklight/cmd_input").unwrap();

loop {
Expand All @@ -26,48 +27,35 @@ pub fn cmd_input_thread() {
continue;
}

unsafe {
if STREAMS_HANDLER.config.closed == true {
STREAMS_HANDLER.config.tcp = false;
STREAMS_HANDLER.config.serial = false;
}
}

match inputs[0] {
"config" => {
match inputs[1] {
"closed" => {
let value = inputs[2].replace("\n", "");
println!("closed: {}", value);
unsafe {
STREAMS_HANDLER.config.closed = {
if value == "true" {
true
} else if value == "false" {
println!("false");
false
} else {
println!("closed must be \"true\" or \"false\"");
continue;
}
};
}
driver.config.closed = {
if value == "true" {
true
} else if value == "false" {
println!("false");
false
} else {
println!("closed must be \"true\" or \"false\"");
continue;
}
};
}
"tcp" => {
let value = inputs[2].replace("\n", "");
println!("tcp: {}", value);
unsafe {
STREAMS_HANDLER.config.tcp = {
if value == "true" {
true
} else if value == "false" {
false
} else {
println!("tcp must be \"true\" or \"false\"");
continue;
}
};
}
driver.config.tcp = {
if value == "true" {
true
} else if value == "false" {
false
} else {
println!("tcp must be \"true\" or \"false\"");
continue;
}
};
}
_ => println!("{} is an invalid argument for \"config\"", inputs[1]),
};
Expand All @@ -81,27 +69,25 @@ pub fn cmd_input_thread() {
let week = inputs[6].parse::<i32>().unwrap();
let month = inputs[7].parse::<i32>().unwrap();

unsafe {
if STREAMS_HANDLER.stream_exists(rid, port) {
println!("Stream {}-{} already exists", rid, port);
println!(
"If this stream is on a different distributor, see issue #(n)"
);
} else {
STREAMS_HANDLER.add_stream(StreamInfo {
rid,
rdid,
port,
instance_id: instance,
connected: true,
sent_connection_request: true,
waited: 0,
received: vec![],
last_minute: 0,
pending: vec![],
info: [day, week, month, 0, 0, 0],
});
}
let local = if rid == local_user_id().unwrap() {
true
} else {
false
};

let stream_info = StreamInfo {
id: rid,
port,
did: rdid,
local
};

if driver.streams_handler.stream_info.contains_key(&stream_info) {
println!("Stream {}-{} already exists", rid, port);
println!("If this stream is on a different distributor, see issue #(n)");
return;
} else {
driver.streams_handler.stream_info.insert(stream_info, Stream::new([day, week, month, 0, 0, 0]));
}

println!("Added {}-{} to stream handler", rid, port);
Expand All @@ -115,69 +101,80 @@ pub fn cmd_input_thread() {

let rid = inputs[1].parse::<u64>().unwrap();
let rdid = inputs[2].parse::<u32>().unwrap();
let instance_id = inputs[3].parse::<u32>().unwrap();

unsafe {
for j in 0..STREAMS_HANDLER.stream_info.len() {
if STREAMS_HANDLER.stream_info[j].rid == rid
&& STREAMS_HANDLER.stream_info[j].rdid == rdid
&& STREAMS_HANDLER.stream_info[j].instance_id == instance_id
{
STREAMS_HANDLER.stream_info[j]
.pending
.push(message_str.clone());
STREAMS_HANDLER.remove_stream_file(
STREAMS_HANDLER.stream_info[j].rid,
STREAMS_HANDLER.stream_info[j].port,
);
STREAMS_HANDLER.create_stream_file(
STREAMS_HANDLER.stream_info[j].rid,
STREAMS_HANDLER.stream_info[j].port,
);
}
}
let port = inputs[4].parse::<u16>().unwrap();
let local = if rid == local_user_id().unwrap() {
true
} else {
false
};

let stream_info = StreamInfo {
id: rid,
port: port,
did: rdid,
local
};

if driver.streams_handler.stream_info.contains_key(&stream_info) == false {
println!("Stream: {:?} does not exist", stream_info);
return;
}

let stream = driver.streams_handler.stream_info.get_mut(&stream_info).unwrap();
stream.pending.push(message_str);

/* for j in 0..driver.streams_handler.stream_info.len() {
if driver.streams_handler.stream_info[j].rid == rid
&& driver.streams_handler.stream_info[j].rdid == rdid
&& driver.streams_handler.stream_info[j].instance_id == instance_id
{
driver.streams_handler.stream_info[j]
.pending
.push(message_str.clone());
driver.streams_handler.remove_stream_file(
driver.streams_handler.stream_info[j].rid,
driver.streams_handler.stream_info[j].port,
);
driver.streams_handler.create_stream_file(
driver.streams_handler.stream_info[j].rid,
driver.streams_handler.stream_info[j].port,
);
}
}*/
}
"DISCONNECT" => {
println!("disconnect arguments: {:?}", inputs);
let rid = inputs[1].parse::<u64>().unwrap();
let port = inputs[2].parse::<u16>().unwrap();
let rdid = inputs[2].parse::<u32>().unwrap();
let local = if rid == local_user_id().unwrap() {
true
} else {
false
};

let stream_info = StreamInfo {
id: rid,
port,
did: rdid,
local
};

sleep(Duration::from_millis(700));
unsafe {
for i in 0..STREAMS_HANDLER.stream_info.len() {
if STREAMS_HANDLER.stream_info[i].rid == local_user_id().unwrap()
&& STREAMS_HANDLER.stream_info[i].port == port
{
STREAMS_HANDLER.remove_stream_file(local_user_id().unwrap(), port);
STREAMS_HANDLER.create_stream_file(local_user_id().unwrap(), port);
}

if STREAMS_HANDLER.stream_info[i].rid == rid
&& STREAMS_HANDLER.stream_info[i].port == port
{
println!("Removing...");
STREAMS_HANDLER.remove_stream_file(rid, port);
STREAMS_HANDLER.stream_info.remove(i);
}
}

if driver.streams_handler.stream_info.contains_key(&stream_info) {
stream_info.remove_file();
driver.streams_handler.stream_info.remove(&stream_info).expect("Failed to remove");
}
}
"REQUEST-ADD-NAME" => {
println!("Requested to add a name");
println!("Shutting down all streams...");

unsafe {
for i in 0..STREAMS_HANDLER.stream_info.len() {
STREAMS_HANDLER.remove_stream_file(
STREAMS_HANDLER.stream_info[i].rid,
STREAMS_HANDLER.stream_info[i].port,
);
}

STREAMS_HANDLER.stream_info.clear();
for (info, stream) in driver.streams_handler.stream_info.iter() {
info.remove_file();
}
driver.streams_handler.stream_info.clear();

if inputs.len() != 4 {
println!("Invalid arguments {}", inputs.len());
Expand Down
8 changes: 7 additions & 1 deletion darklight_driver/src/cns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,13 @@ pub fn cns_add(input: Vec<&str>) {
stream.write(
format!(
"REQUEST_ADD1 {} {} {} {} {} {} {}",
setting[0], setting[1], setting[2], current_key, first_key, libcerpton_encode(setting, name.clone()), input[1]
setting[0],
setting[1],
setting[2],
current_key,
first_key,
libcerpton_encode(setting, name.clone()),
input[1]
),
REQUEST_RESPONSE,
);
Expand Down
71 changes: 71 additions & 0 deletions darklight_driver/src/driver.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use crate::streams::StreamsHandler;
use dlwp::{
config::DLConfig, distributor::READ_AVAILABLE, message::contents_to_string,
serialport::posix::TTYPort,
};
use std::{
io::{Read, Write},
net::TcpStream,
};

pub trait Test: Copy + Read + Write {
fn empty() {}
}

pub struct DarkLightDriver {
pub streams_handler: StreamsHandler,
pub config: DLConfig,
pub tcp_stream: Option<TcpStream>,
pub serial_port: Option<dlwp::serialport::posix::TTYPort>,
}

impl DarkLightDriver {
pub fn empty() -> Self {
return DarkLightDriver {
streams_handler: StreamsHandler::new(),
config: DLConfig::empty(),
tcp_stream: None,
serial_port: None,
};
}

pub fn new(streams_handler: StreamsHandler, config: DLConfig) -> Self {
return DarkLightDriver {
streams_handler,
config,
tcp_stream: None,
serial_port: None,
};
}
}

pub fn read<R: Read>(mut stream: &mut R) -> [u8; 4096] {
let mut buf = [0; 4096];
stream.read(&mut buf).unwrap_or(0);

buf
}

// Returns a message if one is received while waiting for send
pub fn write<RW: Read + Write>(mut stream: &mut RW, write: String, wait: bool) -> Option<String> {
if wait == true {
let mut read_bytes = read(stream);

while read_bytes == [0; 4096] {
read_bytes = read(stream);
}

let read_str = contents_to_string(read_bytes);
if read_str.contains(READ_AVAILABLE) {
stream.write(write.as_bytes()).unwrap();
stream.flush().unwrap();
} else {
return Some(read_str);
}
}

stream.write(write.as_bytes()).unwrap();
stream.flush().unwrap();

None
}
Loading

0 comments on commit b0f8fb4

Please sign in to comment.