diff --git a/FlippR-Driver/src/input/Detector.cpp b/FlippR-Driver/src/input/Detector.cpp index 181fb7b..a147a70 100644 --- a/FlippR-Driver/src/input/Detector.cpp +++ b/FlippR-Driver/src/input/Detector.cpp @@ -15,7 +15,7 @@ namespace Input { -Detector::Detector(InputGPIOInterface* input_gpio_interface, std::map events, EventNotifier* event_notifier) : +Detector::Detector(InputGPIOInterface* input_gpio_interface, std::map events, IEventNotifier* event_notifier) : input_gpio_interface(input_gpio_interface), events(events), is_running(true), event_notifier(event_notifier) { detect_thread = std::thread(&Detector::detect, this); diff --git a/FlippR-Driver/src/input/Detector.h b/FlippR-Driver/src/input/Detector.h index 669eaa0..1ee5d4e 100644 --- a/FlippR-Driver/src/input/Detector.h +++ b/FlippR-Driver/src/input/Detector.h @@ -19,11 +19,11 @@ namespace Input { -class Detector : IDetector +class Detector : public IDetector { public: - Detector(InputGPIOInterface* input_gpio_interface, std::map events, EventNotifier* event_notifier); + Detector(InputGPIOInterface* input_gpio_interface, std::map events, IEventNotifier* event_notifier); ~Detector(); private: @@ -35,7 +35,7 @@ private: std::map events; - EventNotifier* event_notifier; + IEventNotifier* event_notifier; bool is_running; std::thread detect_thread; diff --git a/FlippR-Driver/src/input/EventHandler.hpp b/FlippR-Driver/src/input/EventHandler.hpp index 465364b..e90f227 100644 --- a/FlippR-Driver/src/input/EventHandler.hpp +++ b/FlippR-Driver/src/input/EventHandler.hpp @@ -11,26 +11,21 @@ #ifndef INPUTEVENTHANDLER_H_ #define INPUTEVENTHANDLER_H_ - -#include "../utilities/config.h" +#include "IEventHandler.h" #include "Event.hpp" +#include "IInputDriver.h" + +#include "../utilities/config.h" namespace Input { class EventHandler; -class InputDriver +class EventHandler : public IEventHandler { public: - void register_event_handler(EventHandler* handler); - void unregister_event_handler(EventHandler* handler); -}; - -class EventHandler -{ -public: - EventHandler(std::shared_ptr input_driver) : + EventHandler(std::shared_ptr input_driver) : input_driver(input_driver) { this->input_driver->register_event_handler(this); @@ -51,7 +46,7 @@ public: } private: - std::shared_ptr input_driver; + std::shared_ptr input_driver; }; } diff --git a/FlippR-Driver/src/input/EventNotifier.cpp b/FlippR-Driver/src/input/EventNotifier.cpp index 841b7f2..8fb99cb 100644 --- a/FlippR-Driver/src/input/EventNotifier.cpp +++ b/FlippR-Driver/src/input/EventNotifier.cpp @@ -41,7 +41,7 @@ void EventNotifier::unregister_event_handler(IEventHandler* handler) event_handler.erase(handler); } -void EventNotifier::distribute_event(IEvent& event) +void EventNotifier::distribute_event(Event& event) { event_queue.push(event); } @@ -56,7 +56,7 @@ void EventNotifier::notify() std::lock_guard event_handler_guard(event_handler_mutex); for(auto handler : event_handler) { - boost::thread handler_caller(boost::bind(&EventHandler::handle, handler, event)); + boost::thread handler_caller(boost::bind(&IEventHandler::handle, handler, event)); if(!handler_caller.timed_join(boost::posix_time::milliseconds(HANDLER_TIMEOUT))) { diff --git a/FlippR-Driver/src/input/EventNotifier.h b/FlippR-Driver/src/input/EventNotifier.h index beed119..9f4431d 100644 --- a/FlippR-Driver/src/input/EventNotifier.h +++ b/FlippR-Driver/src/input/EventNotifier.h @@ -23,7 +23,7 @@ namespace Input { -class EventNotifier : IEventNotifier +class EventNotifier : public IEventNotifier { public: @@ -33,13 +33,13 @@ public: void register_event_handler(IEventHandler* handler); void unregister_event_handler(IEventHandler* handler); - void distribute_event(IEvent& event); + void distribute_event(Event& event); private: void notify(); private: - BlockingQueue event_queue; + BlockingQueue event_queue; std::set event_handler; bool is_running; diff --git a/FlippR-Driver/src/input/IEventHandler.h b/FlippR-Driver/src/input/IEventHandler.h index 5a24bf3..350e09c 100644 --- a/FlippR-Driver/src/input/IEventHandler.h +++ b/FlippR-Driver/src/input/IEventHandler.h @@ -8,11 +8,16 @@ #ifndef SRC_INPUT_IEVENTHANDLER_H_ #define SRC_INPUT_IEVENTHANDLER_H_ +#include "Event.hpp" + namespace Input { class IEventHandler { +public: + virtual ~IEventHandler() = 0; + virtual void handle(Event& event) = 0; }; } diff --git a/FlippR-Driver/src/input/IInputDriver b/FlippR-Driver/src/input/IInputDriver deleted file mode 100644 index e69de29..0000000 diff --git a/FlippR-Driver/src/input/IInputDriver.h b/FlippR-Driver/src/input/IInputDriver.h index bb99771..3d2940c 100644 --- a/FlippR-Driver/src/input/IInputDriver.h +++ b/FlippR-Driver/src/input/IInputDriver.h @@ -8,12 +8,17 @@ #ifndef SRC_INPUT_IINPUTDRIVER_H_ #define SRC_INPUT_IINPUTDRIVER_H_ +#include "IEventHandler.h" +#include "IEventNotifier.h" namespace Input { class IInputDriver { - +public: + virtual ~IInputDriver() = 0; + virtual void register_event_handler(IEventHandler* handler) = 0; + virtual void unregister_event_handler(IEventHandler* handler) = 0; }; } diff --git a/FlippR-Driver/src/input/InputDriver.hpp b/FlippR-Driver/src/input/InputDriver.hpp index fd2f05f..2f040de 100644 --- a/FlippR-Driver/src/input/InputDriver.hpp +++ b/FlippR-Driver/src/input/InputDriver.hpp @@ -6,21 +6,20 @@ */ #ifndef SRC_INPUT_INPUTDRIVER_HPP_ #define SRC_INPUT_INPUTDRIVER_HPP_ +#include "IInputDriver.h" #include "../utilities/config.h" -#include "EventHandler.hpp" -#include "EventNotifier.h" -#include "Detector.h" +#include "IDetector.h" namespace Input { -class InputDriver +class InputDriver : public IInputDriver { public: - InputDriver(EventNotifier* event_notifier, Detector* detector) : + InputDriver(IEventNotifier* event_notifier, IDetector* detector) : event_notifier(event_notifier), detector(detector) { CLOG(INFO, INPUT_LOGGER) << "Created InputDriver"; @@ -35,19 +34,19 @@ public: detector = NULL; } - void register_event_handler(EventHandler* handler) + void register_event_handler(IEventHandler* handler) { event_notifier->register_event_handler(handler); } - void unregister_event_handler(EventHandler* handler) + void unregister_event_handler(IEventHandler* handler) { event_notifier->unregister_event_handler(handler); } private: EventNotifier* event_notifier; - Detector* detector; + IDetector* detector; }; } diff --git a/FlippR-Driver/src/input/InputDriverFactory.hpp b/FlippR-Driver/src/input/InputDriverFactory.hpp index f3fc19f..8f8e465 100644 --- a/FlippR-Driver/src/input/InputDriverFactory.hpp +++ b/FlippR-Driver/src/input/InputDriverFactory.hpp @@ -10,13 +10,13 @@ #include -#include "Detector.h" +#include "IDetector.h" #include "../utilities/InputGPIOInterface.h" #include "../utilities/config.h" #include "../lib/json/json.hpp" #include "../lib/easylogging/easylogging++.h" -#include "EventNotifier.h" +#include "IEventNotifier.h" using namespace nlohmann; @@ -29,7 +29,7 @@ class InputFactory { public: - static shared_ptr get_InputDriver(std::string& input_config_path, std::string& matrix_config_path) + static shared_ptr get_InputDriver(std::string& input_config_path, std::string& matrix_config_path) { this->ConfigureLogger(); auto event_notifier = new EventNotifier(); @@ -40,7 +40,7 @@ public: } private: - static Detector* get_detector(std::string& input_config_path, std::string& matrix_config_path) + static IDetector* get_detector(std::string& input_config_path, std::string& matrix_config_path) { std::ifstream input_config_stream(input_config_path); json input_config; @@ -53,7 +53,7 @@ private: auto input_gpio_interface = new InputGPIOInterface(input_config); auto input_notifier = new InputEventNotifier(); - std::map input_events = this->create_input_events(matrix_config); + std::map input_events = this->create_input_events(matrix_config); return new Detector(input_gpio_interface, input_events, input_notifier); } diff --git a/FlippR-Driver/tests/input/TestInputDriver.cpp b/FlippR-Driver/tests/input/TestInputDriver.cpp index 20b72f6..b160f07 100644 --- a/FlippR-Driver/tests/input/TestInputDriver.cpp +++ b/FlippR-Driver/tests/input/TestInputDriver.cpp @@ -23,11 +23,11 @@ SCENARIO("An InputDriver gets created", "[construction}") WHEN("The InputDriver gets created") { - Input::InputDriver input_driver(&event_notifier); + Input::InputDriver input_driver(&event_notifier_mock.get()); THEN("It saved the EventNotifier") { - REQUIRE(input_driver.event_notifier == &event_notifier); + REQUIRE(input_driver.event_notifier == &event_notifier_mock.get()); } } }