TxSearch thread development started

pull/1/head
moneroexamples 8 years ago
parent bd3d02f20b
commit 05eb0b23f9

@ -43,6 +43,18 @@ main()
// }
// 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(

@ -5,7 +5,7 @@ project(myxrm)
set(SOURCE_FILES
MicroCore.cpp
tools.cpp
CmdLineOptions.cpp MySqlConnector.h)
CmdLineOptions.cpp MySqlConnector.h TxSearch.h)
# make static library called libmyxrm
# that we are going to link to

@ -5,6 +5,7 @@
#ifndef RESTBED_XMR_MYSQLCONNECTOR_H
#define RESTBED_XMR_MYSQLCONNECTOR_H
#include "tools.h"
#include <mysql++/mysql++.h>
#include <mysql++/ssqls.h>
@ -19,7 +20,7 @@ namespace xmreg
using namespace mysqlpp;
using namespace std;
using namespace nlohmann;
#define MYSQL_EXCEPTION_MSG(sql_excetption) cerr << "# ERR: SQLException in " << __FILE__ \
<< "(" << __FUNCTION__ << ") on line " << __LINE__ << endl \
@ -89,9 +90,30 @@ sql_create_11(Accounts, 1, 2,
struct XmrAccount : public Accounts
{
using Accounts::Accounts;
};
json
to_json() const
{
json j {{"id" , id},
{"address" , address},
{"total_received" , total_received},
{"total_sent" , total_sent},
{"scanned_block_height", scanned_block_height},
{"blockchain_height" , blockchain_height}
};
return j;
}
friend std::ostream& operator<< (std::ostream& stream, const XmrAccount& acc);
};
ostream& operator<< (std::ostream& os, const XmrAccount& acc) {
os << "XmrAccount: " << acc.to_json().dump() << '\n';
return os;
};
class MySqlAccounts: public MySqlConnector
{
@ -101,11 +123,17 @@ class MySqlAccounts: public MySqlConnector
SELECT * FROM `Accounts` WHERE `address` = (%0q)
)";
static constexpr const char* SELECT_STMT2 = R"(
SELECT * FROM `Accounts` WHERE `id` = (%0q)
)";
static constexpr const char* INSERT_STMT = R"(
INSERT INTO `Accounts` (`address`) VALUES (%0q);
)";
public:
MySqlAccounts(): MySqlConnector() {}
@ -114,13 +142,52 @@ public:
select(const string& address, XmrAccount& account)
{
Query query = conn.query(SELECT_STMT);
query.parse();
static shared_ptr<Query> query;
if (!query)
{
Query q = conn.query(SELECT_STMT);
q.parse();
query = shared_ptr<Query>(new Query(q));
}
try
{
vector<XmrAccount> res;
query->storein(res, address);
if (!res.empty())
{
account = res.at(0);
return true;
}
}
catch (mysqlpp::Exception& e)
{
MYSQL_EXCEPTION_MSG(e);
}
return false;
}
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));
}
try
{
vector<XmrAccount> res;
query.storein(res, address);
query->storein(res, acc_id);
if (!res.empty())
{
@ -149,6 +216,7 @@ public:
if (sr.rows() == 1)
return sr.insert_id();
}
catch (mysqlpp::Exception& e)
{

@ -0,0 +1,65 @@
//
// Created by mwo on 12/12/16.
//
#ifndef RESTBED_XMR_TXSEARCH_H
#define RESTBED_XMR_TXSEARCH_H
#include "MySqlConnector.h"
#include "tools.h"
#include <mysql++/mysql++.h>
#include <mysql++/ssqls.h>
#include <iostream>
#include <memory>
#include <thread>
namespace xmreg
{
class TxSearch
{
bool continue_search {true};
XmrAccount xmr_account;
public:
TxSearch(XmrAccount& _acc):
xmr_account {_acc}
{}
void
search()
{
cout << "TxSearch::Search for: " << xmr_account.address << endl;
while(continue_search)
{
cout << " - searching tx of: " << xmr_account.address << endl;
std::this_thread::sleep_for(std::chrono::seconds(1));
}
}
void
stop()
{
cout << "something to stop the thread by setting continue_search=false" << endl;
}
~TxSearch()
{
cout << "TxSearch destroyed" << endl;
}
};
}
#endif //RESTBED_XMR_TXSEARCH_H

@ -8,6 +8,7 @@
#include <iostream>
#include <functional>
#include "TxSearch.h"
#include "MySqlConnector.h"
#include "tools.h"
@ -73,10 +74,13 @@ struct handel_
class YourMoneroRequests
{
// 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:
static bool show_logs;
@ -108,27 +112,35 @@ public:
string xmr_address = j_request["address"];
// a placeholder for exciting or newly created account's data
// 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))
{
//cout << "Account found: " << acc.id << endl;
j_response = {{"new_address", false}};
}
else
{
//cout << "Account does not exist" << endl;
// account does not exist, so create new one
// for this address
if ((acc_id = xmr_accounts->create(xmr_address)) != 0)
{
//cout << "account created acc_id: " << acc_id << endl;
j_response = {{"new_address", true}};
// 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
@ -142,6 +154,11 @@ public:
// 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())}});
@ -275,6 +292,27 @@ public:
return j;
}
private:
bool
start_tx_search_thread(XmrAccount& acc)
{
if (searching_threads.count(acc.address) > 0)
{
// thread for this address exist, dont make new one
return false;
}
// make a tx_search object for the given xmr account
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()};
t1.detach();
return true;
}
};
bool YourMoneroRequests::show_logs = false;

Loading…
Cancel
Save