pub(crate) fn peer_discover(
    space: &mut Space,
    to_agent: Arc<KitsuneAgent>,
    _from_agent: Arc<KitsuneAgent>,
    // TODO - FIXME - upgrade to KitsuneTimeout
    timeout_ms: u64,
) -> MustBoxFuture<'static, PeerDiscoverResult> {
    let timeout = KitsuneTimeout::from_millis(timeout_ms);

    let i_s = space.i_s.clone();
    let evt_sender = space.evt_sender.clone();
    let ep_hnd = space.ep_hnd.clone();
    let space = space.space.clone();
    async move {
        // run tx.create_channel an conver success result into our return type
        let try_connect = |url: url2::Url2| async {
            let con_hnd = ep_hnd.get_connection(url.clone(), timeout).await?;
            KitsuneP2pResult::Ok(PeerDiscoverResult::OkRemote { url, con_hnd })
        };

        // check if this agent is locally joined
        let check_local = || async {
            if i_s.is_agent_local(to_agent.clone()).await? {
                return Ok(PeerDiscoverResult::OkShortcut);
            }

            KitsuneP2pResult::Err("failed to connect".into())
        };

        // check if we have a reference to this agent in our peer store
        // if so, see if that url is valid via try_connect
        let check_peer_store = || async {
            if let Some(info) = evt_sender
                .get_agent_info_signed(GetAgentInfoSignedEvt {
                    space: space.clone(),
                    agent: to_agent.clone(),
                })
                .await?
            {
                let url = info
                    .url_list
                    .get(0)
                    .ok_or_else(|| KitsuneP2pError::from("no url"))?
                    .clone();
                return try_connect(url.into()).await;
            }

            KitsuneP2pResult::Err("failed to connect".into())
        };

        let start_time = std::time::Instant::now();
        let mut interval_ms = 50;

        loop {
            if let Ok(res) = check_local().await {
                return res;
            }

            if let Ok(res) = check_peer_store().await {
                return res;
            }

            let elapsed_ms = start_time.elapsed().as_millis() as u64;
            if elapsed_ms >= timeout_ms {
                return PeerDiscoverResult::Err("timeout".into());
            }

            interval_ms *= 2;
            if interval_ms > timeout_ms - elapsed_ms {
                interval_ms = timeout_ms - elapsed_ms;
            }

            tokio::time::sleep(std::time::Duration::from_millis(interval_ms)).await;
        }
    }
    .boxed()
    .into()
}
/// attempt to establish a connection to another peer within given timeout
attempt to establish a connection to another peer within given timeout
attempt to establish a connection to another peer within given timeout
pub(crate) fn peer_discover(
    space: &mut Space,
    to_agent: Arc<KitsuneAgent>,
    _from_agent: Arc<KitsuneAgent>,
    // TODO - FIXME - upgrade to KitsuneTimeout
    timeout_ms: u64,
) -> MustBoxFuture<'static, PeerDiscoverResult> {
    let timeout = KitsuneTimeout::from_millis(timeout_ms);
    let i_s = space.i_s.clone();
    let evt_sender = space.evt_sender.clone();
    let ep_hnd = space.ep_hnd.clone();
    let space = space.space.clone();
    async move {
        // run tx.create_channel an conver success result into our return type
        let try_connect = |url: url2::Url2| async {
            let con_hnd = ep_hnd.get_connection(url.clone(), timeout).await?;
            KitsuneP2pResult::Ok(PeerDiscoverResult::OkRemote { url, con_hnd })
        };
        // check if this agent is locally joined
        let check_local = || async {
            if i_s.is_agent_local(to_agent.clone()).await? {
                return Ok(PeerDiscoverResult::OkShortcut);
            }
            KitsuneP2pResult::Err("failed to connect".into())
        };
        // check if we have a reference to this agent in our peer store
        // if so, see if that url is valid via try_connect
        let check_peer_store = || async {
            if let Some(info) = evt_sender
                .get_agent_info_signed(GetAgentInfoSignedEvt {
                    space: space.clone(),
                    agent: to_agent.clone(),
                })
                .await?
            {
                let url = info
                    .url_list
                    .get(0)
                    .ok_or_else(|| KitsuneP2pError::from("no url"))?
                    .clone();
                return try_connect(url.into()).await;
            }
            KitsuneP2pResult::Err("failed to connect".into())
        };
        let start_time = std::time::Instant::now();
        let mut interval_ms = 50;
        loop {
            if let Ok(res) = check_local().await {
                return res;
            }
            if let Ok(res) = check_peer_store().await {
                return res;
            }
            let elapsed_ms = start_time.elapsed().as_millis() as u64;
            if elapsed_ms >= timeout_ms {
                return PeerDiscoverResult::Err("timeout".into());
            }
            interval_ms *= 2;
            if interval_ms > timeout_ms - elapsed_ms {
                interval_ms = timeout_ms - elapsed_ms;
            }
            tokio::time::sleep(std::time::Duration::from_millis(interval_ms)).await;
        }
    }
    .boxed()
    .into()
}
pub ( crate ) fn peer_discover ( space : & mut Space , to_agent : Arc < KitsuneAgent > , _from_agent : Arc < KitsuneAgent > , // TODO - FIXME - upgrade to KitsuneTimeout timeout_ms : u64 , ) -> MustBoxFuture < ' static , PeerDiscoverResult > { let timeout = KitsuneTimeout :: from_millis ( timeout_ms ) ; let i_s = space . i_s . clone ( ) ; let evt_sender = space . evt_sender . clone ( ) ; let ep_hnd = space . ep_hnd . clone ( ) ; let space = space . space . clone ( ) ; async move { // run tx.create_channel an conver success result into our return type let try_connect = | url : url2 :: Url2 | async { let con_hnd = ep_hnd . get_connection ( url . clone ( ) , timeout ) . await ? ; KitsuneP2pResult :: Ok ( PeerDiscoverResult :: OkRemote { url , con_hnd } ) } ; // check if this agent is locally joined let check_local = | | async { if i_s . is_agent_local ( to_agent . clone ( ) ) . await ? { return Ok ( PeerDiscoverResult :: OkShortcut ) ; } KitsuneP2pResult :: Err ( " " . into ( ) ) } ; // check if we have a reference to this agent in our peer store // if so, see if that url is valid via try_connect let check_peer_store = | | async { if let Some ( info ) = evt_sender . get_agent_info_signed ( GetAgentInfoSignedEvt { space : space . clone ( ) , agent : to_agent . clone ( ) , } ) . await ? { let url = info . url_list . get ( 0 ) . ok_or_else ( | | KitsuneP2pError :: from ( " " ) ) ? . clone ( ) ; return try_connect ( url . into ( ) ) . await ; } KitsuneP2pResult :: Err ( " " . into ( ) ) } ; let start_time = std :: time :: Instant :: now ( ) ; let mut interval_ms = 50 ; loop { if let Ok ( res ) = check_local ( ) . await { return res ; } if let Ok ( res ) = check_peer_store ( ) . await { return res ; } let elapsed_ms = start_time . elapsed ( ) . as_millis ( ) as u64 ; if elapsed_ms >= timeout_ms { return PeerDiscoverResult :: Err ( " " . into ( ) ) ; } interval_ms *= 2 ; if interval_ms > timeout_ms - elapsed_ms { interval_ms = timeout_ms - elapsed_ms ; } tokio :: time :: sleep ( std :: time :: Duration :: from_millis ( interval_ms ) ) . await ; } } . boxed ( ) . into ( ) }
attempt to establish a connection to another peer within given timeout
attempt to establish a connection to another peer within given timeout
space
&mut Space
to_agent
Arc<KitsuneAgent>
_from_agent
Arc<KitsuneAgent>
timeout_ms
u64
space
&mut Space
to_agent
Arc<KitsuneAgent>
_from_agent
Arc<KitsuneAgent>
timeout_ms
u64