|
YAACC - UPNP Client and Server
|
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...

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). | |
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).
| de.yaacc.snapcast.control.SnapcastModelUpdater.SnapcastModelUpdater | ( | MonotonicClock | clock, |
| Runnable | reReadRequester ) |
| reReadRequester | called, 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 |
| SnapcastServerStatus de.yaacc.snapcast.control.SnapcastModelUpdater.apply | ( | String | method, |
| JSONObject | params ) |
Applies one notification.
Referenced by de.yaacc.snapcast.control.SnapcastModelUpdater.applyOwnChange(), and de.yaacc.snapcast.SnapcastSession.SnapcastSession().


| 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.

| 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.
| 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().

| 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().
Referenced by de.yaacc.snapcast.control.SnapcastOperations.freshStatusOrModel().
