search thread works if in independent function

pull/1/head
moneroexamples 8 years ago
parent 72a0a4d1cf
commit 00cfd67a83

@ -7,10 +7,136 @@
#include "src/MySqlConnector.h"
#include "src/YourMoneroRequests.h"
#include "src/tools.h"
#include "src/TxSearch.h"
using namespace std;
using namespace restbed;
using namespace nlohmann;
static inline string
body_to_string(const Bytes & body)
{
return string(reinterpret_cast<const char *>(body.data()), body.size());
}
static inline json
body_to_json(const Bytes & body)
{
json j = json::parse(body_to_string(body));
return j;
}
static void
print_json_log(const string& text, const json& j)
{
cout << text << '\n' << j.dump(4) << endl;
}
// map that will keep track of search threads. In the
// map, key is address to which a running thread belongs to.
map<string, shared_ptr<xmreg::TxSearch>> searching_threads;
bool
start_tx_search_thread(xmreg::XmrAccount& acc)
{
if (searching_threads.count(acc.address) > 0)
{
// thread for this address exist, dont make new one
cout << "Thread exisist, dont make new one" << endl;
return false;
}
// make a tx_search object for the given xmr account
searching_threads[acc.address] = make_shared<xmreg::TxSearch>(acc);
// start the thread for the created object
std::thread t1 {&xmreg::TxSearch::search, searching_threads[acc.address].get()};
t1.detach();
return true;
}
void
login2(const shared_ptr< Session > session)
{
const auto request = session->get_request( );
int content_length = request->get_header( "Content-Length", 0);
session->fetch( content_length, [](const shared_ptr< Session > session, const Bytes & body )
{
xmreg::MySqlAccounts xmr_accounts;
json j_request = body_to_json(body);
if (true)
print_json_log("login request: ", j_request);
string xmr_address = j_request["address"];
// a placeholder for exciting or new account data
xmreg::XmrAccount acc;
uint64_t acc_id {0};
json j_response;
// select this account if its existing one
if (xmr_accounts.select(xmr_address, acc))
{
j_response = {{"new_address", false}};
}
else
{
// account does not exist, so create new one
// for this address
if ((acc_id = xmr_accounts.create(xmr_address)) != 0)
{
// select newly created account
if (xmr_accounts.select(acc_id, acc))
{
j_response = {{"new_address", true}};
}
}
}
cout << acc << endl;
// so we have an account now. Either existing or
// newly created. Thus, we can start a tread
// which will scan for transactions belonging to
// that account, using its address and view key.
// the thread will scan the blockchain for txs belonging
// to that account and updated mysql database whenever it
// will find something.
//
// The other client (i.e., a webbrowser) will query other functions to retrieve
// any belonging transactions in a loop. Thus the thread does not need
// to do anything except looking for tx and updating mysql
// with relative tx information
if (start_tx_search_thread(acc))
{
cout << "Search thread started" << endl;
}
string response_body = j_response.dump();
auto response_headers = xmreg::make_headers({{ "Content-Length", to_string(response_body.size())}});
session->close( OK, response_body, response_headers);
} );
}
int
main()
@ -62,8 +188,8 @@ main()
//your_xmr.start_tx_search_thread(acc);
auto login = your_xmr.make_resource(
&xmreg::YourMoneroRequests::login , "/login");
// auto login = your_xmr.make_resource(
// &xmreg::YourMoneroRequests::login , "/login");
// auto login = make_shared< Resource >( );
@ -75,6 +201,14 @@ main()
// );
// login->set_method_handler( "OPTIONS", &xmreg::YourMoneroRequests::generic_options_handler);
auto login = make_shared< Resource >( );
login->set_path( "/login" );
login->set_method_handler( "POST", &login2);
login->set_method_handler( "OPTIONS", &xmreg::YourMoneroRequests::generic_options_handler);
auto get_address_txs = your_xmr.make_resource(
&xmreg::YourMoneroRequests::get_address_txs , "/get_address_txs");

@ -170,6 +170,10 @@ public:
{
MYSQL_EXCEPTION_MSG(e);
}
catch (std::exception& e)
{
MYSQL_EXCEPTION_MSG(e);
}
return false;
}

Loading…
Cancel
Save