Skip to content

Commit

Permalink
Removed modifications in session.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannesd3 committed May 26, 2021
1 parent da129f7 commit 40b474b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 57 deletions.
1 change: 1 addition & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod cache;
pub mod channel;
pub mod config;
mod connection;
#[allow(dead_code)]
mod dealer;
#[doc(hidden)]
pub mod diffie_hellman;
Expand Down
79 changes: 22 additions & 57 deletions core/src/session.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::convert::Infallible;
use std::future::Future;
use std::io;
use std::pin::Pin;
Expand All @@ -16,7 +15,6 @@ use once_cell::sync::OnceCell;
use thiserror::Error;
use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream;
use url::Url;

use crate::apresolve::apresolve;
use crate::audio_key::AudioKeyManager;
Expand All @@ -25,8 +23,6 @@ use crate::cache::Cache;
use crate::channel::ChannelManager;
use crate::config::SessionConfig;
use crate::connection::{self, AuthenticationError};
use crate::dealer::{Builder, Dealer};
use crate::keymaster;
use crate::mercury::MercuryManager;

#[derive(Debug, Error)]
Expand All @@ -53,8 +49,6 @@ struct SessionInternal {
audio_key: OnceCell<AudioKeyManager>,
channel: OnceCell<ChannelManager>,
mercury: OnceCell<MercuryManager>,
dealer: tokio::sync::OnceCell<Dealer>,

cache: Option<Arc<Cache>>,

handle: tokio::runtime::Handle,
Expand Down Expand Up @@ -85,7 +79,25 @@ impl Session {
cache.save_credentials(&reusable_credentials);
}

let (sink, stream) = conn.split();
let session = Session::create(
conn,
config,
cache,
reusable_credentials.username,
tokio::runtime::Handle::current(),
);

Ok(session)
}

fn create(
transport: connection::Transport,
config: SessionConfig,
cache: Option<Cache>,
username: String,
handle: tokio::runtime::Handle,
) -> Session {
let (sink, stream) = transport.split();

let (sender_tx, sender_rx) = mpsc::unbounded_channel();
let session_id = SESSION_COUNTER.fetch_add(1, Ordering::Relaxed);
Expand All @@ -96,7 +108,7 @@ impl Session {
config,
data: RwLock::new(SessionData {
country: String::new(),
canonical_username: reusable_credentials.username,
canonical_username: username,
invalid: false,
time_delta: 0,
}),
Expand All @@ -105,8 +117,7 @@ impl Session {
audio_key: OnceCell::new(),
channel: OnceCell::new(),
mercury: OnceCell::new(),
dealer: tokio::sync::OnceCell::new(),
handle: tokio::runtime::Handle::current(),
handle,
session_id,
}));

Expand All @@ -123,10 +134,7 @@ impl Session {
}
});

// TODO: Initialize the dealer where it makes sense.
let _ = session.dealer().await;

Ok(session)
session
}

pub fn audio_key(&self) -> &AudioKeyManager {
Expand All @@ -147,48 +155,6 @@ impl Session {
.get_or_init(|| MercuryManager::new(self.weak()))
}

pub async fn dealer(&self) -> Result<&Dealer, ()> {
self.0
.dealer
.get_or_try_init(|| {
let weak = self.weak();
let builder = Builder::new();
builder.launch(
move || {
let session = weak.try_upgrade();

async move {
let session = if let Some(session) = session {
session
} else {
// Just don't finish and hope that someone will come along and clean up the Dealer.
futures_util::future::pending::<Infallible>().await;
unreachable!()
};

// TODO: How to handle errors here?

let token = keymaster::get_token(
&session,
"65b708073fc0480ea92a077233ca87bd",
"playlist-read",
)
.await
.unwrap();

let mut url: Url = "wss://dealer.spotify.com:443/".parse().unwrap();
url.query_pairs_mut()
.append_pair("access_token", &token.access_token);
url
}
},
self.0.config.proxy.clone(),
)
})
.await
.map_err(|_| ())
}

pub fn time_delta(&self) -> i64 {
self.0.data.read().unwrap().time_delta
}
Expand Down Expand Up @@ -277,7 +243,6 @@ impl Session {
self.0.data.write().unwrap().invalid = true;
self.mercury().shutdown();
self.channel().shutdown();
// TODO: Enable async shutdown to shutdown dealer properly.
}

pub fn is_invalid(&self) -> bool {
Expand Down

0 comments on commit 40b474b

Please sign in to comment.