Skip to content

Commit

Permalink
feat: initialiser for handoffs for integrations
Browse files Browse the repository at this point in the history
Also do it on all events, not just tag items.
  • Loading branch information
AndyTWF committed Jun 11, 2023
1 parent 8def864 commit 15c909b
Show file tree
Hide file tree
Showing 22 changed files with 514 additions and 473 deletions.
74 changes: 0 additions & 74 deletions src/plugin/handoff/ClearCacheOnActiveCallsignChanges.cpp

This file was deleted.

19 changes: 0 additions & 19 deletions src/plugin/handoff/ClearCacheOnActiveCallsignChanges.h

This file was deleted.

15 changes: 0 additions & 15 deletions src/plugin/handoff/ClearCacheOnRunwayDialogSave.cpp

This file was deleted.

18 changes: 0 additions & 18 deletions src/plugin/handoff/ClearCacheOnRunwayDialogSave.h

This file was deleted.

@@ -1,4 +1,4 @@
#include "DepartureHandoffResolver.h"
#include "DefaultDepartureHandoffResolutionStrategy.h"
#include "FlightplanAirfieldHandoffMapper.h"
#include "FlightplanSidHandoffMapper.h"
#include "HandoffOrder.h"
Expand All @@ -13,7 +13,7 @@ using UKControllerPlugin::Controller::ControllerPositionHierarchy;

