#include <iostream>
#include "TimeBomb.hpp"
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace std;
using namespace boost;
using namespace boost::python;
void ITimeBombCallbackWrapper::ReceiveExplosion()
{
// Yeap, getting the GIL.
PyGILState_STATE gstate = PyGILState_Ensure();
this->get_override("ReceiveExplosion")();
PyGILState_Release(gstate);
}
TimeBomb::TimeBomb(ITimeBombCallback* luckyWinner)
{
this->luckyWinner = luckyWinner;
backgroundThread = NULL;
}
void TimeBomb::Go()
{
backgroundThread = new boost::thread(boost::bind(&TimeBomb::InnerBody, this));
}
void TimeBomb::InnerBody()
{
cout << "Inside the inner body of the time bomb." << endl;
// We'll just do some donuts in the parking lot here while we wait for 5 seconds
boost::this_thread::sleep(boost::posix_time::seconds(5));
cout << "Five seconds are up. Time to deliver payload." << endl;
// Original callback that started this whole mess was firing off quite a few in a short duration,
// so that's why we repeat the call in such close succession.
// Indeed, the first call often survives.
for(int i = 0; i < 5; ++i)
{
luckyWinner->ReceiveExplosion();
}
cout << "Payload delivered." << endl;
}
void TimeBomb::Join()
{
if(backgroundThread != NULL)
{
backgroundThread->join();
}
}