From e350ed91d0704a908611ec12d7e166c7ddd62f5a Mon Sep 17 00:00:00 2001 From: Hashan Gayasri Udugahapattuwa Date: Wed, 23 Sep 2020 10:05:20 +0530 Subject: [PATCH 1/4] Use CHARIBEFS_PORT env variable as the Thrift port if set --- cookbook/recipes.py | 5 ++++- python_client.py | 4 +++- server.cc | 11 +++++++++++ tests/common.py | 4 +++- tests/python_client | 4 +++- 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/cookbook/recipes.py b/cookbook/recipes.py index 4cc5afe..1306927 100644 --- a/cookbook/recipes.py +++ b/cookbook/recipes.py @@ -1,6 +1,7 @@ import errno import sys +import os sys.path.append('gen-py') from server import server @@ -27,7 +28,9 @@ def usage(): def connect(): - transport = TSocket.TSocket('127.0.0.1', 9090) + envPort = os.getenv('CHARIBEFS_PORT', '') + port = int(envPort) if envPort else 9090 + transport = TSocket.TSocket('127.0.0.1', port) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = server.Client(protocol) diff --git a/python_client.py b/python_client.py index 8d32281..893a292 100644 --- a/python_client.py +++ b/python_client.py @@ -11,7 +11,9 @@ try: - transport = TSocket.TSocket('127.0.0.1', 9090) + envPort = os.getenv('CHARIBEFS_PORT', '') + port = int(envPort) if envPort else 9090 + transport = TSocket.TSocket('127.0.0.1', port) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = server.Client(protocol) diff --git a/server.cc b/server.cc index fb294e1..645302b 100644 --- a/server.cc +++ b/server.cc @@ -252,6 +252,17 @@ void server_thread() { int port = 9090; + if(const char* envPort = std::getenv("CHARIBEFS_PORT")) { + char *end; + const long envPortValue = strtol(envPort, &end, 10); + if (envPort == end) { + port = static_cast(envPortValue); + } else { + std::cerr << "Invalid port : " << envPort << + ". Using the default value : " << port << std::endl; + } + } + init_valid_methods(); std::cout << "Server Thread started" << std::endl; diff --git a/tests/common.py b/tests/common.py index cd36987..1e25084 100644 --- a/tests/common.py +++ b/tests/common.py @@ -57,7 +57,9 @@ def has_message(method, message): def connect(): - transport = TSocket.TSocket('127.0.0.1', 9090) + envPort = os.getenv('CHARIBEFS_PORT', '') + port = int(envPort) if envPort else 9090 + transport = TSocket.TSocket('127.0.0.1', port) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = server.Client(protocol) diff --git a/tests/python_client b/tests/python_client index 628cc3c..93b4de4 100644 --- a/tests/python_client +++ b/tests/python_client @@ -12,7 +12,9 @@ from thrift.protocol import TBinaryProtocol try: - transport = TSocket.TSocket('127.0.0.1', 9090) + envPort = os.getenv('CHARIBEFS_PORT', '') + port = int(envPort) if envPort else 9090 + transport = TSocket.TSocket('127.0.0.1', port) transport = TTransport.TBufferedTransport(transport) protocol = TBinaryProtocol.TBinaryProtocol(transport) client = server.Client(protocol) From 3d0ae3b3cb31b97e3318ed7a7be6654c620111f9 Mon Sep 17 00:00:00 2001 From: Hashan Gayasri Udugahapattuwa Date: Mon, 12 Oct 2020 13:48:11 +0530 Subject: [PATCH 2/4] Change env variable name CHARIBEFS_PORT -> CHARYBDEFS_PORT --- cookbook/recipes.py | 2 +- python_client.py | 2 +- server.cc | 2 +- tests/common.py | 2 +- tests/python_client | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cookbook/recipes.py b/cookbook/recipes.py index 1306927..b5f9bd3 100644 --- a/cookbook/recipes.py +++ b/cookbook/recipes.py @@ -28,7 +28,7 @@ def usage(): def connect(): - envPort = os.getenv('CHARIBEFS_PORT', '') + envPort = os.getenv('CHARYBDEFS_PORT', '') port = int(envPort) if envPort else 9090 transport = TSocket.TSocket('127.0.0.1', port) transport = TTransport.TBufferedTransport(transport) diff --git a/python_client.py b/python_client.py index 893a292..fb883a6 100644 --- a/python_client.py +++ b/python_client.py @@ -11,7 +11,7 @@ try: - envPort = os.getenv('CHARIBEFS_PORT', '') + envPort = os.getenv('CHARYBDEFS_PORT', '') port = int(envPort) if envPort else 9090 transport = TSocket.TSocket('127.0.0.1', port) transport = TTransport.TBufferedTransport(transport) diff --git a/server.cc b/server.cc index 645302b..7d8639a 100644 --- a/server.cc +++ b/server.cc @@ -252,7 +252,7 @@ void server_thread() { int port = 9090; - if(const char* envPort = std::getenv("CHARIBEFS_PORT")) { + if(const char* envPort = std::getenv("CHARYBDEFS_PORT")) { char *end; const long envPortValue = strtol(envPort, &end, 10); if (envPort == end) { diff --git a/tests/common.py b/tests/common.py index 1e25084..bc5ae69 100644 --- a/tests/common.py +++ b/tests/common.py @@ -57,7 +57,7 @@ def has_message(method, message): def connect(): - envPort = os.getenv('CHARIBEFS_PORT', '') + envPort = os.getenv('CHARYBDEFS_PORT', '') port = int(envPort) if envPort else 9090 transport = TSocket.TSocket('127.0.0.1', port) transport = TTransport.TBufferedTransport(transport) diff --git a/tests/python_client b/tests/python_client index 93b4de4..767f507 100644 --- a/tests/python_client +++ b/tests/python_client @@ -12,7 +12,7 @@ from thrift.protocol import TBinaryProtocol try: - envPort = os.getenv('CHARIBEFS_PORT', '') + envPort = os.getenv('CHARYBDEFS_PORT', '') port = int(envPort) if envPort else 9090 transport = TSocket.TSocket('127.0.0.1', port) transport = TTransport.TBufferedTransport(transport) From 29c173802ee5e72bf242f9dd7b2827f0886a6998 Mon Sep 17 00:00:00 2001 From: Hashan Gayasri Udugahapattuwa Date: Mon, 12 Oct 2020 15:30:14 +0530 Subject: [PATCH 3/4] Replace tabs with 4 spaces for consistency with the surrounding code --- server.cc | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/server.cc b/server.cc index 7d8639a..21da117 100644 --- a/server.cc +++ b/server.cc @@ -252,16 +252,16 @@ void server_thread() { int port = 9090; - if(const char* envPort = std::getenv("CHARYBDEFS_PORT")) { - char *end; - const long envPortValue = strtol(envPort, &end, 10); - if (envPort == end) { - port = static_cast(envPortValue); - } else { - std::cerr << "Invalid port : " << envPort << - ". Using the default value : " << port << std::endl; - } - } + if(const char* envPort = std::getenv("CHARYBDEFS_PORT")) { + char *end; + const long envPortValue = strtol(envPort, &end, 10); + if (envPort == end) { + port = static_cast(envPortValue); + } else { + std::cerr << "Invalid port : " << envPort << + ". Using the default value : " << port << std::endl; + } + } init_valid_methods(); From f8ff3262b55a1b45059d15967e4795b57da13980 Mon Sep 17 00:00:00 2001 From: Hashan Gayasri Udugahapattuwa Date: Tue, 13 Oct 2020 09:06:07 +0530 Subject: [PATCH 4/4] bugfix: fixed bug in validating the value of env var CHARYBDEFS_PORT in server.cc --- server.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/server.cc b/server.cc index 21da117..575dde9 100644 --- a/server.cc +++ b/server.cc @@ -252,10 +252,11 @@ void server_thread() { int port = 9090; - if(const char* envPort = std::getenv("CHARYBDEFS_PORT")) { + if(const char *envPort = std::getenv("CHARYBDEFS_PORT")) { char *end; + errno = 0; const long envPortValue = strtol(envPort, &end, 10); - if (envPort == end) { + if (envPort != end && errno == 0) { port = static_cast(envPortValue); } else { std::cerr << "Invalid port : " << envPort <<