Skip to content

Commit 9596bcf

Browse files
committed
Add impl.
1 parent 9a126b8 commit 9596bcf

File tree

8 files changed

+110
-31
lines changed

8 files changed

+110
-31
lines changed

src/common/server/NetRemoteServer.cxx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@
99

1010
using namespace Microsoft::Net::Remote;
1111

12-
NetRemoteServer::NetRemoteServer(std::string_view serverAddress) :
13-
m_serverAddress{ serverAddress }
12+
NetRemoteServer::NetRemoteServer(NetRemoteServerConfiguration configuration) :
13+
m_serverAddress(configuration.ServerAddress),
14+
m_service(configuration.AccessPointManager)
1415
{}
1516

1617
NetRemoteServer::~NetRemoteServer()
@@ -42,7 +43,7 @@ NetRemoteServer::Run()
4243
builder.RegisterService(&m_service);
4344

4445
m_server = builder.BuildAndStart();
45-
LOG_INFO << std::format("netremote server started listening on {}", m_serverAddress);
46+
LOG_INFO << std::format("Netremote server started listening on {}", m_serverAddress);
4647
}
4748

4849
void

src/common/server/include/microsoft/net/remote/NetRemoteServer.hxx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
#include <string_view>
88

99
#include <grpcpp/server.h>
10+
#include <microsoft/net/remote/NetRemoteServerConfiguration.hxx>
1011
#include <microsoft/net/remote/NetRemoteService.hxx>
12+
#include <microsoft/net/wifi/AccessPointManager.hxx>
1113

