FREEDM DGI
Public Member Functions | Static Public Member Functions | Private Member Functions | Private Attributes | Friends | List of all members
freedm::broker::device::CAdapterFactory Class Reference

Handles the creation of adapters and their associated devices. More...

#include <CAdapterFactory.hpp>

Inheritance diagram for freedm::broker::device::CAdapterFactory:

Public Member Functions

void CreateAdapter (const boost::property_tree::ptree &p)
 Creates a new adapter and its associated devices. More...
 
void RemoveAdapter (const std::string identifier)
 Removes an adapter and its associated devices. More...
 
void StartSessionProtocol (unsigned short port)
 Starts the session TCP server. More...
 
void Stop ()
 Stop everything! More...
 

Static Public Member Functions

static CAdapterFactoryInstance ()
 Gets the static instance of the factory. More...
 

Private Member Functions

 CAdapterFactory ()
 Constructs the factory. More...
 
void CreateDevice (const std::string name, const std::string type, IAdapter::Pointer adapter)
 Clones a device prototype and registers it with the system. More...
 
void HandleRead (const boost::system::error_code &e)
 Handles plug and play devices that send a session packet. More...
 
void InitializeAdapter (IAdapter::Pointer adapter, const boost::property_tree::ptree &p)
 Initializes the devices stored on an adapter. More...
 
void RunService ()
 Runs the adapter i/o service. More...
 
void SessionProtocol ()
 Session layer protocol for plug-and-play devices. More...
 
void StartSession ()
 Handles one plug and play device session. More...
 
void Timeout (const boost::system::error_code &e)
 Disconnects plug and play devices that timeout. More...
 

Private Attributes

std::map< std::string, IAdapter::Pointerm_adapters
 Set of device adapters managed by the factory. More...
 
boost::asio::streambuf m_buffer
 Packet received from plug and play device. More...
 
CDeviceBuilder m_builder
 Constructs the structure of devices. More...
 
boost::asio::io_service m_ios
 I/O service shared by the adapters. More...
 
CTcpServer::Pointer m_server
 TCP server to accept plug-and-play devices. More...
 
boost::thread m_thread
 Thread for the adapter i/o service. More...
 
boost::asio::deadline_timer m_timeout
 Timer for bad plug and play sessions. More...
 

Friends

class CMqttAdapter
 

Detailed Description

Singleton factory that creates, stores, and runs new device adapters.

Limitations:
This class is not thread safe.

Constructor & Destructor Documentation

freedm::broker::device::CAdapterFactory::CAdapterFactory ( )
private

Constructs an uninitialized factory.

Precondition:
None.
Postcondition:
Registers the known device classes.
Postcondition:
Initializes the session protocol TCP server.
Limitations:
None.

Member Function Documentation

void freedm::broker::device::CAdapterFactory::CreateAdapter ( const boost::property_tree::ptree &  p)

Creates a new adapter and all of its devices. The adapter is registered with each device, and each device is registered with the global device manager. The adapter is configured to recognize its own device signals, and started when the configuration is complete.

Error Handling:
Throws an EDgiConfigError if the property tree is bad, or EBadRequest if a PnP controller has assigned an unexpected signal to a device (which would be an EDgiConfigError otherwise)
Precondition:
The adapter must not begin work until IAdapter::Start.
Precondition:
The adapter's devices must not be specified in other adapters.
Postcondition:
Calls CAdapterFactory::InitializeAdapter to create devices.
Postcondition:
Starts the adapter through IAdapter::Start.
Parameters
pThe property tree that specifies a single adapter.
Limitations:
None.
void freedm::broker::device::CAdapterFactory::CreateDevice ( const std::string  name,
const std::string  type,
IAdapter::Pointer  adapter 
)
private

Creates a new device and registers it with the device manager.

Error Handling:
Throws a std::runtime_error if the name is already in use, the type is not recognized, or the adapter is null.
Precondition:
Type must be registered with CAdapterFactory::RegisterDevicePrototype.
Postcondition:
Creates a new device using m_prototype[type].
Postcondition:
Adds the new device to the device manager.
Parameters
nameThe unique identifier for the device to be created.
typeThe string identifier for the type of device to create.
adapterThe adapter that will handle the data of the new device.
Limitations:
The device types must be registered prior to this call.
void freedm::broker::device::CAdapterFactory::HandleRead ( const boost::system::error_code &  e)
private

Starts the session protocol after a successful read from a device.

