Connection

class kanzi::connect::Connection : public ConnectionAdapterProvider, private NetworkListenerClient::Observer

A class that represents the connection to network server and maintains listener for the network traffic.

This class also handles the internal data exchange between the interfaces and the messages that are sent/received across network.

Public Types

typedef function<void(State)> ConnectionStateCallbackFunction

Callback function type for connection state changes.

typedef shared_ptr<void> ConnectionStateSubscriptionToken

Subscription token for connection callbacks.

enum State

Enumeration for connection state.

Values:

enumerator Connecting

Client is connecting in to server, but connection is not yet established.

enumerator Connected

Connection is established and services are connected.

enumerator Disconnected

There is no connection and services are unavailable.

Public Functions

Connection(InterfaceDomain *domain, Wait *signal)

Constructor.

Parameters
  • domain – Interface domain that provides access to all network interfaces.

  • signal – Signal that can be used to trigger work().

virtual ~Connection()

Destructor. (virtual for SWIG).

ConnectionStateSubscriptionToken addStateSubscription(ConnectionStateCallbackFunction function)

Subscribe to connection state changes.

Parameters

function – Callback function.

Returns

Token that can be used to unsubscribe the callback.

ConnectionStateSubscriptionToken addStateSubscriptionEx(StateObserver *observer)

Subscribe to connection state changes.

Parameters

observer – interface used to report changes

Returns

Token that can be used unsubscribe the callback.

void clearWorkCallback()

Clears a previously set work callback.

bool connect(const string &adapter, const ClientConnectionAdapter::Parameters &parameters, Wait *signal = 0)

Starts connecting to server.

Parameters
  • adapter – Connection adapter type to use.

  • parameters – Configuration parameters for adapter.

  • signal – Signal to invoke when connection is established, optional.

Returns

true if state allowed a new connection establishment to start. false if not.

bool connectEx(const string &adapter, const ClientConnectionAdapter::Parameters &parameters)

Starts connecting to server.

This is override meant to be used by external language bindings.

Parameters
  • adapter – Connection adapter type to use.

  • parameters – Configuration parameters for adapter.

Returns

true if state allowed a new connection establishment to start. false if not.

virtual ClientConnectionAdapterSharedPtr createAdapter(const string &identifier)
void disconnect()

Disconnects from server.

string getAdapterParameter(const string &parameter)

Retrieve parameter of currently used adapter.

Parameters

parameter – Parameter to retrieve from adapter.

Returns

State of given parameter.

string getAdapterState(const string &parameter)

Retrieve state of currently used adapter.

Parameters

parameter – Parameter to retrieve from adapter.

Returns

State of given parameter.

string getAdapterType()

Retrieve currently used adapter type.

Returns

Adapter type.

MessageDispatcher *getMessageDispatcher()

Retrieve message dispatcher that is used to send / receive data across network.

Returns

Message dispatcher object.

State getState() const

Returns current connection state.

Returns

State enumeration representing the current state of operation.

Connection::WorkNotifyCallback *getWorkCallback() const

Retrieve previously set work notification callback.

Returns

work notification interface. can be nullptr. ownership not transfered.

bool registerConnectionAdapter(const string &identifier, ConnectionAdapterCreateFunc creator)

Registers new connection adapter type.

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 removeStateSubscription(ConnectionStateSubscriptionToken token)

Unsubscribe from connection state changes.

Parameters

token – Token that was received when subscription was made.

void setWorkCallback(WorkNotifyCallback *callback)

Registers a work callback.

Parameters

callback – method to invoke when there is work to be done.

bool unregisterConnectionAdapter(const string &identifier)

Unregister connection adapter.

Parameters

identifier – Identifier of the connection adapter.

void work()

Performs work, process received data and connection state changes.

Public Static Functions

static string state2String(const State state)

Convert state to human readable string.

Parameters

state – The state to convert

Returns

string which represents given state

class StateObserver

Alternate approach to listen connection state events - traditional observer pattern.

Public Functions

inline virtual ~StateObserver()
virtual void connectionStateChanged(Connection::State state) = 0

called when connection state changes

Parameters

state – the new connection state.

class WorkNotifyCallback

Callback interface used to notify listeners that there is work to be done.

Subclassed by WorkerBase

Public Functions

inline virtual ~WorkNotifyCallback()

Virtual destructor for cleanup.

virtual void workAvailable() = 0

Invoked to notify that work is available.

Generally invoked in context other than the application “main thread”.