Skip to content

Commit efcef61

Browse files
committed
3532: Add jamulusserver/setDirectory request
1 parent ba07f4e commit efcef61

File tree

3 files changed

+133
-32
lines changed

3 files changed

+133
-32
lines changed

docs/JSON-RPC.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ Results:
321321
| result.city | string | The server city. |
322322
| result.countryId | number | The server country ID (see QLocale::Country). |
323323
| result.welcomeMessage | string | The server welcome message. |
324+
| result.directoryType | string | The directory type as a string (see EDirectoryType and SerializeDirectoryType). |
325+
| result.directoryAddress | string | The string used to look up the directory address (only assume valid if directoryType is "custom" and registrationStatus is "registered"). |
324326
| result.directory | string | The directory with which this server requested registration, or blank if none. |
325327
| result.registrationStatus | string | The server registration status as string (see ESvrRegStatus and SerializeRegistrationStatus). |
326328

@@ -342,6 +344,24 @@ Results:
342344
| result | string | Always "acknowledged". To check if the recording was restarted or if there is any error, call `jamulusserver/getRecorderStatus` again. |
343345

344346

347+
### jamulusserver/setDirectory
348+
349+
Set the directory type and, for custom, the directory address.
350+
351+
Parameters:
352+
353+
| Name | Type | Description |
354+
| --- | --- | --- |
355+
| params.directoryType | string | The directory type as a string (see EDirectoryType and DeserializeDirectoryType). |
356+
| [params.directoryAddress] | string | (optional) The directory address, required if `directoryType` is "custom". |
357+
358+
Results:
359+
360+
| Name | Type | Description |
361+
| --- | --- | --- |
362+
| result | string | Always "ok". |
363+
364+
345365
### jamulusserver/setRecordingDirectory
346366

347367
Sets the server recording directory.

src/serverrpc.cpp

Lines changed: 104 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,72 @@ CServerRpc::CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* pare
122122
/// @result {string} result.city - The server city.
123123
/// @result {number} result.countryId - The server country ID (see QLocale::Country).
124124
/// @result {string} result.welcomeMessage - The server welcome message.
125+
/// @result {string} result.directoryType - The directory type as a string (see EDirectoryType and SerializeDirectoryType).
126+
/// @result {string} result.directoryAddress - The string used to look up the directory address (only assume valid if directoryType is "custom"
127+
/// and registrationStatus is "registered").
125128
/// @result {string} result.directory - The directory with which this server requested registration, or blank if none.
126129
/// @result {string} result.registrationStatus - The server registration status as string (see ESvrRegStatus and SerializeRegistrationStatus).
127130
pRpcServer->HandleMethod ( "jamulusserver/getServerProfile", [=] ( const QJsonObject& params, QJsonObject& response ) {
128-
QString dsName = "";
129-
130-
if ( AT_NONE != pServer->GetDirectoryType() )
131-
dsName = NetworkUtil::GetDirectoryAddress ( pServer->GetDirectoryType(), pServer->GetDirectoryAddress() );
131+
EDirectoryType directoryType = pServer->GetDirectoryType();
132+
QString directoryAddress = pServer->GetDirectoryAddress();
133+
QString dsName = ( AT_NONE == directoryType ) ? "" : NetworkUtil::GetDirectoryAddress ( directoryType, directoryAddress );
132134

133135
QJsonObject result{
134136
{ "name", pServer->GetServerName() },
135137
{ "city", pServer->GetServerCity() },
136138
{ "countryId", pServer->GetServerCountry() },
137139
{ "welcomeMessage", pServer->GetWelcomeMessage() },
140+
{ "directoryType", SerializeDirectoryType ( directoryType ) },
141+
{ "directoryAddress", directoryAddress },
138142
{ "directory", dsName },
139143
{ "registrationStatus", SerializeRegistrationStatus ( pServer->GetSvrRegStatus() ) },
140144
};
141145
response["result"] = result;
142146
Q_UNUSED ( params );
143147
} );
144148

