mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-31 18:06:15 +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"))
|
||||
{
|
||||
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>();
|
||||
client->fromJson(*it);
|
||||
if (client->host.mac.empty())
|
||||
if (client->clientId.empty() || getClientInfo(client->clientId))
|
||||
continue;
|
||||
for (const auto& c: clients)
|
||||
{
|
||||
if (c->host.mac == client->host.mac)
|
||||
continue;
|
||||
}
|
||||
|
||||
client->connected = false;
|
||||
clients.push_back(client);
|
||||
|
@ -77,6 +72,12 @@ Config::Config()
|
|||
}
|
||||
|
||||
|
||||
Config::~Config()
|
||||
{
|
||||
save();
|
||||
}
|
||||
|
||||
|
||||
void Config::save()
|
||||
{
|
||||
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())
|
||||
return nullptr;
|
||||
|
@ -100,16 +101,52 @@ ClientInfoPtr Config::getClientInfo(const std::string& clientId, bool add)
|
|||
return client;
|
||||
}
|
||||
|
||||
if (!add)
|
||||
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);
|
||||
}
|
||||
|
||||
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 result = json::array();
|
||||
|
|
|
@ -220,8 +220,8 @@ struct ClientInfo
|
|||
|
||||
void fromJson(const json& j)
|
||||
{
|
||||
clientId = jGet<std::string>(j, "id", j["host"]);
|
||||
host.fromJson(j["host"]);
|
||||
clientId = jGet<std::string>(j, "id", host.mac);
|
||||
snapclient.fromJson(j["snapclient"]);
|
||||
config.fromJson(j["config"]);
|
||||
lastSeen.tv_sec = jGet<int32_t>(j["lastSeen"], "sec", 0);
|
||||
|
@ -262,16 +262,19 @@ public:
|
|||
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);
|
||||
|
||||
std::vector<ClientInfoPtr> clients;
|
||||
json getClientInfos() const;
|
||||
json getServerStatus(const std::string& clientId, const json& streams) const;
|
||||
|
||||
void save();
|
||||
|
||||
private:
|
||||
Config();
|
||||
~Config();
|
||||
std::string filename_;
|
||||
};
|
||||
|
||||
|
|
|
@ -80,7 +80,6 @@ void StreamServer::onDisconnect(StreamSession* streamSession)
|
|||
return;
|
||||
|
||||
logO << "onDisconnect: " << session->clientId << "\n";
|
||||
ClientInfoPtr clientInfo = Config::instance().getClientInfo(streamSession->clientId);
|
||||
logD << "sessions: " << sessions_.size() << "\n";
|
||||
// don't block: remove StreamSession in a thread
|
||||
auto func = [](shared_ptr<StreamSession> s)->void{s->stop();};
|
||||
|
@ -91,6 +90,7 @@ void StreamServer::onDisconnect(StreamSession* streamSession)
|
|||
logD << "sessions: " << sessions_.size() << "\n";
|
||||
|
||||
// notify controllers if not yet done
|
||||
ClientInfoPtr clientInfo = Config::instance().getClientInfo(streamSession->clientId);
|
||||
if (!clientInfo || !clientInfo->connected)
|
||||
return;
|
||||
|
||||
|
@ -118,42 +118,22 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std::
|
|||
msg::ServerSettings serverSettings;
|
||||
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)
|
||||
throw JsonInternalErrorException("Client not found", request.id);
|
||||
}
|
||||
|
||||
if (request.method == "Server.GetStatus")
|
||||
{
|
||||
json jClient = json::array();
|
||||
if (request.hasParam("client"))
|
||||
{
|
||||
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);
|
||||
string clientId = request.hasParam("client") ? request.getParam("client").get<string>() : "";
|
||||
response = Config::instance().getServerStatus(clientId, streamManager_->toJson());
|
||||
// logO << response.dump(4);
|
||||
}
|
||||
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)
|
||||
throw JsonInternalErrorException("Client not found", request.id);
|
||||
response = clientInfo->host.mac;
|
||||
|
@ -264,24 +244,17 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
|
|||
|
||||
logD << "request kServerSettings: " << connection->clientId << "\n";
|
||||
// std::lock_guard<std::mutex> mlock(mutex_);
|
||||
ClientInfoPtr clientInfo = Config::instance().getClientInfo(connection->clientId, true);
|
||||
if (clientInfo == nullptr)
|
||||
{
|
||||
logE << "could not get client info for client: " << connection->clientId << "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
ClientInfoPtr client = Config::instance().addClientInfo(connection->clientId);
|
||||
|
||||
logD << "request kServerSettings\n";
|
||||
msg::ServerSettings* serverSettings = new msg::ServerSettings();
|
||||
serverSettings->setVolume(clientInfo->config.volume.percent);
|
||||
serverSettings->setMuted(clientInfo->config.volume.muted);
|
||||
serverSettings->setLatency(clientInfo->config.latency);
|
||||
serverSettings->setVolume(client->config.volume.percent);
|
||||
serverSettings->setMuted(client->config.volume.muted);
|
||||
serverSettings->setLatency(client->config.latency);
|
||||
serverSettings->setBufferMs(settings_.bufferMs);
|
||||
serverSettings->refersTo = helloMsg.id;
|
||||
connection->sendAsync(serverSettings);
|
||||
}
|
||||
|
||||
ClientInfoPtr client = Config::instance().getClientInfo(connection->clientId);
|
||||
client->host.mac = helloMsg.getMacAddress();
|
||||
client->host.ip = connection->getIP();
|
||||
client->host.name = helloMsg.getHostName();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue