Skip to content

Commit

Permalink
Add impl.
Browse files Browse the repository at this point in the history
  • Loading branch information
abeltrano committed Jan 18, 2024
1 parent 9a126b8 commit 9596bcf
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 31 deletions.
7 changes: 4 additions & 3 deletions src/common/server/NetRemoteServer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@

using namespace Microsoft::Net::Remote;

NetRemoteServer::NetRemoteServer(std::string_view serverAddress) :
m_serverAddress{ serverAddress }
NetRemoteServer::NetRemoteServer(NetRemoteServerConfiguration configuration) :
m_serverAddress(configuration.ServerAddress),
m_service(configuration.AccessPointManager)
{}

NetRemoteServer::~NetRemoteServer()
Expand Down Expand Up @@ -42,7 +43,7 @@ NetRemoteServer::Run()
builder.RegisterService(&m_service);

m_server = builder.BuildAndStart();
LOG_INFO << std::format("netremote server started listening on {}", m_serverAddress);
LOG_INFO << std::format("Netremote server started listening on {}", m_serverAddress);
}

void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <string_view>

#include <grpcpp/server.h>
#include <microsoft/net/remote/NetRemoteServerConfiguration.hxx>
#include <microsoft/net/remote/NetRemoteService.hxx>
#include <microsoft/net/wifi/AccessPointManager.hxx>