149+
/// @rpc_method jamulusserver/setDirectory
150+
/// @brief Set the directory type and, for custom, the directory address.
151+
/// @param {string} params.directoryType - The directory type as a string (see EDirectoryType and DeserializeDirectoryType).
152+
/// @param {string} [params.directoryAddress] - (optional) The directory address, required if `directoryType` is "custom".
153+
/// @result {string} result - Always "ok".
154+
pRpcServer->HandleMethod ( "jamulusserver/setDirectory", [=] ( const QJsonObject& params, QJsonObject& response ) {
155+
auto jsonDirectoryType = params["directoryType"];
156+
auto directoryAddress = params["directoryAddress"];
157+
EDirectoryType directoryType = AT_NONE;
158+
159+
if ( !jsonDirectoryType.isString() )
160+
{
161+
response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: directory type is not a string" );
162+
return;
163+
}
164+
else
165+
{
166+
directoryType = DeserializeDirectoryType ( jsonDirectoryType.toString().toStdString() );
167+
}
168+
169+
if ( !directoryAddress.isUndefined() )
170+
{
171+
if ( !directoryAddress.isString() )
172+
{
173+
response["error"] =
174+
CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: directory address is not a string" );
175+
return;
176+
}
177+
}
178+
else if ( AT_CUSTOM == directoryType )
179+
{
180+
response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams,
181+
"Invalid params: directory address is required when directory type is \"custom\"" );
182+
return;
183+
}
184+
185+
pServer->SetDirectoryAddress ( directoryAddress.toString() ); // ignored unless AT_CUSTOM == directoryType
186+
pServer->SetDirectoryType ( directoryType );
187+
188+
response["result"] = "ok";
189+
} );
190+
145191
/// @rpc_method jamulusserver/setServerName
146192
/// @brief Sets the server name.
147193
/// @param {string} params.serverName - The new server name.
@@ -226,37 +272,64 @@ CServerRpc::CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* pare
226272
} );
227273
}
228274

