Skip to content

Commit

Permalink
JSON-RPC Enhancements
Browse files Browse the repository at this point in the history
Adds jamulusclient/connect method.
Adds jamulusclient/disconnect method.
Adds jamulusclient/serverInfoReceived notification.
Return country string rather than (QT specific) country code.
Request server info after server list received (to get each server's ping time and num clients).
  • Loading branch information
riban-bw committed Mar 24, 2024
1 parent 86ed64e commit 401c9f9
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 9 deletions.
53 changes: 50 additions & 3 deletions docs/JSON-RPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Parameters:

| Name | Type | Description |
| --- | --- | --- |
| params.directory | string | Socket address of directory, e.g. anygenre1.jamulus.io:22124 |
| params.directory | string | Socket address of directory (hostname:port), e.g. anygenre1.jamulus.io:22124 |

Results:

Expand All @@ -219,6 +219,40 @@ Results:
| result | string | Always "ok". |


### jamulusclient/connect

Connect client to a server.

Parameters:

| Name | Type | Description |
| --- | --- | --- |
| params.address | string | Server socket address (hostname:port) |

Results:

| Name | Type | Description |
| --- | --- | --- |
| result | string | "ok" or error if invalid address.


### jamulusclient/disconnect

Disconnect client from server.

Parameters:

| Name | Type | Description |
| --- | --- | --- |
| params | object | No parameters (empty object). |

Results:

| Name | Type | Description |
| --- | --- | --- |
| result | string | Always "ok".


### jamulusclient/setName

Sets your name.
Expand Down Expand Up @@ -474,12 +508,25 @@ Parameters:
| Name | Type | Description |
| --- | --- | --- |
| params.servers | array | The server list. |
| params.servers[*].address | string | The server's socket address. |
| params.servers[*].address | string | The server's socket address (hostname:port). |
| params.servers[*].name | string | The server’s name. |
| params.servers[*].countryId | number | The servers’s country ID (see QLocale::Country). |
| params.servers[*].country | string | The servers’s country. |
| params.servers[*].city | string | The server’s city. |


### jamulusclient/serverInfoReceived

Emitted when a server info is received.

Parameters:

| Name | Type | Description |
| --- | --- | --- |
| params.servers[*].address | string | The server's socket address (hostname:port). |
| params.servers[*].pingTime | number | The round-trip ping time in ms. |
| params.servers[*].numClients | number | The quantity of clients connected to the server . |


### jamulusclient/connected

Emitted when the client is connected to the server.
Expand Down
66 changes: 60 additions & 6 deletions src/clientrpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ CClientRpc::CClientRpc ( CClient* pClient, CRpcServer* pRpcServer, QObject* pare
/// @param {number} params.clients[*].id - The channel ID.
/// @param {string} params.clients[*].name - The musician’s name.
/// @param {string} params.clients[*].skillLevel - The musician’s skill level (beginner, intermediate, expert, or null).
/// @param {number} params.clients[*].countryId - The musician’s country ID (see QLocale::Country).
/// @param {string} params.clients[*].country - The musician’s country.
/// @param {string} params.clients[*].city - The musician’s city.
/// @param {number} params.clients[*].instrumentId - The musician’s instrument ID (see CInstPictures::GetTable).
connect ( pClient, &CClient::ConClientListMesReceived, [=] ( CVector<CChannelInfo> vecChanInfo ) {
Expand All @@ -64,7 +64,7 @@ CClientRpc::CClientRpc ( CClient* pClient, CRpcServer* pRpcServer, QObject* pare
{ "id", chanInfo.iChanID },
{ "name", chanInfo.strName },
{ "skillLevel", SerializeSkillLevel ( chanInfo.eSkillLevel ) },
{ "countryId", chanInfo.eCountry },
{ "country", QLocale::countryToString ( chanInfo.eCountry ) },
{ "city", chanInfo.strCity },
{ "instrumentId", chanInfo.iInstrument },
};
Expand Down Expand Up @@ -99,7 +99,7 @@ CClientRpc::CClientRpc ( CClient* pClient, CRpcServer* pRpcServer, QObject* pare
/// @param {array} params.servers - The server list.
/// @param {string} params.servers[*].address - Socket address (ip_address:port)
/// @param {string} params.servers[*].name - Server name
/// @param {number} params.servers[*].countryId - Server country code
/// @param {string} params.servers[*].country - Server country
/// @param {string} params.servers[*].city - Server city
connect ( pClient->getConnLessProtocol(), &CProtocol::CLServerListReceived, [=] ( CHostAddress /* unused */, CVector<CServerInfo> vecServerInfo ) {
QJsonArray arrServerInfo;
Expand All @@ -108,17 +108,32 @@ CClientRpc::CClientRpc ( CClient* pClient, CRpcServer* pRpcServer, QObject* pare
QJsonObject objServerInfo{
{ "address", serverInfo.HostAddr.toString() },
{ "name", serverInfo.strName },
{ "countryId", serverInfo.eCountry },
{ "country", QLocale::countryToString ( serverInfo.eCountry) },
{ "city", serverInfo.strCity },
};
arrServerInfo.append ( objServerInfo );
pClient->CreateCLServerListPingMes ( serverInfo.HostAddr );
}
pRpcServer->BroadcastNotification ( "jamulusclient/serverListReceived",
QJsonObject{
{ "servers", arrServerInfo },
} );
} );

/// @rpc_notification jamulusclient/serverInfoReceived
/// @brief Emitted when a server info is received.
/// @param {string} params.address - The server socket address
/// @param {number} params.pingtime - The round-trip ping time in ms
/// @param {number} params.numClients - The quantity of clients connected to the server
connect (pClient, &CClient::CLPingTimeWithNumClientsReceived, [=] ( CHostAddress InetAddr, int iPingTime, int iNumClients ) {
pRpcServer->BroadcastNotification ( "jamulusclient/serverInfoReceived",
QJsonObject{
{"address", InetAddr.toString()},
{"pingTime", iPingTime},
{"numClients", iNumClients}
} );
} );

/// @rpc_notification jamulusclient/disconnected
/// @brief Emitted when the client is disconnected from the server.
/// @param {object} params - No parameters (empty object).
Expand Down Expand Up @@ -151,6 +166,45 @@ CClientRpc::CClientRpc ( CClient* pClient, CRpcServer* pRpcServer, QObject* pare
response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: directory is not a valid socket address" );
}

response["result"] = "ok";
} );

/// @rpc_method jamulusclient/connect
/// @brief Disconnect client from server
/// @param {string} params.address - Server socket address (ip_addr:port).
/// @result {string} result - Always "ok".
pRpcServer->HandleMethod ( "jamulusclient/connect", [=] ( const QJsonObject& params, QJsonObject& response ) {
auto jsonAddr = params["address"];
if ( !jsonAddr.isString() )
{
response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: address is not a string" );
return;
}

if ( pClient->SetServerAddr(jsonAddr.toString()) )
{
if ( !pClient->IsRunning() )
{
pClient->Start();
}
response["result"] = "ok";
}
else
{
response["error"] = CRpcServer::CreateJsonRpcError ( 1, "Bad server address" );
}
} );

/// @rpc_method jamulusclient/disconnect
/// @brief Disconnect client from server
/// @param {object} params - No parameters (empty object).
/// @result {string} result - Always "ok".
pRpcServer->HandleMethod ( "jamulusclient/disconnect", [=] ( const QJsonObject& params, QJsonObject& response ) {
if ( pClient->IsRunning() )
{
pClient->Stop();
}

response["result"] = "ok";
Q_UNUSED ( params );
} );
Expand Down Expand Up @@ -181,15 +235,15 @@ CClientRpc::CClientRpc ( CClient* pClient, CRpcServer* pRpcServer, QObject* pare
/// @result {number} result.id - The channel ID.
/// @result {string} result.name - The musician’s name.
/// @result {string} result.skillLevel - The musician’s skill level (beginner, intermediate, expert, or null).
/// @result {number} result.countryId - The musician’s country ID (see QLocale::Country).
/// @result {string} result.country - The musician’s country.
/// @result {string} result.city - The musician’s city.
/// @result {number} result.instrumentId - The musician’s instrument ID (see CInstPictures::GetTable).
/// @result {string} result.skillLevel - Your skill level (beginner, intermediate, expert, or null).
pRpcServer->HandleMethod ( "jamulusclient/getChannelInfo", [=] ( const QJsonObject& params, QJsonObject& response ) {
QJsonObject result{
// TODO: We cannot include "id" here is pClient->ChannelInfo is a CChannelCoreInfo which lacks that field.
{ "name", pClient->ChannelInfo.strName },
{ "countryId", pClient->ChannelInfo.eCountry },
{ "country", QLocale::countryToString ( pClient->ChannelInfo.eCountry ) },
{ "city", pClient->ChannelInfo.strCity },
{ "instrumentId", pClient->ChannelInfo.iInstrument },
{ "skillLevel", SerializeSkillLevel ( pClient->ChannelInfo.eSkillLevel ) },
Expand Down

0 comments on commit 401c9f9

Please sign in to comment.