/** * @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 #include #include #include #include #include #include #include #include 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 &conn); /** * remove device TCP connection * @param conn pointer to TCP connection */ bool removeTCPConnection(const std::shared_ptr &conn); /** * add a device UDP connection * @param conn pointer to UDP connection */ void addUDPConnection(const std::shared_ptr &conn); /** * remove device UDP connection * @param conn pointer to UDP connection */ bool removeUDPConnection(const std::shared_ptr &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 &server, const std::shared_ptr protoFacade, const std::shared_ptr &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 &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 &msg, const std::shared_ptr> &replyMailbox, uint32_t *error); void checkRequests(void); private: std::string __deviceUid; /**< device uinque ID*/ std::list> __tcpConnections; /**< pointer to TCP connection list*/ mutable std::shared_timed_mutex __tcpConnectionLock; std::set> __udpConnections; /**< pointer to UDP connection list*/ mutable std::shared_timed_mutex __udpConnectionLock; std::unordered_map> __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 &msg); }; #endif // INCLUDE_DEVICE_DEVICE_H_