Skip to content

Commit 2090e4a

Browse files
Copilotrdementi
andcommitted
Add --listen option to specify listening IP address for pcm-sensor-server
- Added -l/--listen command-line option to accept IP address parameter - Updated startHTTPServer() and startHTTPSServer() to accept listenAddr parameter - Updated help text to document the new --listen option - Improved server startup messages to show binding address - Default behavior unchanged: empty string binds to all interfaces (0.0.0.0) - Examples: -l 127.0.0.1 for localhost only, -l 192.168.1.10 for specific IP Co-authored-by: rdementi <25432609+rdementi@users.noreply.github.com>
1 parent 494b210 commit 2090e4a

1 file changed

Lines changed: 20 additions & 8 deletions

File tree

src/pcm-sensor-server.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,8 +3889,8 @@ void my_get_callback( HTTPServer* hs, HTTPRequest const & req, HTTPResponse & re
38893889
}
38903890
}
38913891

3892-
int startHTTPServer( unsigned short port ) {
3893-
HTTPServer server( "", port );
3892+
int startHTTPServer( const std::string& listenAddr, unsigned short port ) {
3893+
HTTPServer server( listenAddr, port );
38943894
try {
38953895
// HEAD is GET without body, we will remove the body in execute()
38963896
server.registerCallback( HTTPRequestMethod::GET, my_get_callback );
@@ -3904,8 +3904,8 @@ int startHTTPServer( unsigned short port ) {
39043904
}
39053905

39063906
#if defined (USE_SSL)
3907-
int startHTTPSServer( unsigned short port, std::string const & cFile, std::string const & pkFile) {
3908-
HTTPSServer server( "", port );
3907+
int startHTTPSServer( const std::string& listenAddr, unsigned short port, std::string const & cFile, std::string const & pkFile) {
3908+
HTTPSServer server( listenAddr, port );
39093909
try {
39103910
server.setPrivateKeyFile ( pkFile );
39113911
server.setCertificateFile( cFile );
@@ -3932,6 +3932,7 @@ void printHelpText( std::string const & programName ) {
39323932
std::cout << " -s : Use https protocol (default port " << DEFAULT_HTTPS_PORT << ")\n";
39333933
#endif
39343934
std::cout << " -p portnumber : Run on port <portnumber> (default port is " << DEFAULT_HTTP_PORT << ")\n";
3935+
std::cout << " -l|--listen address : Listen on IP address <address> (default: all interfaces)\n";
39353936
std::cout << " -r|--reset : Reset programming of the performance counters.\n";
39363937
std::cout << " -D|--debug level : level = 0: no debug info, > 0 increase verbosity.\n";
39373938
#if !defined(__APPLE__) && !defined(_WIN32)
@@ -3972,6 +3973,7 @@ int mainThrows(int argc, char * argv[]) {
39723973
bool printTopology = false;
39733974
unsigned short port = 0;
39743975
unsigned short debug_level = 0;
3976+
std::string listenAddress = ""; // Empty string means listen on all interfaces
39753977
std::string certificateFile;
39763978
std::string privateKeyFile;
39773979
AcceleratorCounterState *accs_ = AcceleratorCounterState::getInstance();
@@ -4008,6 +4010,14 @@ int mainThrows(int argc, char * argv[]) {
40084010
throw std::runtime_error( "main: Error no port argument given" );
40094011
}
40104012
}
4013+
else if ( check_argument_equals( argv[i], {"-l", "--listen"} ) )
4014+
{
4015+
if ( (++i) < argc ) {
4016+
listenAddress = argv[i];
4017+
} else {
4018+
throw std::runtime_error( "main: Error no listen address argument given" );
4019+
}
4020+
}
40114021
#if defined (USE_SSL)
40124022
else if ( check_argument_equals( argv[i], {"-s"} ) )
40134023
{
@@ -4292,15 +4302,17 @@ int mainThrows(int argc, char * argv[]) {
42924302
if ( useSSL ) {
42934303
if ( port == 0 )
42944304
port = DEFAULT_HTTPS_PORT;
4295-
std::cerr << "Starting SSL enabled server on https://localhost:" << port << "/\n";
4296-
startHTTPSServer( port, certificateFile, privateKeyFile );
4305+
std::string displayAddr = listenAddress.empty() ? "all interfaces" : listenAddress;
4306+
std::cerr << "Starting SSL enabled server on https://" << displayAddr << ":" << port << "/\n";
4307+
startHTTPSServer( listenAddress, port, certificateFile, privateKeyFile );
42974308
} else
42984309
#endif
42994310
{
43004311
if ( port == 0 )
43014312
port = DEFAULT_HTTP_PORT;
4302-
std::cerr << "Starting plain HTTP server on http://localhost:" << port << "/\n";
4303-
startHTTPServer( port );
4313+
std::string displayAddr = listenAddress.empty() ? "all interfaces" : listenAddress;
4314+
std::cerr << "Starting plain HTTP server on http://" << displayAddr << ":" << port << "/\n";
4315+
startHTTPServer( listenAddress, port );
43044316
}
43054317
delete pcmInstance;
43064318
} else if ( pid > 0 ) {

0 commit comments

Comments
 (0)