Client

class kanzi::connect::Client : public InterfaceDomain, public WorkQueueInterface, public enable_shared_from_this<Client>

A class that is used to connect an application to Kanzi Connect server.

This class allows to set up network connectivity and provides client-side access to network interfaces of the connected services.

Unnamed Group

virtual void notifyItem (ConnectWorkItemSharedPtr workItem) KZ_OVERRIDE

See

kanzi::connect::WorkQueueInterface

virtual int64_t allocateUniqueHandle () KZ_OVERRIDE

Optionally implementable method that should return a handle that can later on be used cancel work item holding that identifier.

virtual void cancelWorkItem (int64_t handle) KZ_OVERRIDE

Cancels a work item holding an handle previously allocated using allocateUniqueHandle()

Parameters
  • handle: the handle.

Public Functions

Client()

Constructor.

virtual  ~Client () KZ_OVERRIDE

Destructor.

void addInterface(AbstractInterfaceClient *abstractInterface)

Adds client-side network interface to system.

Currently needs to be called before connection is established. Note: The Client takes ownership of the interface, the instance should not be destructed by other party unless removeInterface is called first to resume ownership back to someone else.

Parameters
  • abstractInterface: Interface to register in to system.

void allowMultiThreadedTasks(const unsigned int threadCount)

Allow clients task queue to process multi-threaded tasks.

See

kanzi::connect::connect::Client::notifyItem

Parameters
  • threadCount: The number of worker threads used to process the multi-threaded task queue

void clearWorkCallback()

Clears a previously set work callback.

bool connected() const

Check whether client is currently connected.

Return

true if connected.

Connection &getConnection()

Get connection object, used to connect to server.

Return

Connection object that represents connection to server.

shared_ptr<DiagnosticsServiceClient> getDiagnosticsClient()

Return diagnostics client instance.

Return

diagnostics client instance.

template<typename T>
inline T *getInterface()

Retrieves network interface by it’s type.

virtual AbstractInterfaceClient * getInterfaceById (const string &id) KZ_OVERRIDE

See InterfaceDomain::getInterfaceById.

virtual AbstractInterfaceClient * getInterfaceByIndex (size_t index) KZ_OVERRIDE

See InterfaceDomain::getInterfaceByIndex.

virtual size_t getInterfaceCount () const KZ_OVERRIDE

See InterfaceDomain::getInterfaceCount.

P2PClientSharedPtr getP2PClient()

Retrieve P2P client instance.

Return

peer to peer functionality on client side.

RPCCallStore &getRPCCallStore()

Retrieve reference to RPC call store implementation.

Return

the callstore object used to maintain ongoing RPC calls.

uint32_t getRPCCallTimeoutGranularity() const

Retrieve RPC method call timeout granularity.

Return

millisecond value.

RunningServicesMonitor &getRunningServicesMonitor()

Returns a reference to running services monitor.

Return

reference to running services monitor. Returned reference remains valid as long as Client object is valid.

shared_ptr<ServiceClient> getServiceClient()

Return service client instance.

Return

service client instance.

Connection::WorkNotifyCallback *getWorkCallback()

Retrieve work callback if any.

Return

work callback.

void initialize()

Complete Construction.

void interrupt()

Interrupt wait/waitEx call.

Use this method if Client execution gets always interrupted using the Client class API (interrupt()) and never directly using the underlying ConnectCondition object.

void interruptEx()

Interrupts wait/waitEx call.

Use this method if Client execution gets also interrupted directly using the ConnectCondition object.

Generic usage pattern:

while (..)
{
    m_client.waitEx();
    m_client.work();
}

// .. in some another thread ..
m_client.interruptEx();


// .. in some another thread ..
m_client.signal()->notify_one();

bool registerConnectionAdapter(const string &identifier, ConnectionAdapterCreateFunc creator)

Registers new connection adapter type into client.

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.

void removeInterface(AbstractInterfaceClient *abstractInterface)

Removes a client-side network interface from client.

Note: The Client shifts the ownership of the object to the caller of this function. The caller is responsible for memory management of the pointed object after this point.

Parameters
  • abstractInterface: Interface to unregister from client

void reset()

Resets all internal structures and ensures client is disconnected.

void setRPCCallTimeoutGranularity(uint32_t ms)

Sets the granularity for pending RPC method call timeouts.

Smaller the granularity more often the timeouts of the pending RPC method calls is checked. Defaults to 10ms.

Parameters
  • ms: milliseconds how often timeoutted rpc methods are checked. If 0 is passed then pending RPC method calls are checked for each work() routine call.

void setWorkCallback(Connection::WorkNotifyCallback *callback)

Registers a work callback.

Parameters
  • callback: method to invoke when there is work to be done.

Wait *signal()

Get pointer to signal that is used to notify new work on main thread.

template<typename Func, typename ...Args>
inline void submitTask(Func &&func, Args&&... args)
string translateUri(const string &uri, bool encode)

Translate the given uri to contain an actual server address.

Return

string which contains the <SERVER> tag translated to actual server IP address

Parameters
  • uri: The string which contains <SERVER> tag to translate (allowed to be missing)

  • encode: If true then the URI is encoded

bool unregisterConnectionAdapter(const string &identifier)

Unregister connection adapter from client.

Parameters
  • identifier: Identifier of the connection adapter.

void wait(int ms = -1)

Function to wait for network events to occur.

Blocks (but does not consume CPU) until events are received or timeout occurs. After this call returns, one should call work() in order to process the received events.

Parameters
  • ms: Timeout for wait, -1 for infinite wait.

void waitEx(int ms = -1)

Wait function for external application bindings.

Parameters
  • ms: milliseconds how long to wait.

void work()

This needs to be called periodically, in order for client to process network events.