client-cpp  0.10.0
Configuration

Usage

Retrieving current configuration

#include <kaa/Kaa.hpp>
#include <kaa/common/types/ICommonRecord.hpp>
using namespace kaa;
...
ICommonRecord & rootConfiguration = Kaa::getKaaClient().getConfigurationManager().getConfiguration();
...

Subscribing for configuration updates

#include <kaa/Kaa.hpp>
using namespace kaa;
class ConfigurationReceiver : public IConfigurationReceiver {
public:
void onConfigurationUpdated(const ICommonRecord &configuration) {
std::cout << "Configuration updated!" << std::endl;
}
};
...
IConfigurationReceiver * configurationReceiver = new ConfigurationReceiver();
Kaa::getKaaClient().getConfigurationManager().subscribeForConfigurationChanges(configurationReceiver);

Working with configuration objects

Configuration in C++ sdk is represented as tree of kaa::ICommonValue objects.
Use kaa::CommonValueTools helper class to extract values from ICommonValue object.

Extracting primitive from ICommonValue:

using namespace kaa;
ICommonRecord & configuration = Kaa::getKaaClient().getConfigurationManager().getConfiguration();
std::string fieldName = "foo";
// Variant 1: Check correctness of action to avoid exceptions:
// check if record contains field "foo"
if (configuration.hasField(fieldName)) {
std::shared_ptr<ICommonValue> fooField = configuration.getField(fieldName);
// Assuming that field "foo" is a boolean
if (CommonValueTools::isBoolean(fooField)) {
bool fooValue = boost::any_cast<bool>(fooField->getValue());
}
}
// Variant 2: Catch exceptions
// If field "foo" is not contained in record or field is not of boolean type library
// will throw an exception:
try {
bool fooValue = boost::any_cast<bool>(configuration.getField(fieldName)->getValue());
} catch (const KaaException &e) {
std::cerr << "'foo' field is not present in record. "<< e.what() << std::endl;
} catch (const boost::bad_cast &e) {
std::cerr << "'foo' field is not of boolean type. " << e.what() << std::endl;
}


Extracting data from ICommonRecord:

using namespace kaa;
ICommonRecord & configuration = Kaa::getKaaClient().getConfigurationManager().getConfiguration();
std::string fieldName = "foo";
// Variant 1: Check correctness of action to avoid exceptions
// check if record contains field "foo"
if (configuration.hasField(fieldName)) {
// if field "foo" is present in record get its value
std::shared_ptr<ICommonValue> fooField = configuration.getField(fieldName);
// Assuming that field "foo" is a record extract CommonRecord from ICommonValue
if (CommonValueTools::isRecord(fooField)) {
const ICommonRecord & fooRecord = CommonValueTools::getRecord(fooField);
// actions with fooRecord
}
}
// Variant 2: Catch exceptions
// If field "foo" is not contained in record or field is not of record type library
// will throw an exception:
try {
const ICommonRecord & fooRecord = CommonValueTools::getRecord(configuration.getField(fieldName))
} catch (const KaaException &e) {
std::cerr << "Failed to get record 'foo': "<< e.what() << std::endl;
}


Extracting array from ICommonValue:

using namespace kaa;
ICommonRecord & configuration = Kaa::getKaaClient().getConfigurationManager().getConfiguration();
std::string fieldName = "fooArray";
// Variant 1: Check correctness of action to avoid exceptions:
// check if record contains field "fooArray"
if (configuration.hasField(fieldName)) {
std::shared_ptr<ICommonValue> fooField = configuration.getField(fieldName);
if (CommonValueTools::isArray(fooField)) {
auto fooArray = CommonValueTools::getArray(fooField);
}
}
// Variant 2: Catch exceptions
// If field "fooArray" is not contained in record or field is not of array type library
// will throw an exception:
try {
auto fooArray = CommonValueTools::getArray(configuration.getField(fieldName))
} catch (const KaaException &e) {
std::cerr << "Failed to get array 'fooArray': "<< e.what() << std::endl;
}

Setting configuration persistent storage

#include <vector>
#include <kaa/Kaa.hpp>
using namespace kaa;
class MemoryConfigurationStorage : public IConfigurationStorage {
byte_buffer configurationBytes_;
public:
MemoryConfigurationStorage() {}
void saveConfiguration(const byte_buffer &bytes) {
configurationBytes_.assign(bytes.begin(), bytes.end());
}
byte_buffer loadConfiguration() {
return configurationBytes_;
}
};
...
IConfigurationStorage * storage = new MemoryConfigurationStorage();
Kaa::getKaaClient().getConfigurationPersistanceManager().setConfigurationStorage(storage);

Setting configuration schema persistent storage

#include <vector>
#include <kaa/Kaa.hpp>
#include <kaa/schema/storage/ISchemaStorage.hpp>
using namespace kaa;
class MemoryConfigurationSchemaStorage : public ISchemaStorage {
byte_buffer schemaBytes_;
public:
MemoryConfigurationSchemaStorage() {}
void saveSchema(const byte_buffer &data) {
schemaBytes_.assign(data.begin(), data.end());
}
byte_buffer loadSchema() {
return schemaBytes_;
}
};
...
ISchemaStorage * storage = new MemoryConfigurationSchemaStorage();
Kaa::getKaaClient().getSchemaPersistanceManager().setSchemaStorage(storage);