[Libreoffice-commits] online.git: loolwsd/ClientSession.cpp loolwsd/ClientSession.hpp loolwsd/PrisonerSession.cpp
Ashod Nakashian
ashod.nakashian at collabora.co.uk
Tue May 17 03:16:18 UTC 2016
loolwsd/ClientSession.cpp | 113 +---------------
loolwsd/ClientSession.hpp | 23 +--
loolwsd/PrisonerSession.cpp | 309 --------------------------------------------
3 files changed, 23 insertions(+), 422 deletions(-)
New commits:
commit 7b0da5ceae4a7c5f8ce3ee390ae89102a8fb64cd
Author: Ashod Nakashian <ashod.nakashian at collabora.co.uk>
Date: Mon May 16 19:20:35 2016 -0400
loolwsd: MasterProcessSession splitting: moved ClientSession handlers
Change-Id: I1a660cd17027e1c7290882a301a15b77b3e4940f
Reviewed-on: https://gerrit.libreoffice.org/25042
Reviewed-by: Ashod Nakashian <ashnakash at gmail.com>
Tested-by: Ashod Nakashian <ashnakash at gmail.com>
diff --git a/loolwsd/ClientSession.cpp b/loolwsd/ClientSession.cpp
index 401831d..b4b8205 100644
--- a/loolwsd/ClientSession.cpp
+++ b/loolwsd/ClientSession.cpp
@@ -21,7 +21,7 @@
#include "LOOLWSD.hpp"
#include "ClientSession.hpp"
#include "PrisonerSession.hpp"
-#include "MasterProcessSession.hpp"
+#include "ClientSession.hpp"
#include "Rectangle.hpp"
#include "Storage.hpp"
#include "TileCache.hpp"
@@ -200,45 +200,7 @@ bool ClientSession::loadDocument(const char* /*buffer*/, int /*length*/, StringT
return false;
}
-/*
-void ClientSession::setEditLock(const bool value)
-{
- // Update the sate and forward to child.
- _bEditLock = value;
- const auto msg = std::string("editlock: ") + (value ? "1" : "0");
- forwardToPeer(msg.data(), msg.size());
-}
-*/
-
-#if 0
-using namespace LOOLProtocol;
-
-using Poco::Path;
-using Poco::StringTokenizer;
-
-MasterProcessSession::MasterProcessSession(const std::string& id,
- const Kind kind,
- std::shared_ptr<Poco::Net::WebSocket> ws,
- std::shared_ptr<DocumentBroker> docBroker,
- std::shared_ptr<BasicTileQueue> queue) :
- LOOLSession(id, kind, ws),
- _curPart(0),
- _loadPart(-1),
- _docBroker(docBroker),
- _queue(queue)
-{
- Log::info("MasterProcessSession ctor [" + getName() + "].");
-}
-
-MasterProcessSession::~MasterProcessSession()
-{
- Log::info("~MasterProcessSession dtor [" + getName() + "].");
-
- // Release the save-as queue.
- _saveAsQueue.put("");
-}
-
-bool MasterProcessSession::getStatus(const char *buffer, int length)
+bool ClientSession::getStatus(const char *buffer, int length)
{
const std::string status = _docBroker->tileCache().getTextFile("status.txt");
if (!status.empty())
@@ -263,16 +225,16 @@ bool MasterProcessSession::getStatus(const char *buffer, int length)
forwardToPeer(buffer, length);
return true;
}
-
-void MasterProcessSession::setEditLock(const bool value)
+/*
+void ClientSession::setEditLock(const bool value)
{
// Update the sate and forward to child.
_bEditLock = value;
const auto msg = std::string("editlock: ") + (value ? "1" : "0");
forwardToPeer(msg.data(), msg.size());
}
-
-bool MasterProcessSession::getCommandValues(const char *buffer, int length, StringTokenizer& tokens)
+*/
+bool ClientSession::getCommandValues(const char *buffer, int length, StringTokenizer& tokens)
{
std::string command;
if (tokens.count() != 2 || !getTokenString(tokens[1], "command", command))
@@ -294,7 +256,7 @@ bool MasterProcessSession::getCommandValues(const char *buffer, int length, Stri
return true;
}
-bool MasterProcessSession::getPartPageRectangles(const char *buffer, int length)
+bool ClientSession::getPartPageRectangles(const char *buffer, int length)
{
const std::string partPageRectangles = _docBroker->tileCache().getTextFile("partpagerectangles.txt");
if (partPageRectangles.size() > 0)
@@ -309,13 +271,7 @@ bool MasterProcessSession::getPartPageRectangles(const char *buffer, int length)
return true;
}
-std::string MasterProcessSession::getSaveAs()
-{
- const auto payload = _saveAsQueue.get();
- return std::string(payload.data(), payload.size());
-}
-
-void MasterProcessSession::sendFontRendering(const char *buffer, int length, StringTokenizer& tokens)
+void ClientSession::sendFontRendering(const char *buffer, int length, StringTokenizer& tokens)
{
std::string font;
if (tokens.count() < 2 ||
@@ -351,7 +307,7 @@ void MasterProcessSession::sendFontRendering(const char *buffer, int length, Str
forwardToPeer(buffer, length);
}
-void MasterProcessSession::sendTile(const char * /*buffer*/, int /*length*/, StringTokenizer& tokens)
+void ClientSession::sendTile(const char * /*buffer*/, int /*length*/, StringTokenizer& tokens)
{
try
{
@@ -365,7 +321,7 @@ void MasterProcessSession::sendTile(const char * /*buffer*/, int /*length*/, Str
}
}
-void MasterProcessSession::sendCombinedTiles(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
+void ClientSession::sendCombinedTiles(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
{
int part, pixelWidth, pixelHeight, tileWidth, tileHeight;
std::string tilePositionsX, tilePositionsY;
@@ -438,53 +394,4 @@ void MasterProcessSession::sendCombinedTiles(const char* /*buffer*/, int /*lengt
}
}
-void MasterProcessSession::dispatchChild()
-{
- std::ostringstream oss;
- oss << "load";
- oss << " url=" << _docBroker->getPublicUri().toString();
- oss << " jail=" << _docBroker->getJailedUri().toString();
-
- if (_loadPart >= 0)
- oss << " part=" + std::to_string(_loadPart);
-
- if (_haveDocPassword)
- oss << " password=" << _docPassword;
-
- if (!_docOptions.empty())
- oss << " options=" << _docOptions;
-
- const auto loadRequest = oss.str();
- forwardToPeer(loadRequest.c_str(), loadRequest.size());
-}
-
-void MasterProcessSession::forwardToPeer(const char *buffer, int length)
-{
- const auto message = getAbbreviatedMessage(buffer, length);
-
- auto peer = _peer.lock();
- if (!peer)
- {
- throw Poco::ProtocolException(getName() + ": no peer to forward to: [" + message + "].");
- }
- else if (peer->isCloseFrame())
- {
- Log::trace(getName() + ": peer began the closing handshake. Dropping forward message [" + message + "].");
- return;
- }
-
- Log::trace(getName() + " -> " + peer->getName() + ": " + message);
- peer->sendBinaryFrame(buffer, length);
-}
-
-bool MasterProcessSession::shutdownPeer(Poco::UInt16 statusCode, const std::string& message)
-{
- auto peer = _peer.lock();
- if (peer && !peer->isCloseFrame())
- {
- peer->_ws->shutdown(statusCode, message);
- }
- return peer != nullptr;
-}
-#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/loolwsd/ClientSession.hpp b/loolwsd/ClientSession.hpp
index 19f27e1..1f4bb74 100644
--- a/loolwsd/ClientSession.hpp
+++ b/loolwsd/ClientSession.hpp
@@ -52,9 +52,20 @@ public:
private:
virtual bool _handleInput(const char *buffer, int length) override;
+ virtual bool getStatus(const char *buffer, int length) override;
+
+ virtual bool getCommandValues(const char *buffer, int length, Poco::StringTokenizer& tokens) override;
+
+ virtual bool getPartPageRectangles(const char *buffer, int length) override;
bool loadDocument(const char *buffer, int length, Poco::StringTokenizer& tokens);
+ virtual void sendTile(const char *buffer, int length, Poco::StringTokenizer& tokens);
+
+ virtual void sendCombinedTiles(const char *buffer, int length, Poco::StringTokenizer& tokens);
+
+ virtual void sendFontRendering(const char *buffer, int length, Poco::StringTokenizer& tokens) override;
+
private:
// If this document holds the edit lock.
@@ -74,12 +85,6 @@ private:
std::shared_ptr<BasicTileQueue> queue);
virtual ~MasterProcessSession();
- virtual bool getStatus(const char *buffer, int length) override;
-
- virtual bool getCommandValues(const char *buffer, int length, Poco::StringTokenizer& tokens) override;
-
- virtual bool getPartPageRectangles(const char *buffer, int length) override;
-
/**
* Return the URL of the saved-as document when it's ready. If called
* before it's ready, the call blocks till then.
@@ -97,12 +102,6 @@ public:
bool _bLoadError = false;
protected:
- virtual void sendTile(const char *buffer, int length, Poco::StringTokenizer& tokens);
-
- virtual void sendCombinedTiles(const char *buffer, int length, Poco::StringTokenizer& tokens);
-
- virtual void sendFontRendering(const char *buffer, int length, Poco::StringTokenizer& tokens) override;
-
private:
void dispatchChild();
void forwardToPeer(const char *buffer, int length);
diff --git a/loolwsd/PrisonerSession.cpp b/loolwsd/PrisonerSession.cpp
index 9257616..4078d71 100644
--- a/loolwsd/PrisonerSession.cpp
+++ b/loolwsd/PrisonerSession.cpp
@@ -21,7 +21,7 @@
#include "LOOLWSD.hpp"
#include "ClientSession.hpp"
#include "PrisonerSession.hpp"
-#include "MasterProcessSession.hpp"
+#include "PrisonerSession.hpp"
#include "Rectangle.hpp"
#include "Storage.hpp"
#include "TileCache.hpp"
@@ -119,7 +119,7 @@ bool PrisonerSession::_handleInput(const char *buffer, int length)
if (peer)
{
// Save as completed, inform the other (Kind::ToClient)
- // MasterProcessSession about it.
+ // PrisonerSession about it.
const std::string filePrefix("file:///");
if (url.find(filePrefix) == 0)
@@ -213,309 +213,4 @@ bool PrisonerSession::_handleInput(const char *buffer, int length)
return true;
}
-#if 0
-using namespace LOOLProtocol;
-
-using Poco::Path;
-using Poco::StringTokenizer;
-
-MasterProcessSession::MasterProcessSession(const std::string& id,
- const Kind kind,
- std::shared_ptr<Poco::Net::WebSocket> ws,
- std::shared_ptr<DocumentBroker> docBroker,
- std::shared_ptr<BasicTileQueue> queue) :
- LOOLSession(id, kind, ws),
- _curPart(0),
- _loadPart(-1),
- _docBroker(docBroker),
- _queue(queue)
-{
- Log::info("MasterProcessSession ctor [" + getName() + "].");
-}
-
-MasterProcessSession::~MasterProcessSession()
-{
- Log::info("~MasterProcessSession dtor [" + getName() + "].");
-
- // Release the save-as queue.
- _saveAsQueue.put("");
-}
-
-bool MasterProcessSession::loadDocument(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
-{
- if (tokens.count() < 2)
- {
- sendTextFrame("error: cmd=load kind=syntax");
- return false;
- }
-
- try
- {
- std::string timestamp;
- parseDocOptions(tokens, _loadPart, timestamp);
-
- // Finally, wait for the Child to connect to Master,
- // link the document in jail and dispatch load to child.
- Log::trace("Dispatching child to handle [load].");
- dispatchChild();
-
- return true;
- }
- catch (const Poco::SyntaxException&)
- {
- sendTextFrame("error: cmd=load kind=uriinvalid");
- }
-
- return false;
-}
-
-bool MasterProcessSession::getStatus(const char *buffer, int length)
-{
- const std::string status = _docBroker->tileCache().getTextFile("status.txt");
- if (!status.empty())
- {
- sendTextFrame(status);
-
- // And let clients know if they hold the edit lock.
- std::string message = "editlock: ";
- message += std::to_string(isEditLocked());
- Log::debug("Forwarding [" + message + "] in response to status.");
- sendTextFrame(message);
-
- return true;
- }
-
- if (_peer.expired())
- {
- Log::trace("Dispatching child to handle [getStatus].");
- dispatchChild();
- }
-
- forwardToPeer(buffer, length);
- return true;
-}
-
-void MasterProcessSession::setEditLock(const bool value)
-{
- // Update the sate and forward to child.
- _bEditLock = value;
- const auto msg = std::string("editlock: ") + (value ? "1" : "0");
- forwardToPeer(msg.data(), msg.size());
-}
-
-bool MasterProcessSession::getCommandValues(const char *buffer, int length, StringTokenizer& tokens)
-{
- std::string command;
- if (tokens.count() != 2 || !getTokenString(tokens[1], "command", command))
- {
- sendTextFrame("error: cmd=commandvalues kind=syntax");
- return false;
- }
-
- const std::string cmdValues = _docBroker->tileCache().getTextFile("cmdValues" + command + ".txt");
- if (cmdValues.size() > 0)
- {
- sendTextFrame(cmdValues);
- return true;
- }
-
- if (_peer.expired())
- dispatchChild();
- forwardToPeer(buffer, length);
- return true;
-}
-
-bool MasterProcessSession::getPartPageRectangles(const char *buffer, int length)
-{
- const std::string partPageRectangles = _docBroker->tileCache().getTextFile("partpagerectangles.txt");
- if (partPageRectangles.size() > 0)
- {
- sendTextFrame(partPageRectangles);
- return true;
- }
-
- if (_peer.expired())
- dispatchChild();
- forwardToPeer(buffer, length);
- return true;
-}
-
-std::string MasterProcessSession::getSaveAs()
-{
- const auto payload = _saveAsQueue.get();
- return std::string(payload.data(), payload.size());
-}
-
-void MasterProcessSession::sendFontRendering(const char *buffer, int length, StringTokenizer& tokens)
-{
- std::string font;
- if (tokens.count() < 2 ||
- !getTokenString(tokens[1], "font", font))
- {
- sendTextFrame("error: cmd=renderfont kind=syntax");
- return;
- }
-
- const std::string response = "renderfont: " + Poco::cat(std::string(" "), tokens.begin() + 1, tokens.end()) + "\n";
-
- std::vector<char> output;
- output.resize(response.size());
- std::memcpy(output.data(), response.data(), response.size());
-
- std::unique_ptr<std::fstream> cachedRendering = _docBroker->tileCache().lookupRendering(font, "font");
- if (cachedRendering && cachedRendering->is_open())
- {
- cachedRendering->seekg(0, std::ios_base::end);
- size_t pos = output.size();
- std::streamsize size = cachedRendering->tellg();
- output.resize(pos + size);
- cachedRendering->seekg(0, std::ios_base::beg);
- cachedRendering->read(output.data() + pos, size);
- cachedRendering->close();
-
- sendBinaryFrame(output.data(), output.size());
- return;
- }
-
- if (_peer.expired())
- dispatchChild();
- forwardToPeer(buffer, length);
-}
-
-void MasterProcessSession::sendTile(const char * /*buffer*/, int /*length*/, StringTokenizer& tokens)
-{
- try
- {
- auto tileDesc = TileDesc::parse(tokens);
- _docBroker->handleTileRequest(tileDesc, shared_from_this());
- }
- catch (const std::exception& exc)
- {
- Log::error(std::string("Failed to process tile command: ") + exc.what() + ".");
- sendTextFrame("error: cmd=tile kind=invalid");
- }
-}
-
-void MasterProcessSession::sendCombinedTiles(const char* /*buffer*/, int /*length*/, StringTokenizer& tokens)
-{
- int part, pixelWidth, pixelHeight, tileWidth, tileHeight;
- std::string tilePositionsX, tilePositionsY;
- if (tokens.count() < 8 ||
- !getTokenInteger(tokens[1], "part", part) ||
- !getTokenInteger(tokens[2], "width", pixelWidth) ||
- !getTokenInteger(tokens[3], "height", pixelHeight) ||
- !getTokenString (tokens[4], "tileposx", tilePositionsX) ||
- !getTokenString (tokens[5], "tileposy", tilePositionsY) ||
- !getTokenInteger(tokens[6], "tilewidth", tileWidth) ||
- !getTokenInteger(tokens[7], "tileheight", tileHeight))
- {
- sendTextFrame("error: cmd=tilecombine kind=syntax");
- return;
- }
-
- if (part < 0 || pixelWidth <= 0 || pixelHeight <= 0 ||
- tileWidth <= 0 || tileHeight <= 0 ||
- tilePositionsX.empty() || tilePositionsY.empty())
- {
- sendTextFrame("error: cmd=tilecombine kind=invalid");
- return;
- }
-
- std::string reqTimestamp;
- size_t index = 8;
- if (tokens.count() > index && tokens[index].find("timestamp") == 0)
- {
- getTokenString(tokens[index], "timestamp", reqTimestamp);
- ++index;
- }
-
- int id = -1;
- if (tokens.count() > index && tokens[index].find("id") == 0)
- {
- getTokenInteger(tokens[index], "id", id);
- ++index;
- }
-
- StringTokenizer positionXtokens(tilePositionsX, ",", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
- StringTokenizer positionYtokens(tilePositionsY, ",", StringTokenizer::TOK_IGNORE_EMPTY | StringTokenizer::TOK_TRIM);
-
- size_t numberOfPositions = positionYtokens.count();
-
- // check that number of positions for X and Y is the same
- if (numberOfPositions != positionXtokens.count())
- {
- sendTextFrame("error: cmd=tilecombine kind=invalid");
- return;
- }
-
- for (size_t i = 0; i < numberOfPositions; ++i)
- {
- int x = 0;
- if (!stringToInteger(positionXtokens[i], x))
- {
- sendTextFrame("error: cmd=tilecombine kind=syntax");
- return;
- }
-
- int y = 0;
- if (!stringToInteger(positionYtokens[i], y))
- {
- sendTextFrame("error: cmd=tilecombine kind=syntax");
- return;
- }
-
- const TileDesc tile(part, pixelWidth, pixelHeight, x, y, tileWidth, tileHeight);
- _docBroker->handleTileRequest(tile, shared_from_this());
- }
-}
-
-void MasterProcessSession::dispatchChild()
-{
- std::ostringstream oss;
- oss << "load";
- oss << " url=" << _docBroker->getPublicUri().toString();
- oss << " jail=" << _docBroker->getJailedUri().toString();
-
- if (_loadPart >= 0)
- oss << " part=" + std::to_string(_loadPart);
-
- if (_haveDocPassword)
- oss << " password=" << _docPassword;
-
- if (!_docOptions.empty())
- oss << " options=" << _docOptions;
-
- const auto loadRequest = oss.str();
- forwardToPeer(loadRequest.c_str(), loadRequest.size());
-}
-
-void MasterProcessSession::forwardToPeer(const char *buffer, int length)
-{
- const auto message = getAbbreviatedMessage(buffer, length);
-
- auto peer = _peer.lock();
- if (!peer)
- {
- throw Poco::ProtocolException(getName() + ": no peer to forward to: [" + message + "].");
- }
- else if (peer->isCloseFrame())
- {
- Log::trace(getName() + ": peer began the closing handshake. Dropping forward message [" + message + "].");
- return;
- }
-
- Log::trace(getName() + " -> " + peer->getName() + ": " + message);
- peer->sendBinaryFrame(buffer, length);
-}
-
-bool MasterProcessSession::shutdownPeer(Poco::UInt16 statusCode, const std::string& message)
-{
- auto peer = _peer.lock();
- if (peer && !peer->isCloseFrame())
- {
- peer->_ws->shutdown(statusCode, message);
- }
- return peer != nullptr;
-}
-#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
More information about the Libreoffice-commits
mailing list