DaemonAndroid.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. #include <iostream>
  2. #include <chrono>
  3. #include <thread>
  4. #include <exception>
  5. #include <boost/exception/diagnostic_information.hpp>
  6. #include <boost/exception_ptr.hpp>
  7. //#include "mainwindow.h"
  8. #include "FS.h"
  9. #include "DaemonAndroid.h"
  10. #include "Daemon.h"
  11. namespace i2p
  12. {
  13. namespace android
  14. {
  15. /* Worker::Worker (DaemonAndroidImpl& daemon):
  16. m_Daemon (daemon)
  17. {
  18. }
  19. void Worker::startDaemon()
  20. {
  21. Log.d(TAG"Performing daemon start...");
  22. m_Daemon.start();
  23. Log.d(TAG"Daemon started.");
  24. emit resultReady();
  25. }
  26. void Worker::restartDaemon()
  27. {
  28. Log.d(TAG"Performing daemon restart...");
  29. m_Daemon.restart();
  30. Log.d(TAG"Daemon restarted.");
  31. emit resultReady();
  32. }
  33. void Worker::stopDaemon() {
  34. Log.d(TAG"Performing daemon stop...");
  35. m_Daemon.stop();
  36. Log.d(TAG"Daemon stopped.");
  37. emit resultReady();
  38. }
  39. Controller::Controller(DaemonAndroidImpl& daemon):
  40. m_Daemon (daemon)
  41. {
  42. Worker *worker = new Worker (m_Daemon);
  43. worker->moveToThread(&workerThread);
  44. connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
  45. connect(this, &Controller::startDaemon, worker, &Worker::startDaemon);
  46. connect(this, &Controller::stopDaemon, worker, &Worker::stopDaemon);
  47. connect(this, &Controller::restartDaemon, worker, &Worker::restartDaemon);
  48. connect(worker, &Worker::resultReady, this, &Controller::handleResults);
  49. workerThread.start();
  50. }
  51. Controller::~Controller()
  52. {
  53. Log.d(TAG"Closing and waiting for daemon worker thread...");
  54. workerThread.quit();
  55. workerThread.wait();
  56. Log.d(TAG"Waiting for daemon worker thread finished.");
  57. if(m_Daemon.isRunning())
  58. {
  59. Log.d(TAG"Stopping the daemon...");
  60. m_Daemon.stop();
  61. Log.d(TAG"Stopped the daemon.");
  62. }
  63. }
  64. */
  65. std::string dataDir = "";
  66. DaemonAndroidImpl::DaemonAndroidImpl ()
  67. //:
  68. /*mutex(nullptr), */
  69. //m_IsRunning(false),
  70. //m_RunningChangedCallback(nullptr)
  71. {
  72. }
  73. DaemonAndroidImpl::~DaemonAndroidImpl ()
  74. {
  75. //delete mutex;
  76. }
  77. bool DaemonAndroidImpl::init(int argc, char* argv[])
  78. {
  79. //mutex=new QMutex(QMutex::Recursive);
  80. //setRunningCallback(0);
  81. //m_IsRunning=false;
  82. // make sure assets are ready before proceed
  83. i2p::fs::DetectDataDir(dataDir, false);
  84. int numAttempts = 0;
  85. do
  86. {
  87. if (i2p::fs::Exists (i2p::fs::DataDirPath("assets.ready"))) break; // assets ready
  88. numAttempts++;
  89. std::this_thread::sleep_for (std::chrono::seconds(1)); // otherwise wait for 1 more second
  90. }
  91. while (numAttempts <= 10); // 10 seconds max
  92. return Daemon.init(argc,argv);
  93. }
  94. void DaemonAndroidImpl::start()
  95. {
  96. //QMutexLocker locker(mutex);
  97. //setRunning(true);
  98. Daemon.start();
  99. }
  100. void DaemonAndroidImpl::stop()
  101. {
  102. //QMutexLocker locker(mutex);
  103. Daemon.stop();
  104. //setRunning(false);
  105. }
  106. void DaemonAndroidImpl::restart()
  107. {
  108. //QMutexLocker locker(mutex);
  109. stop();
  110. start();
  111. }
  112. /*
  113. void DaemonAndroidImpl::setRunningCallback(runningChangedCallback cb)
  114. {
  115. m_RunningChangedCallback = cb;
  116. }
  117. bool DaemonAndroidImpl::isRunning()
  118. {
  119. return m_IsRunning;
  120. }
  121. void DaemonAndroidImpl::setRunning(bool newValue)
  122. {
  123. bool oldValue = m_IsRunning;
  124. if(oldValue!=newValue)
  125. {
  126. m_IsRunning = newValue;
  127. if(m_RunningChangedCallback)
  128. m_RunningChangedCallback();
  129. }
  130. }
  131. */
  132. static DaemonAndroidImpl daemon;
  133. static char* argv[1]={strdup("tmp")};
  134. /**
  135. * returns error details if failed
  136. * returns "ok" if daemon initialized and started okay
  137. */
  138. std::string start(/*int argc, char* argv[]*/)
  139. {
  140. try
  141. {
  142. //int result;
  143. {
  144. //Log.d(TAG"Initialising the daemon...");
  145. bool daemonInitSuccess = daemon.init(1,argv);
  146. if(!daemonInitSuccess)
  147. {
  148. //QMessageBox::critical(0, "Error", "Daemon init failed");
  149. return "Daemon init failed";
  150. }
  151. //Log.d(TAG"Initialised, creating the main window...");
  152. //MainWindow w;
  153. //Log.d(TAG"Before main window.show()...");
  154. //w.show ();
  155. {
  156. //i2p::qt::Controller daemonQtController(daemon);
  157. //Log.d(TAG"Starting the daemon...");
  158. //emit daemonQtController.startDaemon();
  159. //daemon.start ();
  160. //Log.d(TAG"Starting GUI event loop...");
  161. //result = app.exec();
  162. //daemon.stop ();
  163. daemon.start();
  164. }
  165. }
  166. //QMessageBox::information(&w, "Debug", "demon stopped");
  167. //Log.d(TAG"Exiting the application");
  168. //return result;
  169. }
  170. catch (boost::exception& ex)
  171. {
  172. std::stringstream ss;
  173. ss << boost::diagnostic_information(ex);
  174. return ss.str();
  175. }
  176. catch (std::exception& ex)
  177. {
  178. std::stringstream ss;
  179. ss << ex.what();
  180. return ss.str();
  181. }
  182. catch(...)
  183. {
  184. return "unknown exception";
  185. }
  186. return "ok";
  187. }
  188. void stop()
  189. {
  190. daemon.stop();
  191. }
  192. void SetDataDir(std::string jdataDir)
  193. {
  194. dataDir = jdataDir;
  195. }
  196. }
  197. }