This repository was archived by the owner on Jan 19, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathserverrpc.cpp
More file actions
262 lines (228 loc) · 11.7 KB
/
Copy pathserverrpc.cpp
File metadata and controls
262 lines (228 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/******************************************************************************\
* Copyright (c) 2021-2024
*
* Author(s):
* dtinth
* Christian Hoffmann
*
******************************************************************************
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*
\******************************************************************************/
#include "serverrpc.h"
CServerRpc::CServerRpc ( CServer* pServer, CRpcServer* pRpcServer, QObject* parent ) : QObject ( parent )
{
// API doc already part of CClientRpc
pRpcServer->HandleMethod ( "jamulus/getMode", [=] ( const QJsonObject& params, QJsonObject& response ) {
QJsonObject result{ { "mode", "server" } };
response["result"] = result;
Q_UNUSED ( params );
} );
/// @rpc_method jamulusserver/getRecorderStatus
/// @brief Returns the recorder state.
/// @param {object} params - No parameters (empty object).
/// @result {boolean} result.initialised - True if the recorder is initialised.
/// @result {string} result.errorMessage - The recorder error message, if any.
/// @result {boolean} result.enabled - True if the recorder is enabled.
/// @result {string} result.recordingDirectory - The recorder recording directory.
pRpcServer->HandleMethod ( "jamulusserver/getRecorderStatus", [=] ( const QJsonObject& params, QJsonObject& response ) {
QJsonObject result{
{ "initialised", pServer->GetRecorderInitialised() },
{ "errorMessage", pServer->GetRecorderErrMsg() },
{ "enabled", pServer->GetRecordingEnabled() },
{ "recordingDirectory", pServer->GetRecordingDir() },
};
response["result"] = result;
Q_UNUSED ( params );
} );
/// @rpc_method jamulusserver/getClients
/// @brief Returns the list of connected clients along with details about them.
/// @param {object} params - No parameters (empty object).
/// @result {number} result.connections - The number of active connections.
/// @result {array} result.clients - The list of connected clients.
/// @result {number} result.clients[*].id - The client’s channel id.
/// @result {string} result.clients[*].address - The client’s address (ip:port).
/// @result {string} result.clients[*].name - The client’s name.
/// @result {number} result.clients[*].jitterBufferSize - The client’s jitter buffer size.
/// @result {number} result.clients[*].channels - The number of audio channels of the client.
/// @result {number} result.clients[*].instrumentCode - The id of the instrument for this channel.
/// @result {string} result.clients[*].city - The city name provided by the user for this channel.
/// @result {number} result.clients[*].countryName - The text name of the country specified by the user for this channel (see QLocale::Country).
/// @result {number} result.clients[*].skillLevelCode - The skill level id provided by the user for this channel.
pRpcServer->HandleMethod ( "jamulusserver/getClients", [=] ( const QJsonObject& params, QJsonObject& response ) {
QJsonArray clients;
CVector<CHostAddress> vecHostAddresses;
CVector<QString> vecsName;
CVector<int> veciJitBufNumFrames;
CVector<int> veciNetwFrameSizeFact;
CVector<CChannelCoreInfo> vecChanInfo;
int connections = 0;
pServer->GetConCliParam ( vecHostAddresses, vecsName, veciJitBufNumFrames, veciNetwFrameSizeFact, vecChanInfo );
// we assume that all vectors have the same length
const int iNumChannels = vecHostAddresses.Size();
// fill list with connected clients
for ( int i = 0; i < iNumChannels; i++ )
{
if ( vecHostAddresses[i].InetAddr == QHostAddress ( static_cast<quint32> ( 0 ) ) )
{
continue;
}
QJsonObject client{
{ "id", i },
{ "address", vecHostAddresses[i].toString ( CHostAddress::SM_IP_PORT ) },
{ "name", vecsName[i] },
{ "jitterBufferSize", veciJitBufNumFrames[i] },
{ "channels", pServer->GetClientNumAudioChannels ( i ) },
{ "instrumentCode", vecChanInfo[i].iInstrument },
{ "city", vecChanInfo[i].strCity },
{ "countryName", QLocale::countryToString ( vecChanInfo[i].eCountry ) },
{ "skillLevelCode", vecChanInfo[i].eSkillLevel },
};
clients.append ( client );
++connections;
}
// create result object
QJsonObject result{
{ "connections", connections },
{ "clients", clients },
};
response["result"] = result;
Q_UNUSED ( params );
} );
/// @rpc_method jamulusserver/getServerProfile
/// @brief Returns the server registration profile and status.
/// @param {object} params - No parameters (empty object).
/// @result {string} result.name - The server name.
/// @result {string} result.city - The server city.
/// @result {number} result.countryId - The server country ID (see QLocale::Country).
/// @result {string} result.welcomeMessage - The server welcome message.
/// @result {string} result.directory - The directory with which this server requested registration, or blank if none.
/// @result {string} result.registrationStatus - The server registration status as string (see ESvrRegStatus and SerializeRegistrationStatus).
pRpcServer->HandleMethod ( "jamulusserver/getServerProfile", [=] ( const QJsonObject& params, QJsonObject& response ) {
QString dsName = "";
if ( AT_NONE != pServer->GetDirectoryType() )
dsName = NetworkUtil::GetDirectoryAddress ( pServer->GetDirectoryType(), pServer->GetDirectoryAddress() );
QJsonObject result{
{ "name", pServer->GetServerName() },
{ "city", pServer->GetServerCity() },
{ "countryId", pServer->GetServerCountry() },
{ "welcomeMessage", pServer->GetWelcomeMessage() },
{ "directory", dsName },
{ "registrationStatus", SerializeRegistrationStatus ( pServer->GetSvrRegStatus() ) },
};
response["result"] = result;
Q_UNUSED ( params );
} );
/// @rpc_method jamulusserver/setServerName
/// @brief Sets the server name.
/// @param {string} params.serverName - The new server name.
/// @result {string} result - Always "ok".
pRpcServer->HandleMethod ( "jamulusserver/setServerName", [=] ( const QJsonObject& params, QJsonObject& response ) {
auto jsonServerName = params["serverName"];
if ( !jsonServerName.isString() )
{
response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: serverName is not a string" );
return;
}
pServer->SetServerName ( jsonServerName.toString() );
response["result"] = "ok";
} );
/// @rpc_method jamulusserver/setWelcomeMessage
/// @brief Sets the server welcome message.
/// @param {string} params.welcomeMessage - The new welcome message.
/// @result {string} result - Always "ok".
pRpcServer->HandleMethod ( "jamulusserver/setWelcomeMessage", [=] ( const QJsonObject& params, QJsonObject& response ) {
auto jsonWelcomeMessage = params["welcomeMessage"];
if ( !jsonWelcomeMessage.isString() )
{
response["error"] = CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: welcomeMessage is not a string" );
return;
}
pServer->SetWelcomeMessage ( jsonWelcomeMessage.toString() );
response["result"] = "ok";
} );
/// @rpc_method jamulusserver/setRecordingDirectory
/// @brief Sets the server recording directory.
/// @param {string} params.recordingDirectory - The new recording directory.
/// @result {string} result - Always "acknowledged".
/// To check if the directory was changed, call `jamulusserver/getRecorderStatus` again.
pRpcServer->HandleMethod ( "jamulusserver/setRecordingDirectory", [=] ( const QJsonObject& params, QJsonObject& response ) {
auto jsonRecordingDirectory = params["recordingDirectory"];
if ( !jsonRecordingDirectory.isString() )
{
response["error"] =
CRpcServer::CreateJsonRpcError ( CRpcServer::iErrInvalidParams, "Invalid params: recordingDirectory is not a string" );
return;
}
pServer->SetRecordingDir ( jsonRecordingDirectory.toString() );
response["result"] = "acknowledged";
} );
/// @rpc_method jamulusserver/startRecording
/// @brief Starts the server recording.
/// @param {object} params - No parameters (empty object).
/// @result {string} result - Always "acknowledged".
/// To check if the recording was enabled, call `jamulusserver/getRecorderStatus` again.
pRpcServer->HandleMethod ( "jamulusserver/startRecording", [=] ( const QJsonObject& params, QJsonObject& response ) {
pServer->SetEnableRecording ( true );
response["result"] = "acknowledged";
Q_UNUSED ( params );
} );
/// @rpc_method jamulusserver/stopRecording
/// @brief Stops the server recording.
/// @param {object} params - No parameters (empty object).
/// @result {string} result - Always "acknowledged".
/// To check if the recording was disabled, call `jamulusserver/getRecorderStatus` again.
pRpcServer->HandleMethod ( "jamulusserver/stopRecording", [=] ( const QJsonObject& params, QJsonObject& response ) {
pServer->SetEnableRecording ( false );
response["result"] = "acknowledged";
Q_UNUSED ( params );
} );
/// @rpc_method jamulusserver/restartRecording
/// @brief Restarts the recording into a new directory.
/// @param {object} params - No parameters (empty object).
/// @result {string} result - Always "acknowledged".
/// To check if the recording was restarted or if there is any error, call `jamulusserver/getRecorderStatus` again.
pRpcServer->HandleMethod ( "jamulusserver/restartRecording", [=] ( const QJsonObject& params, QJsonObject& response ) {
pServer->RequestNewRecording();
response["result"] = "acknowledged";
Q_UNUSED ( params );
} );
}
QJsonValue CServerRpc::SerializeRegistrationStatus ( ESvrRegStatus eSvrRegStatus )
{
switch ( eSvrRegStatus )
{
case SRS_NOT_REGISTERED:
return "not_registered";
case SRS_BAD_ADDRESS:
return "bad_address";
case SRS_REQUESTED:
return "requested";
case SRS_TIME_OUT:
return "time_out";
case SRS_UNKNOWN_RESP:
return "unknown_resp";
case SRS_REGISTERED:
return "registered";
case SRS_SERVER_LIST_FULL:
return "directory_server_full"; // TODO - rename to "server_list_full"
case SRS_VERSION_TOO_OLD:
return "server_version_too_old";
case SRS_NOT_FULFILL_REQUIREMENTS:
return "requirements_not_fulfilled";
}
return QString ( "unknown(%1)" ).arg ( eSvrRegStatus );
}