From b2b181f31155e24481e1d9a508c63093c8f20183 Mon Sep 17 00:00:00 2001
From: Stuart Mark James <stuart.james@esrf.fr>
Date: Mon, 4 Jun 2018 11:25:26 +0200
Subject: [PATCH] Fixed crash on start when no proxy setting in env

---
 CHANGELOG.md                   | 11 ++++++++++
 README.md                      | 28 ++++++++++++------------
 TextToSpeech.cpp               | 39 ++--------------------------------
 scripts/setup.sh               |  1 +
 tts_library/PollyTTSDriver.hpp |  2 +-
 tts_library/TTSRunner.hpp      |  2 +-
 tts_library/TTSUtils.cpp       | 11 +++++++++-
 7 files changed, 39 insertions(+), 55 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 23af8c3..0887fd0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -7,6 +7,17 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
 
 ## [Unreleased]
 
+## [1.3.2] - 2018-06-04
+
+### Fixed
+
+* When no HTTP_PROXY was set in the local environment, the device server would crash on starting.
+
+### Changed
+
+* Moved proxy setting into scripts/setup.sh for convenience.
+* Updated README a little to reflect new settings and better explain install
+
 ## [1.3.1] - 2018-05-02
 
 ### Fixed
diff --git a/README.md b/README.md
index c8d9c00..9e6eaa4 100644
--- a/README.md
+++ b/README.md
@@ -123,13 +123,7 @@ The audio files are converted via the build system and placed under the `build`
 
 ### Proxy Settings
 
-If the PC the device server is run on is behind a proxy, then this must be configured in the environment as normal. Set the HTTP_PROXY environment variable to the proxy setting, eg:
-
-```
-export HTTP_PROXY=proxy.location.fr:1234
-```
-
-This setting is also required for the unit tests
+If the PC the device server is run on is behind a proxy, then this must be configured in the environment as normal. Set the HTTP_PROXY environment variable in `scripts/setup.sh` to the correct proxy setting. If no proxy is required, remove this setting from `scripts/setup.sh`.
 
 ### Jingle and File Cache Locations
 
@@ -139,19 +133,23 @@ These are set as properties on the device server. You must ensure the jingles di
 
 A suggested deployment strategy is as follows:
 
-1. Make a directory called TextToSpeechDir in the servers bin directory.
+1. Make a directory called TextToSpeechDir in the servers bin directory (or the intended install location for  TextToSpeech). This directory will be used to hold the audio cache, jingles, scripts and actual TextToSpeech binary.
 2. Copy the TextToSpeech binary into TextToSpeechDir.
 3. Copy script scripts/setup.sh into TextToSpeechDir.