namespace Microsoft::Net::Remote
{
Expand All @@ -19,11 +21,11 @@ struct NetRemoteServer
virtual ~NetRemoteServer();

/**
* @brief Construct a new NetRemoteServer object.
* @brief Construct a new NetRemoteServer object with the specified configuration.
*
* @param serverAddress
* @param configuration
*/
NetRemoteServer(std::string_view serverAddress);
NetRemoteServer(NetRemoteServerConfiguration configuration);

/**
* @brief Get the GrpcServer object.
Expand All @@ -35,8 +37,8 @@ struct NetRemoteServer

/**
* @brief Get the NetRemoteService object instance.
*
* @return Service::NetRemoteService&
*
* @return Service::NetRemoteService&
*/
Service::NetRemoteService&
GetService() noexcept;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,20 @@
#define NET_REMOTE_SERVER_CONFIGURATION_HXX

#include <cstdint>
#include <memory>
#include <string>
#include <vector>

namespace Microsoft::Net::Wifi
{
class AccessPointManager;
} // namespace Microsoft::Net::Wifi

namespace Microsoft::Net::Remote
{
/**
* @brief Collects configuration options for the NetRemoteServer class.
*/
struct NetRemoteServerConfiguration
{
/**
Expand Down Expand Up @@ -55,6 +64,11 @@ struct NetRemoteServerConfiguration
* and a level of 3 or above will show all verbose messages.
*/
uint32_t LogVerbosity{ 0 };

/**
* @brief Access point manager instance.
*/
std::shared_ptr<Microsoft::Net::Wifi::AccessPointManager> AccessPointManager;
};

} // namespace Microsoft::Net::Remote
Expand Down
13 changes: 8 additions & 5 deletions src/common/service/NetRemoteService.cxx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@

#include <format>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
Expand All @@ -13,7 +12,11 @@ using namespace Microsoft::Net::Remote::Service;
using Microsoft::Net::Wifi::AccessPointManager;

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

NetRemoteService::NetRemoteService(std::shared_ptr<AccessPointManager> accessPointManager) :
m_accessPointManager(std::move(accessPointManager))
{}

std::shared_ptr<AccessPointManager>
Expand All @@ -27,7 +30,7 @@ NetRemoteService::WifiEnumerateAccessPoints([[maybe_unused]] ::grpc::ServerConte
{
using Microsoft::Net::Remote::Wifi::WifiEnumerateAccessPointsResultItem;

LOG_VERBOSE << std::format("Received WifiEnumerateAccessPoints request\n");
LOGD << std::format("Received WifiEnumerateAccessPoints request");

auto accessPoints = m_accessPointManager->GetAllAccessPoints();
std::vector<WifiEnumerateAccessPointsResultItem> accessPointResultItems(std::size(accessPoints));
Expand Down Expand Up @@ -59,7 +62,7 @@ using Microsoft::Net::Wifi::Dot11PhyType;
::grpc::Status
NetRemoteService::WifiAccessPointEnable([[maybe_unused]] ::grpc::ServerContext* context, const ::Microsoft::Net::Remote::Wifi::WifiAccessPointEnableRequest* request, ::Microsoft::Net::Remote::Wifi::WifiAccessPointEnableResult* response)
{
LOG_VERBOSE << std::format("Received WifiAccessPointEnable request for access point id {}\n", request->accesspointid());
LOGD << std::format("Received WifiAccessPointEnable request for access point id {}", request->accesspointid());

WifiAccessPointOperationStatus status{};

Expand All @@ -78,7 +81,7 @@ NetRemoteService::WifiAccessPointEnable([[maybe_unused]] ::grpc::ServerContext*
::grpc::Status
NetRemoteService::WifiAccessPointDisable([[maybe_unused]] ::grpc::ServerContext* context, const ::Microsoft::Net::Remote::Wifi::WifiAccessPointDisableRequest* request, ::Microsoft::Net::Remote::Wifi::WifiAccessPointDisableResult* response)
{
LOG_VERBOSE << std::format("Received WifiAccessPointDisable request for access point id {}\n", request->accesspointid());
LOGD << std::format("Received WifiAccessPointDisable request for access point id {}", request->accesspointid());

WifiAccessPointOperationStatus status{};
// TODO: Disable the access point.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,29 @@

namespace Microsoft::Net::Remote::Service
{
/**
* @brief Implementation of the NetRemote::Service gRPC service.
*/
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.
*
* @param accessPointManager The access point manager to use.
*/
NetRemoteService(std::shared_ptr<Microsoft::Net::Wifi::AccessPointManager> accessPointManager);

/**
* @brief Get the AccessPointManager object for this service.
*
* @return std::shared_ptr<Microsoft::Net::Wifi::AccessPointManager>
*
* @return std::shared_ptr<Microsoft::Net::Wifi::AccessPointManager>
*/
std::shared_ptr<Microsoft::Net::Wifi::AccessPointManager>
GetAccessPointManager() noexcept;
Expand Down
22 changes: 13 additions & 9 deletions src/linux/server/Main.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <microsoft/net/remote/NetRemoteServerConfiguration.hxx>
#include <microsoft/net/wifi/AccessPointDiscoveryAgent.hxx>
#include <microsoft/net/wifi/AccessPointDiscoveryAgentOperationsNetlink.hxx>
#include <microsoft/net/wifi/AccessPointManager.hxx>
#include <notstd/Utility.hxx>
#include <plog/Appenders/ColorConsoleAppender.h>
#include <plog/Appenders/RollingFileAppender.h>
Expand All @@ -20,6 +21,7 @@ using namespace Microsoft::Net::Remote;

using Microsoft::Net::Wifi::AccessPointDiscoveryAgent;
using Microsoft::Net::Wifi::AccessPointDiscoveryAgentOperationsNetlink;
using Microsoft::Net::Wifi::AccessPointManager;

enum class LogInstanceId : int {
// Default logger is 0 and is omitted from this enumeration.
Expand All @@ -35,7 +37,7 @@ main(int argc, char *argv[])
static plog::RollingFileAppender<plog::TxtFormatter> rollingFileAppender(logging::GetLogName("server").c_str());

// Parse command line arguments.
const auto configuration = NetRemoteServerConfiguration::FromCommandLineArguments(argc, argv);
auto configuration = NetRemoteServerConfiguration::FromCommandLineArguments(argc, argv);
const auto logSeverity = logging::LogVerbosityToPlogSeverity(configuration.LogVerbosity);

// Configure logging, appending all loggers to the default instance.
Expand All @@ -45,19 +47,21 @@ main(int argc, char *argv[])
.addAppender(plog::get<notstd::to_underlying(LogInstanceId::Console)>())
.addAppender(plog::get<notstd::to_underlying(LogInstanceId::File)>());

// Create the server.
NetRemoteServer server{ configuration.ServerAddress };

// Create an access point discovery agent, and register it with the server's access point manager.
// Create an access point manager and discovery agent.
{
configuration.AccessPointManager = AccessPointManager::Create();

auto &accessPointManager = configuration.AccessPointManager;
auto accessPointDiscoveryAgentOperationsNetlink = std::make_unique<AccessPointDiscoveryAgentOperationsNetlink>();
auto accessPointDiscoveryAgent = AccessPointDiscoveryAgent::Create(std::move(accessPointDiscoveryAgentOperationsNetlink));
auto accessPointManager = server.GetService().GetAccessPointManager();
accessPointManager->AddDiscoveryAgent(std::move(accessPointDiscoveryAgent));
}

// Create the server.
NetRemoteServer server{ std::move(configuration) };

// Start the server.
LOGI << "Starting netremote server";
LOGI << "Netremote server starting";
server.Run();

// If running in the background, daemonize the process.
Expand All @@ -68,7 +72,7 @@ main(int argc, char *argv[])
if (daemon(nochdir, noclose) != 0) {
const int error = errno;
const auto what = std::format("Failed to daemonize (error={})", error);
LOG_ERROR << what;
LOGE << what;
throw std::runtime_error(what);
}
}
Expand All @@ -77,7 +81,7 @@ main(int argc, char *argv[])
server.GetGrpcServer()->Wait();
}

LOGI << "Server exiting";
LOGI << "Netremote server stopping";

return 0;
}
40 changes: 35 additions & 5 deletions tests/unit/TestNetRemoteServer.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,31 @@
TEST_CASE("Create a NetRemoteServer instance", "[basic][rpc][remote]")
{
using namespace Microsoft::Net::Remote;
using namespace Microsoft::Net::Wifi;

NetRemoteServerConfiguration Configuration{
.ServerAddress = Test::RemoteServiceAddressHttp,
.AccessPointManager = AccessPointManager::Create(),
};

SECTION("Create doesn't cause a crash")
{
REQUIRE_NOTHROW(NetRemoteServer{ Test::RemoteServiceAddressHttp });
REQUIRE_NOTHROW(NetRemoteServer{ Configuration });
}
}

TEST_CASE("Destroy a NetRemoteServer instance", "[basic][rpc][remote]")
{
using namespace Microsoft::Net::Remote;
using namespace Microsoft::Net::Remote::Service;
using namespace Microsoft::Net::Wifi;

NetRemoteServerConfiguration Configuration{
.ServerAddress = Test::RemoteServiceAddressHttp,
.AccessPointManager = AccessPointManager::Create(),
};

std::optional<NetRemoteServer> server{ Test::RemoteServiceAddressHttp };
std::optional<NetRemoteServer> server{ Configuration };
server->Run();

SECTION("Destroy doesn't cause a crash")
Expand All @@ -48,8 +60,14 @@ TEST_CASE("NetRemoteServer can be reached", "[basic][rpc][remote]")
{
using namespace Microsoft::Net::Remote;
using namespace Microsoft::Net::Remote::Service;
using namespace Microsoft::Net::Wifi;

NetRemoteServerConfiguration Configuration{
.ServerAddress = Test::RemoteServiceAddressHttp,
.AccessPointManager = AccessPointManager::Create(),
};

NetRemoteServer server{ Test::RemoteServiceAddressHttp };
NetRemoteServer server{ Configuration };
server.Run();

SECTION("Can be reached using insecure channel")
Expand All @@ -65,8 +83,14 @@ TEST_CASE("NetRemoteServer shuts down correctly", "[basic][rpc][remote]")
{
using namespace Microsoft::Net::Remote;
using namespace Microsoft::Net::Remote::Service;
using namespace Microsoft::Net::Wifi;

NetRemoteServer server{ Test::RemoteServiceAddressHttp };
NetRemoteServerConfiguration Configuration{
.ServerAddress = Test::RemoteServiceAddressHttp,
.AccessPointManager = AccessPointManager::Create(),
};

NetRemoteServer server{ Configuration };
server.Run();

SECTION("Stop() doesn't cause a crash with no connected clients")
Expand Down Expand Up @@ -114,8 +138,14 @@ TEST_CASE("NetRemoteServer can be cycled through run/stop states", "[basic][rpc]
{
using namespace Microsoft::Net::Remote;
using namespace Microsoft::Net::Remote::Service;
using namespace Microsoft::Net::Wifi;

NetRemoteServerConfiguration Configuration{
.ServerAddress = Test::RemoteServiceAddressHttp,
.AccessPointManager = AccessPointManager::Create(),
};

NetRemoteServer server{ Test::RemoteServiceAddressHttp };
NetRemoteServer server{ Configuration };
REQUIRE_NOTHROW(server.Run());

SECTION("Can be cycled multiple times")
Expand Down
16 changes: 14 additions & 2 deletions tests/unit/TestNetRemoteServiceClient.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <magic_enum.hpp>
#include <microsoft/net/remote/NetRemoteServer.hxx>
#include <microsoft/net/remote/protocol/NetRemoteService.grpc.pb.h>
#include <microsoft/net/wifi/AccessPointManager.hxx>

#include "TestNetRemoteCommon.hxx"

Expand All @@ -17,8 +18,14 @@ TEST_CASE("WifiEnumerateAccessPoints API", "[basic][rpc][client][remote]")
using namespace Microsoft::Net::Remote;
using namespace Microsoft::Net::Remote::Service;
using namespace Microsoft::Net::Remote::Wifi;
using namespace Microsoft::Net::Wifi;

NetRemoteServerConfiguration Configuration{
.ServerAddress = Test::RemoteServiceAddressHttp,
.AccessPointManager = AccessPointManager::Create(),
};

NetRemoteServer server{ Test::RemoteServiceAddressHttp };
NetRemoteServer server{ Configuration };
server.Run();

auto channel = grpc::CreateChannel(Test::RemoteServiceAddressHttp, grpc::InsecureChannelCredentials());
Expand Down Expand Up @@ -63,7 +70,12 @@ TEST_CASE("WifiAccessPointEnable API", "[basic][rpc][client][remote]")

constexpr auto SsidName{ "TestWifiAccessPointEnable" };

NetRemoteServer server{ Test::RemoteServiceAddressHttp };
NetRemoteServerConfiguration Configuration{
.ServerAddress = Test::RemoteServiceAddressHttp,
.AccessPointManager = AccessPointManager::Create(),
};

NetRemoteServer server{ Configuration };
server.Run();

auto channel = grpc::CreateChannel(Test::RemoteServiceAddressHttp, grpc::InsecureChannelCredentials());
Expand Down

0 comments on commit 9596bcf

Please sign in to comment.