From 6e6b2d39de743a8f1924425688dd3e0f4b622775 Mon Sep 17 00:00:00 2001
From: Sergey Linev <S.Linev@gsi.de>
Date: Fri, 24 Aug 2018 15:37:51 +0200
Subject: [PATCH] webgui: adjust TWebWindowsManager configuration interface

Instead of using static variables just configure gEnv->SetValue()
parameters directly. Adjust tutorial
---
 .../inc/ROOT/TWebWindowsManager.hxx           | 16 +++--
 gui/webdisplay/src/TWebWindow.cxx             |  4 +-
 gui/webdisplay/src/TWebWindowsManager.cxx     | 66 ++++---------------
 tutorials/v7/draw_mt.cxx                      |  5 +-
 4 files changed, 25 insertions(+), 66 deletions(-)

diff --git a/gui/webdisplay/inc/ROOT/TWebWindowsManager.hxx b/gui/webdisplay/inc/ROOT/TWebWindowsManager.hxx
index 1168ac3424a..45313d93e70 100644
--- a/gui/webdisplay/inc/ROOT/TWebWindowsManager.hxx
+++ b/gui/webdisplay/inc/ROOT/TWebWindowsManager.hxx
@@ -44,10 +44,17 @@ private:
    std::mutex fMutex;                    ///<!  main mutex to protect
    int fMutexBooked{0};                  ///<!  flag indicating that mutex is booked for some long operation
    std::thread::id fBookedThrd;          ///<!  thread where mutex is booked, can be reused
-   unsigned fIdCnt{0};                   ///<! counter for identifiers
+   unsigned fIdCnt{0};                   ///<!  counter for identifiers
+   bool fUseHttpThrd{false};             ///<!  use special thread for THttpServer
+   bool fUseSenderThreads{false};        ///<!  use extra threads for sending data from RWebWindow to clients
    // std::list<std::shared_ptr<TWebWindow>> fDisplays;   ///<! list of existing displays (not used at the moment)
 
-   static bool IsUseHttpThread();
+   /// Returns true if extra threads to send data via websockets will be used (default off)
+   bool IsUseHttpThread() const { return fUseHttpThrd; }
+
+   /// Returns true if extra threads to send data via websockets will be used (default off)
+   bool IsUseSenderThreads() const { return fUseSenderThreads; }
+
    bool CreateHttpServer(bool with_http = false);
 
    void Unregister(TWebWindow &win);
@@ -70,11 +77,6 @@ public:
    /// Returns THttpServer instance
    THttpServer *GetServer() const { return fServer.get(); }
 
-   static void SetUseHttpThread(bool on = true);
-
-   static void SetUseSenderThreads(bool on = true);
-   static bool IsUseSenderThreads();
-
    static std::shared_ptr<TWebWindowsManager> &Instance();
 
    static bool IsMainThrd();
diff --git a/gui/webdisplay/src/TWebWindow.cxx b/gui/webdisplay/src/TWebWindow.cxx
index 7151c2ae660..e25e8213600 100644
--- a/gui/webdisplay/src/TWebWindow.cxx
+++ b/gui/webdisplay/src/TWebWindow.cxx
@@ -109,7 +109,7 @@ void ROOT::Experimental::TWebWindow::SetPanelName(const std::string &name)
 void ROOT::Experimental::TWebWindow::CreateWSHandler()
 {
    if (!fWSHandler) {
-      fSendMT = TWebWindowsManager::IsUseSenderThreads();
+      fSendMT = fMgr->IsUseSenderThreads();
       fWSHandler = std::make_shared<TWebWindowWSHandler>(*this, Form("win%u", GetId()));
    }
 }
