src.dualinventive.com/dinet/sec-multi-proxy/include/device/Device.h

117 lines
3.2 KiB
C++

/**
* @file Device.h
* @brief The device class
* @copyright 2016 Dual Inventive Technology Centre B.V.
*
* The device class holds connections and manages req-rep for a device.
* It holds the routing for connections on server-side.
*/
#ifndef INCLUDE_DEVICE_DEVICE_H_
#define INCLUDE_DEVICE_DEVICE_H_
#include <string>
#include <list>
#include <set>
#include <unordered_map>
#include <memory>
#include <shared_mutex>
#include <di/Mailbox.h>
#include <server/UdpServer.h>
#include <server/ProtocolFacade.h>
class TCPConnection;
class UDPConnection;
class QueuedMessage;
struct Message;
class Device {
public:
/**
* device contstuctor
* @param deviceUid string containing device UID
*/
explicit Device(const std::string &deviceUid);
/**
* device destuctor
*/
~Device();
/**
* add a device TCP connection
* @param conn pointer to TCP connection
*/
bool addTCPConnection(const std::shared_ptr<TCPConnection> &conn);
/**
* remove device TCP connection
* @param conn pointer to TCP connection
*/
bool removeTCPConnection(const std::shared_ptr<TCPConnection> &conn);
/**
* add a device UDP connection
* @param conn pointer to UDP connection
*/
void addUDPConnection(const std::shared_ptr<UDPConnection> &conn);
/**
* remove device UDP connection
* @param conn pointer to UDP connection
*/
bool removeUDPConnection(const std::shared_ptr<UDPConnection> &conn);
/**
* publish an UDP message to the device using the given UdpServer
* @param server pointer to UdpServer which sends the messages to the devices
* @param protoFacade pointer to the protocol facade which encodes the messages
* @param msg pointer to the message to send
*/
void publishUDPMessage(const std::shared_ptr<UdpServer> &server,
const std::shared_ptr<ProtocolFacade> protoFacade, const std::shared_ptr<struct Message> &msg);
/**
* request the number of TCP and UDP connections
* @return number of TCP and UDP connections
*/
size_t numberOfConnections(void);
/**
* reply to request
* @param msg pointer to message
*/
bool repMessage(const std::shared_ptr<struct Message> &msg);
/**
* sending request message to device
* @param msg pointer to message that need to be send
* @param replymailbox pointer to mailbox for reply handling
* @param error pointer to error for if something goes wrong in sending request
*/
bool reqMessage(const std::shared_ptr<struct Message> &msg,
const std::shared_ptr<Di::Mailbox<std::string>> &replyMailbox,
uint32_t *error);
void checkRequests(void);
private:
std::string __deviceUid; /**< device uinque ID*/
std::list<std::shared_ptr<TCPConnection>> __tcpConnections; /**< pointer to TCP connection list*/
mutable std::shared_timed_mutex __tcpConnectionLock;
std::set<std::shared_ptr<UDPConnection>> __udpConnections; /**< pointer to UDP connection list*/
mutable std::shared_timed_mutex __udpConnectionLock;
std::unordered_map<uint32_t, std::shared_ptr<QueuedMessage>> __requestQueue; /**< pointer to request queue*/
mutable std::shared_timed_mutex __requestQueueLock;
/**
* Handling request timeouts for if the device doesn't react in time
* @param msg pointer to the queued message
*/
void __requestTimeout(const std::shared_ptr<QueuedMessage> &msg);
};
#endif // INCLUDE_DEVICE_DEVICE_H_