1214
namespace Microsoft::Net::Remote
1315
{
@@ -19,11 +21,11 @@ struct NetRemoteServer
1921
virtual ~NetRemoteServer();
2022

2123
/**
22-
* @brief Construct a new NetRemoteServer object.
24+
* @brief Construct a new NetRemoteServer object with the specified configuration.
2325
*
24-
* @param serverAddress
26+
* @param configuration
2527
*/
26-
NetRemoteServer(std::string_view serverAddress);
28+
NetRemoteServer(NetRemoteServerConfiguration configuration);
2729

2830
/**
2931
* @brief Get the GrpcServer object.
@@ -35,8 +37,8 @@ struct NetRemoteServer
3537

3638
/**
3739
* @brief Get the NetRemoteService object instance.
38-
*
39-
* @return Service::NetRemoteService&
40+
*
41+
* @return Service::NetRemoteService&
4042
*/
4143
Service::NetRemoteService&
4244
GetService() noexcept;

src/common/server/include/microsoft/net/remote/NetRemoteServerConfiguration.hxx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,20 @@
33
#define NET_REMOTE_SERVER_CONFIGURATION_HXX
44

55
#include <cstdint>
6+
#include <memory>
67
#include <string>
78
#include <vector>
89

10+
namespace Microsoft::Net::Wifi
11+
{
12+
class AccessPointManager;
13+
} // namespace Microsoft::Net::Wifi
14+
915
namespace Microsoft::Net::Remote
1016
{
17+
/**
18+
* @brief Collects configuration options for the NetRemoteServer class.
19+
*/
1120
struct NetRemoteServerConfiguration
1221
{
1322
/**
@@ -55,6 +64,11 @@ struct NetRemoteServerConfiguration
5564
* and a level of 3 or above will show all verbose messages.
5665
*/
5766
uint32_t LogVerbosity{ 0 };
67+
68+
/**
69+
* @brief Access point manager instance.
70+
*/
71+
std::shared_ptr<Microsoft::Net::Wifi::AccessPointManager> AccessPointManager;
5872
};
5973

6074
} // namespace Microsoft::Net::Remote

src/common/service/NetRemoteService.cxx

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11

22
#include <format>
3-
#include <iostream>
43
#include <iterator>
54
#include <string>
65
#include <vector>
@@ -13,7 +12,11 @@ using namespace Microsoft::Net::Remote::Service;
1312
using Microsoft::Net::Wifi::AccessPointManager;
1413

1514
NetRemoteService::NetRemoteService() :
16-
m_accessPointManager(AccessPointManager::Create())
15+
NetRemoteService(AccessPointManager::Create())
16+
{}
17+
18+
NetRemoteService::NetRemoteService(std::shared_ptr<AccessPointManager> accessPointManager) :
19+
m_accessPointManager(std::move(accessPointManager))
1720
{}
1821

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

30-
LOG_VERBOSE << std::format("Received WifiEnumerateAccessPoints request\n");
33+
LOGD << std::format("Received WifiEnumerateAccessPoints request");
3134

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

6467
WifiAccessPointOperationStatus status{};
6568

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

8386
WifiAccessPointOperationStatus status{};
8487
// TODO: Disable the access point.

src/common/service/include/microsoft/net/remote/NetRemoteService.hxx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,29 @@
99

1010
namespace Microsoft::Net::Remote::Service
1111
{
12+
/**
13+
* @brief Implementation of the NetRemote::Service gRPC service.
14+
*/
1215
class NetRemoteService :
1316
public NetRemote::Service
1417
{
1518
public:
19+
/**
20+
* @brief Construct a new NetRemoteService object.
21+
*/
1622
NetRemoteService();
1723

24+
/**
25+
* @brief Construct a new NetRemoteService object with the specified access point manager.
26+
*
27+
* @param accessPointManager The access point manager to use.
28+
*/
29+
NetRemoteService(std::shared_ptr<Microsoft::Net::Wifi::AccessPointManager> accessPointManager);
30+
1831
/**
1932
* @brief Get the AccessPointManager object for this service.
20-
*
21-
* @return std::shared_ptr<Microsoft::Net::Wifi::AccessPointManager>
33+
*
34+
* @return std::shared_ptr<Microsoft::Net::Wifi::AccessPointManager>
2235
*/
2336
std::shared_ptr<Microsoft::Net::Wifi::AccessPointManager>
2437
GetAccessPointManager() noexcept;

src/linux/server/Main.cxx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <microsoft/net/remote/NetRemoteServerConfiguration.hxx>
88
#include <microsoft/net/wifi/AccessPointDiscoveryAgent.hxx>
99
#include <microsoft/net/wifi/AccessPointDiscoveryAgentOperationsNetlink.hxx>
10+
#include <microsoft/net/wifi/AccessPointManager.hxx>
1011
#include <notstd/Utility.hxx>
1112
#include <plog/Appenders/ColorConsoleAppender.h>
1213
#include <plog/Appenders/RollingFileAppender.h>
@@ -20,6 +21,7 @@ using namespace Microsoft::Net::Remote;
2021

2122
using Microsoft::Net::Wifi::AccessPointDiscoveryAgent;
2223
using Microsoft::Net::Wifi::AccessPointDiscoveryAgentOperationsNetlink;
24+
using Microsoft::Net::Wifi::AccessPointManager;
2325

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

3739
// Parse command line arguments.
38-
const auto configuration = NetRemoteServerConfiguration::FromCommandLineArguments(argc, argv);
40+
auto configuration = NetRemoteServerConfiguration::FromCommandLineArguments(argc, argv);
3941
const auto logSeverity = logging::LogVerbosityToPlogSeverity(configuration.LogVerbosity);
4042

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

48-
// Create the server.
49-
NetRemoteServer server{ configuration.ServerAddress };
50-
51-
// Create an access point discovery agent, and register it with the server's access point manager.
50+
// Create an access point manager and discovery agent.
5251
{
52+
configuration.AccessPointManager = AccessPointManager::Create();
53+
54+
auto &accessPointManager = configuration.AccessPointManager;
5355
auto accessPointDiscoveryAgentOperationsNetlink = std::make_unique<AccessPointDiscoveryAgentOperationsNetlink>();
5456
auto accessPointDiscoveryAgent = AccessPointDiscoveryAgent::Create(std::move(accessPointDiscoveryAgentOperationsNetlink));
55-
auto accessPointManager = server.GetService().GetAccessPointManager();
5657
accessPointManager->AddDiscoveryAgent(std::move(accessPointDiscoveryAgent));
5758
}
5859

60+
// Create the server.
61+
NetRemoteServer server{ std::move(configuration) };
62+
5963
// Start the server.
60-
LOGI << "Starting netremote server";
64+
LOGI << "Netremote server starting";
6165
server.Run();
6266

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

80-
LOGI << "Server exiting";
84+
LOGI << "Netremote server stopping";
8185

8286
return 0;
8387
}

tests/unit/TestNetRemoteServer.cxx

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,31 @@
1212
TEST_CASE("Create a NetRemoteServer instance", "[basic][rpc][remote]")
1313
{
1414
using namespace Microsoft::Net::Remote;
15+
using namespace Microsoft::Net::Wifi;
16+
17+
NetRemoteServerConfiguration Configuration{
18+
.ServerAddress = Test::RemoteServiceAddressHttp,
19+
.AccessPointManager = AccessPointManager::Create(),
20+
};
1521

1622
SECTION("Create doesn't cause a crash")
1723
{
18-
REQUIRE_NOTHROW(NetRemoteServer{ Test::RemoteServiceAddressHttp });
24+
REQUIRE_NOTHROW(NetRemoteServer{ Configuration });
1925
}
2026
}
2127

2228
TEST_CASE("Destroy a NetRemoteServer instance", "[basic][rpc][remote]")
2329
{
2430
using namespace Microsoft::Net::Remote;
2531
using namespace Microsoft::Net::Remote::Service;
32+
using namespace Microsoft::Net::Wifi;
33+
34+
NetRemoteServerConfiguration Configuration{
35+
.ServerAddress = Test::RemoteServiceAddressHttp,
36+
.AccessPointManager = AccessPointManager::Create(),
37+
};
2638

27-
std::optional<NetRemoteServer> server{ Test::RemoteServiceAddressHttp };
39+
std::optional<NetRemoteServer> server{ Configuration };
2840
server->Run();
2941

3042
SECTION("Destroy doesn't cause a crash")
@@ -48,8 +60,14 @@ TEST_CASE("NetRemoteServer can be reached", "[basic][rpc][remote]")
4860
{
4961
using namespace Microsoft::Net::Remote;
5062
using namespace Microsoft::Net::Remote::Service;
63+
using namespace Microsoft::Net::Wifi;
64+
65+
NetRemoteServerConfiguration Configuration{
66+
.ServerAddress = Test::RemoteServiceAddressHttp,
67+
.AccessPointManager = AccessPointManager::Create(),
68+
};
5169

52-
NetRemoteServer server{ Test::RemoteServiceAddressHttp };
70+
NetRemoteServer server{ Configuration };
5371
server.Run();
5472

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

69-
NetRemoteServer server{ Test::RemoteServiceAddressHttp };
88+
NetRemoteServerConfiguration Configuration{
89+
.ServerAddress = Test::RemoteServiceAddressHttp,
90+
.AccessPointManager = AccessPointManager::Create(),
91+
};
92+
93+
NetRemoteServer server{ Configuration };
7094
server.Run();
7195

7296
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]
114138
{
115139
using namespace Microsoft::Net::Remote;
116140
using namespace Microsoft::Net::Remote::Service;
141+
using namespace Microsoft::Net::Wifi;
142+
143+
NetRemoteServerConfiguration Configuration{
144+
.ServerAddress = Test::RemoteServiceAddressHttp,
145+
.AccessPointManager = AccessPointManager::Create(),
146+
};
117147

118-
NetRemoteServer server{ Test::RemoteServiceAddressHttp };
148+
NetRemoteServer server{ Configuration };
119149
REQUIRE_NOTHROW(server.Run());
120150

121151
SECTION("Can be cycled multiple times")

tests/unit/TestNetRemoteServiceClient.cxx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <magic_enum.hpp>
1010
#include <microsoft/net/remote/NetRemoteServer.hxx>
1111
#include <microsoft/net/remote/protocol/NetRemoteService.grpc.pb.h>
12+
#include <microsoft/net/wifi/AccessPointManager.hxx>
1213

1314
#include "TestNetRemoteCommon.hxx"
1415

@@ -17,8 +18,14 @@ TEST_CASE("WifiEnumerateAccessPoints API", "[basic][rpc][client][remote]")
1718
using namespace Microsoft::Net::Remote;
1819
using namespace Microsoft::Net::Remote::Service;
1920
using namespace Microsoft::Net::Remote::Wifi;
21+
using namespace Microsoft::Net::Wifi;
22+
23+
NetRemoteServerConfiguration Configuration{
24+
.ServerAddress = Test::RemoteServiceAddressHttp,
25+
.AccessPointManager = AccessPointManager::Create(),
26+
};
2027

21-
NetRemoteServer server{ Test::RemoteServiceAddressHttp };
28+
NetRemoteServer server{ Configuration };
2229
server.Run();
2330

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

6471
constexpr auto SsidName{ "TestWifiAccessPointEnable" };
6572

66-
NetRemoteServer server{ Test::RemoteServiceAddressHttp };
73+
NetRemoteServerConfiguration Configuration{
74+
.ServerAddress = Test::RemoteServiceAddressHttp,
75+
.AccessPointManager = AccessPointManager::Create(),
76+
};
77+
78+
NetRemoteServer server{ Configuration };
6779
server.Run();
6880

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

0 commit comments

Comments
 (0)