[Libreoffice-commits] online.git: 2 commits - loolwsd/Admin.cpp loolwsd/AdminModel.cpp loolwsd/DocumentBroker.cpp loolwsd/DocumentBroker.hpp loolwsd/LOOLWSD.cpp

Ashod Nakashian ashod.nakashian at collabora.co.uk
Sun Oct 23 21:05:39 UTC 2016


 loolwsd/Admin.cpp          |    1 
 loolwsd/AdminModel.cpp     |   97 ++++++++++++++++++++++++---------------------
 loolwsd/DocumentBroker.cpp |   16 +++++++
 loolwsd/DocumentBroker.hpp |    7 ---
 loolwsd/LOOLWSD.cpp        |    8 ---
 5 files changed, 71 insertions(+), 58 deletions(-)

New commits:
commit 6c0be6d90dbb0808f667467bd67dc6b7582bcda0
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sat Oct 22 10:25:57 2016 -0400

    loolwsd: move admin updates into DocumentBroker
    
    Change-Id: Ic35198a7e4457a775fac25954279501e37b94b42
    Reviewed-on: https://gerrit.libreoffice.org/30199
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/DocumentBroker.cpp b/loolwsd/DocumentBroker.cpp
index 61c1fc5..ca10d50 100644
--- a/loolwsd/DocumentBroker.cpp
+++ b/loolwsd/DocumentBroker.cpp
@@ -17,6 +17,7 @@
 #include <Poco/SHA1Engine.h>
 #include <Poco/StringTokenizer.h>
 
+#include "Admin.hpp"
 #include "ClientSession.hpp"
 #include "Exceptions.hpp"
 #include "LOOLProtocol.hpp"
@@ -162,6 +163,15 @@ DocumentBroker::DocumentBroker(const Poco::URI& uriPublic,
     Log::info("DocumentBroker [" + _uriPublic.toString() + "] created. DocKey: [" + _docKey + "]");
 }
 
+DocumentBroker::~DocumentBroker()
+{
+    Admin::instance().rmDoc(_docKey);
+
+    Log::info() << "~DocumentBroker [" << _uriPublic.toString()
+                << "] destroyed with " << getSessionsCount()
+                << " sessions left." << Log::end;
+}
+
 bool DocumentBroker::load(const std::string& sessionId, const std::string& jailId)
 {
     {
@@ -464,6 +474,9 @@ size_t DocumentBroker::addSession(std::shared_ptr<ClientSession>& session)
         throw;
     }
 
+    // Tell the admin console about this new doc
+    Admin::instance().addDoc(_docKey, getPid(), getFilename(), id);
+
     auto prisonerSession = std::make_shared<PrisonerSession>(id, shared_from_this());
 
     // Connect the prison session to the client.
@@ -504,6 +517,9 @@ size_t DocumentBroker::removeSession(const std::string& id)
         _childProcess->sendTextFrame(msg);
     }
 
+    // Lets remove this session from the admin console too
+    Admin::instance().rmDoc(_docKey, id);
+
     return _sessions.size();
 }
 
diff --git a/loolwsd/DocumentBroker.hpp b/loolwsd/DocumentBroker.hpp
index 9472fb9..170613c 100644
--- a/loolwsd/DocumentBroker.hpp
+++ b/loolwsd/DocumentBroker.hpp
@@ -173,12 +173,7 @@ public:
                    const std::string& childRoot,
                    const std::shared_ptr<ChildProcess>& childProcess);
 
-    ~DocumentBroker()
-    {
-        Log::info() << "~DocumentBroker [" << _uriPublic.toString()
-                    << "] destroyed with " << getSessionsCount()
-                    << " sessions left." << Log::end;
-    }
+    ~DocumentBroker();
 
     /// Loads a document from the public URI into the jail.
     bool load(const std::string& sessionId, const std::string& jailId);
diff --git a/loolwsd/LOOLWSD.cpp b/loolwsd/LOOLWSD.cpp
index e0723e6..ef88a77 100644
--- a/loolwsd/LOOLWSD.cpp
+++ b/loolwsd/LOOLWSD.cpp
@@ -831,9 +831,6 @@ private:
                 ws->sendFrame(status.data(), (int) status.size());
             }
 
-            // Tell the admin console about this new doc
-            Admin::instance().addDoc(docKey, docBroker->getPid(), docBroker->getFilename(), id);
-
             LOOLWSD::dumpEventTrace(docBroker->getJailId(), id, "NewSession: " + uri);
 
             // Let messages flow.
