YAACC - UPNP Client and Server
Loading...
Searching...
No Matches
de.yaacc.snapcast.control.SnapcastModelUpdater Class Reference

Keeps an immutable SnapcastServerStatus up to date from the notifications of the server, so that the full status has to be read once and not again after every change. More...

Collaboration diagram for de.yaacc.snapcast.control.SnapcastModelUpdater:
Collaboration graph

Public Member Functions

 SnapcastModelUpdater (MonotonicClock clock, Runnable reReadRequester)
 
synchronized SnapcastServerStatus getStatus ()
 The current model; null before the first status.
 
synchronized void setStatus (SnapcastServerStatus fresh)
 Takes a freshly read status as the model; it also settles a re-read that was held back.
 
void onNotificationsLost ()
 The server's notifications were dropped on the way (or a line was unreadable): the model may be behind.
 
SnapcastServerStatus applyOwnChange (String method, JSONObject params)
 Applies a change that this client made itself, in the shape of the notification the server sends to the others (for example Client.OnVolumeChanged).
 
SnapcastServerStatus apply (String method, JSONObject params)
 Applies one notification.
 
boolean poll ()
 Delivers a re-read that was held back by the rate limit, if it is allowed now.
 
void requestReRead ()
 Asks for a full status now, within the rate limit (a request that is held back is delivered by poll()).
 
boolean tryGrantRead ()
 Asks the rate limit for permission to read the whole status now (a read that the caller makes itself, such as the one before a destructive change).
 

Detailed Description

Keeps an immutable SnapcastServerStatus up to date from the notifications of the server, so that the full status has to be read once and not again after every change.

After any doubt the full status is asked for again: an unknown notification, a client or group or stream that the model does not have, parameters that do not make sense, or notifications that were dropped. That request is rate limited, at most one per second and ten per minute, so that a server (or a flood) can not make the phone read the status in a loop; a request held back is delivered by poll() when it is allowed.

The notifications handled are the ones of the Snapcast control API: Client.OnConnect, Client.OnDisconnect, Client.OnVolumeChanged, Client.OnLatencyChanged, Client.OnNameChanged, Group.OnMute, Group.OnStreamChanged, Group.OnNameChanged, Stream.OnUpdate and Server.OnUpdate (the whole status). Stream.OnProperties (metadata of a stream) changes nothing in the model and is ignored. Thread safe.

Order. The model is right only if setStatus and apply run in the order of the lines of the connection. Use SnapcastOperations#SnapcastOperations(SnapcastCaller, SnapcastModelUpdater) (the status and our own changes are applied through SnapcastCaller#callOrdered) and a listener that hands every notification to apply: both run on the listener executor, one after the other, in line order. A status that is read elsewhere and set later can overwrite a newer notification.

Our own changes. The server does not notify the connection that made a change, so the caller has to patch the model itself (applyOwnChange; SnapcastOperations does it after every successful call).

Re-reads. The re-read is requested, not made: it is the caller's to do, and to repeat if it fails (the wish is cleared when it is handed out). A new connection needs a fresh status as well: read it on a worker when SnapcastRpc.Listener#onConnected() is told. The requester runs on the thread that found the doubt, which is the listener thread of the connection, so it must only hand the read to a worker: a getStatus() in it throws an IllegalStateException (see SnapcastRpc#callOrdered).

Constructor & Destructor Documentation

◆ SnapcastModelUpdater()

de.yaacc.snapcast.control.SnapcastModelUpdater.SnapcastModelUpdater ( MonotonicClock clock,
Runnable reReadRequester )
Parameters
reReadRequestercalled, on the thread that found the doubt (or that calls poll(); for the notifications that is the listener thread of the connection), when the full status should be read again; it must only hand the work to a worker, which reads the status and gives the answer to setStatus

Member Function Documentation

◆ apply()

SnapcastServerStatus de.yaacc.snapcast.control.SnapcastModelUpdater.apply ( String method,
JSONObject params )

Applies one notification.

Returns
the model after the notification; unchanged when it needed a re-read

Referenced by de.yaacc.snapcast.control.SnapcastModelUpdater.applyOwnChange(), and de.yaacc.snapcast.SnapcastSession.SnapcastSession().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ applyOwnChange()

SnapcastServerStatus de.yaacc.snapcast.control.SnapcastModelUpdater.applyOwnChange ( String method,
JSONObject params )

Applies a change that this client made itself, in the shape of the notification the server sends to the others (for example Client.OnVolumeChanged).

Does nothing before the first status.

Returns
the model after the change, null if there is none yet
Here is the call graph for this function:

◆ poll()

boolean de.yaacc.snapcast.control.SnapcastModelUpdater.poll ( )

Delivers a re-read that was held back by the rate limit, if it is allowed now.

Call it now and then (once per second is enough) while the model is in use.

Returns
true if the re-read was requested now

◆ requestReRead()

void de.yaacc.snapcast.control.SnapcastModelUpdater.requestReRead ( )

Asks for a full status now, within the rate limit (a request that is held back is delivered by poll()).

For changes that the model can not patch, such as a stream that was added.

Referenced by de.yaacc.snapcast.control.SnapcastModelUpdater.apply(), and de.yaacc.snapcast.control.SnapcastModelUpdater.onNotificationsLost().

Here is the caller graph for this function:

◆ tryGrantRead()

boolean de.yaacc.snapcast.control.SnapcastModelUpdater.tryGrantRead ( )

Asks the rate limit for permission to read the whole status now (a read that the caller makes itself, such as the one before a destructive change).

Uses up a grant, like requestReRead().

Returns
true if the read is allowed

Referenced by de.yaacc.snapcast.control.SnapcastOperations.freshStatusOrModel().

Here is the caller graph for this function:

The documentation for this class was generated from the following file: