-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7224a09
commit 0d60124
Showing
12 changed files
with
210 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// | ||
// Created by Pavel Akhtyamov on 2019-04-16. | ||
// | ||
|
||
#include "Dispatcher.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// | ||
// Created by Pavel Akhtyamov on 2019-04-16. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <string> | ||
#include <memory> | ||
|
||
#include "../vehicles/Vehicle.h" | ||
|
||
class Dispatcher : public std::enable_shared_from_this<Dispatcher> { | ||
public: | ||
virtual bool Notify( | ||
std::shared_ptr<Vehicle> vehicle, | ||
std::string message | ||
) = 0; | ||
}; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// | ||
// Created by Pavel Akhtyamov on 2019-04-16. | ||
// | ||
|
||
#include "PlaneDispatcher.h" | ||
bool PlaneDispatcher::Notify(std::shared_ptr<Vehicle> vehicle, std::string message) { | ||
if (message == "Want to land") { | ||
std::lock_guard<std::mutex> guard(spared_lane_); | ||
if (!spare_) { | ||
return false; | ||
} else { | ||
spare_ = false; | ||
return true; | ||
} | ||
} else { | ||
std::lock_guard<std::mutex> guard(spared_lane_); | ||
spare_ = true; | ||
return true; | ||
} | ||
} | ||
|
||
void PlaneDispatcher::AddPlane(std::shared_ptr<Plane> vehicle) { | ||
vehicle->Connect(shared_from_this()); | ||
vehicles_.push_back(vehicle); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// Created by Pavel Akhtyamov on 2019-04-16. | ||
// | ||
|
||
#pragma once | ||
#include <memory> | ||
#include <vector> | ||
#include <thread> | ||
|
||
#include "Dispatcher.h" | ||
#include "../vehicles/Plane.h" | ||
|
||
|
||
class PlaneDispatcher : public Dispatcher { | ||
public: | ||
bool Notify( | ||
std::shared_ptr<Vehicle> vehicle, | ||
std::string message | ||
) override; | ||
void AddPlane(std::shared_ptr<Plane> vehicle); | ||
|
||
private: | ||
std::vector<std::shared_ptr<Vehicle>> vehicles_; | ||
std::mutex spared_lane_; | ||
bool spare_ = true; | ||
}; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<PlaneDispatcher>(); | ||
|
||
auto first = std::make_shared<Plane>(); | ||
auto second = std::make_shared<Plane>(); | ||
|
||
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// | ||
// Created by Pavel Akhtyamov on 2019-04-16. | ||
// | ||
|
||
#include "Plane.h" | ||
#include "../dispatcher/Dispatcher.h" | ||
|
||
#include <iostream> | ||
#include <thread> | ||
|
||
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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
}; | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// | ||
// Created by Pavel Akhtyamov on 2019-04-16. | ||
// | ||
|
||
#pragma once | ||
|
||
|
||
enum PlaneCommand { | ||
LANDED, | ||
ATTACHED, | ||
CONNECTED | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
// | ||
// Created by Pavel Akhtyamov on 2019-04-16. | ||
// | ||
|
||
#include "Vehicle.h" | ||
|
||
void Vehicle::Connect(std::shared_ptr<Dispatcher> mediator) { | ||
mediator_ = mediator; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// Created by Pavel Akhtyamov on 2019-04-16. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <memory> | ||
|
||
class Dispatcher; | ||
|
||
class Vehicle : public std::enable_shared_from_this<Vehicle> { | ||
public: | ||
void Connect(std::shared_ptr<Dispatcher> mediator); | ||
protected: | ||
std::shared_ptr<Dispatcher> mediator_; | ||
}; | ||
|
||
|
||
|