2019-11-18 00:21:34 +02:00
# include "Controller.h"
2019-11-18 08:29:33 +02:00
Controller : : Controller ( const std : : shared_ptr < Options > & o )
: Mgr ( nullptr ) , options ( o )
2019-11-18 00:21:34 +02:00
{
2019-11-18 08:29:33 +02:00
setObjectName ( " Controller " ) ;
_thread . setObjectName ( objectName ( ) ) ;
2019-11-18 00:21:34 +02:00
}
2019-11-18 00:34:24 +02:00
Controller : : ~ Controller ( ) { Debug ( __FUNCTION__ ) ; cleanup ( ) ; }
2019-11-18 00:21:34 +02:00
auto Controller : : stats ( ) const - > Stats
{
auto st = srvmgr - > statsSafe ( ) ;
Util : : updateMap ( st , bitcoindmgr - > statsSafe ( ) ) ;
return st ;
}
void Controller : : startup ( )
{
bitcoindmgr = std : : make_unique < BitcoinDMgr > ( options - > bitcoind . first , options - > bitcoind . second , options - > rpcuser , options - > rpcpassword ) ;
2019-11-18 08:29:33 +02:00
{
// some setup code that waits for bitcoind to be ready before kicking off our "process" method
2019-11-18 09:47:18 +02:00
auto waitForBitcoinD = [ this ] {
auto constexpr waitTimer = " wait4bitcoind " , callProcessTimer = " callProcess " ;
int constexpr msgPeriod = 10000 , // 10sec
smallDelay = 100 ;
stopTimer ( callProcessTimer ) ;
callOnTimerSoon ( msgPeriod , waitTimer , [ ] { Log ( " Waiting for bitcoind... " ) ; return true ; } , false , Qt : : TimerType : : VeryCoarseTimer ) ;
// connection to kick off our 'process' method once the first auth is received
auto connPtr = std : : make_shared < QMetaObject : : Connection > ( ) ;
* connPtr = connect ( bitcoindmgr . get ( ) , & BitcoinDMgr : : gotFirstGoodConnection , this , [ this , connPtr ] ( quint64 id ) mutable {
if ( connPtr ) {
stopTimer ( waitTimer ) ;
if ( ! disconnect ( * connPtr ) ) Fatal ( ) < < " Failed to disconnect 'authenticated' signal! FIXME! " ; // this should never happen but if it does, app quits.
connPtr . reset ( ) ; // clear connPtr right away to 1. delte it asap and 2. so we are guaranteed not to reenter this block for this connection should there be a spurious signal emitted.
Debug ( ) < < " Auth recvd from bicoind with id: " < < id < < " , proceeding with processing ... " ;
callOnTimerSoonNoRepeat ( smallDelay , callProcessTimer , [ this ] { process ( ) ; } , true ) ;
}
} ) ;
} ;
waitForBitcoinD ( ) ;
conns + = connect ( bitcoindmgr . get ( ) , & BitcoinDMgr : : allConnectionsLost , this , waitForBitcoinD ) ;
2019-11-18 08:29:33 +02:00
}
2019-11-18 00:21:34 +02:00
bitcoindmgr - > startup ( ) ; // may throw
2019-11-18 08:29:33 +02:00
srvmgr = std : : make_unique < SrvMgr > ( options - > interfaces , nullptr /* we are not parent so it won't follow us to our thread*/ ) ;
2019-11-18 00:21:34 +02:00
srvmgr - > startup ( ) ; // may throw Exception, waits for servers to bind
2019-11-18 08:29:33 +02:00
start ( ) ; // start our thread
2019-11-18 00:21:34 +02:00
}
void Controller : : cleanup ( )
{
2019-11-18 08:29:33 +02:00
stop ( ) ;
2019-11-18 00:21:34 +02:00
if ( srvmgr ) { Log ( " Stopping SrvMgr ... " ) ; srvmgr - > cleanup ( ) ; srvmgr . reset ( ) ; }
if ( bitcoindmgr ) { Log ( " Stopping BitcoinDMgr ... " ) ; bitcoindmgr - > cleanup ( ) ; bitcoindmgr . reset ( ) ; }
2019-11-18 08:29:33 +02:00
sm . reset ( ) ;
}
struct Controller : : StateMachine
{
// TODO: put state stuff here
} ;
void Controller : : process ( )
{
Debug ( ) < < " Process called... " ;
if ( ! sm ) sm = std : : make_unique < StateMachine > ( ) ; // create statemachine if doest not exist
2019-11-18 14:32:48 +02:00
bitcoindmgr - > submitRequest ( this , IdMixin : : newId ( ) , " getmempoolinfo " , { } , [ ] ( auto resp ) { // testing
Trace ( ) < < resp . id . toString ( ) < < " : result reply: " < < resp . toJsonString ( ) ;
} , [ ] ( auto resp ) {
Trace ( ) < < resp . id . toString ( ) < < " : error response: " < < resp . toJsonString ( ) ;
} , [ ] ( auto id , auto msg ) {
Warning ( ) < < id . toString ( ) < < " : FAIL: " < < msg ;
} ) ;
2019-11-18 00:21:34 +02:00
}