Store own PeerId in CommHandle

This commit is contained in:
Philip (a-0) 2024-02-25 12:35:35 +01:00
parent 29ff183c08
commit ec0a55b286

View file

@ -22,7 +22,7 @@ use crate::Config;
pub struct CommHandle {
state: Arc<CommState>,
i2p_server: Arc<I2pListener>,
peer_id: RwLock<PeerId>,
peer_id: PeerId,
// Maps peer addresses to existing connections to them
clients: Arc<RwLock<HashMap<I2pSocketAddr, Arc<RwLock<I2pStream>>>>>,
thread: RwLock<Option<JoinHandle<()>>>,
@ -38,12 +38,13 @@ impl CommHandle {
}
let listener = listener_builder.build().unwrap();
let own_peer_id: PeerId = (&listener).local_addr().map_err(|e| anyhow!(e))?.into();
let mut own_peer_id: PeerId = (&listener).local_addr().map_err(|e| anyhow!(e))?.into();
own_peer_id.b32_addr();
Ok(CommHandle {
state: Arc::new(state),
i2p_server: Arc::new(listener),
peer_id: RwLock::new(own_peer_id),
peer_id: own_peer_id,
clients: Default::default(),
thread: RwLock::new(None),
})
@ -104,6 +105,7 @@ impl CommHandle {
}
pub async fn send(&self, dest: &I2pSocketAddr, msg: Message) -> anyhow::Result<()> {
debug!("To '{dest:?}': '{msg:?}");
match serde_json::to_string(&msg) {
Ok(msg_string) => {
self.send_to_addr(dest, msg_string.as_bytes()).await?;
@ -116,6 +118,7 @@ impl CommHandle {
pub async fn send_to_addr(&self, addr: &I2pSocketAddr, msg: &[u8]) -> anyhow::Result<()> {
// Create client for this connection if necessary
if !self.clients.read().await.contains_key(addr) {
debug!("No client exists for requested connection, creating one");
match I2pStream::connect(addr) {
Ok(client) => {
//client.inner.sam.conn.set_nodelay(true)?;
@ -153,15 +156,15 @@ impl CommHandle {
}
pub fn i2p_address(&self) -> I2pSocketAddr {
self.peer_id.blocking_read().addr()
self.peer_id.addr()
}
pub fn i2p_b32_address(&self) -> anyhow::Result<I2pAddr> {
self.peer_id.blocking_write().b32_addr()
self.peer_id.b32_addr_nocache()
}
pub fn own_peer_id(&self) -> anyhow::Result<PeerId> {
Ok(self.peer_id.blocking_read().to_owned())
Ok(self.peer_id.to_owned())
}
fn read_connection(