Skip to content

Commit

Permalink
update: finish handshake process
Browse files Browse the repository at this point in the history
  • Loading branch information
luftaquila committed May 1, 2024
1 parent 8b55f41 commit d1afd51
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl Client {
let server_disp_map: HashMap<Did, Display> = deserialize(&buffer).unwrap();
let server_disp: Vec<Display> = server_disp_map.values().cloned().collect();

// configure our displays' attach position
// configure our displays' attach position and transmit to server
self.set_display_position(server_disp);
tcp_stream_write!(self.tcp, self.displays);

Expand Down
53 changes: 53 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use std::collections::HashMap;
use std::io::{Error, ErrorKind::*};
use std::sync::{Arc, RwLock};

use display_info::DisplayInfo;
use rand;
Expand Down Expand Up @@ -163,3 +165,54 @@ pub fn create_warpzones(a: &mut Vec<Display>, b: &mut Vec<Display>, eq: bool) ->

Ok(())
}

pub fn create_warpzones_hashmap(
map: &mut Arc<RwLock<HashMap<u32, Display>>>,
b: &mut Vec<Display>,
) -> Result<Vec<Did>, Error> {
let mut hashmap = map.write().unwrap();
let a: Vec<Display> = hashmap.values().cloned().collect();

let mut new = Vec::new();

// check overlap first
for disp in a.iter() {
for target in b.iter() {
if disp.is_overlap(target.clone()) {
return Err(Error::new(
InvalidInput,
"[ERR] two displays are overlapping",
));
}
}
}

// add warpzones
for disp in a.iter() {
for target in b.iter() {
if let Some((start, end, direction)) = disp.is_touch(target.clone()) {
hashmap.get_mut(&disp.id).unwrap().warpzones.push(WarpZone {
start,
end,
direction,
to: target.owner,
});

let mut target = target.clone();
let tid = target.id;

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

hashmap.insert(target.id, target);
new.push(tid);
}
}
}

Ok(new)
}
41 changes: 26 additions & 15 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::*;
pub struct Server {
clients: Arc<RwLock<HashMap<Cid, Client>>>,
displays: Arc<RwLock<HashMap<Did, Display>>>,
disp_ids: AssignedDisplays,
disp_ids: Arc<RwLock<AssignedDisplays>>,
current: Did,
}

Expand Down Expand Up @@ -58,10 +58,10 @@ impl Server {
Ok(Server {
clients: Arc::new(RwLock::new(HashMap::new())),
displays,
disp_ids: AssignedDisplays {
disp_ids: Arc::new(RwLock::new(AssignedDisplays {
system,
client: Vec::new(),
},
})),
current,
})
}
Expand All @@ -70,18 +70,21 @@ impl Server {
// let (tx, rx) = mpsc::channel();
let clients = self.clients.clone();
let displays = self.displays.clone();
let disp_ids = self.disp_ids.clone();

let authorized = authorized_clients(client_config).expect("[ERR] failed to read client config");
let authorized =
authorized_clients(client_config).expect("[ERR] failed to read client config");

thread::spawn(move || {
handle_client(clients, displays, authorized);
handle_client(clients, displays, disp_ids, authorized);
});
}
}

fn handle_client(
clients: Arc<RwLock<HashMap<Cid, Client>>>,
displays: Arc<RwLock<HashMap<Did, Display>>>,
mut displays: Arc<RwLock<HashMap<Did, Display>>>,
disp_ids: Arc<RwLock<AssignedDisplays>>,
authorized: Vec<Cid>,
) -> Result<(), Error> {
let tcp = TcpListener::bind(("0.0.0.0", PORT)).expect("[ERR] TCP binding failed");
Expand All @@ -101,27 +104,35 @@ fn handle_client(
tcp_stream_write!(stream, displays.read().unwrap().len() as u32);

// transmit current displays
let disp = displays.read().unwrap();
tcp_stream_write!(stream, *disp);
{
let disp = displays.read().unwrap();
tcp_stream_write!(stream, *disp);
}

// receive display attach request
tcp_stream_read_resize!(stream, buffer);
let client_disp: Vec<Display> = deserialize(&buffer).unwrap();

// calculate warpzones for new displays
// TODO

let mut client_disp: Vec<Display> = deserialize(&buffer).unwrap();

// update warpzones for new displays
let new = match create_warpzones_hashmap(&mut displays, &mut client_disp) {
Ok(new) => new,
Err(_) => {
return Err(Error::new(InvalidData, "[ERR] system display init failed"));
}
};

// transmit ack
tcp_stream_write!(stream, HandshakeStatus::HandshakeOk);

// add accepted client
// TODO
// add accepted client and display list
let client = Client {
tcp: stream,
cid,
displays: Vec::new(), // not using at server
};

clients.write().unwrap().insert(cid, client);
disp_ids.write().unwrap().client.extend(new);
}

Ok(())
Expand Down

0 comments on commit d1afd51

Please sign in to comment.