diff --git a/server/config.cpp b/server/config.cpp index cfa2606b..9cd1e4da 100644 --- a/server/config.cpp +++ b/server/config.cpp @@ -51,16 +51,14 @@ Config::Config() ifs >> j; if (j.count("ConfigVersion")) { - json jClient = j["Client"]; - for (auto it = jClient.begin(); it != jClient.end(); ++it) + json jGroups = j["Groups"]; + for (auto it = jGroups.begin(); it != jGroups.end(); ++it) { - ClientInfoPtr client = make_shared(); - client->fromJson(*it); - if (client->clientId.empty() || getClientInfo(client->clientId)) - continue; - - client->connected = false; - clients.push_back(client); + GroupPtr group = make_shared(); + group->fromJson(*it); +// if (client->clientId.empty() || getClientInfo(client->clientId)) +// continue; + groups.push_back(group); } } } @@ -82,8 +80,8 @@ void Config::save() { std::ofstream ofs(filename_.c_str(), std::ofstream::out|std::ofstream::trunc); json clients = { - {"ConfigVersion", 1}, - {"Client", getClientInfos()} + {"ConfigVersion", 2}, + {"Groups", getGroups()} }; ofs << std::setw(4) << clients; ofs.close(); @@ -118,6 +116,26 @@ ClientInfoPtr Config::addClientInfo(const std::string& clientId) } +GroupPtr Config::getGroup(ClientInfoPtr client) +{ + for (auto group: groups) + { + for (auto c: group->clients) + { + if (c->clientId == client->clientId) + return group; + } + } + + GroupPtr group = std::make_shared(); + group->id = generateUUID(); + group->clients.push_back(client); + groups.push_back(group); + + return group; +} + + json Config::getServerStatus(const std::string& clientId, const json& streams) const { json jClient = json::array(); @@ -156,8 +174,21 @@ json Config::getClientInfos() const } +json Config::getGroups() const +{ + json result = json::array(); + for (auto group: groups) + result.push_back(group->toJson()); + return result; +} + + void Config::remove(ClientInfoPtr client) { + auto group = getGroup(client); + if (group->clients.size() == 1) + groups.erase(std::remove(groups.begin(), groups.end(), group), groups.end()); + clients.erase(std::remove(clients.begin(), clients.end(), client), clients.end()); } diff --git a/server/config.h b/server/config.h index 434e2bc6..4ab38aef 100644 --- a/server/config.h +++ b/server/config.h @@ -29,6 +29,13 @@ using json = nlohmann::json; +struct ClientInfo; +struct Group; + +typedef std::shared_ptr ClientInfoPtr; +typedef std::shared_ptr GroupPtr; + + template T jGet(const json& j, const std::string& what, const T& def) { @@ -114,7 +121,7 @@ struct Host struct ClientConfig { - ClientConfig() : name(""), volume(100), latency(0), streamId(""), instance(1) + ClientConfig() : name(""), volume(100), latency(0), instance(1) { } @@ -123,7 +130,6 @@ struct ClientConfig name = trim_copy(jGet(j, "name", "")); volume.fromJson(j["volume"]); latency = jGet(j, "latency", 0); - streamId = trim_copy(jGet(j, "stream", "")); instance = jGet(j, "instance", 1); } @@ -133,7 +139,6 @@ struct ClientConfig j["name"] = trim_copy(name); j["volume"] = volume.toJson(); j["latency"] = latency; - j["stream"] = trim_copy(streamId); j["instance"] = instance; return j; } @@ -141,7 +146,6 @@ struct ClientConfig std::string name; Volume volume; int32_t latency; - std::string streamId; size_t instance; }; @@ -250,7 +254,50 @@ struct ClientInfo bool connected; }; -typedef std::shared_ptr ClientInfoPtr; + +struct Group +{ + Group() + { + } + + void fromJson(const json& j) + { + name = trim_copy(jGet(j, "name", "")); + id = trim_copy(jGet(j, "id", "")); + streamId = trim_copy(jGet(j, "stream", "")); + clients.clear(); + if (j.count("clients")) + { + for (auto& jClient : j["clients"]) + { + ClientInfoPtr client = std::make_shared(); + client->fromJson(jClient); + client->connected = false; + clients.push_back(client); + } + } + } + + json toJson() + { + json j; + j["name"] = trim_copy(name); + j["id"] = trim_copy(id); + j["stream"] = trim_copy(streamId); + + json jClients = json::array(); + for (auto client: clients) + jClients.push_back(client->toJson()); + j["clients"] = jClients; + return j; + } + + std::string name; + std::string id; + std::string streamId; + std::vector clients; +}; class Config @@ -266,12 +313,17 @@ public: ClientInfoPtr addClientInfo(const std::string& clientId); void remove(ClientInfoPtr client); - std::vector clients; + GroupPtr getGroup(ClientInfoPtr client); + json getClientInfos() const; + json getGroups() const; json getServerStatus(const std::string& clientId, const json& streams) const; void save(); + std::vector groups; + std::vector clients; + private: Config(); ~Config(); diff --git a/server/streamServer.cpp b/server/streamServer.cpp index 41aeb6bb..e7f5445f 100644 --- a/server/streamServer.cpp +++ b/server/streamServer.cpp @@ -155,6 +155,7 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std:: } else if (request.method == "Client.SetStream") { +/* TODO: Group.SetStream string streamId = request.getParam("id").get(); PcmStreamPtr stream = streamManager_->getStream(streamId); if (stream == nullptr) @@ -169,6 +170,7 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std:: session->sendAsync(stream->getHeader()); session->setPcmStream(stream); } +*/ } else if (request.method == "Client.SetLatency") { @@ -246,6 +248,8 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM // std::lock_guard mlock(mutex_); ClientInfoPtr client = Config::instance().addClientInfo(connection->clientId); + GroupPtr group = Config::instance().getGroup(client); + logD << "request kServerSettings\n"; msg::ServerSettings* serverSettings = new msg::ServerSettings(); serverSettings->setVolume(client->config.volume.percent); @@ -268,12 +272,14 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM gettimeofday(&client->lastSeen, NULL); // Assign and update stream - PcmStreamPtr stream = streamManager_->getStream(client->config.streamId); + PcmStreamPtr stream = streamManager_->getStream(group->streamId); if (!stream) { stream = streamManager_->getDefaultStream(); - client->config.streamId = stream->getId(); + group->streamId = stream->getId(); } + logO << "Group: " << group->id << ", stream: " << group->streamId << "\n"; + Config::instance().save(); connection->setPcmStream(stream);