Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/listeners #44

Merged
merged 5 commits into from
Jul 19, 2022
Merged

Feature/listeners #44

merged 5 commits into from
Jul 19, 2022

Conversation

LGUG2Z
Copy link
Contributor

@LGUG2Z LGUG2Z commented Jul 18, 2022

No description provided.

@jtroo
Copy link
Owner

jtroo commented Jul 19, 2022

Thanks for the contribution!

I did a high level skim and looks good so far. I'll look into it with more detail at a later time.

src/cfg.rs Outdated
let layer_idxs = parse_layer_indexes(&layer_exprs, mapping_order.len())?;
let mut sorted_idxs: Vec<(String, usize)> = layer_idxs
.iter()
.map(|tuple| (tuple.0.clone(), tuple.1.clone()))
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this could be succinctly replaced with .cloned()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried getting this to compile with cloned() but didn't have much luck. I tried to simplify this a little be creating a tuple of references here and then just cloning the name as required further down in the fn

src/cfg.rs Outdated
.iter()
.map(|tuple| (tuple.0.clone(), tuple.1.clone()))
.collect();
sorted_idxs.sort_by(|&(_, a), &(_, b)| a.cmp(&b));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe sort_by_key expresses the intent better here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented this change

src/cfg.rs Outdated
@@ -262,6 +284,15 @@ fn parse_cfg_raw(
})
.collect::<Vec<_>>();

let mut layer_info = vec![];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer a direct assignment to layer_info by using map and collect rather than making it mut and pushing.

// note: not checked for correctness
let layer_info: Vec<LayerInfo> = layer_names
    .iter()
    .zip(layer_strings)
    .map(|(name, cfg_text)| LayerInfo {name, cfg_text})
    .collect();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented this change

src/kanata.rs Outdated

impl NotificationServer {
pub fn new(port: i32) -> Self {
let server = Self {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can return Self directly here:

        Self {
            port,
            connections: Arc::new(Mutex::new(HashMap::new())),
        }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implemented this change

src/kanata.rs Outdated
panic!("channel disconnected")
}
Ok(event) => {
let k = kanata.lock();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't like that we're acquiring a lock on the Kanata struct here; it means the processing loop is still dependent on and could be blocked by TCP operations.

It seems to me that the NotificationServer doesn't need to be part of the Kanata struct, so it would be better to move it out to avoid having lock contention between TCP operations and the keyboard operations.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed NotificationServer from the Kanata struct

src/kanata.rs Outdated
}

pub fn start_notification_loop(kanata: Arc<Mutex<Self>>, rx: Receiver<EventNotification>) {
info!("Kanata: entering the event notification loop");
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to fix the log message here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed this to Kanata: listening for event notifications to relay to connected clients but I'm open to whatever you wanna put here

src/kanata.rs Outdated
log::info!("Entered layer:\n{}", self.layer_info[layer].cfg_text);
}

pub fn start_notification_loop(kanata: Arc<Mutex<Self>>, rx: Receiver<EventNotification>) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May need to think about TCP timeout for the clients, e.g. have a heartbeat event sent every 30s (the processing loop can keep track of the timer for this one).

Also handling (and ignoring) the RX on the TCP socket so that the kernel buffers don't fill up.

@jtroo jtroo merged commit ff87be4 into jtroo:main Jul 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants