[Libreoffice-commits] online.git: loolwsd/Admin.hpp loolwsd/LOOLBroker.cpp loolwsd/Util.cpp loolwsd/Util.hpp

Pranav Kant pranavk at collabora.com
Thu Mar 3 16:23:38 UTC 2016


 loolwsd/Admin.hpp      |   60 +-----------------------------------------
 loolwsd/LOOLBroker.cpp |   70 +------------------------------------------------
 loolwsd/Util.cpp       |   60 ++++++++++++++++++++++++++++++++++++++++++
 loolwsd/Util.hpp       |    4 ++
 4 files changed, 68 insertions(+), 126 deletions(-)

New commits:
commit 190861395141587de2d15752be85b025809cd187
Author: Pranav Kant <pranavk at collabora.com>
Date:   Tue Mar 1 23:47:34 2016 +0530

    loolwsd: Factor out pipe reading polling logic
    
    Change-Id: I7c06c68031ea14147130d21376ab348bdc672a7f
    Reviewed-on: https://gerrit.libreoffice.org/22820
    Reviewed-by: Tor Lillqvist <tml at collabora.com>
    Tested-by: Tor Lillqvist <tml at collabora.com>

diff --git a/loolwsd/Admin.hpp b/loolwsd/Admin.hpp
index 759647b..1216505 100644
--- a/loolwsd/Admin.hpp
+++ b/loolwsd/Admin.hpp
@@ -236,22 +236,12 @@ public:
         // Start a server listening on the admin port.
         _srv.start();
 
-        // Start listening for data changes
-        std::string message;
-        char buffer[READ_BUFFER_SIZE];
-        char* start;
-        char* end;
-
         struct pollfd pollPipeNotify;
-        ssize_t bytes = -1;
 
         pollPipeNotify.fd = NotifyPipe;
         pollPipeNotify.events = POLLIN;
         pollPipeNotify.revents = 0;
 
-        start = buffer;
-        end = buffer;
-
         static const std::string thread_name = "admin_thread";
 
         if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(thread_name.c_str()), 0, 0, 0) != 0)
@@ -259,55 +249,9 @@ public:
 
         Log::info("Thread [" + thread_name + "] started.");
 
-        while (!TerminationFlag)
-        {
-            if (start == end)
-            {
-                if (poll(&pollPipeNotify, 1, POLL_TIMEOUT_MS) < 0)
-                {
-                    Log::error("Failed to poll pipe [" + FIFO_NOTIFY + "].");
-                    continue;
-                }
-                else if (pollPipeNotify.revents & (POLLIN | POLLPRI))
-                {
-                    bytes = Util::readFIFO(NotifyPipe, buffer, sizeof(buffer));
-                    if (bytes < 0)
-                    {
-                        start = end = nullptr;
-                        Log::error("Error reading message from pipe [" + FIFO_NOTIFY + "].");
-                        continue;
-                    }
-                    start = buffer;
-                    end = buffer + bytes;
-                }
-                else if (pollPipeNotify.revents & (POLLERR | POLLHUP))
-                {
-                    Log::error("Broken pipe [" + FIFO_NOTIFY + "] with wsd.");
-                    break;
-                }
-            }
-
-            if (start != end)
-            {
-                char byteChar = *start++;
-                while (start != end && byteChar != '\r' && byteChar != '\n')
-                {
-                    message += byteChar;
-                    byteChar = *start++;
-                }
+        Util::pollPipeForReading(pollPipeNotify, FIFO_NOTIFY, NotifyPipe,
+                                 [this](std::string& message) { return handleInput(message); } );
 
-                if (byteChar == '\r' && *start == '\n')
-                {
-                    start++;
-                    Log::trace("NotifyData: " + message);
-                    if (message == "eof")
-                        break;
-
-                    handleInput(message);
-                    message.clear();
-                }
-            }
-        }
         Log::debug("Thread [" + thread_name + "] finished.");
     }
 
diff --git a/loolwsd/LOOLBroker.cpp b/loolwsd/LOOLBroker.cpp
index a9ace30..2e759cf 100644
--- a/loolwsd/LOOLBroker.cpp
+++ b/loolwsd/LOOLBroker.cpp
@@ -346,21 +346,12 @@ public:
 
     void run() override
     {
-        std::string message;
-        char  buffer[READ_BUFFER_SIZE];
-        char* start;
-        char* end;
-
         struct pollfd pollPipeBroker;
-        ssize_t bytes = -1;
 
         pollPipeBroker.fd = readerBroker;
         pollPipeBroker.events = POLLIN;
         pollPipeBroker.revents = 0;
 
-        start = buffer;
-        end = buffer;
-
         static const std::string thread_name = "brk_pipe_reader";
 
         if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(thread_name.c_str()), 0, 0, 0) != 0)
@@ -368,65 +359,8 @@ public:
 
         Log::debug("Thread [" + thread_name + "] started.");
 
