work on search thread continues

pull/1/head
moneroexamples 8 years ago
parent 0bd13e038e
commit 72a0a4d1cf

@ -91,7 +91,7 @@ add_executable(restbed_xmr
${SOURCE_FILES})
target_link_libraries(restbed_xmr PRIVATE
target_link_libraries(restbed_xmr
myxrm
myext
restbed

@ -65,6 +65,16 @@ main()
auto login = your_xmr.make_resource(
&xmreg::YourMoneroRequests::login , "/login");
// auto login = make_shared< Resource >( );
// login->set_path( "/login" );
// login->set_method_handler( "POST",
// std::bind(&xmreg::YourMoneroRequests::login2,
// your_xmr,
// std::placeholders::_1)
// );
// 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");

@ -142,19 +142,22 @@ public:
select(const string& address, XmrAccount& account)
{
static shared_ptr<Query> query;
if (!query)
{
Query q = conn.query(SELECT_STMT);
q.parse();
query = shared_ptr<Query>(new Query(q));
}
// static shared_ptr<Query> query;
//
// if (!query)
// {
// Query q = conn.query(SELECT_STMT);
// q.parse();
// query = shared_ptr<Query>(new Query(q));
// }
Query query = conn.query(SELECT_STMT);
query.parse();
try
{
vector<XmrAccount> res;
query->storein(res, address);
query.storein(res, address);
if (!res.empty())
{
@ -174,20 +177,23 @@ public:
bool
select(const int64_t& acc_id, XmrAccount& account)
{
static shared_ptr<Query> query;
if (!query)
{
Query q = conn.query(SELECT_STMT2);
q.parse();
query = shared_ptr<Query>(new Query(q));
}
//
// static shared_ptr<Query> query;
//
// if (!query)
// {
// Query q = conn.query(SELECT_STMT2);
// q.parse();
// query = shared_ptr<Query>(new Query(q));
// }
Query query = conn.query(SELECT_STMT2);
query.parse();
try
{
vector<XmrAccount> res;
query->storein(res, acc_id);
query.storein(res, acc_id);
if (!res.empty())
{

@ -17,14 +17,18 @@
#include <iostream>
#include <memory>
#include <thread>
#include <mutex>
namespace xmreg
{
mutex mtx;
class TxSearch
{
bool continue_search {true};
XmrAccount xmr_account;
@ -44,6 +48,8 @@ public:
while(true)
{
//std::lock_guard<std::mutex> lck (mtx);
cout << " - searching tx of: " << xmr_account.address << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));

@ -166,6 +166,82 @@ public:
session->close( OK, response_body, response_headers);
}
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 )
{
json j_request = body_to_json(body);
if (show_logs)
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 = make_headers({{ "Content-Length", to_string(response_body.size())}});
session->close( OK, response_body, response_headers);
} );
}
void
get_address_txs(const shared_ptr< Session > session, const Bytes & body)
{
@ -303,7 +379,7 @@ public:
}
// make a tx_search object for the given xmr account
searching_threads[acc.address] = make_shared<TxSearch>();
searching_threads[acc.address] = make_shared<TxSearch>(acc);
// start the thread for the created object
std::thread t1 {&TxSearch::search, searching_threads[acc.address].get()};

Loading…
Cancel
Save