TxSearch thread launches does not crash now.

pull/1/head
moneroexamples 8 years ago
parent 19c09e33f0
commit fbba97dfed

@ -8,206 +8,22 @@
#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()
{
// xmreg::MySqlAccounts xmr_accounts;
//
// //xmr_accounts.create_account("41vEA7Ye8Bpeda6g59v5t46koWrVn2PNgEKgzquJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB");
//
//
// string addr = "41vEA7Ye8Bpeda6g9v5t46koWrVn2PNgEKgluJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB";
//
// xmreg::XmrAccount acc;
//
// bool r = xmr_accounts.select(addr, acc);
//
// if (r)
// {
// cout << "Account foudn: " << acc.id << endl;
//
// xmreg::XmrAccount new_acc = acc;
// new_acc.total_received = 20e12;
//
// cout << xmr_accounts.update(acc, new_acc) << endl;
//
// }
// else
// {
// cout << "Account does not exist" << endl;
// cout << xmr_accounts.create(addr) << endl;
// }
// xmreg::MySqlAccounts xmr_accounts;
////
// string addr = "41vEA7Ye8Bpeda6g9v5t46koWrVn2PNgEKgluJjmiKCFTsh9gajr8J3pad49rqu581TAtFGCH9CYTCkYrCpuWUG9GkgeB";
//
// xmreg::XmrAccount acc;
//
// cout << xmr_accounts.select(addr, acc) << endl;
//
// cout << xmr_accounts.select("fdfdfd", acc) << endl;
//
// cout << xmr_accounts.select(addr, acc) << endl;
xmreg::YourMoneroRequests::show_logs = true;
xmreg::YourMoneroRequests your_xmr(
shared_ptr<xmreg::MySqlAccounts>(new xmreg::MySqlAccounts{}));
//your_xmr.start_tx_search_thread(acc);
// 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 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 login = your_xmr.make_resource(
&xmreg::YourMoneroRequests::login , "/login");
auto get_address_txs = your_xmr.make_resource(
&xmreg::YourMoneroRequests::get_address_txs , "/get_address_txs");

@ -91,11 +91,16 @@ struct XmrAccount : public Accounts
{
using Accounts::Accounts;
// viewkey is not stored in mysql db or anywhere
// so need to be populated when user logs in.
string viewkey;
json
to_json() const
{
json j {{"id" , id},
{"address" , address},
{"viewkey" , viewkey},
{"total_received" , total_received},
{"total_sent" , total_sent},
{"scanned_block_height", scanned_block_height},

@ -44,13 +44,13 @@ public:
void
search()
{
cout << "TxSearch::Search for: " << xmr_account.address << endl;
cout << "TxSearch::Search for: " << xmr_account << endl;
while(true)
while(continue_search)
{
//std::lock_guard<std::mutex> lck (mtx);
cout << " - searching tx of: " << xmr_account.address << endl;
std::lock_guard<std::mutex> lck (mtx);
cout << " - searching tx of: " << xmr_account << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}

@ -74,12 +74,14 @@ struct handel_
class YourMoneroRequests
{
// this manages all mysql queries
// map that will keep track of search threads. In the
// map, key is address to which a running thread belongs to.
// make it static to garantee only one such map exist.
static map<string, shared_ptr<xmreg::TxSearch>> searching_threads;
// this manages all mysql queries
shared_ptr<MySqlAccounts> xmr_accounts;
// 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;
public:
@ -139,6 +141,8 @@ public:
}
}
acc.viewkey = j_request["view_key"];
cout << acc << endl;
// so we have an account now. Either existing or
@ -166,85 +170,6 @@ 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 )
{
xmreg::MySqlAccounts xmr_accounts;
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)
{
@ -372,7 +297,7 @@ public:
}
bool
start_tx_search_thread(XmrAccount& acc)
start_tx_search_thread(XmrAccount acc)
{
if (searching_threads.count(acc.address) > 0)
{
@ -397,7 +322,11 @@ private:
};
// define static variables
bool YourMoneroRequests::show_logs = false;
map<string, shared_ptr<xmreg::TxSearch>> YourMoneroRequests::searching_threads;
}
#endif //RESTBED_XMR_YOURMONEROREQUESTS_H

Loading…
Cancel
Save