Skip to content

Commit

Permalink
fixup! feat(plugin): add async io for the plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzopalazzo committed Mar 24, 2024
1 parent 856cf04 commit a42bb4d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
18 changes: 11 additions & 7 deletions plugin/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ impl AsyncIO {
Ok(())
}

pub fn into_loop<F: FnMut(String) -> String>(&mut self, mut async_callback: F) {
pub fn into_loop<F: FnMut(String) -> Option<String>>(
&mut self,

Check warning on line 35 in plugin/src/io.rs

View workflow job for this annotation

GitHub Actions / clippy

methods called `into_*` usually take `self` by value

warning: methods called `into_*` usually take `self` by value --> plugin/src/io.rs:35:9 | 35 | &mut self, | ^^^^^^^^^ | = help: consider choosing a less ambiguous name = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#wrong_self_convention = note: `#[warn(clippy::wrong_self_convention)]` on by default
mut async_callback: F,
) -> io::Result<()> {
let mut events = mio::Events::with_capacity(1024);
loop {
let mut events = mio::Events::with_capacity(1024);
self.poll.poll(&mut events, None).unwrap();

self.poll.poll(&mut events, None)?;
for event in events.iter() {
#[cfg(feature = "log")]
log::info!("getting the event: {:?}", event);
Expand All @@ -56,9 +58,11 @@ impl AsyncIO {
break; // Exit the loop
}
}
let resp = async_callback(buffer.clone());
io::stdout().write_all(resp.as_bytes()).unwrap();
io::stdout().flush().unwrap();
let Some(resp) = async_callback(buffer.clone()) else {
continue;
};
io::stdout().write_all(resp.as_bytes())?;
io::stdout().flush()?;
}
}
_ => unreachable!(),
Expand Down
30 changes: 19 additions & 11 deletions plugin/src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use std::io::Write;
use std::string::String;

Check warning on line 7 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

the item `String` is imported redundantly

Check warning on line 7 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

the item `String` is imported redundantly

Check warning on line 7 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

the item `String` is imported redundantly

Check warning on line 7 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

the item `String` is imported redundantly

Check warning on line 7 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / Build (beta)

the item `String` is imported redundantly

Check warning on line 7 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

the item `String` is imported redundantly

Check warning on line 7 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / Build (nightly)

the item `String` is imported redundantly
use std::sync::Arc;

use serde_json::Value;

use clightningrpc_common::json_utils::{add_str, init_payload, init_success_response};
use clightningrpc_common::types::Request;
use serde_json::Value;

use crate::commands::builtin::{InitRPC, ManifestRPC};
use crate::commands::types::{CLNConf, RPCHookInfo, RPCMethodInfo};
Expand Down Expand Up @@ -256,7 +257,7 @@ impl<'a, T: 'a + Clone> Plugin<T> {
}
}

pub fn start(mut self) {
pub fn start(mut self) -> io::Result<()> {
#[cfg(feature = "log")]
{
use std::str::FromStr;
Expand All @@ -273,26 +274,33 @@ impl<'a, T: 'a + Clone> Plugin<T> {
on_init: self.on_init.clone(),
}),
);
let mut asyncio = AsyncIO::new().unwrap();
asyncio.register().unwrap();
let mut asyncio = AsyncIO::new()?;
asyncio.register()?;
asyncio.into_loop(|buffer| {
self.log(
LogLevel::Info,
&format!("looping around the string: {buffer}"),
);
#[cfg(feature = "log")]
log::info!("looping around the string: {buffer}");
let request: Request<serde_json::Value> = serde_json::from_str(&buffer).unwrap();

Check failure on line 282 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

error: used `unwrap()` on a `Result` value --> plugin/src/plugin.rs:282:55 | 282 | let request: Request<serde_json::Value> = serde_json::from_str(&buffer).unwrap(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: consider using `expect()` to provide a better panic message = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used
if let Some(id) = request.id {
// when the id is specified this is a RPC or Hook, so we need to return a response
let response = self.call_rpc_method(&request.method, request.params);
let mut rpc_response = init_success_response(id);
self.write_respose(&response, &mut rpc_response);
return serde_json::to_string(&rpc_response).unwrap();
#[cfg(feature = "log")]
log::info!(
"rpc or hook: {} with reponse {:?}",
request.method,
rpc_response
);
return Some(serde_json::to_string(&rpc_response).unwrap());

Check failure on line 294 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / clippy

used `unwrap()` on a `Result` value

error: used `unwrap()` on a `Result` value --> plugin/src/plugin.rs:294:29 | 294 | return Some(serde_json::to_string(&rpc_response).unwrap()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = note: if this value is an `Err`, it will panic = help: consider using `expect()` to provide a better panic message = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#unwrap_used

Check warning on line 294 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> plugin/src/plugin.rs:294:17 | 294 | return Some(serde_json::to_string(&rpc_response).unwrap()); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return = note: `#[warn(clippy::needless_return)]` on by default help: remove `return` | 294 - return Some(serde_json::to_string(&rpc_response).unwrap()); 294 + Some(serde_json::to_string(&rpc_response).unwrap()) |
} else {
// in case of the id is None, we are receiving the notification, so the server is not
// interested in the answer.
self.handle_notification(&request.method, request.params);
return String::new();
#[cfg(feature = "log")]
log::info!("notification: {}", request.method);
return None;

Check warning on line 301 in plugin/src/plugin.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> plugin/src/plugin.rs:301:17 | 301 | return None; | ^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return help: remove `return` | 301 - return None; 301 + None |
}
});
})?;
Ok(())
}
}

0 comments on commit a42bb4d

Please sign in to comment.