namespace UKControllerPlugin::Handoff {

DepartureHandoffResolver::DepartureHandoffResolver(
DefaultDepartureHandoffResolutionStrategy::DefaultDepartureHandoffResolutionStrategy(
const FlightplanSidHandoffMapper& sidMapper,
const FlightplanAirfieldHandoffMapper& airfieldMapper,
const Controller::ActiveCallsignCollection& activeCallsigns)
Expand All @@ -28,7 +28,8 @@ namespace UKControllerPlugin::Handoff {
* nothing to map to, resolve to unicom anyway... We'll need a flightplan change to be able to attempt
* it again.
*/
auto DepartureHandoffResolver::Resolve(const Euroscope::EuroScopeCFlightPlanInterface& flightplan) const
auto
DefaultDepartureHandoffResolutionStrategy::Resolve(const Euroscope::EuroScopeCFlightPlanInterface& flightplan) const
-> std::shared_ptr<ResolvedHandoff>
{
std::shared_ptr<Controller::ControllerPosition> controller = nullptr;
Expand Down Expand Up @@ -68,7 +69,7 @@ namespace UKControllerPlugin::Handoff {
/**
* For a given handoff, resolve it to a particular controller position.
*/
auto DepartureHandoffResolver::ResolveController(const HandoffOrder& handoff) const
auto DefaultDepartureHandoffResolutionStrategy::ResolveController(const HandoffOrder& handoff) const
-> std::shared_ptr<Controller::ControllerPosition>
{
for (auto& controller : *handoff.order) {
Expand Down
@@ -1,4 +1,5 @@
#pragma once
#include "DepartureHandoffResolverInterface.h"

namespace UKControllerPlugin {
namespace Controller {
Expand All @@ -22,7 +23,7 @@ namespace UKControllerPlugin::Handoff {
* Given a flightplan, resolves the handoff frequency
* that should be used for the after departure TAG item.
*/
class DepartureHandoffResolver
class DepartureHandoffResolver : public DepartureHandoffResolverInterface
{
public:
DepartureHandoffResolver(
Expand All @@ -31,7 +32,7 @@ namespace UKControllerPlugin::Handoff {
const Controller::ActiveCallsignCollection& activeCallsigns);

[[nodiscard]] auto Resolve(const Euroscope::EuroScopeCFlightPlanInterface& flightplan) const
-> std::shared_ptr<ResolvedHandoff>;
-> std::shared_ptr<ResolvedHandoff> override;

private:
[[nodiscard]] auto ResolveController(const HandoffOrder& handoff) const
Expand Down
45 changes: 0 additions & 45 deletions src/plugin/handoff/HandoffCache.cpp

This file was deleted.

23 changes: 0 additions & 23 deletions src/plugin/handoff/HandoffCache.h

This file was deleted.

13 changes: 11 additions & 2 deletions src/plugin/handoff/HandoffEventHandler.cpp
Expand Up @@ -43,8 +43,7 @@ namespace UKControllerPlugin::Handoff {
}

// Resolve the handoff and return
const auto resolvedHandoff = this->resolver->Resolve(flightplan);
this->cache->Add(resolvedHandoff);
const auto resolvedHandoff = ResolveHandoffAndCache(flightplan);
tagData.SetItemString(FormatFrequency(resolvedHandoff));
this->FireHandoffUpdatedEvent(flightplan.GetCallsign());
}
Expand All @@ -53,6 +52,7 @@ namespace UKControllerPlugin::Handoff {
EuroScopeCFlightPlanInterface& flightPlan, EuroScopeCRadarTargetInterface& radarTarget)
{
this->cache->Delete(flightPlan.GetCallsign());
static_cast<void>(ResolveHandoffAndCache(flightPlan));
}

void HandoffEventHandler::FlightPlanDisconnectEvent(EuroScopeCFlightPlanInterface& flightPlan)
Expand Down Expand Up @@ -82,4 +82,13 @@ namespace UKControllerPlugin::Handoff {
sprintf_s(frequencyString, "%.3f", handoff->resolvedController->GetFrequency()); // NOLINT
return frequencyString; // NOLINT
}

auto HandoffEventHandler::ResolveHandoffAndCache(const EuroScopeCFlightPlanInterface& flightplan) const
-> std::shared_ptr<ResolvedHandoff>
{
const auto resolvedHandoff = this->resolver->Resolve(flightplan);
this->cache->Add(resolvedHandoff);

return std::move(resolvedHandoff);
}
} // namespace UKControllerPlugin::Handoff
2 changes: 2 additions & 0 deletions src/plugin/handoff/HandoffEventHandler.h
Expand Up @@ -44,6 +44,8 @@ namespace UKControllerPlugin::Handoff {
private:
void FireHandoffUpdatedEvent(const std::string& callsign);
[[nodiscard]] static auto FormatFrequency(const std::shared_ptr<ResolvedHandoff>& handoff) -> std::string;
[[nodiscard]] auto ResolveHandoffAndCache(const Euroscope::EuroScopeCFlightPlanInterface& flightplan) const
-> std::shared_ptr<ResolvedHandoff>;

// Resolves handoffs
const std::shared_ptr<DepartureHandoffResolver> resolver;
Expand Down
98 changes: 98 additions & 0 deletions src/plugin/handoff/InvalidateHandoffsOnActiveCallsignChanges.cpp
@@ -0,0 +1,98 @@
#include "ClearCacheOnActiveCallsignChanges.h"
#include "DepartureHandoffResolver.h"
#include "ResolvedHandoff.h"
#include "controller/ActiveCallsign.h"
#include "controller/ControllerPosition.h"
#include "controller/ControllerPositionHierarchy.h"
#include "euroscope/EuroscopePluginLoopbackInterface.h"

namespace UKControllerPlugin::Handoff {

ClearCacheOnActiveCallsignChanges::ClearCacheOnActiveCallsignChanges(
const std::shared_ptr<DepartureHandoffResolver>& resolver, Euroscope::EuroscopePluginLoopbackInterface& plugin)
: resolver(std::move(resolver)), plugin(plugin)
{
assert(this->resolver != nullptr && "Resolver must not be null");
}

/**
* When new controllers come online, we need to evict from the cache any controller who preceeds them
* in the hierarchy.
*/
void ClearCacheOnActiveCallsignChanges::ActiveCallsignAdded(const Controller::ActiveCallsign& callsign)
{
plugin.ApplyFunctionToAllFlightplans([&callsign, this](const auto& fp, const auto& rt) -> void {
auto resolvedHandoff = this->resolver->Resolve(*fp);

if (!ShouldInvalidateOnCallsignAdded(*resolvedHandoff, callsign)) {
return;
}

this->resolver->Invalidate(*fp);
static_cast<void>(this->resolver->Resolve(*fp));
});
}

/**
* If we have a controller that logs off, we only need to evict from the cache instances where
* this controller is the resolved controller.
*/
void ClearCacheOnActiveCallsignChanges::ActiveCallsignRemoved(const Controller::ActiveCallsign& callsign)
{
plugin.ApplyFunctionToAllFlightplans([&callsign, this](const auto& fp, const auto& rt) -> void {
auto resolvedHandoff = this->resolver->Resolve(*fp);
if (*resolvedHandoff->resolvedController != callsign.GetNormalisedPosition()) {
return;
}

this->resolver->Invalidate(*fp);
static_cast<void>(this->resolver->Resolve(*fp));
});
}

/**
* If we lose track of who's online alltogether, time to clear the cache.
*/
void ClearCacheOnActiveCallsignChanges::CallsignsFlushed()
{
plugin.ApplyFunctionToAllFlightplans(
[this](const auto& fp, const auto& rt) -> void { this->resolver->Invalidate(*fp); });
}

auto ClearCacheOnActiveCallsignChanges::ShouldInvalidateOnCallsignAdded(
const ResolvedHandoff& handoff, const Controller::ActiveCallsign& callsign) -> bool
{
/*
* The SID hierarchy has the highest priority. So if the resolved controller is from the SIDs hierarchy,
* then the controller coming online must preceed it.
*/
if (handoff.sidHierarchy->PositionInHierarchy(*handoff.resolvedController)) {
return handoff.sidHierarchy->PositionPreceeds(
callsign.GetNormalisedPosition(), *handoff.resolvedController);
}

/*
* If the resolved controller isn't int he SID hierarchy, but the controller logging on is, then
* this takes precedence.
*/
if (handoff.sidHierarchy->PositionInHierarchy(callsign.GetNormalisedPosition())) {
return true;
}

/*
* If the resolved controller is in the airfield fallback hierarchy, then the controller coming online must
* preceed them in that.
*/
if (handoff.airfieldHierarchy->PositionInHierarchy(*handoff.resolvedController)) {
return handoff.airfieldHierarchy->PositionPreceeds(
callsign.GetNormalisedPosition(), *handoff.resolvedController);
}

/*
* Finally, if the resolved controller isn't from a hierarchy, it must be unicom.
*
* The last check here is to see if the controller in question is in the airfield hierarchy.
*/
return handoff.airfieldHierarchy->PositionInHierarchy(callsign.GetNormalisedPosition());
}
} // namespace UKControllerPlugin::Handoff

0 comments on commit 15c909b

Please sign in to comment.