diff --git a/CMakeLists.txt b/CMakeLists.txt index f25b11c..ed97880 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,6 +40,7 @@ add_subdirectory(ChainResponsibility/cpp-source) add_subdirectory(Strategy/cpp-source) add_subdirectory(Observer/cpp-source) add_subdirectory(Command/cpp-source) +add_subdirectory(Mediator/cpp-source) if(NOT ON_PI) add_subdirectory(Adapter/cpp-source) diff --git a/Mediator/cpp-source/CMakeLists.txt b/Mediator/cpp-source/CMakeLists.txt new file mode 100644 index 0000000..9020771 --- /dev/null +++ b/Mediator/cpp-source/CMakeLists.txt @@ -0,0 +1,5 @@ +project(Mediator) + +set(SOURCES main.cpp dispatcher/Dispatcher.cpp dispatcher/Dispatcher.h vehicles/Vehicle.cpp vehicles/Vehicle.h vehicles/Plane.cpp vehicles/Plane.h vehicles/PlaneCommands.h dispatcher/PlaneDispatcher.cpp dispatcher/PlaneDispatcher.h) + +add_executable(Mediator ${SOURCES}) \ No newline at end of file diff --git a/Mediator/cpp-source/dispatcher/Dispatcher.cpp b/Mediator/cpp-source/dispatcher/Dispatcher.cpp new file mode 100644 index 0000000..880592c --- /dev/null +++ b/Mediator/cpp-source/dispatcher/Dispatcher.cpp @@ -0,0 +1,5 @@ +// +// Created by Pavel Akhtyamov on 2019-04-16. +// + +#include "Dispatcher.h" diff --git a/Mediator/cpp-source/dispatcher/Dispatcher.h b/Mediator/cpp-source/dispatcher/Dispatcher.h new file mode 100644 index 0000000..2fa30c7 --- /dev/null +++ b/Mediator/cpp-source/dispatcher/Dispatcher.h @@ -0,0 +1,21 @@ +// +// Created by Pavel Akhtyamov on 2019-04-16. +// + +#pragma once + +#include +#include + +#include "../vehicles/Vehicle.h" + +class Dispatcher : public std::enable_shared_from_this { + public: + virtual bool Notify( + std::shared_ptr vehicle, + std::string message + ) = 0; +}; + + + diff --git a/Mediator/cpp-source/dispatcher/PlaneDispatcher.cpp b/Mediator/cpp-source/dispatcher/PlaneDispatcher.cpp new file mode 100644 index 0000000..7e14a4a --- /dev/null +++ b/Mediator/cpp-source/dispatcher/PlaneDispatcher.cpp @@ -0,0 +1,25 @@ +// +// Created by Pavel Akhtyamov on 2019-04-16. +// + +#include "PlaneDispatcher.h" +bool PlaneDispatcher::Notify(std::shared_ptr vehicle, std::string message) { + if (message == "Want to land") { + std::lock_guard guard(spared_lane_); + if (!spare_) { + return false; + } else { + spare_ = false; + return true; + } + } else { + std::lock_guard guard(spared_lane_); + spare_ = true; + return true; + } +} + +void PlaneDispatcher::AddPlane(std::shared_ptr vehicle) { + vehicle->Connect(shared_from_this()); + vehicles_.push_back(vehicle); +} diff --git a/Mediator/cpp-source/dispatcher/PlaneDispatcher.h b/Mediator/cpp-source/dispatcher/PlaneDispatcher.h new file mode 100644 index 0000000..9ba52ec --- /dev/null +++ b/Mediator/cpp-source/dispatcher/PlaneDispatcher.h @@ -0,0 +1,29 @@ +// +// Created by Pavel Akhtyamov on 2019-04-16. +// + +#pragma once +#include +#include +#include + +#include "Dispatcher.h" +#include "../vehicles/Plane.h" + + +class PlaneDispatcher : public Dispatcher { + public: + bool Notify( + std::shared_ptr vehicle, + std::string message + ) override; + void AddPlane(std::shared_ptr vehicle); + + private: + std::vector> vehicles_; + std::mutex spared_lane_; + bool spare_ = true; +}; + + + diff --git a/Mediator/cpp-source/main.cpp b/Mediator/cpp-source/main.cpp new file mode 100644 index 0000000..86b0626 --- /dev/null +++ b/Mediator/cpp-source/main.cpp @@ -0,0 +1,23 @@ +// +// Created by Pavel Akhtyamov on 2019-04-16. +// + +#include "dispatcher/Dispatcher.h" +#include "dispatcher/PlaneDispatcher.h" + +int main() { + auto plane_dispatcher = std::make_shared(); + + auto first = std::make_shared(); + auto second = std::make_shared(); + + plane_dispatcher->AddPlane(first); + plane_dispatcher->AddPlane(second); + + std::thread first_thread(&Plane::Land, first); + std::thread second_thread(&Plane::Land, second); + + first_thread.join(); + second_thread.join(); + +} \ No newline at end of file diff --git a/Mediator/cpp-source/vehicles/Plane.cpp b/Mediator/cpp-source/vehicles/Plane.cpp new file mode 100644 index 0000000..38feb36 --- /dev/null +++ b/Mediator/cpp-source/vehicles/Plane.cpp @@ -0,0 +1,41 @@ +// +// Created by Pavel Akhtyamov on 2019-04-16. +// + +#include "Plane.h" +#include "../dispatcher/Dispatcher.h" + +#include +#include + +void Plane::HasLanded() { + mediator_->Notify(shared_from_this(), "Landed"); +} + +void Plane::GetStatus() { + switch (status_) { + case LANDED: std::cout << "Landed" << std::endl; + break; + case ATTACHED: std::cout << "Attached" << std::endl; + break; + case CONNECTED: std::cout << "Connected" << std::endl; + break; + } +} + +void Plane::Land() { + GetStatus(); + + bool land_started = false; + while (!land_started) { + land_started = mediator_->Notify(shared_from_this(), "Want to land"); + std::this_thread::sleep_for(std::chrono::milliseconds(580)); + } + + status_ = ATTACHED; + GetStatus(); + std::this_thread::sleep_for(std::chrono::seconds(2)); + status_ = LANDED; + mediator_->Notify(shared_from_this(), "Landed"); + GetStatus(); +} diff --git a/Mediator/cpp-source/vehicles/Plane.h b/Mediator/cpp-source/vehicles/Plane.h new file mode 100644 index 0000000..9d3c36c --- /dev/null +++ b/Mediator/cpp-source/vehicles/Plane.h @@ -0,0 +1,20 @@ +// +// Created by Pavel Akhtyamov on 2019-04-16. +// + +#pragma once + +#include "Vehicle.h" +#include "PlaneCommands.h" + +class Plane : public Vehicle { + public: + void HasLanded(); + void GetStatus(); + void Land(); + private: + PlaneCommand status_ = CONNECTED; +}; + + + diff --git a/Mediator/cpp-source/vehicles/PlaneCommands.h b/Mediator/cpp-source/vehicles/PlaneCommands.h new file mode 100644 index 0000000..bc7c9c0 --- /dev/null +++ b/Mediator/cpp-source/vehicles/PlaneCommands.h @@ -0,0 +1,12 @@ +// +// Created by Pavel Akhtyamov on 2019-04-16. +// + +#pragma once + + +enum PlaneCommand { + LANDED, + ATTACHED, + CONNECTED +}; \ No newline at end of file diff --git a/Mediator/cpp-source/vehicles/Vehicle.cpp b/Mediator/cpp-source/vehicles/Vehicle.cpp new file mode 100644 index 0000000..75346c3 --- /dev/null +++ b/Mediator/cpp-source/vehicles/Vehicle.cpp @@ -0,0 +1,9 @@ +// +// Created by Pavel Akhtyamov on 2019-04-16. +// + +#include "Vehicle.h" + +void Vehicle::Connect(std::shared_ptr mediator) { + mediator_ = mediator; +} diff --git a/Mediator/cpp-source/vehicles/Vehicle.h b/Mediator/cpp-source/vehicles/Vehicle.h new file mode 100644 index 0000000..c1f4ad6 --- /dev/null +++ b/Mediator/cpp-source/vehicles/Vehicle.h @@ -0,0 +1,19 @@ +// +// Created by Pavel Akhtyamov on 2019-04-16. +// + +#pragma once + +#include + +class Dispatcher; + +class Vehicle : public std::enable_shared_from_this { + public: + void Connect(std::shared_ptr mediator); + protected: + std::shared_ptr mediator_; +}; + + +