Files

88 lines
2.7 KiB
C++

#include "MyClass.h"
#include <utils/primitives/PrivateKey.h>
#include <utils/Logger.h>
#include <utils/WorkerThreads.h>
#include <p2p/DownloadManager.h>
#include <boost/filesystem.hpp>
namespace {
// probably easier to do cleanly if you have
// an Application singleton...
static WorkerThreads *g_wt = nullptr;
static DownloadManager *g_dlm = nullptr;
void HandleSigTerm(int) {
g_dlm->shutdown();
g_wt->stopThreads();
}
// this part is only needed if you want to use crypto primitives.
struct ECC_State
{
ECC_State() {
// Init crypto lib.
ECC_Start();
globalVerifyHandle.reset(new ECCVerifyHandle());
}
~ECC_State() {
globalVerifyHandle.reset();
ECC_Stop();
}
std::unique_ptr<ECCVerifyHandle> globalVerifyHandle;
};
}
int main(int x, char **y)
{
/* Logging is by default ignoring debug and info messages, you can change
* this here (and much more). Though for service setup you likely just
* want to aim for a config file where these things are set.
*
* Please be aware that the p2p lib and networkmanager libs in Flowee will
* remove all 'debug' level log-lines when the library is compiled for
* release.
* You can do the same for your app by adding cxx flags: -DBCH_NO_DEBUG_OUTPUT
*/
auto *logger = Log::Manager::instance();
logger->clearLogLevels(Log::DebugLevel);
// logger->clearLogLevels(Log::CriticalLevel);
ECC_State crypo_state; // allows the secp256k1 to function.
WorkerThreads wt;
auto basedir = boost::filesystem::path("example-data");
boost::filesystem::create_directories(basedir);
DownloadManager dm(wt.ioService(), basedir, P2PNet::MainChain);
// this starts the p2p net for BitcoinCash.
// it starts looking for peers, fills the peers database with IPs and
// if present will sync SPV wallets.
// Notice that we will fetch the headers-chain, so be prepared for about
// 70MB download to happen which goes to the above 'example-data' dir.
dm.start();
std::shared_ptr<MyClass> myClass = std::make_shared<MyClass>(&dm);
boost::asio::deadline_timer waitABit(wt.ioService());
// wait a little to ensure there are some addresses in the DB
waitABit.expires_from_now(boost::posix_time::seconds(45));
waitABit.async_wait(std::bind(&MyClass::start, myClass, std::placeholders::_1));
// Clean shutdown on SIGTERM
g_dlm = &dm;
g_wt = &wt;
struct sigaction sa;
sa.sa_handler = HandleSigTerm;
sigemptyset(&sa.sa_mask);
sa.sa_flags = 0;
sigaction(SIGTERM, &sa, nullptr);
sigaction(SIGINT, &sa, nullptr);
// Ignore SIGPIPE
signal(SIGPIPE, SIG_IGN);
wt.joinAll();
return 0;
}