2021-07-04 14:38:44 +01:00
|
|
|
use std::{collections::HashSet, sync::Arc};
|
2021-06-28 14:23:28 +01:00
|
|
|
|
2021-07-12 21:59:08 +01:00
|
|
|
use librqbit_core::id20::Id20;
|
2021-07-03 19:10:59 +01:00
|
|
|
use librqbit_core::lengths::{ChunkInfo, ValidPieceIndex};
|
2021-06-28 14:23:28 +01:00
|
|
|
use tokio::sync::{Notify, Semaphore};
|
|
|
|
|
|
2021-07-03 19:10:59 +01:00
|
|
|
use crate::type_aliases::BF;
|
2021-06-30 00:00:44 +01:00
|
|
|
|
|
|
|
|
#[derive(Debug, Hash, PartialEq, Eq)]
|
|
|
|
|
pub struct InflightRequest {
|
|
|
|
|
pub piece: ValidPieceIndex,
|
|
|
|
|
pub chunk: u32,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl From<&ChunkInfo> for InflightRequest {
|
|
|
|
|
fn from(c: &ChunkInfo) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
piece: c.piece_index,
|
|
|
|
|
chunk: c.chunk_index,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-06-28 14:23:28 +01:00
|
|
|
|
2021-07-04 12:17:06 +01:00
|
|
|
#[derive(Debug)]
|
2021-06-28 14:23:28 +01:00
|
|
|
pub enum PeerState {
|
2021-07-04 12:17:06 +01:00
|
|
|
Queued,
|
|
|
|
|
Connecting,
|
2021-06-28 14:23:28 +01:00
|
|
|
Live(LivePeerState),
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-04 12:17:06 +01:00
|
|
|
#[derive(Debug)]
|
2021-06-28 14:23:28 +01:00
|
|
|
pub struct LivePeerState {
|
2021-07-12 21:59:08 +01:00
|
|
|
pub peer_id: Id20,
|
2021-06-28 14:23:28 +01:00
|
|
|
pub i_am_choked: bool,
|
|
|
|
|
pub peer_interested: bool,
|
2021-06-28 14:58:53 +01:00
|
|
|
pub requests_sem: Arc<Semaphore>,
|
2021-06-28 14:23:28 +01:00
|
|
|
pub have_notify: Arc<Notify>,
|
|
|
|
|
pub bitfield: Option<BF>,
|
|
|
|
|
pub inflight_requests: HashSet<InflightRequest>,
|
|
|
|
|
}
|
2021-06-28 14:42:19 +01:00
|
|
|
|
|
|
|
|
impl LivePeerState {
|
2021-07-12 21:59:08 +01:00
|
|
|
pub fn new(peer_id: Id20) -> Self {
|
2021-06-28 14:42:19 +01:00
|
|
|
LivePeerState {
|
2021-07-02 01:38:07 +01:00
|
|
|
peer_id,
|
2021-06-28 14:42:19 +01:00
|
|
|
i_am_choked: true,
|
|
|
|
|
peer_interested: false,
|
|
|
|
|
bitfield: None,
|
|
|
|
|
have_notify: Arc::new(Notify::new()),
|
2021-06-28 14:58:53 +01:00
|
|
|
requests_sem: Arc::new(Semaphore::new(0)),
|
2021-06-28 14:42:19 +01:00
|
|
|
inflight_requests: Default::default(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|