From ad568f10761c96280ba7b8614846c6c70b4ef42f Mon Sep 17 00:00:00 2001 From: badaix Date: Mon, 28 Nov 2016 09:47:03 +0100 Subject: [PATCH] config getServerStatus --- server/config.cpp | 61 +++++++++++++++++++++++++++++++++-------- server/config.h | 7 +++-- server/streamServer.cpp | 61 ++++++++++++----------------------------- 3 files changed, 71 insertions(+), 58 deletions(-) diff --git a/server/config.cpp b/server/config.cpp index f83c6424..cfa2606b 100644 --- a/server/config.cpp +++ b/server/config.cpp @@ -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(); 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; + return nullptr; +} - ClientInfoPtr client = make_shared(clientId); - clients.push_back(client); + +ClientInfoPtr Config::addClientInfo(const std::string& clientId) +{ + ClientInfoPtr client = getClientInfo(clientId); + if (!client) + { + client = make_shared(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(); diff --git a/server/config.h b/server/config.h index 862a4052..434e2bc6 100644 --- a/server/config.h +++ b/server/config.h @@ -220,8 +220,8 @@ struct ClientInfo void fromJson(const json& j) { - clientId = jGet(j, "id", j["host"]); host.fromJson(j["host"]); + clientId = jGet(j, "id", host.mac); snapclient.fromJson(j["snapclient"]); config.fromJson(j["config"]); lastSeen.tv_sec = jGet(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 clients; json getClientInfos() const; + json getServerStatus(const std::string& clientId, const json& streams) const; void save(); private: Config(); + ~Config(); std::string filename_; }; diff --git a/server/streamServer.cpp b/server/streamServer.cpp index 55fe0b2d..41aeb6bb 100644 --- a/server/streamServer.cpp +++ b/server/streamServer.cpp @@ -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 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(), false); + clientInfo = Config::instance().getClientInfo(request.getParam("client").get()); 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(), 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() : ""; + 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(), false); + clientInfo = Config::instance().getClientInfo(request.getParam("client").get()); 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 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 - { - 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->setBufferMs(settings_.bufferMs); - serverSettings->refersTo = helloMsg.id; - connection->sendAsync(serverSettings); - } + ClientInfoPtr client = Config::instance().addClientInfo(connection->clientId); + + logD << "request kServerSettings\n"; + msg::ServerSettings* serverSettings = new msg::ServerSettings(); + 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();