Skip to content

Commit

Permalink
add: warpzones analyzation
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed Apr 17, 2024
1 parent 36b42e8 commit 09bc7cd
Showing 1 changed file with 131 additions and 14 deletions.
145 changes: 131 additions & 14 deletions src/server.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
use std::cell::RefCell;
use std::fs;
use std::io::{Error, ErrorKind, Read};
use std::net::TcpListener;
use std::rc::Rc;

use display_info::DisplayInfo;

use crate::client::*;
use crate::display::*;
use crate::utils::config_dir;

pub struct Server {
tcp: TcpListener,
clients: Vec<Client>,
displays: Vec<Display>,
clients: Vec<Rc<RefCell<Client>>>,
displays: Vec<Rc<RefCell<Display>>>,
}

impl Server {
Expand Down Expand Up @@ -53,7 +57,27 @@ impl Server {

match serde_json::from_str(&json) {
Ok(clients) => {
self.clients = clients;
let clients: Vec<Client> = clients;

self.clients = clients
.into_iter()
.map(|c| Rc::new(RefCell::new(c)))
.collect();
/* set disp for local references from deserialized display */
self.clients.iter_mut().for_each(|c| {
let mut c_ref = c.borrow_mut();
c_ref.disp = c_ref
.displays
.clone()
.into_iter()
.map(|display| Rc::new(RefCell::new(display)))
.collect();

c_ref.disp.iter_mut().for_each(|d| {
d.borrow_mut().owner = Some(Rc::downgrade(c));
d.borrow_mut().owner_type = DisplayOwnerType::CLIENT;
});
});
}
Err(e) => {
return Err(Error::new(
Expand All @@ -64,26 +88,119 @@ impl Server {
}

/* analyze warpzones */
if !self.analyze() {
if let Err(e) = self.analyze() {
return Err(Error::new(
ErrorKind::InvalidData,
format!(
"display configuration is not valid. config.json: {}",
config.as_os_str().to_str().unwrap()
"{} is not valid: {}",
config.as_os_str().to_str().unwrap(),
e.to_string()
),
));
}

Ok(())
}

fn analyze(&self) -> bool {
// TODO
fn analyze(&mut self) -> Result<(), Error> {
/* set system displays */
let system_disp: Vec<Rc<RefCell<Display>>> = DisplayInfo::all()
.unwrap()
.into_iter()
.map(|disp| Rc::new(RefCell::new(Display::from(disp))))
.collect();

/* set client displays */
self.displays = self
.clients
.iter()
.flat_map(|c| c.borrow().disp.clone())
.collect();

/* analyze warpzones for system displays ←→ client displays */
for disp in system_disp.iter() {
let mut disp_ref = disp.borrow_mut();

disp_ref.owner = None;
disp_ref.owner_type = DisplayOwnerType::SERVER;

for target in self.displays.iter() {
/* check overlap */
if disp_ref.is_overlap(target) {
return Err(Error::new(
ErrorKind::InvalidInput,
format!(
"two displays are overlapping.\ndisp_A: {:#?}, disp_B: {:#?}",
disp_ref,
target.borrow()
),
));
}

/* create warpzones if touching each other */
if let Some((start, end, direction)) = disp_ref.is_touch(target) {
disp_ref.warpzones.push(WarpZone {
start,
end,
direction,
to: Rc::downgrade(target),
});

target.borrow_mut().warpzones.push(WarpZone {
start,
end,
direction: direction.reverse(),
to: Rc::downgrade(disp),
});
}
}
}

/* analyze warpzones for client displays ←→ client displays */
for disp in self.displays.iter() {
let mut disp_ref = disp.borrow_mut();

for target in self.displays.iter() {
/* skip if target is disp */
if Rc::ptr_eq(disp, target) {
continue;
}

/* check overlap */
if disp_ref.is_overlap(target) {
return Err(Error::new(
ErrorKind::InvalidInput,
format!(
"two displays are overlapping.\ndisp_A: {:#?}, disp_B: {:#?}",
disp_ref,
target.borrow()
),
));
}

/* create warpzones if touching each other */
if let Some((start, end, direction)) = disp_ref.is_touch(target) {
disp_ref.warpzones.push(WarpZone {
start,
end,
direction,
to: Rc::downgrade(target),
});

target.borrow_mut().warpzones.push(WarpZone {
start,
end,
direction: direction.reverse(),
to: Rc::downgrade(disp),
});
}
}
}

/* analyze warpzones for the system displays first */
for (i, display) in self.displays.iter().enumerate() {}
/* merge all configured displays */
self.displays.extend(system_disp);

true
Ok(())
}

pub fn start(&mut self) -> Result<(), Error> {
Expand Down Expand Up @@ -121,16 +238,16 @@ impl Server {

/* verify client */
for (i, client) in self.clients.iter_mut().enumerate() {
if client.cid == incoming_client.cid {
if client.borrow().cid == incoming_client.cid {
/* TODO: verify configured displays */

verified[i] = true;
client.ip = Some(stream.peer_addr().unwrap());
client.borrow_mut().ip = Some(stream.peer_addr().unwrap());

println!(
"client {}({}) verified",
incoming_client.cid,
client.ip.unwrap()
client.borrow().ip.unwrap()
);
}
}
Expand Down

0 comments on commit 09bc7cd

Please sign in to comment.