30 lines
1.3 KiB
Rust
30 lines
1.3 KiB
Rust
|
|
use crate::common::binary_conversions::binary_to_ip;
|
||
|
|
use crate::records::memory::connections::CONNECTIONS;
|
||
|
|
use crate::records::memory::enums::ClientType;
|
||
|
|
use crate::Arc;
|
||
|
|
use crate::Mutex;
|
||
|
|
use crate::TcpStream;
|
||
|
|
|
||
|
|
pub async fn get_nodes_from_memory() -> Vec<(String, Arc<Mutex<TcpStream>>)> {
|
||
|
|
// Snapshot the current connection manager so piece requests can iterate without holding the lock.
|
||
|
|
let connection_storage = CONNECTIONS.read().await;
|
||
|
|
let mut nodes = Vec::new();
|
||
|
|
if let Some(connection) = &*connection_storage {
|
||
|
|
for connection_info in connection.connection_map.values() {
|
||
|
|
// Only miner peers are expected to serve block pieces.
|
||
|
|
if ClientType::from_bytes(&connection_info.client_type) != Some(ClientType::Miner) {
|
||
|
|
continue;
|
||
|
|
}
|
||
|
|
// Use ip:port as the scheduler key and clone the shared stream handle for requests.
|
||
|
|
let ip = binary_to_ip(connection_info.ip.clone());
|
||
|
|
let port = connection_info.port;
|
||
|
|
let key = format!("{ip}:{port}");
|
||
|
|
let stream_arc = Arc::clone(&connection_info.stream);
|
||
|
|
nodes.push((key, stream_arc));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
// Release the connection map before returning the cloned stream handles.
|
||
|
|
drop(connection_storage);
|
||
|
|
nodes
|
||
|
|
}
|