Skip to content

Commit f44c814

Browse files
author
minium
committed
Merge branch 'xloem-persistent-connection'
2 parents 860b36d + ca8cfd4 commit f44c814

File tree

3 files changed

+22
-36
lines changed

3 files changed

+22
-36
lines changed

src/bitcoinapi/bitcoinapi.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,17 @@ using std::string;
3030
using std::vector;
3131

3232

33-
BitcoinAPI::BitcoinAPI(){
34-
conn = {};
33+
BitcoinAPI::BitcoinAPI(const string& user, const string& password, const string& host, int port)
34+
: httpClient(new HttpClient("http://" + user + ":" + password + "@" + host + ":" + NumberToString(port))),
35+
client(new Client(*httpClient, JSONRPC_CLIENT_V1))
36+
{
37+
httpClient->SetTimeout(20000);
3538
}
3639

37-
BitcoinAPI::BitcoinAPI(const string& user, const string& password, const string& host, int port){
38-
conn.user = user;
39-
conn.password = password;
40-
conn.host = host;
41-
conn.port = port;
42-
conn.url = "http://" + conn.user + ":" + conn.password + "@" + conn.host + ":" + NumberToString(conn.port);
43-
}
44-
45-
bool BitcoinAPI::IsInit(){
46-
return !(conn.user.empty() || conn.password.empty() || conn.host.empty() || conn.port == 0);
40+
BitcoinAPI::~BitcoinAPI()
41+
{
42+
delete client;
43+
delete httpClient;
4744
}
4845

4946
string BitcoinAPI::NumberToString (int number){
@@ -59,14 +56,10 @@ int BitcoinAPI::StringToNumber (const string &text){
5956
}
6057

6158
Value BitcoinAPI::sendcommand(const string& command, const Value& params){
62-
HttpClient client(conn.url);
63-
client.SetTimeout(20000);
64-
65-
Client c(client, JSONRPC_CLIENT_V1);
6659
Value result;
6760

6861
try{
69-
result = c.CallMethod(command, params);
62+
result = client->CallMethod(command, params);
7063
}
7164
catch (JsonRpcException& e){
7265
BitcoinException err(e.GetCode(), e.GetMessage());

src/bitcoinapi/bitcoinapi.h

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,26 +14,19 @@
1414
#include "types.h"
1515
#include "exception.h"
1616

17+
namespace jsonrpc { class HttpClient; class Client; }
1718

1819
class BitcoinAPI
1920
{
2021

2122
private:
22-
struct connection
23-
{
24-
std::string url;
25-
std::string user;
26-
std::string password;
27-
std::string host;
28-
int port;
29-
} conn;
23+
jsonrpc::HttpClient * httpClient;
24+
jsonrpc::Client * client;
3025

3126
public:
32-
/* === Constructors === */
33-
BitcoinAPI();
27+
/* === Constructor and Destructor === */
3428
BitcoinAPI(const std::string& user, const std::string& password, const std::string& host, int port);
35-
36-
bool IsInit();
29+
~BitcoinAPI();
3730

3831
/* === Auxiliary functions === */
3932
Json::Value sendcommand(const std::string& command, const Json::Value& params);

src/test/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ struct MyFixture {
1616

1717
BitcoinAPI btc;
1818

19-
MyFixture() {
20-
username = "Ulysses";
21-
password = "Random";
22-
address = "127.0.0.1";
23-
port = 8332;
24-
btc = BitcoinAPI(username, password, address, port);
25-
}
19+
MyFixture()
20+
: username("Ulysses"),
21+
password("Random"),
22+
address("127.0.0.1"),
23+
port(8332),
24+
btc(username, password, address, port)
25+
{ }
2626
~MyFixture() { }
2727
};
2828

0 commit comments

Comments
 (0)