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:
Philip (a-0) 2023-12-08 22:31:47 +01:00
parent 32bbe8a8ce
commit 98393b9bf6
16 changed files with 326 additions and 138 deletions

View file

@ -6,7 +6,7 @@ use comm::{CommHandle, Peer};
use config::Config;
use i2p::net::I2pSocketAddr;
use serde::{Serialize, Deserialize};
use state::{State, types::{ElementContent, ElementId, MessageId, PeerId}};
use state::{State, types::{ElementContent, ElementId, MessageId, PeerId}, CommState, ApiState};
mod api;
pub mod comm;
@ -28,10 +28,10 @@ pub struct Ubisync {
impl Ubisync {
pub async fn new(config: &Config) -> anyhow::Result<Self> {
let state = State::new().await?;
let comm_handle = Arc::new(CommHandle::new(state.clone(), config)?);
let comm_handle = Arc::new(CommHandle::new(CommState::new(state.clone()), config)?);
state.set_comm_handle(comm_handle.clone());
let api = Arc::new(ApiBuilder::from(config.api_config.clone()).build(state.clone()).await);
let api = Arc::new(ApiBuilder::from(config.api_config.clone()).build(ApiState::new(state.clone())).await);
comm_handle.run().await;
Ok(Ubisync {
@ -64,10 +64,6 @@ impl Ubisync {
pub fn get_destination(&self) -> anyhow::Result<I2pSocketAddr> {
self.comm_handle.i2p_address()
}
pub fn create_element(&self, content: &ElementContent) -> anyhow::Result<ElementId> {
self.state_handle.create_element(content)
}
}