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().

~Connection()

Destructor. (virtual for SWIG).

ConnectionStateSubscriptionToken addStateSubscription(ConnectionStateCallbackFunction function)

Subscribe to connection state changes.

Return

Token that can be used to unsubscribe the callback.

Parameters
  • function: Callback function.

ConnectionStateSubscriptionToken addStateSubscriptionEx(StateObserver *observer)

Subscribe to connection state changes.

Return

Token that can be used unsubscribe the callback.

Parameters
  • observer: interface used to report changes

void clearWorkCallback()

Clears a previously set work callback.

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

Starts connecting to server.

Return

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

Parameters
  • adapter: Connection adapter type to use.

  • parameters: Configuration parameters for adapter.

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

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

Starts connecting to server.

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

Return

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

Parameters
  • adapter: Connection adapter type to use.

  • parameters: Configuration parameters for adapter.

ClientConnectionAdapterSharedPtr createAdapter(const string &identifier)

Create connection adapter based on name.

Return

newly created adapter

Parameters
  • identifier: Identifier of the connection adapter. This name needs to match with the ones in configuration files.

void disconnect()

Disconnects from server.

string getAdapterParameter(const string &parameter)

Retrieve parameter of currently used adapter.

Return

State of given parameter.

Parameters
  • parameter: Parameter to retrieve from adapter.

string getAdapterState(const string &parameter)

Retrieve state of currently used adapter.

Return

State of given parameter.

Parameters
  • parameter: Parameter to retrieve from adapter.

string getAdapterType()

Retrieve currently used adapter type.

Return

Adapter type.

MessageDispatcher *getMessageDispatcher()

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

Return

Message dispatcher object.

State getState() const

Returns current connection state.

Return

State enumeration representing the current state of operation.

Connection::WorkNotifyCallback *getWorkCallback() const

Retrieve previously set work notification callback.

Return

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

void notifyWorkAvailable()

Notify work available callback if one has been set.

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

string state2String(const State state)

Convert state to human readable string.

Return

string which represents given state

Parameters
  • state: The state to convert

class StateObserver

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

Public Functions

~StateObserver()
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

~WorkNotifyCallback()

Virtual destructor for cleanup.

void workAvailable() = 0

Invoked to notify that work is available.

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