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

The JSON-RPC 2.0 client of a Snapcast server: newline-delimited messages over one long-lived TCP connection that carries the calls, their answers and the server's notifications. More...

Inheritance diagram for de.yaacc.snapcast.control.SnapcastRpc:
Inheritance graph
Collaboration diagram for de.yaacc.snapcast.control.SnapcastRpc:
Collaboration graph

Classes

class  Config
 The settings; the defaults are the limits of the design.
 
interface  Delivery
 One entry of the queue to the listeners. More...
 
interface  Listener
 Receives what happens, on the listener executor (never on the socket thread). More...
 
enum  State
 The state of the connection. More...
 

Public Member Functions

 SnapcastRpc (SnapcastTransport transport, Config config, RetryScheduler scheduler, Executor listenerExecutor, MonotonicClock clock)
 
void addListener (Listener listener)
 
void removeListener (Listener listener)
 
State getState ()
 
int getPendingCount ()
 The number of calls waiting for an answer.
 
int getQueuedEventCount ()
 The number of events waiting for the listeners (the bound is Config#notificationQueueSize plus a few).
 
long getQueuedNotificationBytes ()
 The bytes the waiting notifications are charged (the bound is Config#notificationQueueBytes).
 
InetAddress getPeerAddress ()
 The address that the control connection is connected to; null while there is no connection.
 
long getConnectionGeneration ()
 A number that grows with every connection attempt and with the close: it differs between two connections to the same server, and never goes back.
 
void start ()
 Starts connecting; does nothing unless the client was not started yet.
 
void retry ()
 Connects now when the client waits for a retry or gave up (the failure count starts again); does nothing while connecting or connected, and after close().
 
void reconnect ()
 Replaces the connection by a new one now, also from CONNECTED: use it when the connection looks alive but is not (the calls end in timeouts).
 
JSONObject call (String method, JSONObject params) throws SnapcastException
 Calls a method and waits for the answer.
 
void close ()
 Closes the connection, fails the waiting calls, stops the threads.
 
- Public Member Functions inherited from de.yaacc.snapcast.control.SnapcastCaller

Static Public Member Functions

static SnapcastRpc forAddress (String host, int port)
 A client for the control port of a server with the default settings and own threads.
 

Detailed Description

The JSON-RPC 2.0 client of a Snapcast server: newline-delimited messages over one long-lived TCP connection that carries the calls, their answers and the server's notifications.

Threads. One reader thread per connection attempt reads, parses and routes; one writer thread sends the requests (so a server that stops reading can not hold a caller beyond its deadline); the listeners are called, in order, on a separate executor and never on the socket thread. Calls block the calling thread.

Order. Everything that changes the model is handed to the listener executor in the order of the lines of the connection: the notifications, and the results of callOrdered. So an answer (a status) and the notification the server sent right behind it can not overtake each other.

Hostile servers. A line is read at the byte level and capped (1 MiB), the text is scanned before org.json sees it (SnapcastJson), a batch is capped (64), the notification queue is capped (256 entries and 4 MiB of lines; the rest is dropped and reported through Listener#onNotificationsLost()), notifications of methods that the model does not know are not queued at all (they only report "read again"), the pending requests are capped (32), every call has an overall deadline, ids are made here, and a Throwable at the worker boundary drops the connection instead of ending the thread. A call that ends at its deadline although nothing at all was received since its request was written (a box that was switched off) drops the connection.

Reconnect. A lost connection is retried with a doubling delay (1 s, 2 s, ... at most 30 s); after 5 failures in a row it stops (State#GAVE_UP) until retry() is called. A connection that held for Config#stableConnectionMs counts as a success. reconnect() replaces a connection that looks alive. retry() and reconnect() start the failure count again, so a caller that calls them after every failure removes the ceiling of 5: keep them out of automatic loops.

Events. Listener#onConnected() and Listener#onDisconnected are queued together with the change of the state they report, so they arrive in the order of the changes (a connection that was replaced by reconnect() is reported as disconnected before the new one is reported as connected). An attempt that could not connect reports "disconnected" without a "connected" before it.

For the code that uses this (Group 3). Give the client a listener executor that does nothing but the deliveries (null makes an own thread). callOrdered throws an IllegalStateException when it is called from inside a delivery; it can not see a task of another kind on the same single-thread executor, which blocks the delivery that the call waits for, so the call ends in TIMEOUT. The re-read requester of SnapcastModelUpdater runs on the listener thread: it hands the read to a worker. Read a fresh status in Listener#onConnected() (on a worker), and repeat a re-read that failed. A call that waits for the whole deadline while the server sends nothing at all drops the connection, also when the server is only slow. When the executor rejects the tasks (it was shut down while the client is open), the ordered calls that wait for their delivery fail as CLOSED at once.

Constructor & Destructor Documentation

◆ SnapcastRpc()

de.yaacc.snapcast.control.SnapcastRpc.SnapcastRpc ( SnapcastTransport transport,
Config config,
RetryScheduler scheduler,
Executor listenerExecutor,
MonotonicClock clock )
Parameters
transportopens the connection
configthe settings (copied)
schedulerruns the reconnect timers; null: an own single-thread scheduler
listenerExecutorruns the listeners, one task at a time in order; null: an own thread
clockthe time source for the line deadline and the stable-connection rule

Referenced by de.yaacc.snapcast.control.SnapcastRpc.forAddress().

Here is the caller graph for this function:

Member Function Documentation

◆ call()

JSONObject de.yaacc.snapcast.control.SnapcastRpc.call ( String method,
JSONObject params ) throws SnapcastException

Calls a method and waits for the answer.

Parameters
paramsthe parameters, or null for none
Returns
the result object of the answer
Exceptions
SnapcastExceptionwhen the server answers an error or the call fails

Implements de.yaacc.snapcast.control.SnapcastCaller.

Referenced by de.yaacc.snapcast.control.SnapcastRpc.close(), and de.yaacc.snapcast.control.SnapcastRpc.reconnect().

Here is the caller graph for this function:

◆ close()

void de.yaacc.snapcast.control.SnapcastRpc.close ( )

Closes the connection, fails the waiting calls, stops the threads.

Safe to call twice.

Referenced by de.yaacc.snapcast.client.ServerControl.fetch().

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

◆ getConnectionGeneration()

long de.yaacc.snapcast.control.SnapcastRpc.getConnectionGeneration ( )

A number that grows with every connection attempt and with the close: it differs between two connections to the same server, and never goes back.

The feed uses it to be sure that the peer address it reads belongs to the connection that was up when its stream was made.

◆ getPeerAddress()

InetAddress de.yaacc.snapcast.control.SnapcastRpc.getPeerAddress ( )

The address that the control connection is connected to; null while there is no connection.

The feed of a stream connects to this address and to no other (never to an address that a server reply names).

◆ reconnect()

void de.yaacc.snapcast.control.SnapcastRpc.reconnect ( )

Replaces the connection by a new one now, also from CONNECTED: use it when the connection looks alive but is not (the calls end in timeouts).

Waiting calls fail as CLOSED, the failure count starts again (so do not call it in an automatic loop: the ceiling of the reconnects is gone). Does nothing while a connection is being opened, and after close().

Here is the call graph for this function:

◆ retry()

void de.yaacc.snapcast.control.SnapcastRpc.retry ( )

Connects now when the client waits for a retry or gave up (the failure count starts again); does nothing while connecting or connected, and after close().

Call it when the receivers page becomes visible. The failure count starts again, so do not call it in an automatic loop: the ceiling of the reconnects is gone.


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