229-
QJsonValue CServerRpc::SerializeRegistrationStatus ( ESvrRegStatus eSvrRegStatus )
275+
const std::unordered_map<EDirectoryType, std::string> CServerRpc::sumDirectoryTypeToString = {
276+
{ EDirectoryType::AT_NONE, "none" },
277+
{ EDirectoryType::AT_DEFAULT, "any_genre_1" },
278+
{ EDirectoryType::AT_ANY_GENRE2, "any_genre_2" },
279+
{ EDirectoryType::AT_ANY_GENRE3, "any_genre_3" },
280+
{ EDirectoryType::AT_GENRE_ROCK, "genre_rock" },
281+
{ EDirectoryType::AT_GENRE_JAZZ, "genre_jazz" },
282+
{ EDirectoryType::AT_GENRE_CLASSICAL_FOLK, "genre_classical_folk" },
283+
{ EDirectoryType::AT_GENRE_CHORAL, "genre_choral_barbershop" },
284+
{ EDirectoryType::AT_CUSTOM, "custom" },
285+
};
286+
287+
inline QJsonValue CServerRpc::SerializeDirectoryType ( EDirectoryType eAddrType )
230288
{
231-
switch ( eSvrRegStatus )
232-
{
233-
case SRS_NOT_REGISTERED:
234-
return "not_registered";
235-
236-
case SRS_BAD_ADDRESS:
237-
return "bad_address";
289+
auto found = sumDirectoryTypeToString.find ( eAddrType );
290+
if ( found == sumDirectoryTypeToString.end() )
291+
return QJsonValue ( QString ( "unknown(%1)" ).arg ( eAddrType ) );
238292

239-
case SRS_REQUESTED:
240-
return "requested";
241-
242-
case SRS_TIME_OUT:
243-
return "time_out";
244-
245-
case SRS_UNKNOWN_RESP:
246-
return "unknown_resp";
247-
248-
case SRS_REGISTERED:
249-
return "registered";
293+
return QJsonValue ( QString::fromStdString ( found->second ) );
294+
}
250295

251-
case SRS_SERVER_LIST_FULL:
252-
return "directory_server_full"; // TODO - rename to "server_list_full"
296+
const std::unordered_map<std::string, EDirectoryType> CServerRpc::sumStringToDirectoryType = {
297+
{ "none", EDirectoryType::AT_NONE },
298+
{ "any_genre_1", EDirectoryType::AT_DEFAULT },
299+
{ "any_genre_2", EDirectoryType::AT_ANY_GENRE2 },
300+
{ "any_genre_3", EDirectoryType::AT_ANY_GENRE3 },
301+
{ "genre_rock", EDirectoryType::AT_GENRE_ROCK },
302+
{ "genre_jazz", EDirectoryType::AT_GENRE_JAZZ },
303+
{ "genre_classical_folk", EDirectoryType::AT_GENRE_CLASSICAL_FOLK },
304+
{ "genre_choral_barbershop", EDirectoryType::AT_GENRE_CHORAL },
305+
{ "custom", EDirectoryType::AT_CUSTOM },
306+
};
307+
308+
inline EDirectoryType CServerRpc::DeserializeDirectoryType ( std::string sAddrType )
309+
{
310+
auto found = sumStringToDirectoryType.find ( sAddrType );
311+
if ( found == sumStringToDirectoryType.end() )
312+
return AT_DEFAULT;
253313

254-
case SRS_VERSION_TOO_OLD:
255-
return "server_version_too_old";
314+
return found->second;
315+
}
256316

257-
case SRS_NOT_FULFILL_REQUIREMENTS:
258-
return "requirements_not_fulfilled";
259-
}
317+
const std::unordered_map<ESvrRegStatus, std::string> CServerRpc::sumSvrRegStatusToString = {
318+
{ SRS_NOT_REGISTERED, "not_registered" },
319+
{ SRS_BAD_ADDRESS, "bad_address" },
320+
{ SRS_REQUESTED, "requested" },
321+
{ SRS_TIME_OUT, "time_out" },
322+
{ SRS_UNKNOWN_RESP, "unknown_resp" },
323+
{ SRS_REGISTERED, "registered" },
324+
{ SRS_SERVER_LIST_FULL, "directory_server_full" }, // TODO: rename to "server_list_full"
325+
{ SRS_VERSION_TOO_OLD, "server_version_too_old" },
326+
{ SRS_NOT_FULFILL_REQUIREMENTS, "requirements_not_fulfilled" } };
327+
328+
inline QJsonValue CServerRpc::SerializeRegistrationStatus ( ESvrRegStatus eSvrRegStatus )
329+
{
330+
auto found = sumSvrRegStatusToString.find ( eSvrRegStatus );
331+
if ( found == sumSvrRegStatusToString.end() )
332+
return QJsonValue ( QString ( "unknown(%1)" ).arg ( eSvrRegStatus ) );
260333

261-
return QString ( "unknown(%1)" ).arg ( eSvrRegStatus );
334+
return QJsonValue ( QString::fromStdString ( found->second ) );
262335
}

src/serverrpc.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#pragma once
2727

28+
#include <unordered_map>
2829
#include "server.h"
2930
#include "rpcserver.h"
3031

@@ -35,5 +36,12 @@ class CServerRpc : public QObject
3536

3637
public:
3738
CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* parent = nullptr );
38-
static QJsonValue SerializeRegistrationStatus ( ESvrRegStatus eSvrRegStatus );
39+
40+
const static std::unordered_map<EDirectoryType, std::string> sumDirectoryTypeToString;
41+
const static std::unordered_map<std::string, EDirectoryType> sumStringToDirectoryType;
42+
const static std::unordered_map<ESvrRegStatus, std::string> sumSvrRegStatusToString;
43+
44+
QJsonValue SerializeDirectoryType ( EDirectoryType eAddrType );
45+
EDirectoryType DeserializeDirectoryType ( std::string sAddrType );
46+
QJsonValue SerializeRegistrationStatus ( ESvrRegStatus eSvrRegStatus );
3947
};

0 commit comments

Comments
 (0)