mirror of
https://github.com/badaix/snapcast.git
synced 2025-06-02 10:51:45 +02:00
config getServerStatus
This commit is contained in:
parent
5ba3301a4d
commit
ad568f1076
3 changed files with 71 additions and 58 deletions
|
@ -52,17 +52,12 @@ Config::Config()
|
||||||
if (j.count("ConfigVersion"))
|
if (j.count("ConfigVersion"))
|
||||||
{
|
{
|
||||||
json jClient = j["Client"];
|
json jClient = j["Client"];
|
||||||
for (json::iterator it = jClient.begin(); it != jClient.end(); ++it)
|
for (auto it = jClient.begin(); it != jClient.end(); ++it)
|
||||||
{
|
{
|
||||||
ClientInfoPtr client = make_shared<ClientInfo>();
|
ClientInfoPtr client = make_shared<ClientInfo>();
|
||||||
client->fromJson(*it);
|
client->fromJson(*it);
|
||||||
if (client->host.mac.empty())
|
if (client->clientId.empty() || getClientInfo(client->clientId))
|
||||||
continue;
|
continue;
|
||||||
for (const auto& c: clients)
|
|
||||||
{
|
|
||||||
if (c->host.mac == client->host.mac)
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
client->connected = false;
|
client->connected = false;
|
||||||
clients.push_back(client);
|
clients.push_back(client);
|
||||||
|
@ -77,6 +72,12 @@ Config::Config()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
Config::~Config()
|
||||||
|
{
|
||||||
|
save();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void Config::save()
|
void Config::save()
|
||||||
{
|
{
|
||||||
std::ofstream ofs(filename_.c_str(), std::ofstream::out|std::ofstream::trunc);
|
std::ofstream ofs(filename_.c_str(), std::ofstream::out|std::ofstream::trunc);
|
||||||
|
@ -89,7 +90,7 @@ void Config::save()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
ClientInfoPtr Config::getClientInfo(const std::string& clientId, bool add)
|
ClientInfoPtr Config::getClientInfo(const std::string& clientId) const
|
||||||
{
|
{
|
||||||
if (clientId.empty())
|
if (clientId.empty())
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
@ -100,16 +101,52 @@ ClientInfoPtr Config::getClientInfo(const std::string& clientId, bool add)
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!add)
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
ClientInfoPtr client = make_shared<ClientInfo>(clientId);
|
|
||||||
|
ClientInfoPtr Config::addClientInfo(const std::string& clientId)
|
||||||
|
{
|
||||||
|
ClientInfoPtr client = getClientInfo(clientId);
|
||||||
|
if (!client)
|
||||||
|
{
|
||||||
|
client = make_shared<ClientInfo>(clientId);
|
||||||
clients.push_back(client);
|
clients.push_back(client);
|
||||||
|
}
|
||||||
|
|
||||||
return client;
|
return client;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
json Config::getServerStatus(const std::string& clientId, const json& streams) const
|
||||||
|
{
|
||||||
|
json jClient = json::array();
|
||||||
|
if (clientId != "")
|
||||||
|
{
|
||||||
|
ClientInfoPtr client = getClientInfo(clientId);
|
||||||
|
if (client)
|
||||||
|
jClient += client->toJson();
|
||||||
|
}
|
||||||
|
else
|
||||||
|
jClient = getClientInfos();
|
||||||
|
|
||||||
|
Host host;
|
||||||
|
host.update();
|
||||||
|
//TODO: Set MAC and IP
|
||||||
|
Snapserver snapserver("Snapserver", VERSION);
|
||||||
|
json serverStatus = {
|
||||||
|
{"server", {
|
||||||
|
{"host", host.toJson()},//getHostName()},
|
||||||
|
{"snapserver", snapserver.toJson()}
|
||||||
|
}},
|
||||||
|
{"clients", jClient},
|
||||||
|
{"streams", streams}
|
||||||
|
};
|
||||||
|
|
||||||
|
return serverStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
json Config::getClientInfos() const
|
json Config::getClientInfos() const
|
||||||
{
|
{
|
||||||
json result = json::array();
|
json result = json::array();
|
||||||
|
|
|
@ -220,8 +220,8 @@ struct ClientInfo
|
||||||
|
|
||||||
void fromJson(const json& j)
|
void fromJson(const json& j)
|
||||||
{
|
{
|
||||||
clientId = jGet<std::string>(j, "id", j["host"]);
|
|
||||||
host.fromJson(j["host"]);
|
host.fromJson(j["host"]);
|
||||||
|
clientId = jGet<std::string>(j, "id", host.mac);
|
||||||
snapclient.fromJson(j["snapclient"]);
|
snapclient.fromJson(j["snapclient"]);
|
||||||
config.fromJson(j["config"]);
|
config.fromJson(j["config"]);
|
||||||
lastSeen.tv_sec = jGet<int32_t>(j["lastSeen"], "sec", 0);
|
lastSeen.tv_sec = jGet<int32_t>(j["lastSeen"], "sec", 0);
|
||||||
|
@ -262,16 +262,19 @@ public:
|
||||||
return instance_;
|
return instance_;
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientInfoPtr getClientInfo(const std::string& mac, bool add = true);
|
ClientInfoPtr getClientInfo(const std::string& clientId) const;
|
||||||
|
ClientInfoPtr addClientInfo(const std::string& clientId);
|
||||||
void remove(ClientInfoPtr client);
|
void remove(ClientInfoPtr client);
|
||||||
|
|
||||||
std::vector<ClientInfoPtr> clients;
|
std::vector<ClientInfoPtr> clients;
|
||||||
json getClientInfos() const;
|
json getClientInfos() const;
|
||||||
|
json getServerStatus(const std::string& clientId, const json& streams) const;
|
||||||
|
|
||||||
void save();
|
void save();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Config();
|
Config();
|
||||||
|
~Config();
|
||||||
std::string filename_;
|
std::string filename_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -80,7 +80,6 @@ void StreamServer::onDisconnect(StreamSession* streamSession)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
logO << "onDisconnect: " << session->clientId << "\n";
|
logO << "onDisconnect: " << session->clientId << "\n";
|
||||||
ClientInfoPtr clientInfo = Config::instance().getClientInfo(streamSession->clientId);
|
|
||||||
logD << "sessions: " << sessions_.size() << "\n";
|
logD << "sessions: " << sessions_.size() << "\n";
|
||||||
// don't block: remove StreamSession in a thread
|
// don't block: remove StreamSession in a thread
|
||||||
auto func = [](shared_ptr<StreamSession> s)->void{s->stop();};
|
auto func = [](shared_ptr<StreamSession> s)->void{s->stop();};
|
||||||
|
@ -91,6 +90,7 @@ void StreamServer::onDisconnect(StreamSession* streamSession)
|
||||||
logD << "sessions: " << sessions_.size() << "\n";
|
logD << "sessions: " << sessions_.size() << "\n";
|
||||||
|
|
||||||
// notify controllers if not yet done
|
// notify controllers if not yet done
|
||||||
|
ClientInfoPtr clientInfo = Config::instance().getClientInfo(streamSession->clientId);
|
||||||
if (!clientInfo || !clientInfo->connected)
|
if (!clientInfo || !clientInfo->connected)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -118,42 +118,22 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std::
|
||||||
msg::ServerSettings serverSettings;
|
msg::ServerSettings serverSettings;
|
||||||
serverSettings.setBufferMs(settings_.bufferMs);
|
serverSettings.setBufferMs(settings_.bufferMs);
|
||||||
|
|
||||||
if (request.method.find("Client.Set") == string::npos)
|
if (request.method.find("Client.Set") == 0)
|
||||||
{
|
{
|
||||||
clientInfo = Config::instance().getClientInfo(request.getParam("client").get<string>(), false);
|
clientInfo = Config::instance().getClientInfo(request.getParam("client").get<string>());
|
||||||
if (clientInfo == nullptr)
|
if (clientInfo == nullptr)
|
||||||
throw JsonInternalErrorException("Client not found", request.id);
|
throw JsonInternalErrorException("Client not found", request.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (request.method == "Server.GetStatus")
|
if (request.method == "Server.GetStatus")
|
||||||
{
|
{
|
||||||
json jClient = json::array();
|
string clientId = request.hasParam("client") ? request.getParam("client").get<string>() : "";
|
||||||
if (request.hasParam("client"))
|
response = Config::instance().getServerStatus(clientId, streamManager_->toJson());
|
||||||
{
|
// logO << response.dump(4);
|
||||||
ClientInfoPtr client = Config::instance().getClientInfo(request.getParam("client").get<string>(), false);
|
|
||||||
if (client)
|
|
||||||
jClient += client->toJson();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
jClient = Config::instance().getClientInfos();
|
|
||||||
|
|
||||||
Host host;
|
|
||||||
host.update();
|
|
||||||
//TODO: Set MAC and IP
|
|
||||||
Snapserver snapserver("Snapserver", VERSION);
|
|
||||||
response = {
|
|
||||||
{"server", {
|
|
||||||
{"host", host.toJson()},//getHostName()},
|
|
||||||
{"snapserver", snapserver.toJson()}
|
|
||||||
}},
|
|
||||||
{"clients", jClient},
|
|
||||||
{"streams", streamManager_->toJson()}
|
|
||||||
};
|
|
||||||
// cout << response.dump(4);
|
|
||||||
}
|
}
|
||||||
else if (request.method == "Server.DeleteClient")
|
else if (request.method == "Server.DeleteClient")
|
||||||
{
|
{
|
||||||
clientInfo = Config::instance().getClientInfo(request.getParam("client").get<string>(), false);
|
clientInfo = Config::instance().getClientInfo(request.getParam("client").get<string>());
|
||||||
if (clientInfo == nullptr)
|
if (clientInfo == nullptr)
|
||||||
throw JsonInternalErrorException("Client not found", request.id);
|
throw JsonInternalErrorException("Client not found", request.id);
|
||||||
response = clientInfo->host.mac;
|
response = clientInfo->host.mac;
|
||||||
|
@ -264,24 +244,17 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
|
||||||
|
|
||||||
logD << "request kServerSettings: " << connection->clientId << "\n";
|
logD << "request kServerSettings: " << connection->clientId << "\n";
|
||||||
// std::lock_guard<std::mutex> mlock(mutex_);
|
// std::lock_guard<std::mutex> mlock(mutex_);
|
||||||
ClientInfoPtr clientInfo = Config::instance().getClientInfo(connection->clientId, true);
|
ClientInfoPtr client = Config::instance().addClientInfo(connection->clientId);
|
||||||
if (clientInfo == nullptr)
|
|
||||||
{
|
|
||||||
logE << "could not get client info for client: " << connection->clientId << "\n";
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
logD << "request kServerSettings\n";
|
logD << "request kServerSettings\n";
|
||||||
msg::ServerSettings* serverSettings = new msg::ServerSettings();
|
msg::ServerSettings* serverSettings = new msg::ServerSettings();
|
||||||
serverSettings->setVolume(clientInfo->config.volume.percent);
|
serverSettings->setVolume(client->config.volume.percent);
|
||||||
serverSettings->setMuted(clientInfo->config.volume.muted);
|
serverSettings->setMuted(client->config.volume.muted);
|
||||||
serverSettings->setLatency(clientInfo->config.latency);
|
serverSettings->setLatency(client->config.latency);
|
||||||
serverSettings->setBufferMs(settings_.bufferMs);
|
serverSettings->setBufferMs(settings_.bufferMs);
|
||||||
serverSettings->refersTo = helloMsg.id;
|
serverSettings->refersTo = helloMsg.id;
|
||||||
connection->sendAsync(serverSettings);
|
connection->sendAsync(serverSettings);
|
||||||
}
|
|
||||||
|
|
||||||
ClientInfoPtr client = Config::instance().getClientInfo(connection->clientId);
|
|
||||||
client->host.mac = helloMsg.getMacAddress();
|
client->host.mac = helloMsg.getMacAddress();
|
||||||
client->host.ip = connection->getIP();
|
client->host.ip = connection->getIP();
|
||||||
client->host.name = helloMsg.getHostName();
|
client->host.name = helloMsg.getHostName();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue