Part 2 : Backend Definition



Several backends are used in the project but we wanted to keep the implementation flexible, so you can easly adjust the Daemon backend to fit your needs.

The defined Backend can adjust :
  • The kind of socket to be used (Unix Socket, ZMQ, NetMQ, RabbitMQ, etc)
  • The kind of clock to be used (clock, chrono, with different precision)
Both backends can be used in three different modes :
  • The normal mode (or NO_MOCK) uses the defined official backend (Unix Socket, ZMQ, NetMQ, RabbitMQ, etc)
  • The mock mode (or MOCK) uses the mock to play a recorded sequence of data (exchange of a socket, time calls to clock, etc)
  • The record mode (or MOCK_RECORD) where the official backend is used but the mock backend is recording what the official backend does


Since we do not want to impose our choices to you, we add Backend to let you choose.

The DaemonEmptyBackend.h is the perfect starting point.

The struct DaemonEmptyBackend defines the backends which are used by the Daemon.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
///@brief Simple, default backend of the Daemon to manage Socket and Clock
struct DaemonEmptyBackend{
	///Define the socket backend
	typedef PEmptyBackend SocketBackend;
	///Define the socket mock
	typedef PMockBackend SocketMock;
	
	///Socket manager with no official backend 
	PGenericSocketManager<PString, SocketBackend, SocketMock> socketManager;
	///Clock backend
	PGenericClock<PClockNsBackend, PClockMock> clock;

	/// Extra parameters used by the current Daemon to create its socket
	SocketBackend::Param extraServerParam{SocketBackend::server()};
	/// Extra parameters used by the current Daemon to create sockets to communicate with other Daemon
	SocketBackend::Param extraClientParam{SocketBackend::client()};
	/// Extra parameters used by the current Daemon to create its socket for mock
	SocketMock::Param extraMockServerParam{SocketMock::server()};
	/// Extra parameters used by the current Daemon to create sockets to communicate with other Daemon for mock
	SocketMock::Param extraMockClientParam{SocketMock::client()};
};


The Socket backend defines the socketManager, here the PEmptyBackend is a dummy backend, which could be replaced by PZmqBackend defined in PhoenixZMQ if you want to use ZMQ. Typedefs SocketBackend and SocketMock are needed by the Daemon to initialise sockets with proper url and ports.

The extraParameters allows to initialize the backends using specific Parameters. Base use case uses the server() and client() static functions to get the default parameters for server and client sockets, but you can adjust them to fit your needs, by adding extra constructor for the DaemonEmptyBackend and adjusting the parameters as you want.

for example, you can use the extraParameters to set the type of the socket to be used by the Daemon (ZMQ_PUSH or ZMQ_PULL for ZMQ backend), or to set the url of the socket to be used by the Daemon.

Finally the clock backend uses PClockNsBackend which is the backend of PClockNs, a clock with a nano second precision, so it can run for 60 years.