Skip to content

Commit

Permalink
core/connect: update log msg, fix wrong behavior
Browse files Browse the repository at this point in the history
- handle became inactive separately
- remove duplicate stop
- adjust docs for websocket request
  • Loading branch information
photovoltex committed Nov 26, 2024
1 parent ffc31bb commit 85a68c8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
16 changes: 4 additions & 12 deletions connect/src/spirc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -530,10 +530,7 @@ impl SpircTask {
match self.session.spclient().get_context(context_uri).await {
Err(why) => error!("failed to resolve context '{context_uri}': {why}"),
Ok(ctx) if update => {
if let Err(why) = self.connect_state.update_context(ctx, UpdateContext::Default) {
error!("failed loading context: {why}");
self.handle_stop()
}
self.connect_state.update_context(ctx, UpdateContext::Default)?
}
Ok(mut ctx) if matches!(ctx.pages.first(), Some(p) if !p.tracks.is_empty()) => {
debug!("update context from single page, context {} had {} pages", ctx.uri, ctx.pages.len());
Expand Down Expand Up @@ -921,12 +918,8 @@ impl SpircTask {
self.connect_state.active && cluster.active_device_id != self.session.device_id();
if became_inactive {
info!("device became inactive");
self.connect_state.became_inactive(&self.session).await?;
self.handle_stop();
self.connect_state.reset();
let _ = self
.connect_state
.update_state(&self.session, PutStateReason::BECAME_INACTIVE)
.await?;
} else if self.connect_state.active {
// fixme: workaround fix, because of missing information why it behaves like it does
// background: when another device sends a connect-state update, some player's position de-syncs
Expand Down Expand Up @@ -1125,9 +1118,7 @@ impl SpircTask {
.update_position_in_relation(self.now_ms());
self.notify().await?;

self.connect_state
.update_state(&self.session, PutStateReason::BECAME_INACTIVE)
.await?;
self.connect_state.became_inactive(&self.session).await?;

self.player
.emit_session_disconnected_event(self.session.connection_id(), self.session.username());
Expand Down Expand Up @@ -1538,6 +1529,7 @@ impl SpircTask {

fn load_track(&mut self, start_playing: bool, position_ms: u32) -> Result<(), Error> {
if self.connect_state.current_track(MessageField::is_none) {
debug!("current track is none, stopping playback");
self.handle_stop();
return Ok(());
}
Expand Down
9 changes: 8 additions & 1 deletion connect/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,12 +397,19 @@ impl ConnectState {
self.player.timestamp = timestamp;
}

pub async fn became_inactive(&mut self, session: &Session) -> SpClientResult {
self.reset();
self.reset_context(None);

session.spclient().put_connect_state_inactive(false).await
}

/// Updates the connect state for the connect session
///
/// Prepares a [PutStateRequest] from the current connect state
pub async fn update_state(&self, session: &Session, reason: PutStateReason) -> SpClientResult {
if matches!(reason, PutStateReason::BECAME_INACTIVE) {
return session.spclient().put_connect_state_inactive(false).await;
warn!("should use <ConnectState::became_inactive> instead")
}

let now = SystemTime::now();
Expand Down
4 changes: 3 additions & 1 deletion connect/src/state/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ impl ConnectState {
self.autoplay_context = None;
self.shuffle_context = None;

if matches!(new_context, Some(ctx) if self.player.context_uri != ctx) {
let reset_default_context = new_context.is_none()
|| matches!(new_context, Some(ctx) if self.player.context_uri != ctx);
if reset_default_context {
self.context = None;
self.next_contexts.clear();
} else if let Some(ctx) = self.context.as_mut() {
Expand Down
6 changes: 5 additions & 1 deletion core/src/dealer/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,11 @@ impl WebsocketRequest {
let payload = String::from_utf8(payload)?;

if log::max_level() >= LevelFilter::Trace {
trace!("{:#?}", serde_json::from_str::<serde_json::Value>(&payload));
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&payload) {
trace!("websocket request: {json:#?}");
} else {
trace!("websocket request: {payload}");
}
}

serde_json::from_str(&payload)
Expand Down

0 comments on commit 85a68c8

Please sign in to comment.