Skip to content

Commit

Permalink
use sideband_port from config
Browse files Browse the repository at this point in the history
  • Loading branch information
amehra-ni committed Nov 21, 2024
1 parent b5a65e5 commit edf007c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
1 change: 1 addition & 0 deletions source/config/localhost_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"address": "[::1]",
"port": 31763,
"sideband_address": "[::1]",
"sideband_port": 50050,
"security" : {
"server_cert": "",
"server_key": "",
Expand Down
17 changes: 10 additions & 7 deletions source/server/core_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@ using FeatureToggles = nidevice_grpc::FeatureToggles;

struct ServerConfiguration {
std::string config_file_path;
std::string server_address;
std::string sideband_address;
std::string address;
std::string server_cert;
std::string server_key;
std::string root_cert;
int max_message_size;
int server_port;
int sideband_port;
nidevice_grpc::FeatureToggles feature_toggles;
};

Expand All @@ -47,8 +48,9 @@ static ServerConfiguration GetConfiguration(const std::string& config_file_path)
: nidevice_grpc::ServerConfigurationParser(config_file_path);

config.config_file_path = server_config_parser.get_config_file_path();
config.server_address = server_config_parser.parse_address();
config.sideband_address = server_config_parser.parse_sideband_address();
config.address = server_config_parser.parse_bind_address();
config.server_port = server_config_parser.parse_port();
config.sideband_port = server_config_parser.parse_sideband_port();
config.server_cert = server_config_parser.parse_server_cert();
config.server_key = server_config_parser.parse_server_key();
config.root_cert = server_config_parser.parse_root_cert();
Expand Down Expand Up @@ -92,7 +94,8 @@ static void RunServer(const ServerConfiguration& config)
grpc::ServerBuilder builder;
int listeningPort = 0;
nidevice_grpc::ServerSecurityConfiguration server_security_config(config.server_cert, config.server_key, config.root_cert);
builder.AddListeningPort(config.server_address, server_security_config.get_credentials(), &listeningPort);
std::string server_address = config.address + ":" + std::to_string(config.server_port);
builder.AddListeningPort(server_address, server_security_config.get_credentials(), &listeningPort);

auto services = nidevice_grpc::register_all_services(builder, config.feature_toggles);

Expand All @@ -110,7 +113,7 @@ static void RunServer(const ServerConfiguration& config)
}
server = builder.BuildAndStart();
if (ni::data_monikers::is_sideband_streaming_enabled(config.feature_toggles)) {
auto sideband_socket_thread = new std::thread(RunSidebandSocketsAccept, config.sideband_address.c_str(), 50055);
auto sideband_socket_thread = new std::thread(RunSidebandSocketsAccept, config.address.c_str(), config.sideband_port);
// auto sideband_rdma_send_thread = new std::thread(AcceptSidebandRdmaSendRequests);
// auto sideband_rdma_recv_thread = new std::thread(AcceptSidebandRdmaReceiveRequests);
}
Expand All @@ -119,7 +122,7 @@ static void RunServer(const ServerConfiguration& config)
if (!server) {
nidevice_grpc::logging::log(
nidevice_grpc::logging::Level_Error,
"Server failed to start on %s", config.server_address.c_str());
"Server failed to start on %s", server_address.c_str());
exit(EXIT_FAILURE);
}

Expand Down
23 changes: 16 additions & 7 deletions source/server/server_configuration_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace nidevice_grpc {
static const char* kDefaultFilename = "server_config.json";
static const char* kAddressJsonKey = "address";
static const char* kPortJsonKey = "port";
static const char* kSidebandAddressJsonKey = "sideband_address";
static const char* kSidebandPortJsonKey = "sideband_port";
static const char* kServerCertJsonKey = "server_cert";
static const char* kServerKeyJsonKey = "server_key";
static const char* kRootCertJsonKey = "root_cert";
Expand Down Expand Up @@ -102,12 +102,6 @@ std::string ServerConfigurationParser::parse_address() const
return address;
}

std::string ServerConfigurationParser::parse_sideband_address() const
{
auto it = config_file_.find(kSidebandAddressJsonKey);
return it != config_file_.end() ? it->get<std::string>() : "";
}

std::string ServerConfigurationParser::parse_server_cert() const
{
auto file_name = parse_key_from_security_section(kServerCertJsonKey);
Expand Down Expand Up @@ -141,6 +135,21 @@ int ServerConfigurationParser::parse_max_message_size() const
return UNLIMITED_MAX_MESSAGE_SIZE;
}

int ServerConfigurationParser::parse_sideband_port() const
{
auto sideband_port = config_file_.find(kSidebandPortJsonKey);
if (sideband_port != config_file_.end()) {
try {
return sideband_port->get<int>();
}
catch (const nlohmann::json::type_error& ex) {
throw InvalidMaxMessageSizeException(ex.what());
}
}

return DEFAULT_SIDEBAND_PORT;
}

FeatureToggles ServerConfigurationParser::parse_feature_toggles() const
{
FeatureToggleConfigurationMap map;
Expand Down
7 changes: 4 additions & 3 deletions source/server/server_configuration_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ static const char* kInvalidFeatureToggleMessage = "Feature Toggles must be speci
static const char* kInvalidCodeReadinessMessage = "code_readiness must be a string in [Release, RestrictedRelease, NextRelease, RestrictedNextRelease, Incomplete, Prototype].\n\n";
static const char* kDefaultAddress = "[::]";
constexpr int UNLIMITED_MAX_MESSAGE_SIZE = -1;
constexpr int DEFAULT_SIDEBAND_PORT = 50055;

class ServerConfigurationParser {
public:
Expand All @@ -36,11 +37,13 @@ class ServerConfigurationParser {
const std::string& get_config_file_path() const { return config_file_path_; }

std::string parse_address() const;
std::string parse_sideband_address() const;
std::string parse_bind_address() const;
std::string parse_server_cert() const;
std::string parse_server_key() const;
std::string parse_root_cert() const;
int parse_max_message_size() const;
int parse_port() const;
int parse_sideband_port() const;
FeatureToggles parse_feature_toggles() const;
FeatureToggles::CodeReadiness parse_code_readiness() const;

Expand Down Expand Up @@ -101,8 +104,6 @@ class ServerConfigurationParser {
static std::string read_keycert(const std::string& filename);
static std::string get_certs_directory(const std::string& config_file_path);
std::string parse_key_from_security_section(const char* key) const;
std::string parse_bind_address() const;
int parse_port() const;

std::string config_file_path_;
nlohmann::json config_file_;
Expand Down

0 comments on commit edf007c

Please sign in to comment.