-4. Set the AWS keys in setup.sh to the correct account keys (generate keys via AWS website).
-5. Ensure proxy environment variable is set correctly.
-6. Copy the build/jingles (see [Assets](#Assets)) to TextToSpeechDir.
-7. Copy the script scripts/TextToSpeech to TextToSpeechDirÅ› parent directory (servers bin directory).
-8. Copy the debian9/libs directory to the servers library directory. 
-8. Setup with Astor as normal, except use the script as the executable. 
+    * Set the AWS keys in setup.sh to the correct account keys (generate keys via AWS website).
+    * Set the proxy environment variable in setup.sh to the correct proxy setting.
+4. Copy the build/jingles (see [Assets](#Assets)) to TextToSpeechDir.
+5. Copy the script scripts/TextToSpeech to TextToSpeechDirs parent directory (servers bin directory). This script is used to run the device server. It will call the setup.sh script first and ensure the environment is configured for runtime.
+6. Copy the debian9/libs directory to the servers library directory.
+7. Setup with Astor as normal, except use the TextToSpeech script as the executable.
 
 ## Running Tests
 
-The tts_library is covered by a number of unit tests to verify its functionality. There are based on the Catch2 Unit Test framework and are built by default (since TEXT_TO_SPEECH_BUILD_TESTS is set to ON). The tests require both a working internet connection (for AWS Polly), sound hardware (for PulseAudio) and the proxy to be set as mentioned in [Proxy Settings](#Proxy-Settings). 
+The tts_library is covered by a number of unit tests to verify its functionality. There are based on the Catch2 Unit Test framework and are built by default (since TEXT_TO_SPEECH_BUILD_TESTS is set to ON). The tests require both a working internet connection (for AWS Polly), sound hardware (for PulseAudio) and the proxy to be set. Set the proxy as via an environment variable, i.e:
+
+```
+export HTTP_PROXY=proxy.esrf.fr:3128
+```
 
 To run the tests from the build directory:
 
diff --git a/TextToSpeech.cpp b/TextToSpeech.cpp
index 011d05b..f46b824 100644
--- a/TextToSpeech.cpp
+++ b/TextToSpeech.cpp
@@ -266,48 +266,13 @@ void TextToSpeech::init_device()
 	// tts_library and have to connect to a number of services.
 	if (!tts_runner.initialised())
 	{
-        string proxy_env(getenv("HTTP_PROXY"));
-        string proxy;
-        int port;
-
-        if (!proxy_env.empty())
-        {
-            DEBUG_STREAM << "TextToSpeech::init_device() read proxy " << proxy << endl;
-
-            string port_str = proxy_env.substr(proxy_env.find_last_of(":") + 1, proxy_env.size());
-            proxy = proxy_env.substr(0, proxy_env.find_last_of(":"));
-
-            if (!port_str.empty())
-            {
-                DEBUG_STREAM << "TextToSpeech::init_device() read port " << port_str << endl;
-
-                try
-                {
-                    port = stoi(port_str);
-                }
-                catch (const exception &e)
-                {
-                    ERROR_STREAM << "TextToSpeech::init_device() unable to convert port number, starting with no proxy" << endl;
-                    proxy = "";
-                }
-            }
-            else
-            {
-                WARN_STREAM << "TextToSpeech::init_device() failed to read port, starting with no proxy settings" << endl;
-                proxy = "";
-            }
-        }
-        else
-        {
-            WARN_STREAM << "TextToSpeech::init_device() no HTTP_PROXY env detected, starting with no proxy settings" << endl;
-            proxy = "";
-        }
+        pair<string, int> proxy_settings = get_proxy_settings();
 
 		// space out the bring up, so we can be a little more detailed on failures
 		// to configure the device server correctly
 		try
 		{
-			tts_runner.initialise(proxy, port);
+			tts_runner.initialise(proxy_settings.first, proxy_settings.second);
 		}
 		catch (const exception &e)
 		{
diff --git a/scripts/setup.sh b/scripts/setup.sh
index b11804c..4306172 100755
--- a/scripts/setup.sh
+++ b/scripts/setup.sh
@@ -1,4 +1,5 @@
 #!/bin/bash
 # Setup the runtime environment for the TextToSpeech device server
+export HTTP_PROXY=proxy.esrf.fr:3128
 export AWS_ACCESS_KEY_ID=ADD_YOUR_KEY_ID_HERE
 export AWS_SECRET_ACCESS_KEY=ADD_YOUR_KEY_HERE
diff --git a/tts_library/PollyTTSDriver.hpp b/tts_library/PollyTTSDriver.hpp
index 158abe9..cb0f459 100644
--- a/tts_library/PollyTTSDriver.hpp
+++ b/tts_library/PollyTTSDriver.hpp
@@ -42,7 +42,7 @@ public:
     ~PollyTTSDriver();
 
     // This call will connect to amazon services
-    void initialise(const std::string &proxy, int proxy_port);
+    void initialise(const std::string &proxy = "", int proxy_port = 0);
 
     void destroy();
 
diff --git a/tts_library/TTSRunner.hpp b/tts_library/TTSRunner.hpp
index 83a6a31..2fa7064 100644
--- a/tts_library/TTSRunner.hpp
+++ b/tts_library/TTSRunner.hpp
@@ -64,7 +64,7 @@ public:
     // the stepped init process means we can bring up the base system, and only
     // init other features we are interested in. By doing this, we can catch
     // errors from them systems and handle them differently
-    void initialise(const std::string &proxy, int proxy_port);
+    void initialise(const std::string &proxy = "", int proxy_port = 0);
 
     // if fallback_message is empty, no fallback audio will be generated
     void initialiseCache(const std::string &cache_location, const std::string &fallback_message);
diff --git a/tts_library/TTSUtils.cpp b/tts_library/TTSUtils.cpp
index 7072715..f9058d8 100644
--- a/tts_library/TTSUtils.cpp
+++ b/tts_library/TTSUtils.cpp
@@ -18,9 +18,11 @@
 // along with TextToSpeech.  If not, see <http://www.gnu.org/licenses/>.
 
 #include "TTSUtils.hpp"
+#include "Log.h"
 #include <iostream>
 
 using namespace std;
+using namespace LoggingUtils;
 
 namespace TTSLibrary
 {
@@ -30,10 +32,13 @@ pair<string, int> get_proxy_settings()
 {
     string proxy_env = "";
     string proxy = "";
-    int port;
+    int port = 0;
 
     if (getenv("HTTP_PROXY") != nullptr)
+    {
         proxy_env = getenv("HTTP_PROXY");
+        LOG(Debug) << "Read proxy setting: " << proxy_env << " from environment" << endl;
+    }
 
     if (!proxy_env.empty())
     {
@@ -48,16 +53,20 @@ pair<string, int> get_proxy_settings()
             }
             catch (const exception &e)
             {
+                LOG(Error) << "Unable to convert port number for proxy setting" << endl;
                 proxy = "";
+                port = 0;
             }
         }
         else
         {
+            LOG(Debug) << "No proxy port setting detected" << endl;
             proxy = "";
         }
     }
     else
     {
+        LOG(Debug) << "No proxy setting detected" << endl;
         proxy = "";
     }
 
-- 
GitLab