2020-05-22 13:18:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013-2020, The PurpleI2P Project
|
|
|
|
*
|
|
|
|
* This file is part of Purple i2pd project and licensed under BSD3
|
|
|
|
*
|
|
|
|
* See full license text in LICENSE file at top of project tree
|
|
|
|
*/
|
|
|
|
|
2016-07-10 01:42:42 +00:00
|
|
|
#include <iostream>
|
2018-08-17 15:17:17 +00:00
|
|
|
#include <chrono>
|
|
|
|
#include <thread>
|
|
|
|
#include <exception>
|
2016-07-10 01:42:42 +00:00
|
|
|
#include <boost/exception/diagnostic_information.hpp>
|
|
|
|
#include <boost/exception_ptr.hpp>
|
2016-07-09 20:54:11 +00:00
|
|
|
//#include "mainwindow.h"
|
2018-08-17 15:17:17 +00:00
|
|
|
#include "FS.h"
|
|
|
|
#include "DaemonAndroid.h"
|
|
|
|
#include "Daemon.h"
|
2016-07-09 20:54:11 +00:00
|
|
|
|
|
|
|
namespace i2p
|
|
|
|
{
|
|
|
|
namespace android
|
|
|
|
{
|
|
|
|
/* Worker::Worker (DaemonAndroidImpl& daemon):
|
|
|
|
m_Daemon (daemon)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Worker::startDaemon()
|
|
|
|
{
|
|
|
|
Log.d(TAG"Performing daemon start...");
|
|
|
|
m_Daemon.start();
|
|
|
|
Log.d(TAG"Daemon started.");
|
|
|
|
emit resultReady();
|
|
|
|
}
|
|
|
|
void Worker::restartDaemon()
|
|
|
|
{
|
|
|
|
Log.d(TAG"Performing daemon restart...");
|
|
|
|
m_Daemon.restart();
|
|
|
|
Log.d(TAG"Daemon restarted.");
|
|
|
|
emit resultReady();
|
|
|
|
}
|
|
|
|
void Worker::stopDaemon() {
|
|
|
|
Log.d(TAG"Performing daemon stop...");
|
|
|
|
m_Daemon.stop();
|
|
|
|
Log.d(TAG"Daemon stopped.");
|
|
|
|
emit resultReady();
|
|
|
|
}
|
|
|
|
|
2019-01-18 12:44:30 +00:00
|
|
|
Controller::Controller(DaemonAndroidImpl& daemon):
|
2016-07-09 20:54:11 +00:00
|
|
|
m_Daemon (daemon)
|
|
|
|
{
|
|
|
|
Worker *worker = new Worker (m_Daemon);
|
|
|
|
worker->moveToThread(&workerThread);
|
|
|
|
connect(&workerThread, &QThread::finished, worker, &QObject::deleteLater);
|
|
|
|
connect(this, &Controller::startDaemon, worker, &Worker::startDaemon);
|
|
|
|
connect(this, &Controller::stopDaemon, worker, &Worker::stopDaemon);
|
|
|
|
connect(this, &Controller::restartDaemon, worker, &Worker::restartDaemon);
|
|
|
|
connect(worker, &Worker::resultReady, this, &Controller::handleResults);
|
|
|
|
workerThread.start();
|
|
|
|
}
|
|
|
|
Controller::~Controller()
|
|
|
|
{
|
|
|
|
Log.d(TAG"Closing and waiting for daemon worker thread...");
|
|
|
|
workerThread.quit();
|
|
|
|
workerThread.wait();
|
|
|
|
Log.d(TAG"Waiting for daemon worker thread finished.");
|
2019-01-18 12:44:30 +00:00
|
|
|
if(m_Daemon.isRunning())
|
|
|
|
{
|
|
|
|
Log.d(TAG"Stopping the daemon...");
|
|
|
|
m_Daemon.stop();
|
|
|
|
Log.d(TAG"Stopped the daemon.");
|
2016-07-09 20:54:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2019-04-20 19:47:06 +00:00
|
|
|
std::string dataDir = "";
|
|
|
|
|
2016-07-10 01:42:42 +00:00
|
|
|
DaemonAndroidImpl::DaemonAndroidImpl ()
|
|
|
|
//:
|
2019-01-18 12:44:30 +00:00
|
|
|
/*mutex(nullptr), */
|
|
|
|
//m_IsRunning(false),
|
|
|
|
//m_RunningChangedCallback(nullptr)
|
2016-07-09 20:54:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
DaemonAndroidImpl::~DaemonAndroidImpl ()
|
|
|
|
{
|
|
|
|
//delete mutex;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DaemonAndroidImpl::init(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
//mutex=new QMutex(QMutex::Recursive);
|
2016-07-10 01:42:42 +00:00
|
|
|
//setRunningCallback(0);
|
2019-01-18 12:44:30 +00:00
|
|
|
//m_IsRunning=false;
|
|
|
|
|
|
|
|
// make sure assets are ready before proceed
|
2019-04-20 19:47:06 +00:00
|
|
|
i2p::fs::DetectDataDir(dataDir, false);
|
2018-08-17 15:17:17 +00:00
|
|
|
int numAttempts = 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (i2p::fs::Exists (i2p::fs::DataDirPath("assets.ready"))) break; // assets ready
|
|
|
|
numAttempts++;
|
|
|
|
std::this_thread::sleep_for (std::chrono::seconds(1)); // otherwise wait for 1 more second
|
|
|
|
}
|
2019-01-18 12:44:30 +00:00
|
|
|
while (numAttempts <= 10); // 10 seconds max
|
2016-07-09 20:54:11 +00:00
|
|
|
return Daemon.init(argc,argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void DaemonAndroidImpl::start()
|
|
|
|
{
|
|
|
|
//QMutexLocker locker(mutex);
|
2016-07-10 01:42:42 +00:00
|
|
|
//setRunning(true);
|
2016-07-09 20:54:11 +00:00
|
|
|
Daemon.start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void DaemonAndroidImpl::stop()
|
|
|
|
{
|
|
|
|
//QMutexLocker locker(mutex);
|
|
|
|
Daemon.stop();
|
2016-07-10 01:42:42 +00:00
|
|
|
//setRunning(false);
|
2016-07-09 20:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DaemonAndroidImpl::restart()
|
|
|
|
{
|
|
|
|
//QMutexLocker locker(mutex);
|
|
|
|
stop();
|
|
|
|
start();
|
|
|
|
}
|
2016-07-10 01:42:42 +00:00
|
|
|
/*
|
2016-07-09 20:54:11 +00:00
|
|
|
void DaemonAndroidImpl::setRunningCallback(runningChangedCallback cb)
|
|
|
|
{
|
|
|
|
m_RunningChangedCallback = cb;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DaemonAndroidImpl::isRunning()
|
|
|
|
{
|
2019-01-18 12:44:30 +00:00
|
|
|
return m_IsRunning;
|
2016-07-09 20:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DaemonAndroidImpl::setRunning(bool newValue)
|
|
|
|
{
|
2019-01-18 12:44:30 +00:00
|
|
|
bool oldValue = m_IsRunning;
|
2016-07-09 20:54:11 +00:00
|
|
|
if(oldValue!=newValue)
|
|
|
|
{
|
2019-01-18 12:44:30 +00:00
|
|
|
m_IsRunning = newValue;
|
|
|
|
if(m_RunningChangedCallback)
|
2016-07-09 20:54:11 +00:00
|
|
|
m_RunningChangedCallback();
|
|
|
|
}
|
|
|
|
}
|
2016-07-10 01:42:42 +00:00
|
|
|
*/
|
2016-07-09 20:54:11 +00:00
|
|
|
static DaemonAndroidImpl daemon;
|
2018-01-22 17:59:34 +00:00
|
|
|
static char* argv[1]={strdup("tmp")};
|
2016-07-09 20:54:11 +00:00
|
|
|
/**
|
2016-07-10 01:42:42 +00:00
|
|
|
* returns error details if failed
|
|
|
|
* returns "ok" if daemon initialized and started okay
|
2016-07-09 20:54:11 +00:00
|
|
|
*/
|
2018-01-22 17:59:34 +00:00
|
|
|
std::string start(/*int argc, char* argv[]*/)
|
2016-07-09 20:54:11 +00:00
|
|
|
{
|
2016-07-10 01:42:42 +00:00
|
|
|
try
|
2016-07-09 20:54:11 +00:00
|
|
|
{
|
2016-07-10 01:42:42 +00:00
|
|
|
//int result;
|
2016-07-09 20:54:11 +00:00
|
|
|
|
|
|
|
{
|
2016-07-10 01:42:42 +00:00
|
|
|
//Log.d(TAG"Initialising the daemon...");
|
2018-01-22 17:59:34 +00:00
|
|
|
bool daemonInitSuccess = daemon.init(1,argv);
|
2016-07-10 01:42:42 +00:00
|
|
|
if(!daemonInitSuccess)
|
|
|
|
{
|
|
|
|
//QMessageBox::critical(0, "Error", "Daemon init failed");
|
|
|
|
return "Daemon init failed";
|
|
|
|
}
|
|
|
|
//Log.d(TAG"Initialised, creating the main window...");
|
|
|
|
//MainWindow w;
|
|
|
|
//Log.d(TAG"Before main window.show()...");
|
|
|
|
//w.show ();
|
|
|
|
|
|
|
|
{
|
|
|
|
//i2p::qt::Controller daemonQtController(daemon);
|
|
|
|
//Log.d(TAG"Starting the daemon...");
|
|
|
|
//emit daemonQtController.startDaemon();
|
|
|
|
//daemon.start ();
|
|
|
|
//Log.d(TAG"Starting GUI event loop...");
|
|
|
|
//result = app.exec();
|
|
|
|
//daemon.stop ();
|
|
|
|
daemon.start();
|
|
|
|
}
|
2016-07-09 20:54:11 +00:00
|
|
|
}
|
|
|
|
|
2016-07-10 01:42:42 +00:00
|
|
|
//QMessageBox::information(&w, "Debug", "demon stopped");
|
|
|
|
//Log.d(TAG"Exiting the application");
|
|
|
|
//return result;
|
|
|
|
}
|
|
|
|
catch (boost::exception& ex)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2019-01-18 12:44:30 +00:00
|
|
|
ss << boost::diagnostic_information(ex);
|
|
|
|
return ss.str();
|
2016-07-10 01:42:42 +00:00
|
|
|
}
|
|
|
|
catch (std::exception& ex)
|
|
|
|
{
|
|
|
|
std::stringstream ss;
|
2019-01-18 12:44:30 +00:00
|
|
|
ss << ex.what();
|
|
|
|
return ss.str();
|
2016-07-10 01:42:42 +00:00
|
|
|
}
|
|
|
|
catch(...)
|
|
|
|
{
|
|
|
|
return "unknown exception";
|
|
|
|
}
|
|
|
|
return "ok";
|
2016-07-09 20:54:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void stop()
|
|
|
|
{
|
|
|
|
daemon.stop();
|
|
|
|
}
|
2019-04-20 19:47:06 +00:00
|
|
|
|
|
|
|
void SetDataDir(std::string jdataDir)
|
|
|
|
{
|
|
|
|
dataDir = jdataDir;
|
|
|
|
}
|
2016-07-09 20:54:11 +00:00
|
|
|
}
|
|
|
|
}
|