@@ -883,9 +880,6 @@ private:
                     sessionsCount = docBroker->removeSession(id);
                     Log::trace(docKey + ", ws_sessions--: " + std::to_string(sessionsCount));
                 }
-
-                // Lets remove this session from the admin console too
-                Admin::instance().rmDoc(docKey, id);
             }
 
             if (sessionsCount == 0)
@@ -897,8 +891,6 @@ private:
                 --LOOLWSD::NumDocBrokers;
                 logNumDocBrokers(__LINE__);
 #endif
-                Log::info("Removing complete doc [" + docKey + "] from Admin.");
-                Admin::instance().rmDoc(docKey);
             }
 
             LOOLWSD::dumpEventTrace(docBroker->getJailId(), id, "EndSession: " + uri);
commit 2e9b3ae247879d199af80e52ef250bfe20038078
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date:   Sat Oct 22 10:24:27 2016 -0400

    loolwsd: admin cleanups
    
    Change-Id: I1ed9cdfe1c78665722ed517218ce45055c661b69
    Reviewed-on: https://gerrit.libreoffice.org/30198
    Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
    Tested-by: Ashod Nakashian <ashnakash at gmail.com>

diff --git a/loolwsd/Admin.cpp b/loolwsd/Admin.cpp
index 3ef735f..04d479b 100644
--- a/loolwsd/Admin.cpp
+++ b/loolwsd/Admin.cpp
@@ -317,6 +317,7 @@ void Admin::rmDoc(const std::string& docKey, const std::string& sessionId)
 void Admin::rmDoc(const std::string& docKey)
 {
     std::unique_lock<std::mutex> modelLock(_modelMutex);
+    Log::info("Removing complete doc [" + docKey + "] from Admin.");
     _model.removeDocument(docKey);
 }
 
diff --git a/loolwsd/AdminModel.cpp b/loolwsd/AdminModel.cpp
index a025400..45afd2a 100644
--- a/loolwsd/AdminModel.cpp
+++ b/loolwsd/AdminModel.cpp
@@ -20,6 +20,7 @@
 #include <Poco/StringTokenizer.h>
 #include <Poco/URI.h>
 
+#include "LOOLProtocol.hpp"
 #include "Log.hpp"
 #include "Unit.hpp"
 #include "Util.hpp"
