config holds list of clients

This commit is contained in:
badaix 2015-09-01 22:19:46 +02:00
parent 3a0b856afe
commit 707f198a26
3 changed files with 56 additions and 14 deletions

View file

@ -34,15 +34,37 @@ void Config::test()
json j; json j;
ifs >> j; ifs >> j;
std::cout << std::setw(4) << j << std::endl; std::cout << std::setw(4) << j << std::endl;
json j1 = j["Client"];//["Hobbys"]; json j1 = j["Client"];
for (json::iterator it = j1.begin(); it != j1.end(); ++it) for (json::iterator it = j1.begin(); it != j1.end(); ++it)
{ {
// json j2 = *it;
// cout << "Key: " << j2.key() << ", value: " j2.value() << "\n";
ClientInfo client; ClientInfo client;
client.fromJson(*it); client.fromJson(*it);
std::cout << "Client:\n" << std::setw(4) << client.toJson() << '\n'; std::cout << "Client:\n" << std::setw(4) << client.toJson() << '\n';
} }
// std::cout << std::setw(4) << j["Inhaber"]["Hobbys2"] << std::endl;
} }
ClientInfoPtr Config::getClientInfo(const std::string& macAddress)
{
for (auto client: clients)
{
if (client->macAddress == macAddress)
return client;
}
ClientInfoPtr client = make_shared<ClientInfo>(macAddress);
clients.push_back(client);
return client;
}
json Config::getClientInfos() const
{
json result = json::array();
for (auto client: clients)
result.push_back(client->toJson());
return result;
}

View file

@ -20,6 +20,8 @@
#define CONFIG_H #define CONFIG_H
#include <string> #include <string>
#include <memory>
#include <vector>
#include <sys/time.h> #include <sys/time.h>
#include "json.hpp" #include "json.hpp"
@ -41,6 +43,12 @@ T jGet(json j, const std::string& what, const T& def)
struct ClientInfo struct ClientInfo
{ {
ClientInfo(const std::string& _macAddress = "") : macAddress(_macAddress), volume(1.0), connected(false)
{
lastSeen.tv_sec = 0;
lastSeen.tv_usec = 0;
}
void fromJson(const json& j) void fromJson(const json& j)
{ {
macAddress = jGet<std::string>(j, "MAC", ""); macAddress = jGet<std::string>(j, "MAC", "");
@ -61,7 +69,7 @@ struct ClientInfo
j["IP"] = ipAddress; j["IP"] = ipAddress;
j["host"] = hostName; j["host"] = hostName;
j["version"] = version; j["version"] = version;
j["name"] = name; j["name"] = getName();
j["volume"] = volume; j["volume"] = volume;
j["lastSeen"]["sec"] = lastSeen.tv_sec; j["lastSeen"]["sec"] = lastSeen.tv_sec;
j["lastSeen"]["usec"] = lastSeen.tv_usec; j["lastSeen"]["usec"] = lastSeen.tv_usec;
@ -69,6 +77,14 @@ struct ClientInfo
return j; return j;
} }
std::string getName()
{
if (name.empty())
return hostName;
else
return name;
}
std::string macAddress; std::string macAddress;
std::string ipAddress; std::string ipAddress;
std::string hostName; std::string hostName;
@ -79,6 +95,8 @@ struct ClientInfo
bool connected; bool connected;
}; };
typedef std::shared_ptr<ClientInfo> ClientInfoPtr;
class Config class Config
{ {
@ -90,11 +108,14 @@ public:
} }
void test(); void test();
ClientInfoPtr getClientInfo(const std::string& mac);
std::vector<ClientInfoPtr> clients;
json getClientInfos() const;
private: private:
Config(); Config();
}; };

View file

@ -25,6 +25,7 @@
#include "common/log.h" #include "common/log.h"
#include "common/snapException.h" #include "common/snapException.h"
#include "jsonrpc.h" #include "jsonrpc.h"
#include "config.h"
#include <iostream> #include <iostream>
using namespace std; using namespace std;
@ -107,11 +108,16 @@ void ControlServer::onMessageReceived(ControlSession* connection, const std::str
for (string s: request.params) for (string s: request.params)
logO << "param: " << s << "\n"; logO << "param: " << s << "\n";
json response;
if (request.method == "get") if (request.method == "get")
{ {
if (request.isParam(0, "status")) if (request.isParam(0, "status"))
{ {
response = {
{"server", "xxx"},
{"clients", Config::instance().getClientInfos()}
};
} }
else else
throw JsonInvalidParamsException(request); throw JsonInvalidParamsException(request);
@ -139,13 +145,6 @@ void ControlServer::onMessageReceived(ControlSession* connection, const std::str
else else
throw JsonMethodNotFoundException(request); throw JsonMethodNotFoundException(request);
json response = {
{"test", "123"},
{"error", {
{"code", 12},
{"message", true}
}}};
connection->send(request.getResponse(response).dump()); connection->send(request.getResponse(response).dump());
} }
catch (const JsonRequestException& e) catch (const JsonRequestException& e)