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.

ResourceSharedPtr acquireResourceAsync(string resourceUri, shared_ptr<void> handler, ResourceLoadedCallback resourceLoadedCallback, size_t retryCount = 5, unsigned int retryIntervalMs = 5000)

Request loading remote resource using the server attached to this instance of Context.

Return

pointer to resource if resource has been previously loaded, nullptr otherwise.

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.

template<class T>
inline shared_ptr<T> acquireService()

Provides type-safe access to service.

Return

Pointer to service.

template<class T>
inline shared_ptr<T> acquireService(const string &identifier)

Provides type-safe access to service instance.

Return

Pointer to service.

Parameters
  • identifier: Identifier of the new service instance, usually equals to Service::TYPE.

AbstractServiceClientSharedPtr acquireService(const string &identifier, ServiceCreationFunction creator)

Provides access to service.

Return

Pointer 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.

template<class T>
inline kanzi::shared_ptr<T> acquireService(Domain &domain)
ClientSharedPtr getClient() const

Access to client, which is used to connect to server.

Return

Client instance.

int getContextValue(int key, int defaultValue = 0)

Retrieves a free format value for a specific key.

Return

value that was found or provided default value otherwise. (default defaults to 0).

Parameters
  • key: identifies to object to retrieve.

string getFileServerAddress() const

Returns IP address of the remote file server.

Return

IP address of the remote file server.

string getName() const

Access to name of the connected client.

Return

Name of this client.

string getResourceDirectory() const

Returns directory where local resources are stored.

Return

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.

Return

true if registration was successful

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.

template<class T>
inline void registerService()

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

template<class T>
static shared_ptr<T> acquireService(Domain &domain)
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

Return

Shared pointer to Context.

Parameters
  • domain: Kanzi UI Domain that is used to host all Kanzi Connect runtime plugins.

  • serverName: Identifies which server configuration to use

static ContextSharedPtr findContextForService(Domain &domain, const string &serviceIdentifier)

Provides access for the client-side context instance for registered service.

Return

Shared pointer to Context.

Parameters
  • domain: Kanzi UI Domain that is used to host all Kanzi Connect runtime plugins.

  • serviceIdentifier: Registered service.

static ContextSharedPtr instance(Domain &domain, const string &serverName)

Provides access for the client-side context instance.

Never creates the instance

Return

Shared pointer to Context. Null if not found

Parameters
  • domain: Kanzi UI Domain that is used to host all Kanzi Connect runtime plugins.

  • serverName: Identifies which server configuration to use

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.

Return

True if the creator was registered successfully

Parameters
  • identifier: Identifies the connection adapter

  • creator: The factory method to construct the connection adapter

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.