Copyright 2014-2016 CyberVision, Inc.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Usage examples
Retrieving current configuration
#include <kaa/common/types/ICommonRecord.hpp>
using namespace kaa;
...
ICommonRecord & rootConfiguration = Kaa::getKaaClient().getConfigurationManager().getConfiguration();
...
Subscribing for configuration updates
using namespace kaa;
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";
if (configuration.hasField(fieldName)) {
std::shared_ptr<ICommonValue> fooField = configuration.getField(fieldName);
if (CommonValueTools::isBoolean(fooField)) {
bool fooValue = boost::any_cast<bool>(fooField->getValue());
}
}
try {
bool fooValue = boost::any_cast<bool>(configuration.getField(fieldName)->getValue());
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";
if (configuration.hasField(fieldName)) {
std::shared_ptr<ICommonValue> fooField = configuration.getField(fieldName);
if (CommonValueTools::isRecord(fooField)) {
const ICommonRecord & fooRecord = CommonValueTools::getRecord(fooField);
}
}
try {
const ICommonRecord & fooRecord = CommonValueTools::getRecord(configuration.getField(fieldName))
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";
if (configuration.hasField(fieldName)) {
std::shared_ptr<ICommonValue> fooField = configuration.getField(fieldName);
if (CommonValueTools::isArray(fooField)) {
auto fooArray = CommonValueTools::getArray(fooField);
}
}
try {
auto fooArray = CommonValueTools::getArray(configuration.getField(fieldName))
std::cerr <<
"Failed to get array 'fooArray': "<< e.
what() << std::endl;
}
Setting configuration persistent storage
#include <vector>
using namespace kaa;
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/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);