// will return once the server has finished
will return once the server has finished
will return once the server has finished
pub async fn new(
        app_data: AppDataHolder,
        kodi_port: u16,
        http_server_port: u16,
        result: tokio::sync::oneshot::Sender<Session>,
        exit: exit::Exit,
        kodi_control_args: kodi_control::Args,
    ) -> Result<(), Error> {
        let url = Url::parse(
            format!(
                "http://{}:{}/jsonrpc",
                app_data.lock().unwrap().kodi_address,
                kodi_port
            )
            .as_str(),
        )?;
        let wsurl = Url::parse(
            format!(
                "ws://{}:9090/jsonrpc",
                app_data.lock().unwrap().kodi_address
            )
            .as_str(),
        )?;
        let auth = app_data.lock().unwrap().kodi_auth.clone();
        let jsonrpc_info = kodi_rpc::jsonrpc_get(&url, &auth).await?;
        let mut jsonrpc_session: kodi_rpc::WsJsonRPCSession = kodi_rpc::connect(&wsurl).await?;
        // let introspect = ws_jsonrpc_introspect(&mut self.jsonrpc_session).await?;
        // log::debug!("introspect: {}", introspect);
        // let mut file = std::fs::File::create("introspect.json").expect("create failed");
        // file.write_all(introspect.to_string().as_bytes())
        //     .expect("write failed");
        let players = kodi_rpc::get_players(&mut jsonrpc_session).await?;
        log::debug!("players: {}", players);
        // let mut file = std::fs::File::create("jsonrpc.json").expect("create failed");
        // file.write_all(&result.bytes).expect("write failed");
        // let server = make_server((result.local_addr.ip(), 0), filename);
        let files = app_data.lock().unwrap().files.clone();
        let urls_order = app_data.lock().unwrap().urls_order.clone();
        let (rpc_handler_done_tx, rpc_handler_done_rx) = tokio::sync::oneshot::channel();
        let (stop_server_tx, stop_server_rx) = tokio::sync::oneshot::channel();
        let (server_info_tx, server_info_rx) = tokio::sync::oneshot::channel();
        tokio::spawn({
            let exit = exit.clone();
            async move {
                let server_info = server_info_rx.await.expect("Failed to receive server_info");
                let mut ordered_urls: Vec<(usize, Url)> = files
                    .iter()
                    .map(|(url, _file)| {
                        (
                            urls_order.get(url).unwrap().clone(),
                            url_for_file(server_info, url).expect("Failed to create URL for file"),
                        )
                    })
                    .collect();
                ordered_urls.sort();
                let urls: Vec<Url> = ordered_urls.into_iter().map(|(_k, v)| v).collect();
                let (sigint_tx, sigint_rx) = mpsc::channel(1);
                tokio::spawn(handle_ctrl_c(sigint_tx));
                tokio::task::spawn(kodi_control::rpc_handler(
                    jsonrpc_session,
                    urls.clone(),
                    sigint_rx,
                    stop_server_tx,
                    rpc_handler_done_tx,
                    exit.clone(),
                    kodi_control_args,
                ));
                let session = Session {
                    rpc_handler_done_rx,
                };
                result
                    .send(session)
                    .expect("Failed to send result to caller");
            }
        });
        let result = Self::run_server(
            app_data,
            (jsonrpc_info.local_addr.ip(), http_server_port),
            server_info_tx,
            stop_server_rx,
        )
        .await;
        exit.signal();
        result?;
        Ok(())
    }
pub async fn new ( app_data : AppDataHolder , kodi_port : u16 , http_server_port : u16 , result : tokio :: sync :: oneshot :: Sender < Session > , exit : exit :: Exit , kodi_control_args : kodi_control :: Args , ) -> Result < ( ) , Error > { let url = Url :: parse ( format ! ( " " app_data lock ( ) unwrap ( ) kodi_address kodi_port ) . as_str ( ) , ) ? ; let wsurl = Url :: parse ( format ! ( " " app_data lock ( ) unwrap ( ) kodi_address ) . as_str ( ) , ) ? ; let auth = app_data . lock ( ) . unwrap ( ) . kodi_auth . clone ( ) ; let jsonrpc_info = kodi_rpc :: jsonrpc_get ( & url , & auth ) . await ? ; let mut jsonrpc_session : kodi_rpc :: WsJsonRPCSession = kodi_rpc :: connect ( & wsurl ) . await ? ; // let introspect = ws_jsonrpc_introspect(&mut self.jsonrpc_session).await?; // log::debug!("introspect: {}", introspect); // let mut file = std::fs::File::create("introspect.json").expect("create failed"); // file.write_all(introspect.to_string().as_bytes()) //     .expect("write failed"); let players = kodi_rpc :: get_players ( & mut jsonrpc_session ) . await ? ; log :: debug ! ( " " players ) ; // let mut file = std::fs::File::create("jsonrpc.json").expect("create failed"); // file.write_all(&result.bytes).expect("write failed"); // let server = make_server((result.local_addr.ip(), 0), filename); let files = app_data . lock ( ) . unwrap ( ) . files . clone ( ) ; let urls_order = app_data . lock ( ) . unwrap ( ) . urls_order . clone ( ) ; let ( rpc_handler_done_tx , rpc_handler_done_rx ) = tokio :: sync :: oneshot :: channel ( ) ; let ( stop_server_tx , stop_server_rx ) = tokio :: sync :: oneshot :: channel ( ) ; let ( server_info_tx , server_info_rx ) = tokio :: sync :: oneshot :: channel ( ) ; tokio :: spawn ( { let exit = exit . clone ( ) ; async move { let server_info = server_info_rx . await . expect ( " " ) ; let mut ordered_urls : Vec < ( usize , Url ) > = files . iter ( ) . map ( | ( url , _file ) | { ( urls_order . get ( url ) . unwrap ( ) . clone ( ) , url_for_file ( server_info , url ) . expect ( " " ) , ) } ) . collect ( ) ; ordered_urls . sort ( ) ; let urls : Vec < Url > = ordered_urls . into_iter ( ) . map ( | ( _k , v ) | v ) . collect ( ) ; let ( sigint_tx , sigint_rx ) = mpsc :: channel ( 1 ) ; tokio :: spawn ( handle_ctrl_c ( sigint_tx ) ) ; tokio :: task :: spawn ( kodi_control :: rpc_handler ( jsonrpc_session , urls . clone ( ) , sigint_rx , stop_server_tx , rpc_handler_done_tx , exit . clone ( ) , kodi_control_args , ) ) ; let session = Session { rpc_handler_done_rx , } ; result . send ( session ) . expect ( " " ) ; } } ) ; let result = Self :: run_server ( app_data , ( jsonrpc_info . local_addr . ip ( ) , http_server_port ) , server_info_tx , stop_server_rx , ) . await ; exit . signal ( ) ; result ? ; Ok ( ( ) ) }
will return once the server has finished
will return once the server has finished
app_data
AppDataHolder
kodi_port
u16
http_server_port
u16
result
tokio::sync::oneshot::Sender<Session>
exit
exit::Exit
kodi_control_args
kodi_control::Args