Precondition:
None.
Postcondition:
If a successful read, calls CAdapterFactory::SessionProtocol.
Parameters
eThe error code associated with the last read operation.
Limitations:
None.
void freedm::broker::device::CAdapterFactory::InitializeAdapter ( IAdapter::Pointer  adapter,
const boost::property_tree::ptree &  p 
)
private

Initializes an adapter to contain a set of device signals.

Error Handling:
Throws EDgiConfigError if the property tree has a bad specification format. Could also throw EBadRequest if the adapter is a CPnpAdapter and the Hello message assigns an unexpected signal to a device.
Precondition:
The property tree must contain an adapter specification.
Postcondition:
Associates a set of device signals with the passed adapter.
Parameters
adapterThe adapter to initialize.
pThe property tree that contains the buffer data.
Limitations:
None.
CAdapterFactory & freedm::broker::device::CAdapterFactory::Instance ( )
static

Retrieves the singleton factory instance.

Precondition:
None.
Postcondition:
Creates a new factory on the first call.
Returns
The global instance of CAdapterFactory.
Limitations:
None.
void freedm::broker::device::CAdapterFactory::RemoveAdapter ( const std::string  identifier)

Removes an adapter and all of its associated devices.

Error Handling:
Throws a std::runtime_error if no such adapter exists.
Precondition:
An adapter must exist with the provided identifier.
Postcondition:
Removes the specified adapter from m_adapters.
Postcondition:
Removes the adapter's devices from the device manager.
Parameters
identifierThe identifier of the adapter to remove.
Limitations:
None.
void freedm::broker::device::CAdapterFactory::RunService ( )
private

Runs the i/o service with an infinite workload.

Precondition:
None.
Postcondition:
Runs m_ios and blocks the calling thread.
Limitations:
None.
void freedm::broker::device::CAdapterFactory::SessionProtocol ( )
private

Handles the hello message for the plug and play session protoocl.

Precondition:
m_buffer must contain the device hello packet.
Postcondition:
If the packet is well-formed, creates a new adapter and responds to the plug and play connection with a start packet.
Postcondition:
Otherwise, responds with a bad request that indicates the error.
Limitations:
None.

Reformat the packet as a property tree that can be used with CreateAdapter.

The config property tree now contains a valid adapter specification.

void freedm::broker::device::CAdapterFactory::StartSession ( )
private

Prepares to read the hello message from a new plug and play device.

Precondition:
m_server must be connected to a remote endpoint.
Postcondition:
m_timeout is started to disconnect the device if it does not respond.
Postcondition:
Schedules a read into m_buffer from the current m_server connection.
Limitations:
This function must only be called by m_server.
void freedm::broker::device::CAdapterFactory::StartSessionProtocol ( unsigned short  port)

Initializes the plug and play session protocol.

Parameters
portthe port over which to run the session protocol
Error Handling:
Throws a std::logic_error if the session protoocl has been initialized through a prior call to this function.
Precondition:
m_server must not be initialized by a prior call to this function.
Postcondition:
m_server is created to accept connections from plug and play devices.
Limitations:
This function must be called at most once.
void freedm::broker::device::CAdapterFactory::Stop ( )

Stops the i/o service and removes all devices from the device manager. This function must be called from outside the devices thread.

Precondition:
None
Postcondition:
All the devices of every adapter in the system are removed.
Postcondition:
The IOService has stopped.
Postcondition:
The devices thread is detatched and stopped (unless called from it).
Error Handling:
Guaranteed not to throw. Errors are only logged.
Limitations:
MUST be called from outside the devices thread.
void freedm::broker::device::CAdapterFactory::Timeout ( const boost::system::error_code &  e)
private

Closes a plug and play connection if it does not send a well-formed packet.

Precondition:
None.
Postcondition:
If timeout or error, closes the current m_server connection.
Parameters
eThe error code associated with the timer.
Limitations:
None.

Friends And Related Function Documentation

friend class CMqttAdapter
friend

Member Data Documentation

std::map<std::string, IAdapter::Pointer> freedm::broker::device::CAdapterFactory::m_adapters
private
boost::asio::streambuf freedm::broker::device::CAdapterFactory::m_buffer
private
CDeviceBuilder freedm::broker::device::CAdapterFactory::m_builder
private
boost::asio::io_service freedm::broker::device::CAdapterFactory::m_ios
private
CTcpServer::Pointer freedm::broker::device::CAdapterFactory::m_server
private
boost::thread freedm::broker::device::CAdapterFactory::m_thread
private
boost::asio::deadline_timer freedm::broker::device::CAdapterFactory::m_timeout
private

The documentation for this class was generated from the following files: