Initializing Libraries
Introduction to Libraries
This section guides you about how you should initiate,configure and unload Smarter AI platform libraries into your application.
The Initialization Procedure
In this section we shall focus on following libraries:
- Access Library
libacacces.so
is placed in lib folderAccess.h
is placed in include folder. It holds the APIs related to access operations.
- Stream Library
libstream.so
Stream.h
is placed in include folder. It holds the APIs related to stream operations.
- Onboard Library
libonoard.so
is placed in lib folderOnboardService.h
is placed in include folder. It holds the APIs related to onboarding operations.
Library: Onboard
Load library
#include <dlfcn.h>
std::string libPath("../../libonboard.so");
void* onboardHandle = dlopen(libPath.c_str(), RTLD_LAZY);
Create Onboard Instance
auto create = (com::anyconnect::onboard::OnboardService* (*)()) dlsym(onboardHandle, "create");
char* error = dlerror();
if (nullptr != error) {
// error found
}
com::anyconnect::onboard::OnboardService* onboardInstance = create();
if(onboardInstance) {
// instance created successfuly
} else {
// instance creation failed
}
Library: Access
Create Access Builder
AccessBuilder uses builder
design pattern to create an access instance. Let's define the access pointer and access builder
std::shared_ptr<com::anyconnect::access::Access> access;
std::shared_ptr<com::anyconnect::access::AccessBuilder> accessBuilder;
Now create a Access builder.
accessBuilder = com::anyconnect::access::AccessBuilder::getBuilder();
Preparing Access Builder
Set Cache Directory
Cache directory is the path of the db which access uses internally
accessBuilder->setCacheDirectory("./");
Field Provisioning (Pairing)
Details of pairing process is discussed later. Before creating access instance the access builder has to be set with pairing credentials. Below is the method for setting up those credentials.
Sample Code
//userId - User Id of the user attempting to pair.
//pairingToken - Token received from the user attempting to pair.
//deviceName - Name of the endpoint.
//UUID - Unique identifier for the calling machine. This should not change for a certain machine.
accessBuilder->fieldProvisionDevice(userId, pairingToken, deviceName, UUID);
Create Access Instance
When the access builder is set up with neccessary information like given above, now can use the build() method as per builder-pattern
to create access instance
access = accessBuilder->build();
Release Access instance gracefully
Access sdk has a stop
method which return asynchronously. Send a function pointer to read the callback from sdk. Look at the Sample Code below.
std::promise<void> stopTask;
access->stop([&stopTask](const int& status) {
//access is stopped
stopTask.set_value();
});
stopTask.get_future().get();
access.reset();
accessBuilder.reset();
Library: Stream
Create Stream Builder
StreamBuilder uses builder-patter
to create an stream instance. Let's define the stream pointer and stream builder
std::shared_ptr<com::anyconnect::stream::Stream> stream;
std::shared_ptr<com::anyconnect::stream::StreamBuilder> streamBuilder;
Now create a Stream builder.
streamBuilder = com::anyconnect::stream::StreamBuilder::getBuilder();
prepare Stream Builder
We have to set following information in StreamBuilder to prepare it for building a stream instance
//log directory holds some cache information to make to process faster (todo:)
streamBuilder->setLogDirectory("./");
//Streaming reconnection timeout, in seconds. When there is no internet connectivity, app will wait for the specified amount of time and try to reconnect the currently running streaming before terminating.
streamBuilder->setMaxNetworkChangeTime(300*1000);
// purpose _todo_
streamBuilder->setPluginLibraryDirectory("./../lib");
Set Access to Stream The previously created access instance has to be set to stream library.
streamBuilder->setAccess(access);
Create Stream Instance
When the access builder is set up with neccessary information like given above, now can use the build() method as per builder-pattern
to create stream instance
stream = streamBuilder->build();
Release Stream Instance gracefully
When the stream instance is created and the streaming is already initiated at first we have to stop streaming asynchronously. For asynchronous operation call the method with a function pointer like below.
std::promise<void> streamStopTask;
auto apiRet = stream->stop([&streamStopTask]() {
// stream stopped
streamStopTask.set_value();
});
if(apiRet == com::anyconnect::stream::StreamRet::OK) {
streamStopTask.get_future().get();
}
stream.reset();
streamBuilder.reset();
Updated over 2 years ago