Skip to content

Commit

Permalink
Mediator added
Browse files Browse the repository at this point in the history
  • Loading branch information
akhtyamovpavel committed Apr 16, 2019
1 parent 7224a09 commit 0d60124
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions Mediator/cpp-source/CMakeLists.txt
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})
5 changes: 5 additions & 0 deletions Mediator/cpp-source/dispatcher/Dispatcher.cpp
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"
21 changes: 21 additions & 0 deletions Mediator/cpp-source/dispatcher/Dispatcher.h
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;
};



25 changes: 25 additions & 0 deletions Mediator/cpp-source/dispatcher/PlaneDispatcher.cpp
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);
}
29 changes: 29 additions & 0 deletions Mediator/cpp-source/dispatcher/PlaneDispatcher.h
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;
};



23 changes: 23 additions & 0 deletions Mediator/cpp-source/main.cpp
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();

}
41 changes: 41 additions & 0 deletions Mediator/cpp-source/vehicles/Plane.cpp
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();
}
20 changes: 20 additions & 0 deletions Mediator/cpp-source/vehicles/Plane.h
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;
};



12 changes: 12 additions & 0 deletions Mediator/cpp-source/vehicles/PlaneCommands.h
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
};
9 changes: 9 additions & 0 deletions Mediator/cpp-source/vehicles/Vehicle.cpp
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;
}
19 changes: 19 additions & 0 deletions Mediator/cpp-source/vehicles/Vehicle.h
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_;
};



0 comments on commit 0d60124

Please sign in to comment.