@@ -56,64 +57,70 @@ int Document::expireView(const std::string& sessionId)
 
 bool Subscriber::notify(const std::string& message)
 {
-    StringTokenizer tokens(message, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
-
-    if (_subscriptions.find(tokens[0]) == _subscriptions.end())
+    if (_subscriptions.find(LOOLProtocol::getFirstToken(message)) == _subscriptions.end())
+    {
+        // No subscribers for the given message.
         return true;
+    }
 
     auto webSocket = _ws.lock();
     if (webSocket)
     {
-        UnitWSD::get().onAdminNotifyMessage(message);
-        webSocket->sendFrame(message.data(), message.length());
-        return true;
-    }
-    else
-    {
-        return false;
+        try
+        {
+            UnitWSD::get().onAdminNotifyMessage(message);
+            webSocket->sendFrame(message.data(), message.length());
+            return true;
+        }
+        catch (const std::exception& ex)
+        {
+            Log::error() << "Failed to notify Admin subscriber with message ["
+                         << message << "] due to [" << ex.what() << "]." << Log::end;
+        }
     }
+
+    return false;
 }
 
-bool  Subscriber::subscribe(const std::string& command)
+bool Subscriber::subscribe(const std::string& command)
 {
     auto ret = _subscriptions.insert(command);
     return ret.second;
 }
 
-void  Subscriber::unsubscribe(const std::string& command)
+void Subscriber::unsubscribe(const std::string& command)
 {
     _subscriptions.erase(command);
 }
 
 std::string AdminModel::query(const std::string& command)
 {
-    StringTokenizer tokens(command, " ", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
-
-    if (tokens[0] == "documents")
+    const auto token = LOOLProtocol::getFirstToken(command);
+    if (token == "documents")
     {
         return getDocuments();
     }
-    else if (tokens[0] == "active_users_count")
+    else if (token == "active_users_count")
     {
         return std::to_string(getTotalActiveViews());
     }
-    else if (tokens[0] == "active_docs_count")
+    else if (token == "active_docs_count")
     {
         return std::to_string(_documents.size());
     }
-    else if (tokens[0] == "mem_stats")
+    else if (token == "mem_stats")
     {
         return getMemStats();
     }
-    else if (tokens[0] == "mem_stats_size")
+    else if (token == "mem_stats_size")
     {
         return std::to_string(_memStatsSize);
     }
-    else if (tokens[0] == "cpu_stats")
+    else if (token == "cpu_stats")
     {
         return getCpuStats();
     }
-    else if (tokens[0] == "cpu_stats_size")
+    else if (token == "cpu_stats_size")
     {
         return std::to_string(_cpuStatsSize);
     }
@@ -127,10 +134,14 @@ unsigned AdminModel::getTotalMemoryUsage()
     unsigned totalMem = 0;
     for (auto& it: _documents)
     {
-        if (it.second.isExpired())
-            continue;
-
-        totalMem += Util::getMemoryUsage(it.second.getPid());
+        if (!it.second.isExpired())
+        {
+            const int mem = Util::getMemoryUsage(it.second.getPid());
+            if (mem > 0)
+            {
+                totalMem += mem;
+            }
+        }
     }
 
     return totalMem;
@@ -148,19 +159,19 @@ void AdminModel::subscribe(int nSessionId, std::shared_ptr<Poco::Net::WebSocket>
 void AdminModel::subscribe(int nSessionId, const std::string& command)
 {
     auto subscriber = _subscribers.find(nSessionId);
-    if (subscriber == _subscribers.end() )
-        return;
-
-    subscriber->second.subscribe(command);
+    if (subscriber != _subscribers.end())
+    {
+        subscriber->second.subscribe(command);
+    }
 }
 
 void AdminModel::unsubscribe(int nSessionId, const std::string& command)
 {
     auto subscriber = _subscribers.find(nSessionId);
-    if (subscriber == _subscribers.end())
-        return;
-
-    subscriber->second.unsubscribe(command);
+    if (subscriber != _subscribers.end())
+    {
+        subscriber->second.unsubscribe(command);
+    }
 }
 
 void AdminModel::addMemStats(unsigned memUsage)
@@ -225,6 +236,7 @@ void AdminModel::setMemStatsSize(unsigned size)
 
 void AdminModel::notify(const std::string& message)
 {
+    Log::debug("Message to admin console: " + message);
     auto it = std::begin(_subscribers);
     while (it != std::end(_subscribers))
     {
@@ -234,7 +246,7 @@ void AdminModel::notify(const std::string& message)
         }
         else
         {
-            it++;
+            ++it;
         }
     }
 }
@@ -246,7 +258,7 @@ void AdminModel::addDocument(const std::string& docKey, Poco::Process::PID pid,
     ret.first->second.addView(sessionId);
 
     // Notify the subscribers
-    unsigned memUsage = Util::getMemoryUsage(pid);
+    const unsigned memUsage = Util::getMemoryUsage(pid);
     std::ostringstream oss;
     std::string encodedFilename;
     Poco::URI::encode(filename, " ", encodedFilename);
@@ -255,7 +267,6 @@ void AdminModel::addDocument(const std::string& docKey, Poco::Process::PID pid,
         << encodedFilename << " "
         << sessionId << " "
         << std::to_string(memUsage);
-    Log::info("Message to admin console: " + oss.str());
     notify(oss.str());
 }
 
@@ -269,7 +280,6 @@ void AdminModel::removeDocument(const std::string& docKey, const std::string& se
         oss << "rmdoc "
             << docIt->second.getPid() << " "
             << sessionId;
-        Log::info("Message to admin console: " + oss.str());
         notify(oss.str());
 
         // TODO: The idea is to only expire the document and keep the history
@@ -294,7 +304,6 @@ void AdminModel::removeDocument(const std::string& docKey)
             oss << "rmdoc "
                 << docIt->second.getPid() << " "
                 << pair.first;
-            Log::info("Message to admin console: " + oss.str());
             notify(oss.str());
         }
 
@@ -304,24 +313,24 @@ void AdminModel::removeDocument(const std::string& docKey)
 
 std::string AdminModel::getMemStats()
 {
-    std::string response;
+    std::ostringstream oss;
     for (auto& i: _memStats)
     {
-        response += std::to_string(i) + ",";
+        oss << i << ',';
     }
 
-    return response;
+    return oss.str();
 }
 
 std::string AdminModel::getCpuStats()
 {
-    std::string response;
+    std::ostringstream oss;
     for (auto& i: _cpuStats)
     {
-        response += std::to_string(i) + ",";
+        oss << i << ',';
     }
 
-    return response;
+    return oss.str();
 }
 
 unsigned AdminModel::getTotalActiveViews()


More information about the Libreoffice-commits mailing list