Skip to content

Commit

Permalink
Merge pull request #115 from microsoft/apfactorylinux
Browse files Browse the repository at this point in the history
Incorporate use of access point factories
  • Loading branch information
abeltrano authored Jan 19, 2024
2 parents ffeb358 + 04206af commit 995d01b
Show file tree
Hide file tree
Showing 41 changed files with 623 additions and 333 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ struct NetRemoteServer
private:
std::string m_serverAddress;
std::unique_ptr<grpc::Server> m_server;
Service::NetRemoteService m_service{};
Service::NetRemoteService m_service;
};
} // namespace Microsoft::Net::Remote

Expand Down
6 changes: 1 addition & 5 deletions src/common/service/NetRemoteService.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
using namespace Microsoft::Net::Remote::Service;
using Microsoft::Net::Wifi::AccessPointManager;

NetRemoteService::NetRemoteService() :
NetRemoteService(AccessPointManager::Create())
{}

NetRemoteService::NetRemoteService(std::shared_ptr<AccessPointManager> accessPointManager) :
m_accessPointManager(std::move(accessPointManager))
{}
Expand All @@ -38,7 +34,7 @@ NetRemoteService::WifiEnumerateAccessPoints([[maybe_unused]] ::grpc::ServerConte
WifiEnumerateAccessPointsResultItem item{};
auto accessPoint = accessPointWeak.lock();
if (accessPoint != nullptr) {
auto interfaceName = accessPoint->GetInterface();
auto interfaceName = accessPoint->GetInterfaceName();
std::string accessPointId{ std::cbegin(interfaceName), std::cend(interfaceName) };
item.set_accesspointid(std::move(accessPointId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ class NetRemoteService :
public NetRemote::Service
{
public:
/**
* @brief Construct a new NetRemoteService object.
*/
NetRemoteService();

/**
* @brief Construct a new NetRemoteService object with the specified access point manager.
*
Expand Down
17 changes: 5 additions & 12 deletions src/common/wifi/apmanager/AccessPointManager.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,6 @@ AccessPointManager::Create(std::unique_ptr<IAccessPointFactory> accessPointFacto
return std::make_shared<notstd::enable_make_protected<AccessPointManager>>(std::move(accessPointFactory));
}

/* static */
std::shared_ptr<AccessPointManager>
AccessPointManager::Create()
{
return Create(std::make_unique<AccessPointFactory>());
}

std::shared_ptr<AccessPointManager>
AccessPointManager::GetInstance() noexcept
{
Expand All @@ -40,7 +33,7 @@ AccessPointManager::AddAccessPoint(std::shared_ptr<IAccessPoint> accessPoint)
{
const auto accessPointsLock = std::scoped_lock{ m_accessPointGate };
const auto accessPointExists = std::ranges::any_of(m_accessPoints, [&](const auto& accessPointExisting) {
return (accessPointExisting->GetInterface() == accessPoint->GetInterface());
return (accessPointExisting->GetInterfaceName() == accessPoint->GetInterfaceName());
});

if (accessPointExists) {
Expand All @@ -55,7 +48,7 @@ AccessPointManager::RemoveAccessPoint(std::shared_ptr<IAccessPoint> accessPoint)
{
const auto accessPointsLock = std::scoped_lock{ m_accessPointGate };
const auto accessPointToRemove = std::ranges::find_if(m_accessPoints, [&](const auto& accessPointExisting) {
return (accessPointExisting->GetInterface() == accessPoint->GetInterface());
return (accessPointExisting->GetInterfaceName() == accessPoint->GetInterfaceName());
});

if (accessPointToRemove == std::cend(m_accessPoints)) {
Expand All @@ -70,7 +63,7 @@ AccessPointManager::GetAccessPoint(std::string_view interfaceName) const
{
const auto accessPointsLock = std::scoped_lock{ m_accessPointGate };
const auto accessPoint = std::ranges::find_if(m_accessPoints, [&](const auto& accessPointExisting) {
return (accessPointExisting->GetInterface() == interfaceName);
return (accessPointExisting->GetInterfaceName() == interfaceName);
});

if (accessPoint == std::cend(m_accessPoints)) {
Expand Down Expand Up @@ -107,7 +100,7 @@ AccessPointManager::AddDiscoveryAgent(std::shared_ptr<AccessPointDiscoveryAgent>
// instance is still valid upon each callback invocation.
discoveryAgent->RegisterDiscoveryEventCallback([discoveryAgentPtr = discoveryAgent.get(), weakThis = std::weak_ptr<AccessPointManager>(GetInstance())](auto&& presence, auto&& interfaceName) {
if (auto strongThis = weakThis.lock()) {
std::shared_ptr<IAccessPoint> accessPointChanged{ std::make_shared<AccessPoint>(interfaceName) }; // TODO: use factory to create access point
auto accessPointChanged = strongThis->m_accessPointFactory->Create(interfaceName);
strongThis->OnAccessPointPresenceChanged(discoveryAgentPtr, presence, std::move(accessPointChanged));
}
});
Expand Down Expand Up @@ -136,7 +129,7 @@ AccessPointManager::AddDiscoveryAgent(std::shared_ptr<AccessPointDiscoveryAgent>
if (waitResult == std::future_status::ready) {
auto existingAccessPointInterfaceNames = existingAccessPointInterfaceNamesProbe.get();
for (const auto& existingAccessPointInterfaceName : existingAccessPointInterfaceNames) {
std::shared_ptr<IAccessPoint> existingAccessPoint{ std::make_shared<AccessPoint>(existingAccessPointInterfaceName) }; // TODO: use factory to create access point
auto existingAccessPoint = m_accessPointFactory->Create(existingAccessPointInterfaceName);
AddAccessPoint(std::move(existingAccessPoint));
}
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,6 @@ class AccessPointManager :
public std::enable_shared_from_this<AccessPointManager>
{
public:
/**
* @brief Safely create an instance of the access point manager with a default access point factory that creates
* AccessPoint instances.
*
* @return std::shared_ptr<AccessPointManager>
*/
[[nodiscard]] static std::shared_ptr<AccessPointManager>
Create();

/**
* @brief Safely create an instance of the access point manager.
*
Expand Down
35 changes: 27 additions & 8 deletions src/common/wifi/core/AccessPoint.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,43 @@

using namespace Microsoft::Net::Wifi;

AccessPoint::AccessPoint(std::string_view id) :
m_interface(id)
AccessPoint::AccessPoint(std::string_view interfaceName, std::shared_ptr<IAccessPointControllerFactory> accessPointControllerFactory) :
m_interfaceName(interfaceName),
m_accessPointControllerFactory(std::move(accessPointControllerFactory))
{}

std::string_view
AccessPoint::GetInterface() const noexcept
AccessPoint::GetInterfaceName() const noexcept
{
return m_interface;
return m_interfaceName;
}

std::unique_ptr<Microsoft::Net::Wifi::IAccessPointController>
std::unique_ptr<IAccessPointController>
AccessPoint::CreateController()
{
throw std::runtime_error("this function must be overridden by a derived class");
return (m_accessPointControllerFactory != nullptr)
? m_accessPointControllerFactory->Create(m_interfaceName)
: nullptr;
}

AccessPointFactory::AccessPointFactory(std::shared_ptr<IAccessPointControllerFactory> accessPointControllerFactory) :
m_accessPointControllerFactory(std::move(accessPointControllerFactory))
{}

std::shared_ptr<IAccessPointControllerFactory>
AccessPointFactory::GetControllerFactory() const noexcept
{
return m_accessPointControllerFactory;
}

std::shared_ptr<IAccessPoint>
AccessPointFactory::Create(std::string_view interfaceName)
{
return Create(interfaceName, nullptr);
}

std::shared_ptr<IAccessPoint>
AccessPointFactory::Create(std::string_view interface)
AccessPointFactory::Create(std::string_view interfaceName, [[maybe_unused]] std::unique_ptr<IAccessPointCreateArgs> accessPointCreateArgs)
{
return std::make_shared<AccessPoint>(interface);
return std::make_shared<AccessPoint>(interfaceName, GetControllerFactory());
}
13 changes: 7 additions & 6 deletions src/common/wifi/core/AccessPointController.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@

using namespace Microsoft::Net::Wifi;

AccessPointController::AccessPointController(std::weak_ptr<IAccessPoint> accessPointWeak) :
m_accessPointWeak(accessPointWeak)
{}
AccessPointController::AccessPointController(std::string_view interface) :
m_interfaceName(interface)
{
}

std::weak_ptr<IAccessPoint>
AccessPointController::GetAccessPoint() const noexcept
std::string_view
AccessPointController::GetInterfaceName() const noexcept
{
return m_accessPointWeak;
return m_interfaceName;
}
2 changes: 1 addition & 1 deletion src/common/wifi/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ target_sources(wifi-core
BASE_DIRS ${WIFI_CORE_PUBLIC_INCLUDE}
FILES
${WIFI_CORE_PUBLIC_INCLUDE_PREFIX}/AccessPoint.hxx
${WIFI_CORE_PUBLIC_INCLUDE_PREFIX}/AccessPointCapabilities.hxx
${WIFI_CORE_PUBLIC_INCLUDE_PREFIX}/AccessPointController.hxx
${WIFI_CORE_PUBLIC_INCLUDE_PREFIX}/IAccessPoint.hxx
${WIFI_CORE_PUBLIC_INCLUDE_PREFIX}/IAccessPointController.hxx
${WIFI_CORE_PUBLIC_INCLUDE_PREFIX}/IAccessPointFactory.hxx
${WIFI_CORE_PUBLIC_INCLUDE_PREFIX}/Ieee80211.hxx
)

Expand Down
54 changes: 42 additions & 12 deletions src/common/wifi/core/include/microsoft/net/wifi/AccessPoint.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#define ACCESS_POINT_HXX

#include <microsoft/net/wifi/IAccessPoint.hxx>
#include <microsoft/net/wifi/IAccessPointFactory.hxx>
#include <microsoft/net/wifi/IAccessPointController.hxx>

#include <string>
#include <string_view>
Expand All @@ -18,20 +18,20 @@ struct AccessPoint :
public IAccessPoint
{
/**
* @brief Construct a new AccessPoint object with the given network
* interface name.
*
* @param interface The network interface name representing the access point.
* @brief Construct a new AccessPoint object with the given network interface name.
*
* @param interfaceName The network interface name representing the access point.
* @param accessPointControllerFactory The factory used to create controller objects.
*/
AccessPoint(std::string_view interface);
AccessPoint(std::string_view interfaceName, std::shared_ptr<IAccessPointControllerFactory> accessPointControllerFactory);

/**
* @brief Get the network interface name representing the access point.
*
* @return std::string_view
*/
std::string_view
GetInterface() const noexcept override;
GetInterfaceName() const noexcept override;

/**
* @brief Create a controller object.
Expand All @@ -42,7 +42,8 @@ struct AccessPoint :
CreateController() override;

private:
const std::string m_interface;
const std::string m_interfaceName;
std::shared_ptr<IAccessPointControllerFactory> m_accessPointControllerFactory;
};

/**
Expand All @@ -52,13 +53,42 @@ struct AccessPointFactory :
public IAccessPointFactory
{
/**
* @brief Construct a new Access Point Factory object
*
* @param accessPointControllerFactory
*/
AccessPointFactory(std::shared_ptr<IAccessPointControllerFactory> accessPointControllerFactory);

/**
* @brief Create a new access point object for the given network interface.
*
* @param interface
* @return std::shared_ptr<AccessPoint>
*
* @param interfaceName
* @return std::shared_ptr<IAccessPoint>
*/
virtual std::shared_ptr<IAccessPoint>
Create(std::string_view interface) override;
Create(std::string_view interfaceName) override;

/**
* @brief Create a new access point object for the given network interface with the specified creation arguments.
*
* @param interfaceName The name of the interface.
* @param createArgs Arguments to be passed to the access point during creation.
* @return std::shared_ptr<IAccessPoint>
*/
virtual std::shared_ptr<IAccessPoint>
Create(std::string_view interfaceName, std::unique_ptr<IAccessPointCreateArgs> createArgs) override;

protected:
/**
* @brief Get the ControllerFactory object.
*
* @return std::shared_ptr<IAccessPointControllerFactory>
*/
std::shared_ptr<IAccessPointControllerFactory>
GetControllerFactory() const noexcept;

private:
std::shared_ptr<IAccessPointControllerFactory> m_accessPointControllerFactory;
};
} // namespace Microsoft::Net::Wifi

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

#ifndef ACCESS_POINT_CAPABILITIES_HXX
#define ACCESS_POINT_CAPABILITIES_HXX

#include <microsoft/net/wifi/Ieee80211.hxx>

namespace Microsoft::Net::Wifi
{
struct AccessPointCapabilities2 // FIXME
{

};
} // namespace Microsoft::Net::Wifi

#endif // ACCESS_POINT_CAPABILITIES_HXX
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,13 @@
#ifndef ACCESS_POINT_CONTROLLER_HXX
#define ACCESS_POINT_CONTROLLER_HXX

#include <memory>
#include <string>
#include <string_view>

#include <microsoft/net/wifi/IAccessPointController.hxx>

namespace Microsoft::Net::Wifi
{
struct IAccessPoint;

/**
* @brief Implementation of IAccessPointController for operations that should be
* common to all implementations.
Expand All @@ -19,13 +18,23 @@ struct AccessPointController :
{
virtual ~AccessPointController() = default;

AccessPointController(std::weak_ptr<IAccessPoint> accessPointWeak);

virtual std::weak_ptr<Microsoft::Net::Wifi::IAccessPoint>
GetAccessPoint() const noexcept override;
/**
* @brief Construct a new AccessPointController object to control the specified interface.
*
* @param interfaceName
*/
AccessPointController(std::string_view interfaceName);

/**
* @brief Get the interface name associated with this controller.
*
* @return std::string_view
*/
std::string_view
GetInterfaceName() const noexcept override;

private:
std::weak_ptr<Microsoft::Net::Wifi::IAccessPoint> m_accessPointWeak;
std::string m_interfaceName;
};
} // namespace Microsoft::Net::Wifi

Expand Down
Loading

0 comments on commit 995d01b

Please sign in to comment.