-        while (!TerminationFlag)
-        {
-            if (start == end)
-            {
-                if (poll(&pollPipeBroker, 1, POLL_TIMEOUT_MS) < 0)
-                {
-                    Log::error("Failed to poll pipe [" + FIFO_LOOLWSD + "].");
-                    continue;
-                }
-                else
-                if (pollPipeBroker.revents & (POLLIN | POLLPRI))
-                {
-                    bytes = Util::readFIFO(readerBroker, buffer, sizeof(buffer));
-                    if (bytes < 0)
-                    {
-                        start = end = nullptr;
-                        Log::error("Error reading message from pipe [" + FIFO_LOOLWSD + "].");
-                        continue;
-                    }
-                    start = buffer;
-                    end = buffer + bytes;
-                }
-                else
-                if (pollPipeBroker.revents & (POLLERR | POLLHUP))
-                {
-                    Log::error("Broken pipe [" + FIFO_LOOLWSD + "] with wsd.");
-                    break;
-                }
-            }
-
-            if (start != end)
-            {
-                char byteChar = *start++;
-                while (start != end && byteChar != '\r' && byteChar != '\n')
-                {
-                    message += byteChar;
-                    byteChar = *start++;
-                }
-
-                if (byteChar == '\r' && *start == '\n')
-                {
-                    start++;
-
-                    Log::trace("BrokerFromMaster: " + message);
-                    if (message == "eof")
-                        break;
-
-                    const auto duration = (std::chrono::steady_clock::now() - lastMaintenanceTime);
-                    if (duration >= std::chrono::seconds(10))
-                    {
-                        syncChildren();
-                        lastMaintenanceTime = std::chrono::steady_clock::now();
-                    }
-
-                    handleInput(message);
-                    message.clear();
-                }
-            }
-        }
+        Util::pollPipeForReading(pollPipeBroker, FIFO_LOOLWSD, readerBroker,
+                                 [this](std::string& message) {return handleInput(message); } );
 
         Log::debug("Thread [" + thread_name + "] finished.");
     }
diff --git a/loolwsd/Util.cpp b/loolwsd/Util.cpp
index 5a2c864..4a7789c 100644
--- a/loolwsd/Util.cpp
+++ b/loolwsd/Util.cpp
@@ -544,6 +544,66 @@ namespace Util
             Log::warn("Exception: " + exc.message());
         }
     }
+
+    void pollPipeForReading(pollfd& pollPipe, const std::string& targetPipeName , const int& targetPipe,
+                            std::function<void(std::string& message)> handler)
+    {
+        std::string message;
+        char buffer[READ_BUFFER_SIZE];
+        char* start = buffer;
+        char* end = buffer;
+        ssize_t bytes = -1;
+
+        while (!TerminationFlag)
+        {
+            if (start == end)
+            {
+                if (poll(&pollPipe, 1, POLL_TIMEOUT_MS) < 0)
+                {
+                    Log::error("Failed to poll pipe [" + targetPipeName + "].");
+                    continue;
+                }
+                else if (pollPipe.revents & (POLLIN | POLLPRI))
+                {
+                    bytes = Util::readFIFO(targetPipe, buffer, sizeof(buffer));
+                    if (bytes < 0)
+                    {
+                        start = end = nullptr;
+                        Log::error("Error reading message from pipe [" + targetPipeName + "].");
+                        continue;
+                    }
+                    start = buffer;
+                    end = buffer + bytes;
+                }
+                else if (pollPipe.revents & (POLLERR | POLLHUP))
+                {
+                    Log::error("Broken pipe [" + targetPipeName + "] with wsd.");
+                    break;
+                }
+            }
+
+            if (start != end)
+            {
+                char byteChar = *start++;
+                while (start != end && byteChar != '\r' && byteChar != '\n')
+                {
+                    message += byteChar;
+                    byteChar = *start++;
+                }
+
+                if (byteChar == '\r' && *start == '\n')
+                {
+                    start++;
+                    Log::trace(targetPipeName + " recv: " + message);
+                    if (message == "eof")
+                        break;
+
+                    handler(message);
+                    message.clear();
+                }
+            }
+        }
+    }
 }
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loolwsd/Util.hpp b/loolwsd/Util.hpp
index 0d4f5d2..b4d8251 100644
--- a/loolwsd/Util.hpp
+++ b/loolwsd/Util.hpp
@@ -14,6 +14,7 @@
 #include <sstream>
 #include <functional>
 #include <memory>
+#include <sys/poll.h>
 
 #include <Poco/File.h>
 #include <Poco/Path.h>
@@ -122,6 +123,9 @@ namespace Util
     int getSignalStatus(const int code);
 
     void requestTermination(const Poco::Process::PID& pid);
+
+    void pollPipeForReading(pollfd& pollPipe, const std::string& targetPipeName , const int& targetPipe,
+                            std::function<void(std::string& message)> handler);
 };
 
 //TODO: Move to own file.


More information about the Libreoffice-commits mailing list