loolwsd: revert, refactor lokit Document

I did not consider shared document case.
This commit is contained in:
Henry Castro 2016-01-09 07:27:57 -04:00
parent f3735f145f
commit d20e9399fc

View file

@ -470,13 +470,14 @@ private:
class Document
{
public:
Document(LibreOfficeKit *loKit, const std::string& jailId)
Document(LibreOfficeKit *loKit, const std::string& jailId,
const std::string& url)
: _loKit(loKit),
_jailId(jailId),
_url(""),
_url(url),
_loKitDocument(nullptr)
{
Log::info("Document ctor on child [" + jailId + "].");
Log::info("Document ctor for url [" + url + "] on child [" + jailId + "].");
}
~Document()
@ -505,10 +506,9 @@ public:
}
}
void createSession(const std::string& sessionId, const std::string& url)
void createSession(const std::string& sessionId)
{
const auto& aItem = _connections.find(sessionId);
_url = url;
if (aItem != _connections.end())
{
@ -579,19 +579,13 @@ public:
return _connections.size() > 0;
}
const std::string& getURL()
{
return _url;
}
private:
void onLoad(LibreOfficeKitDocument *loKitDocument, const int viewId)
{
Log::info("Document [" + _url + "] loaded as view #" + std::to_string(viewId) + ".");
// TODO. destroy lokit document when changed URL
// if (_loKitDocument != nullptr)
// assert(_loKitDocument == loKitDocument);
if (_loKitDocument != nullptr)
assert(_loKitDocument == loKitDocument);
_loKitDocument = loKitDocument;
}
@ -604,7 +598,7 @@ private:
LibreOfficeKit *_loKit;
const std::string _jailId;
std::string _url;
const std::string _url;
LibreOfficeKitDocument *_loKitDocument;
@ -627,6 +621,8 @@ void lokit_main(const std::string &loSubPath, const std::string& jailId, const s
assert(!jailId.empty());
assert(!loSubPath.empty());
std::map<std::string, std::shared_ptr<Document>> _documents;
static const std::string process_name = "loolkit";
#ifdef __linux
if (prctl(PR_SET_NAME, reinterpret_cast<unsigned long>(process_name.c_str()), 0, 0, 0) != 0)
@ -650,9 +646,6 @@ void lokit_main(const std::string &loSubPath, const std::string& jailId, const s
exit(-1);
}
// Singlenton instance
std::shared_ptr<Document> pDocument(std::make_shared<Document>(loKit.get(), jailId));
try
{
int writerBroker;
@ -729,16 +722,14 @@ void lokit_main(const std::string &loSubPath, const std::string& jailId, const s
if (tokens[0] == "search")
{
// remove unloaded documents
pDocument->purgeSessions();
if (!pDocument->hasConnections())
if (_documents.empty())
{
aResponse += "empty \r\n";
}
else
{
aResponse += (pDocument->getURL() == tokens[1] ? "ok \r\n" : "no \r\n");
const auto& it = _documents.find(tokens[1]);
aResponse += (it != _documents.end() ? "ok \r\n" : "no \r\n");
}
}
else if (tokens[0] == "thread")
@ -746,16 +737,13 @@ void lokit_main(const std::string &loSubPath, const std::string& jailId, const s
const std::string& sessionId = tokens[1];
const std::string& url = tokens[2];
if (!pDocument->hasConnections() || pDocument->getURL() == url)
{
Log::debug("Thread request for session [" + sessionId + "], url: [" + url + "].");
pDocument->createSession(sessionId, url);
aResponse += "ok \r\n";
}
else
{
aResponse += "no \r\n";
}
Log::debug("Thread request for session [" + sessionId + "], url: [" + url + "].");
auto it = _documents.lower_bound(url);
if (it == _documents.end())
it = _documents.emplace_hint(it, url, std::make_shared<Document>(loKit.get(), jailId, url));
it->second->createSession(sessionId);
aResponse += "ok \r\n";
}
else
{
@ -786,7 +774,7 @@ void lokit_main(const std::string &loSubPath, const std::string& jailId, const s
Log::error(std::string("Exception: ") + exc.what());
}
pDocument.reset();
_documents.clear();
// Destroy LibreOfficeKit
loKit->pClass->destroy(loKit.get());