diff --git a/src/common/server/NetRemoteServer.cxx b/src/common/server/NetRemoteServer.cxx index d8f2ad19..72ac40bd 100644 --- a/src/common/server/NetRemoteServer.cxx +++ b/src/common/server/NetRemoteServer.cxx @@ -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() @@ -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 diff --git a/src/common/server/include/microsoft/net/remote/NetRemoteServer.hxx b/src/common/server/include/microsoft/net/remote/NetRemoteServer.hxx index 7021efd8..63a4d92f 100644 --- a/src/common/server/include/microsoft/net/remote/NetRemoteServer.hxx +++ b/src/common/server/include/microsoft/net/remote/NetRemoteServer.hxx @@ -7,7 +7,9 @@ #include #include +#include #include +#include namespace Microsoft::Net::Remote { @@ -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. @@ -35,8 +37,8 @@ struct NetRemoteServer /** * @brief Get the NetRemoteService object instance. - * - * @return Service::NetRemoteService& + * + * @return Service::NetRemoteService& */ Service::NetRemoteService& GetService() noexcept; diff --git a/src/common/server/include/microsoft/net/remote/NetRemoteServerConfiguration.hxx b/src/common/server/include/microsoft/net/remote/NetRemoteServerConfiguration.hxx index a049b7b1..e6d90722 100644 --- a/src/common/server/include/microsoft/net/remote/NetRemoteServerConfiguration.hxx +++ b/src/common/server/include/microsoft/net/remote/NetRemoteServerConfiguration.hxx @@ -3,11 +3,20 @@ #define NET_REMOTE_SERVER_CONFIGURATION_HXX #include +#include #include #include +namespace Microsoft::Net::Wifi +{ +class AccessPointManager; +} // namespace Microsoft::Net::Wifi + namespace Microsoft::Net::Remote { +/** + * @brief Collects configuration options for the NetRemoteServer class. + */ struct NetRemoteServerConfiguration { /** @@ -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 AccessPointManager; }; } // namespace Microsoft::Net::Remote diff --git a/src/common/service/NetRemoteService.cxx b/src/common/service/NetRemoteService.cxx index 07c32820..a7e648a6 100644 --- a/src/common/service/NetRemoteService.cxx +++ b/src/common/service/NetRemoteService.cxx @@ -1,6 +1,5 @@ #include -#include #include #include #include @@ -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) : + m_accessPointManager(std::move(accessPointManager)) {} std::shared_ptr @@ -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 accessPointResultItems(std::size(accessPoints)); @@ -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{}; @@ -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. diff --git a/src/common/service/include/microsoft/net/remote/NetRemoteService.hxx b/src/common/service/include/microsoft/net/remote/NetRemoteService.hxx index 8f9843a7..84dc0ec0 100644 --- a/src/common/service/include/microsoft/net/remote/NetRemoteService.hxx +++ b/src/common/service/include/microsoft/net/remote/NetRemoteService.hxx @@ -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 accessPointManager); + /** * @brief Get the AccessPointManager object for this service. - * - * @return std::shared_ptr + * + * @return std::shared_ptr */ std::shared_ptr GetAccessPointManager() noexcept; diff --git a/src/linux/server/Main.cxx b/src/linux/server/Main.cxx index 4a028629..e9e3d28d 100644 --- a/src/linux/server/Main.cxx +++ b/src/linux/server/Main.cxx @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -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. @@ -35,7 +37,7 @@ main(int argc, char *argv[]) static plog::RollingFileAppender 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. @@ -45,19 +47,21 @@ main(int argc, char *argv[]) .addAppender(plog::get()) .addAppender(plog::get()); - // 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(); 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. @@ -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); } } @@ -77,7 +81,7 @@ main(int argc, char *argv[]) server.GetGrpcServer()->Wait(); } - LOGI << "Server exiting"; + LOGI << "Netremote server stopping"; return 0; } diff --git a/tests/unit/TestNetRemoteServer.cxx b/tests/unit/TestNetRemoteServer.cxx index 306d1113..b7fa6ec2 100644 --- a/tests/unit/TestNetRemoteServer.cxx +++ b/tests/unit/TestNetRemoteServer.cxx @@ -12,10 +12,16 @@ 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 }); } } @@ -23,8 +29,14 @@ 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 server{ Test::RemoteServiceAddressHttp }; + std::optional server{ Configuration }; server->Run(); SECTION("Destroy doesn't cause a crash") @@ -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") @@ -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") @@ -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") diff --git a/tests/unit/TestNetRemoteServiceClient.cxx b/tests/unit/TestNetRemoteServiceClient.cxx index caa30b02..b5bf6136 100644 --- a/tests/unit/TestNetRemoteServiceClient.cxx +++ b/tests/unit/TestNetRemoteServiceClient.cxx @@ -9,6 +9,7 @@ #include #include #include +#include #include "TestNetRemoteCommon.hxx" @@ -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()); @@ -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());