|
47 | 47 |
|
48 | 48 | #include "clientrpc.h" |
49 | 49 |
|
| 50 | +static QString ConnectionStateToString ( const EConnectionState eState ) |
| 51 | +{ |
| 52 | + switch ( eState ) |
| 53 | + { |
| 54 | + case CS_CONNECTING: |
| 55 | + return "connecting"; |
| 56 | + |
| 57 | + case CS_CONNECTED: |
| 58 | + return "connected"; |
| 59 | + |
| 60 | + default: |
| 61 | + return "disconnected"; |
| 62 | + } |
| 63 | +} |
| 64 | + |
50 | 65 | CClientRpc::CClientRpc ( CClient* pClient, CClientSettings* pSettings, CRpcServer* pRpcServer, QObject* parent ) : |
51 | 66 | QObject ( parent ), |
52 | 67 | m_pSettings ( pSettings ) |
@@ -168,6 +183,36 @@ CClientRpc::CClientRpc ( CClient* pClient, CClientSettings* pSettings, CRpcServe |
168 | 183 | /// @param {object} params - No parameters (empty object). |
169 | 184 | connect ( pClient, &CClient::Disconnected, [=]() { pRpcServer->BroadcastNotification ( "jamulusclient/disconnected", QJsonObject{} ); } ); |
170 | 185 |
|
| 186 | + /// @rpc_notification jamulusclient/connecting |
| 187 | + /// @brief Emitted when a connection to a server has been requested but is not yet established. |
| 188 | + /// @param {string} params.serverName - The human readable server name (or the address if no name is known). |
| 189 | + connect ( pClient, &CClient::Connecting, [=] ( QString strServerName ) { |
| 190 | + pRpcServer->BroadcastNotification ( "jamulusclient/connecting", |
| 191 | + QJsonObject{ |
| 192 | + { "serverName", strServerName }, |
| 193 | + } ); |
| 194 | + } ); |
| 195 | + |
| 196 | + /// @rpc_notification jamulusclient/connectingFailed |
| 197 | + /// @brief Emitted when a connection attempt failed before it could be requested from the server. |
| 198 | + /// @param {string} params.error - The error message. |
| 199 | + connect ( pClient, &CClient::ConnectingFailed, [=] ( QString strError ) { |
| 200 | + pRpcServer->BroadcastNotification ( "jamulusclient/connectingFailed", |
| 201 | + QJsonObject{ |
| 202 | + { "error", strError }, |
| 203 | + } ); |
| 204 | + } ); |
| 205 | + |
| 206 | + /// @rpc_notification jamulusclient/connectionStateChanged |
| 207 | + /// @brief Emitted whenever the connection state changes. |
| 208 | + /// @param {string} params.state - The new connection state (disconnected, connecting, or connected). |
| 209 | + connect ( pClient, &CClient::ConnectionStateChanged, [=] ( EConnectionState eState ) { |
| 210 | + pRpcServer->BroadcastNotification ( "jamulusclient/connectionStateChanged", |
| 211 | + QJsonObject{ |
| 212 | + { "state", ConnectionStateToString ( eState ) }, |
| 213 | + } ); |
| 214 | + } ); |
| 215 | + |
171 | 216 | /// @rpc_notification jamulusclient/recorderState |
172 | 217 | /// @brief Emitted when the client is connected to a server whose recorder state changes. |
173 | 218 | /// @param {number} params.state - The recorder state. |
@@ -212,6 +257,58 @@ CClientRpc::CClientRpc ( CClient* pClient, CClientSettings* pSettings, CRpcServe |
212 | 257 | Q_UNUSED ( params ); |
213 | 258 | } ); |
214 | 259 |
|
| 260 | + /// @rpc_method jamulusclient/connect |
| 261 | + /// @brief Connects the client to a server. Any current connection is terminated first. |
| 262 | + /// The connection is established asynchronously: subscribe to the jamulusclient/connecting, |
| 263 | + /// jamulusclient/connected, jamulusclient/connectingFailed and jamulusclient/connectionStateChanged |
| 264 | + /// notifications to follow its progress. |
| 265 | + /// @param {string} params.address - Socket address of the server (host:port). |
| 266 | + /// @param {string} params.serverName - Optional human readable server name used for display purposes. Defaults to the address. |
| 267 | + /// @result {string} result - "ok" once the connection attempt has been initiated. |
| 268 | + pRpcServer->HandleMethod ( "jamulusclient/connect", [=] ( const QJsonObject& params, QJsonObject& response ) { |
| 269 | + auto jsonAddress = params["address"]; |
| 270 | + if ( !jsonAddress.isString() ) |
| 271 | + { |
| 272 | + response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: address is not a string" ); |
| 273 | + return; |
| 274 | + } |
| 275 | + |
| 276 | + auto jsonServerName = params["serverName"]; |
| 277 | + const QString strAddress = NetworkUtil::FixAddress ( jsonAddress.toString() ); |
| 278 | + const QString strServerName = jsonServerName.isString() ? jsonServerName.toString() : strAddress; |
| 279 | + |
| 280 | + pClient->Connect ( strAddress, strServerName ); |
| 281 | + |
| 282 | + response["result"] = "ok"; |
| 283 | + } ); |
| 284 | + |
| 285 | + /// @rpc_method jamulusclient/disconnect |
| 286 | + /// @brief Disconnects the client from the current server. Does nothing if the client is not connected. |
| 287 | + /// @param {object} params - No parameters (empty object). |
| 288 | + /// @result {string} result - Always "ok". |
| 289 | + pRpcServer->HandleMethod ( "jamulusclient/disconnect", [=] ( const QJsonObject& params, QJsonObject& response ) { |
| 290 | + pClient->Disconnect(); |
| 291 | + |
| 292 | + response["result"] = "ok"; |
| 293 | + Q_UNUSED ( params ); |
| 294 | + } ); |
| 295 | + |
| 296 | + /// @rpc_method jamulusclient/getConnectionState |
| 297 | + /// @brief Returns the current connection state. |
| 298 | + /// @param {object} params - No parameters (empty object). |
| 299 | + /// @result {string} result.state - The connection state (disconnected, connecting, or connected). |
| 300 | + /// @result {string} result.serverName - The human readable name of the current server (empty if disconnected). |
| 301 | + pRpcServer->HandleMethod ( "jamulusclient/getConnectionState", [=] ( const QJsonObject& params, QJsonObject& response ) { |
| 302 | + const EConnectionState eState = pClient->GetConnectionState(); |
| 303 | + |
| 304 | + QJsonObject result{ |
| 305 | + { "state", ConnectionStateToString ( eState ) }, |
| 306 | + { "serverName", eState == CS_DISCONNECTED ? QString() : pClient->GetConnectedServerName() }, |
| 307 | + }; |
| 308 | + response["result"] = result; |
| 309 | + Q_UNUSED ( params ); |
| 310 | + } ); |
| 311 | + |
215 | 312 | /// @rpc_method jamulus/getMode |
216 | 313 | /// @brief Returns the current mode, i.e. whether Jamulus is running as a server or client. |
217 | 314 | /// @param {object} params - No parameters (empty object). |
|
0 commit comments