added Group entity

This commit is contained in:
badaix 2016-11-30 17:40:52 +01:00
parent db2b5e06a3
commit 7272be0d1b
3 changed files with 108 additions and 19 deletions

View file

@ -51,16 +51,14 @@ Config::Config()
ifs >> j; ifs >> j;
if (j.count("ConfigVersion")) if (j.count("ConfigVersion"))
{ {
json jClient = j["Client"]; json jGroups = j["Groups"];
for (auto it = jClient.begin(); it != jClient.end(); ++it) for (auto it = jGroups.begin(); it != jGroups.end(); ++it)
{ {
ClientInfoPtr client = make_shared<ClientInfo>(); GroupPtr group = make_shared<Group>();
client->fromJson(*it); group->fromJson(*it);
if (client->clientId.empty() || getClientInfo(client->clientId)) // if (client->clientId.empty() || getClientInfo(client->clientId))
continue; // continue;
groups.push_back(group);
client->connected = false;
clients.push_back(client);
} }
} }
} }
@ -82,8 +80,8 @@ 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);
json clients = { json clients = {
{"ConfigVersion", 1}, {"ConfigVersion", 2},
{"Client", getClientInfos()} {"Groups", getGroups()}
}; };
ofs << std::setw(4) << clients; ofs << std::setw(4) << clients;
ofs.close(); 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>();
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 Config::getServerStatus(const std::string& clientId, const json& streams) const
{ {
json jClient = json::array(); 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) 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()); clients.erase(std::remove(clients.begin(), clients.end(), client), clients.end());
} }

View file

@ -29,6 +29,13 @@
using json = nlohmann::json; using json = nlohmann::json;
struct ClientInfo;
struct Group;
typedef std::shared_ptr<ClientInfo> ClientInfoPtr;
typedef std::shared_ptr<Group> GroupPtr;
template<typename T> template<typename T>
T jGet(const json& j, const std::string& what, const T& def) T jGet(const json& j, const std::string& what, const T& def)
{ {
@ -114,7 +121,7 @@ struct Host
struct ClientConfig 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<std::string>(j, "name", "")); name = trim_copy(jGet<std::string>(j, "name", ""));
volume.fromJson(j["volume"]); volume.fromJson(j["volume"]);
latency = jGet<int32_t>(j, "latency", 0); latency = jGet<int32_t>(j, "latency", 0);
streamId = trim_copy(jGet<std::string>(j, "stream", ""));
instance = jGet<size_t>(j, "instance", 1); instance = jGet<size_t>(j, "instance", 1);
} }
@ -133,7 +139,6 @@ struct ClientConfig
j["name"] = trim_copy(name); j["name"] = trim_copy(name);
j["volume"] = volume.toJson(); j["volume"] = volume.toJson();
j["latency"] = latency; j["latency"] = latency;
j["stream"] = trim_copy(streamId);
j["instance"] = instance; j["instance"] = instance;
return j; return j;
} }
@ -141,7 +146,6 @@ struct ClientConfig
std::string name; std::string name;
Volume volume; Volume volume;
int32_t latency; int32_t latency;
std::string streamId;
size_t instance; size_t instance;
}; };
@ -250,7 +254,50 @@ struct ClientInfo
bool connected; bool connected;
}; };
typedef std::shared_ptr<ClientInfo> ClientInfoPtr;
struct Group
{
Group()
{
}
void fromJson(const json& j)
{
name = trim_copy(jGet<std::string>(j, "name", ""));
id = trim_copy(jGet<std::string>(j, "id", ""));
streamId = trim_copy(jGet<std::string>(j, "stream", ""));
clients.clear();
if (j.count("clients"))
{
for (auto& jClient : j["clients"])
{
ClientInfoPtr client = std::make_shared<ClientInfo>();
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<ClientInfoPtr> clients;
};
class Config class Config
@ -266,12 +313,17 @@ public:
ClientInfoPtr addClientInfo(const std::string& clientId); ClientInfoPtr addClientInfo(const std::string& clientId);
void remove(ClientInfoPtr client); void remove(ClientInfoPtr client);
std::vector<ClientInfoPtr> clients; GroupPtr getGroup(ClientInfoPtr client);
json getClientInfos() const; json getClientInfos() const;
json getGroups() const;
json getServerStatus(const std::string& clientId, const json& streams) const; json getServerStatus(const std::string& clientId, const json& streams) const;
void save(); void save();
std::vector<GroupPtr> groups;
std::vector<ClientInfoPtr> clients;
private: private:
Config(); Config();
~Config(); ~Config();

View file

@ -155,6 +155,7 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std::
} }
else if (request.method == "Client.SetStream") else if (request.method == "Client.SetStream")
{ {
/* TODO: Group.SetStream
string streamId = request.getParam("id").get<string>(); string streamId = request.getParam("id").get<string>();
PcmStreamPtr stream = streamManager_->getStream(streamId); PcmStreamPtr stream = streamManager_->getStream(streamId);
if (stream == nullptr) if (stream == nullptr)
@ -169,6 +170,7 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std::
session->sendAsync(stream->getHeader()); session->sendAsync(stream->getHeader());
session->setPcmStream(stream); session->setPcmStream(stream);
} }
*/
} }
else if (request.method == "Client.SetLatency") else if (request.method == "Client.SetLatency")
{ {
@ -246,6 +248,8 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
// std::lock_guard<std::mutex> mlock(mutex_); // std::lock_guard<std::mutex> mlock(mutex_);
ClientInfoPtr client = Config::instance().addClientInfo(connection->clientId); ClientInfoPtr client = Config::instance().addClientInfo(connection->clientId);
GroupPtr group = Config::instance().getGroup(client);
logD << "request kServerSettings\n"; logD << "request kServerSettings\n";
msg::ServerSettings* serverSettings = new msg::ServerSettings(); msg::ServerSettings* serverSettings = new msg::ServerSettings();
serverSettings->setVolume(client->config.volume.percent); serverSettings->setVolume(client->config.volume.percent);
@ -268,12 +272,14 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
gettimeofday(&client->lastSeen, NULL); gettimeofday(&client->lastSeen, NULL);
// Assign and update stream // Assign and update stream
PcmStreamPtr stream = streamManager_->getStream(client->config.streamId); PcmStreamPtr stream = streamManager_->getStream(group->streamId);
if (!stream) if (!stream)
{ {
stream = streamManager_->getDefaultStream(); stream = streamManager_->getDefaultStream();
client->config.streamId = stream->getId(); group->streamId = stream->getId();
} }
logO << "Group: " << group->id << ", stream: " << group->streamId << "\n";
Config::instance().save(); Config::instance().save();
connection->setPcmStream(stream); connection->setPcmStream(stream);