@@ -723,7 +723,7 @@ void ROOT::Experimental::TWebWindow::SetDataCallBack(WebWindowDataCallback_t fun
    fDataThrdId = std::this_thread::get_id();
    if (!TWebWindowsManager::IsMainThrd()) {
       fProcessMT = true;
-   } else if (TWebWindowsManager::IsUseHttpThread()) {
+   } else if (fMgr->IsUseHttpThread()) {
       // special thread is used by the manager, but main thread used for the canvas - not supported
       R__ERROR_HERE("webgui") << "create web window from main thread when THttpServer created with special thread - not supported";
    }
diff --git a/gui/webdisplay/src/TWebWindowsManager.cxx b/gui/webdisplay/src/TWebWindowsManager.cxx
index d62d25f78a0..2ccef04032b 100644
--- a/gui/webdisplay/src/TWebWindowsManager.cxx
+++ b/gui/webdisplay/src/TWebWindowsManager.cxx
@@ -45,9 +45,6 @@
 namespace ROOT {
 namespace Experimental {
 
-static int sUseHttpThread = -1; // undefined
-static int sUseSenderThreads = -1; // undefined
-
 class TWebWindowManagerGuard {
 
    TWebWindowsManager &fMgr;
@@ -150,54 +147,6 @@ ROOT::Experimental::TWebWindowsManager::~TWebWindowsManager()
       gApplication->Disconnect("Terminate(Int_t)", "THttpServer", fServer.get(), "SetTerminate()");
 }
 
-
-//////////////////////////////////////////////////////////////////////////////////////////
-/// Configure usage of extra thread for the processing of THttpServer requests (default off)
-/// Required when application does not run main ROOT event loop or
-/// when many different instances of THttpServer should be used in the system
-/// When enabled, TWebWindow MUST run in there own threads, using TWebWindow::Run() functionality
-/// Equivalent to configuring option in rootrc file:
-///      WebGui.HttpThrd: yes | no
-
-void ROOT::Experimental::TWebWindowsManager::SetUseHttpThread(bool on)
-{
-   sUseHttpThread = on ? 1 : 0;
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////
-/// Returns true if extra thread will be used for THttpServer processing (default false)
-/// If not configured before, WebGui.HttpThrd value from rootrc file is used
-
-bool ROOT::Experimental::TWebWindowsManager::IsUseHttpThread()
-{
-   if (sUseHttpThread > 0) return true;
-   if (sUseHttpThread == 0) return false;
-
-   const char *serv_thrd = gEnv->GetValue("WebGui.HttpThrd", "no");
-   return (serv_thrd && strstr(serv_thrd, "yes"));
-}
-
-
-//////////////////////////////////////////////////////////////////////////////////////////
-/// Configure usage of extra threads to send data via websockets to connection (default off)
-/// Can be very useful when many (slow) clients connected to the same window and consume much resources
-/// Equivalent to configuring option in rootrc file:
-///      WebGui.SenderThrds: yes | no
-
-void ROOT::Experimental::TWebWindowsManager::SetUseSenderThreads(bool on)
-{
-   sUseSenderThreads = on ? 1 : 0;
-}
-
-//////////////////////////////////////////////////////////////////////////////////////////
-/// Returns true if extra threads to send data via websockets will be used (default off)
-
-bool ROOT::Experimental::TWebWindowsManager::IsUseSenderThreads()
-{
-   return (sUseSenderThreads > 0);
-}
-
-
 //////////////////////////////////////////////////////////////////////////////////////////
 /// Creates http server, if required - with real http engine (civetweb)
 /// One could configure concrete HTTP port, which should be used for the server,
@@ -246,6 +195,12 @@ bool ROOT::Experimental::TWebWindowsManager::CreateHttpServer(bool with_http)
 
       fServer = std::make_unique<THttpServer>("basic_sniffer");
 
+      const char *serv_thrd = gEnv->GetValue("WebGui.HttpThrd", "");
+      if (serv_thrd && strstr(serv_thrd, "yes"))
+         fUseHttpThrd = true;
+      else if (serv_thrd && strstr(serv_thrd, "no"))
+         fUseHttpThrd = false;
+
       if (IsUseHttpThread())
          fServer->CreateServerThread();
 
@@ -269,10 +224,11 @@ bool ROOT::Experimental::TWebWindowsManager::CreateHttpServer(bool with_http)
    const char *http_ssl = gEnv->GetValue("WebGui.UseHttps", "no");
    const char *ssl_cert = gEnv->GetValue("WebGui.ServerCert", "rootserver.pem");
 
-   if (sUseSenderThreads < 0) {
-      const char *send_thrds = gEnv->GetValue("WebGui.SenderThrds", "");
-      if (send_thrds && strstr(send_thrds,"yes")) sUseSenderThreads = 1;
-   }
+   const char *send_thrds = gEnv->GetValue("WebGui.SenderThrds", "");
+   if (send_thrds && strstr(send_thrds, "yes"))
+      fUseSenderThreads = true;
+   else if (send_thrds && strstr(send_thrds, "no"))
+      fUseSenderThreads = false;
 
    bool assign_loopback = http_loopback && strstr(http_loopback, "yes");
    bool use_secure = http_ssl && strstr(http_ssl, "yes");
diff --git a/tutorials/v7/draw_mt.cxx b/tutorials/v7/draw_mt.cxx
index 4ca5a1904c4..cb9b6f8e0a0 100644
--- a/tutorials/v7/draw_mt.cxx
+++ b/tutorials/v7/draw_mt.cxx
@@ -29,6 +29,7 @@ R__LOAD_LIBRARY(libROOTGpadv7);
 #include "ROOT/TWebWindowsManager.hxx"
 
 #include "TRandom3.h"
+#include "TEnv.h"
 
 #include <thread>
 // #include <chrono>
@@ -83,8 +84,8 @@ void draw_canvas(const std::string &title, RColor col)
 
 void draw_mt()
 {
-   TWebWindowsManager::SetUseHttpThread(true);
-   TWebWindowsManager::SetUseSenderThreads(true);
+   gEnv->SetValue("WebGui.HttpThrd","yes");
+   gEnv->SetValue("WebGui.SenderThrds","yes");
 
    std::thread thrd1(draw_canvas, "First canvas", RColor::kRed);
    std::thread thrd2(draw_canvas, "Second canvas", RColor::kBlue);
-- 
GitLab