Split State
in separate views for Api
and CommHandle
- State changes can now be handled differently, depending on whether they were caused locallly (API) or by a remote peer (Comm) - State functions have more readable names (`write...` and `update...` have similar meanings, but using different names helps readability in the respective (API/Comm) context)
This commit is contained in:
parent
32bbe8a8ce
commit
98393b9bf6
16 changed files with 326 additions and 138 deletions
|
@ -5,6 +5,7 @@ use tracing::{warn, error, debug};
|
|||
pub use types::*;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ops::Deref;
|
||||
use std::sync::Arc;
|
||||
use std::io::{Read, Write};
|
||||
|
||||
|
@ -15,13 +16,13 @@ use tokio::sync::RwLock;
|
|||
use tokio::task::JoinHandle;
|
||||
|
||||
use crate::Config;
|
||||
use crate::state::State;
|
||||
use crate::state::CommState;
|
||||
use crate::state::types::PeerId;
|
||||
|
||||
use self::messages::Message;
|
||||
|
||||
pub struct CommHandle {
|
||||
state: Arc<State>,
|
||||
state: Arc<CommState>,
|
||||
i2p_server: Arc<I2pListener>,
|
||||
// Maps peer addresses to existing connections to them
|
||||
clients: Arc<RwLock<HashMap<I2pSocketAddr, Arc<RwLock<I2pStream>>>>>,
|
||||
|
@ -29,7 +30,7 @@ pub struct CommHandle {
|
|||
}
|
||||
|
||||
impl CommHandle {
|
||||
pub fn new(state: Arc<State>, config: &Config) -> anyhow::Result<Self> {
|
||||
pub fn new(state: CommState, config: &Config) -> anyhow::Result<Self> {
|
||||
let mut listener_builder = I2pListenerBuilder::default()
|
||||
.with_options(SAMOptions::default());
|
||||
|
||||
|
@ -42,7 +43,7 @@ impl CommHandle {
|
|||
.unwrap();
|
||||
|
||||
Ok(CommHandle {
|
||||
state: state,
|
||||
state: Arc::new(state),
|
||||
i2p_server: Arc::new(listener),
|
||||
clients: Default::default(),
|
||||
thread: RwLock::new(None),
|
||||
|
@ -155,7 +156,7 @@ impl CommHandle {
|
|||
Ok(i2p_dest)
|
||||
}
|
||||
|
||||
fn read_connection(wrapped_stream: Arc<RwLock<I2pStream>>, state: Arc<State>) -> JoinHandle<()> {
|
||||
fn read_connection(wrapped_stream: Arc<RwLock<I2pStream>>, state: Arc<CommState>) -> JoinHandle<()> {
|
||||
tokio::spawn(async move {
|
||||
let mut stream = wrapped_stream.write().await;
|
||||
let peer: PeerId = stream.peer_addr().expect("Failed to get peer addr").into();
|
||||
|
@ -172,7 +173,7 @@ impl CommHandle {
|
|||
Ok(value) => {
|
||||
match serde_json::from_value::<Message>(value) {
|
||||
Ok(message) => {
|
||||
message_processor::handle(state.clone(), &peer, message);
|
||||
message_processor::handle(state.deref(), &peer, message);
|
||||
},
|
||||
Err(e) => warn!("Deserialization failed: {:?}", e),
|
||||
}
|
||||
|
@ -200,14 +201,14 @@ mod tests {
|
|||
use i2p::sam_options::SAMOptions;
|
||||
|
||||
use crate::Config;
|
||||
use crate::state::State;
|
||||
use crate::state::{State, CommState};
|
||||
use crate::comm::{messages, Message};
|
||||
use crate::state::types::ElementId;
|
||||
|
||||
use super::CommHandle;
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
pub async fn msg() {
|
||||
let ch = CommHandle::new(State::new().await.unwrap(), &Config::default() ).unwrap();
|
||||
let ch = CommHandle::new(CommState::new(State::new().await.unwrap()), &Config::default() ).unwrap();
|
||||
ch.run().await;
|
||||
println!("My address: {:?}", ch.i2p_b32_address());
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue