Store own PeerId
in CommHandle
This commit is contained in:
parent
29ff183c08
commit
ec0a55b286
1 changed files with 9 additions and 6 deletions
|
@ -22,7 +22,7 @@ use crate::Config;
|
||||||
pub struct CommHandle {
|
pub struct CommHandle {
|
||||||
state: Arc<CommState>,
|
state: Arc<CommState>,
|
||||||
i2p_server: Arc<I2pListener>,
|
i2p_server: Arc<I2pListener>,
|
||||||
peer_id: RwLock<PeerId>,
|
peer_id: PeerId,
|
||||||
// Maps peer addresses to existing connections to them
|
// Maps peer addresses to existing connections to them
|
||||||
clients: Arc<RwLock<HashMap<I2pSocketAddr, Arc<RwLock<I2pStream>>>>>,
|
clients: Arc<RwLock<HashMap<I2pSocketAddr, Arc<RwLock<I2pStream>>>>>,
|
||||||
thread: RwLock<Option<JoinHandle<()>>>,
|
thread: RwLock<Option<JoinHandle<()>>>,
|
||||||
|
@ -38,12 +38,13 @@ impl CommHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
let listener = listener_builder.build().unwrap();
|
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 {
|
Ok(CommHandle {
|
||||||
state: Arc::new(state),
|
state: Arc::new(state),
|
||||||
i2p_server: Arc::new(listener),
|
i2p_server: Arc::new(listener),
|
||||||
peer_id: RwLock::new(own_peer_id),
|
peer_id: own_peer_id,
|
||||||
clients: Default::default(),
|
clients: Default::default(),
|
||||||
thread: RwLock::new(None),
|
thread: RwLock::new(None),
|
||||||
})
|
})
|
||||||
|
@ -104,6 +105,7 @@ impl CommHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn send(&self, dest: &I2pSocketAddr, msg: Message) -> anyhow::Result<()> {
|
pub async fn send(&self, dest: &I2pSocketAddr, msg: Message) -> anyhow::Result<()> {
|
||||||
|
debug!("To '{dest:?}': '{msg:?}");
|
||||||
match serde_json::to_string(&msg) {
|
match serde_json::to_string(&msg) {
|
||||||
Ok(msg_string) => {
|
Ok(msg_string) => {
|
||||||
self.send_to_addr(dest, msg_string.as_bytes()).await?;
|
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<()> {
|
pub async fn send_to_addr(&self, addr: &I2pSocketAddr, msg: &[u8]) -> anyhow::Result<()> {
|
||||||
// Create client for this connection if necessary
|
// Create client for this connection if necessary
|
||||||
if !self.clients.read().await.contains_key(addr) {
|
if !self.clients.read().await.contains_key(addr) {
|
||||||
|
debug!("No client exists for requested connection, creating one");
|
||||||
match I2pStream::connect(addr) {
|
match I2pStream::connect(addr) {
|
||||||
Ok(client) => {
|
Ok(client) => {
|
||||||
//client.inner.sam.conn.set_nodelay(true)?;
|
//client.inner.sam.conn.set_nodelay(true)?;
|
||||||
|
@ -153,15 +156,15 @@ impl CommHandle {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn i2p_address(&self) -> I2pSocketAddr {
|
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> {
|
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> {
|
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(
|
fn read_connection(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue