add call queue check

Added queue check for multiple calls from the main program
This commit is contained in:
zeheyler 2020-10-21 00:10:58 +03:00
parent 3af4217075
commit 8ab1e576bd

View File

@ -33,7 +33,8 @@ QStringList languages
}; };
int docfound = -1, targetNodeId = -1, session = -1, pageenabled = -1, useragentflag = -1; int docfound = -1, targetNodeId = -1, session = -1, pageenabled = -1, useragentflag = -1;
long update = -1; long update = -1, callnumber = 0;
std::vector<long> callqueue;
std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devtools) std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devtools)
{ {
@ -118,6 +119,20 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
useragentflag = 1; useragentflag = 1;
} }
float timer = 0;
int timer_stop = 10;
long calltag = ++callnumber;
callqueue.insert(callqueue.begin(), calltag);
while (callqueue.back() != calltag && timer < 2 * timer_stop)
{
std::this_thread::sleep_for(std::chrono::milliseconds(100));
timer += 0.1;
}
if (timer >= timer_stop)
{
callqueue.pop_back();
return { false, FormatString(L"%s: %d ", ERROR_GOT_TIMEOUT, 2 * timer_stop) };
}
long navigate = devtools->methodToReceive("Page.navigatedWithinDocument"); long navigate = devtools->methodToReceive("Page.navigatedWithinDocument");
long target = devtools->methodToReceive("DOM.attributeModified", { { "value" , "lmt__mobile_share_container" } }); long target = devtools->methodToReceive("DOM.attributeModified", { { "value" , "lmt__mobile_share_container" } });
if (update == -1) if (update == -1)
@ -129,12 +144,12 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
QString fullurl = URL + "#ja/" + S(translateTo.Copy()) + "/" + qtext; QString fullurl = URL + "#ja/" + S(translateTo.Copy()) + "/" + qtext;
if (!devtools->SendRequest("Page.navigate", { {"url", fullurl} }, root)) if (!devtools->SendRequest("Page.navigate", { {"url", fullurl} }, root))
{ {
callqueue.pop_back();
return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) }; return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) };
} }
// Wait until page is loaded // Wait until page is loaded
float timer = 0; timer = 0;
int timer_stop = 10;
while (!devtools->checkMethod(navigate) && timer < timer_stop) while (!devtools->checkMethod(navigate) && timer < timer_stop)
{ {
std::this_thread::sleep_for(std::chrono::milliseconds(100)); std::this_thread::sleep_for(std::chrono::milliseconds(100));
@ -142,6 +157,7 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
} }
if (timer >= timer_stop) if (timer >= timer_stop)
{ {
callqueue.pop_back();
return { false, FormatString(L"%s: %d ", ERROR_GOT_TIMEOUT, timer_stop) }; return { false, FormatString(L"%s: %d ", ERROR_GOT_TIMEOUT, timer_stop) };
} }
@ -158,6 +174,7 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
{ {
if (!devtools->SendRequest("DOM.getDocument", {}, root)) if (!devtools->SendRequest("DOM.getDocument", {}, root))
{ {
callqueue.pop_back();
return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) }; return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) };
} }
docfound = root.value("result").toObject().value("root").toObject().value("nodeId").toInt(); docfound = root.value("result").toObject().value("root").toObject().value("nodeId").toInt();
@ -170,6 +187,7 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
|| root.value("result").toObject().value("nodeId").toInt() == 0) || root.value("result").toObject().value("nodeId").toInt() == 0)
{ {
docfound = -1; docfound = -1;
callqueue.pop_back();
return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) }; return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) };
} }
targetNodeId = root.value("result").toObject().value("nodeId").toInt(); targetNodeId = root.value("result").toObject().value("nodeId").toInt();
@ -188,9 +206,11 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
{ {
docfound = -1; docfound = -1;
targetNodeId = -1; targetNodeId = -1;
callqueue.pop_back();
return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) }; return { false, FormatString(L"%s", ERROR_COMMAND_FAIL) };
} }
QString OuterHTML = root.value("result").toObject().value("outerHTML").toString(); QString OuterHTML = root.value("result").toObject().value("outerHTML").toString();
callqueue.pop_back();
if (OuterHTML == "<div></div>") if (OuterHTML == "<div></div>")
{ {
// Try to catch the notification // Try to catch the notification
@ -231,6 +251,5 @@ std::pair<bool, std::wstring> Translate(const std::wstring& text, DevTools* devt
} }
} }
} }
return { true, S(OuterHTML) }; return { true, S(OuterHTML) };
} }