HelperLauncher.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. //
  2. // Created by Tobias Hieta on 28/08/15.
  3. //
  4. #include "HelperLauncher.h"
  5. #include "QsLog.h"
  6. #include "settings/SettingsComponent.h"
  7. #include "settings/SettingsSection.h"
  8. #include "utils/Utils.h"
  9. #include "Names.h"
  10. #include <QTimer>
  11. /////////////////////////////////////////////////////////////////////////////////////////
  12. HelperLauncher::HelperLauncher(QObject* parent) : QObject(parent)
  13. {
  14. start();
  15. }
  16. /////////////////////////////////////////////////////////////////////////////////////////
  17. void HelperLauncher::start()
  18. {
  19. m_jsonClient = new LocalJsonClient("pmpHelper");
  20. connect(m_jsonClient, &QLocalSocket::connected, this, &HelperLauncher::didConnect);
  21. connect(m_jsonClient, static_cast<void (QLocalSocket::*)(QLocalSocket::LocalSocketError)>(&QLocalSocket::error), this, &HelperLauncher::socketError);
  22. connect(m_jsonClient, &LocalJsonClient::messageReceived, this, &HelperLauncher::gotMessage);
  23. connect(m_jsonClient, &QLocalSocket::disconnected, this, &HelperLauncher::socketDisconnect);
  24. #ifdef Q_OS_MAC
  25. m_launchd = new HelperLaunchd(this);
  26. #endif
  27. m_helperProcess = new QProcess(this);
  28. connect(SettingsComponent::Get().getSection(SETTINGS_SECTION_WEBCLIENT), &SettingsSection::valuesUpdated, [=](const QVariantMap& values)
  29. {
  30. if (values.contains("clientID"))
  31. updateClientId();
  32. });
  33. }
  34. /////////////////////////////////////////////////////////////////////////////////////////
  35. bool HelperLauncher::connectToHelper()
  36. {
  37. if (!helperEnabled())
  38. return true;
  39. if (m_jsonClient->state() == QLocalSocket::ConnectedState ||
  40. m_jsonClient->state() == QLocalSocket::ConnectingState)
  41. return true;
  42. QLOG_DEBUG() << "Connecting to helper";
  43. m_jsonClient->connectToServer();
  44. return true;
  45. }
  46. /////////////////////////////////////////////////////////////////////////////////////////
  47. bool HelperLauncher::killHelper()
  48. {
  49. if (!helperEnabled())
  50. return true;
  51. QVariantMap msg;
  52. msg.insert("command", "quit");
  53. m_jsonClient->sendMessage(msg);
  54. if (m_jsonClient->waitForBytesWritten(1000))
  55. {
  56. QLOG_DEBUG() << "Waiting for helper to die";
  57. if (!m_jsonClient->waitForDisconnected(3000))
  58. {
  59. QLOG_ERROR() << "Helper refused to disconnect. Didn't it get the PM?";
  60. return false;
  61. }
  62. QLOG_DEBUG() << "Helper gone";
  63. return true;
  64. }
  65. return false;
  66. }
  67. /////////////////////////////////////////////////////////////////////////////////////////
  68. void HelperLauncher::gotMessage(const QVariantMap& message)
  69. {
  70. if (message.value("version").toString() != Version::GetVersionString())
  71. {
  72. QLOG_WARN() << "Running helper does not match our current version. Killing it and starting a new one.";
  73. killHelper();
  74. }
  75. else
  76. {
  77. QLOG_DEBUG() << "Helper is running version:" << message.value("version").toString();
  78. }
  79. }
  80. /////////////////////////////////////////////////////////////////////////////////////////
  81. void HelperLauncher::socketError(QLocalSocket::LocalSocketError error)
  82. {
  83. QLOG_DEBUG() << "Failed to connect to helper:" << m_jsonClient->errorString();
  84. if (error == QLocalSocket::ConnectionRefusedError ||
  85. error == QLocalSocket::ServerNotFoundError)
  86. launch();
  87. }
  88. /////////////////////////////////////////////////////////////////////////////////////////
  89. void HelperLauncher::socketDisconnect()
  90. {
  91. QLOG_DEBUG() << "Disconnected from helper, trying to relaunch";
  92. connectToHelper();
  93. }
  94. /////////////////////////////////////////////////////////////////////////////////////////
  95. void HelperLauncher::updateClientId()
  96. {
  97. if (!helperEnabled())
  98. return;
  99. // update clientId if we have it
  100. if (!SettingsComponent::Get().value(SETTINGS_SECTION_WEBCLIENT, "clientID").toString().isEmpty())
  101. {
  102. QVariantMap msg;
  103. msg.insert("command", "info");
  104. QVariantMap arg;
  105. arg.insert("clientId", SettingsComponent::Get().value(SETTINGS_SECTION_WEBCLIENT, "clientID").toString());
  106. QString userId = Utils::CurrentUserId();
  107. if (!userId.isEmpty())
  108. arg.insert("userId", userId);
  109. msg.insert("argument", arg);
  110. m_jsonClient->sendMessage(msg);
  111. }
  112. };
  113. /////////////////////////////////////////////////////////////////////////////////////////
  114. bool HelperLauncher::helperEnabled()
  115. {
  116. return SettingsComponent::Get().value(SETTINGS_SECTION_MAIN, "useHelper").toBool();
  117. }
  118. /////////////////////////////////////////////////////////////////////////////////////////
  119. void HelperLauncher::didConnect()
  120. {
  121. QLOG_DEBUG() << "Connected to helper";
  122. updateClientId();
  123. }
  124. /////////////////////////////////////////////////////////////////////////////////////////
  125. void HelperLauncher::launch()
  126. {
  127. if (!helperEnabled())
  128. return;
  129. QLOG_DEBUG() << "Launching helper:" << HelperPath();
  130. #ifdef Q_OS_MAC
  131. m_launchd->start();
  132. #else
  133. if (!m_helperProcess->startDetached(HelperPath(), QStringList()))
  134. {
  135. QLOG_ERROR() << "Failed to open helper: " + m_helperProcess->errorString();
  136. throw FatalException("Failed to launch helper: " + m_helperProcess->errorString());
  137. }
  138. #endif
  139. QTimer::singleShot(1000, this, &HelperLauncher::connectToHelper);
  140. }
  141. /////////////////////////////////////////////////////////////////////////////////////////
  142. QString HelperLauncher::HelperPath()
  143. {
  144. QString programName = Names::HelperName();
  145. #ifdef Q_OS_WIN
  146. programName += ".exe";
  147. #endif
  148. QString helperPath = SettingsComponent::Get().value(SETTINGS_SECTION_PATH, "helperprogram").toString();
  149. // fallback to the resource dir
  150. if (helperPath.isEmpty() || !QFile().exists(helperPath))
  151. helperPath = Paths::resourceDir(programName);
  152. if (!QFile().exists(helperPath))
  153. throw FatalException("Can't find helper program");
  154. return helperPath;
  155. }
  156. /////////////////////////////////////////////////////////////////////////////////////////
  157. void HelperLauncher::stop()
  158. {
  159. // this method needs to disconnect all signals from the helper as well, so it doesn't start up again.
  160. m_jsonClient->disconnect();
  161. killHelper();
  162. }