Context¶
-
class
kanzi::connect
::
Context
¶ Context class is used to manage all client side objects that are related either to connectivity or services.
Public Types
-
typedef function<void(shared_ptr<void> handler, const string &resourceUri, bool result)>
ResourceLoadedCallback
¶
-
typedef function<AbstractServiceClientSharedPtr(ClientSharedPtr, const string&)>
ServiceCreationFunction
¶ A function callback type that is invoked when a registered service needs to be instantiated.
Public Functions
-
Context
()¶ Constructor.
One instance of this class should be created (per connected server). During the application start-up, the Kanzi runtime plug-ins are loaded and an instance of the Context class is instantiated per server. Usually this is created by the plugin that is loaded first and then instance() function can be used to access / share it elsewhere.
-
~Context
()¶ Destructor.
Request loading remote resource using the server attached to this instance of Context.
- Parameters
resourceUri – Resource identifier.
handler – Handler to be passed to callback. Is used for ensuring the validity of callback.
resourceLoadedCallback – Callback to be called when operation completes.
retryCount – Count of the retries if loading fails. This is common for all requests for same resource uri.
retryIntervalMs – Time, in milli seconds, to wait before retrying resource loading.
- Returns
pointer to resource if resource has been previously loaded, nullptr otherwise.
Provides type-safe access to service.
- Returns
Pointer to service.
Provides type-safe access to service instance.
- Parameters
identifier – Identifier of the new service instance, usually equals to Service::TYPE.
- Returns
Pointer to service.
-
AbstractServiceClientSharedPtr
acquireService
(const string &identifier, ServiceCreationFunction creator)¶ Provides access to service.
- Parameters
identifier – Identifier of the service instance, usually equals to Service::TYPE.
creator – A callback function that can be used to construct an instance of the service.
- Returns
Pointer to service.
-
ClientSharedPtr
getClient
() const¶ Access to client, which is used to connect to server.
- Returns
Client instance.
-
int
getContextValue
(int key, int defaultValue = 0)¶ Retrieves a free format value for a specific key.
- Parameters
key – identifies to object to retrieve.
- Returns
value that was found or provided default value otherwise. (default defaults to 0).
-
string
getFileServerAddress
() const¶ Returns IP address of the remote file server.
- Returns
IP address of the remote file server.
-
string
getName
() const¶ Access to name of the connected client.
- Returns
Name of this client.
-
string
getResourceDirectory
() const¶ Returns directory where local resources are stored.
- Returns
File path to local resource folder.
-
bool
registerConnectionAdapter
(const string &identifier, ConnectionAdapterCreateFunc creator)¶ Registers new connection adapter type into context.
Context must not have been started when registering new adapter.
- Parameters
identifier – Identifier of the connection adapter. This name needs to match with the ones in configuration files.
creator – Function that instantiates a connection adapter of given type.
- Returns
true if registration was successful
-
template<class
T
>
inline voidregisterService
()¶ Convenience function to register service creators.
-
void
registerService
(const string &identifier, ServiceCreationFunction creator)¶ Registers service type to context.
- Parameters
identifier – Identifier of the service. In most cases this matches with type of a service.
creator – Function that instantiates a new service of given type.
-
void
releaseService
(const string &identifier)¶ Ask context to release its handle to a service.
If there no other users for the service service will be released.
- Parameters
identifier – Identifier of the service instance, usually equals to Service::TYPE.
-
void
setContextValue
(int key, int value)¶ Stores a key-value pair into the context.
- Parameters
key – the key to identify the value
value – the value to save.
-
void
setServiceEnabledState
(const string &serviceIdentifier, bool enabled)¶ Enable/disable the specified service.
- Parameters
serviceIdentifier – Identifies the service to enable/disable
enabled – True enables and false disables the service
-
bool
unregisterConnectionAdapter
(const string &identifier)¶ Unregister connection adapter from context.
Context must not have been started when unregistering an adapter.
- Parameters
identifier – Identifier of the connection adapter.
-
void
unregisterService
(const string &identifier)¶ Unregister service type from context.
- Parameters
identifier – Identifier of the service.
Public Static Functions
-
static ContextSharedPtr
createOrFetchInstance
(Domain &domain, const string &serverName)¶ Create or fetch a context instance This function will create a new context if one does not yet exist.
Use this version only in places where creating new context instance is allowed. For normal instance access use the Context::instance method
- Parameters
domain – Kanzi UI Domain that is used to host all Kanzi Connect runtime plugins.
serverName – Identifies which server configuration to use
- Returns
Shared pointer to Context.
-
static ContextSharedPtr
findContextForService
(Domain &domain, const string &serviceIdentifier)¶ Provides access for the client-side context instance for registered service.
- Parameters
domain – Kanzi UI Domain that is used to host all Kanzi Connect runtime plugins.
serviceIdentifier – Registered service.
- Returns
Shared pointer to Context.
-
static ContextSharedPtr
instance
(Domain &domain, const string &serverName)¶ Provides access for the client-side context instance.
Never creates the instance
- Parameters
domain – Kanzi UI Domain that is used to host all Kanzi Connect runtime plugins.
serverName – Identifies which server configuration to use
- Returns
Shared pointer to Context. Null if not found
-
static void
notifyResourceLoadingResult
(Domain *domain, const string &uri, bool result)¶ Notify resource loading status. Called by framework.
-
static bool
registerConnectionAdapterCreator
(const string &identifier, ConnectionAdapterCreateFunc creator)¶ Register creator for connection adapters Each Context constructor will use these to actually register the connection adapters to client.
- Parameters
identifier – Identifies the connection adapter
creator – The factory method to construct the connection adapter
- Returns
True if the creator was registered successfully
-
static void
registerServiceToServerConnection
(const string &serviceIdentifier, const string &serverName)¶ Register certain service to be handled by specified server.
- Parameters
serviceIdentifier – Identifies the service
serverName – Identifies the server
-
static void
releaseService
(Domain &domain, const string &identifier)¶ Ask context to release its handle to a service.
If there no other users for the service service will be released.
- Parameters
identifier – Identifier of the service instance, usually equals to Service::TYPE.
-
typedef function<void(shared_ptr<void> handler, const string &resourceUri, bool result)>