Skip to content

Commit 1f2638b

Browse files
committed
Move derived session creation to session_impl as a factory method
Following this change, session no longer knows about the derived impls.
1 parent d0f7469 commit 1f2638b

File tree

7 files changed

+44
-41
lines changed

7 files changed

+44
-41
lines changed

src/ga_rust.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ namespace sdk {
6868
}
6969
} // namespace
7070

71-
ga_rust::ga_rust(const nlohmann::json& net_params)
72-
: session_impl(net_params)
71+
ga_rust::ga_rust(const nlohmann::json& net_params, nlohmann::json& defaults)
72+
: session_impl(net_params, defaults)
7373
{
7474
GDKRUST_create_session(&m_session, convert_json(m_net_params.get_json()).get());
7575
}

src/ga_rust.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ namespace sdk {
1010

1111
class ga_rust final : public session_impl {
1212
public:
13-
explicit ga_rust(const nlohmann::json& net_params);
13+
ga_rust(const nlohmann::json& net_params, nlohmann::json& defaults);
1414
~ga_rust();
1515

16-
nlohmann::json call_session(const std::string& method, const nlohmann::json& input) const;
17-
1816
void on_failed_login();
1917

2018
bool is_connected() const;
@@ -156,6 +154,8 @@ namespace sdk {
156154
static int32_t spv_verify_tx(const nlohmann::json& details);
157155

158156
private:
157+
nlohmann::json call_session(const std::string& method, const nlohmann::json& input) const;
158+
159159
static void GDKRUST_notif_handler(void* self_context, struct GDKRUST_json* json);
160160

161161
std::shared_ptr<tor_controller> m_tor_ctrl;

src/ga_session.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -421,8 +421,8 @@ namespace sdk {
421421
boost::asio::executor_work_guard<boost::asio::io_context::executor_type> m_work_guard;
422422
};
423423

424-
ga_session::ga_session(const nlohmann::json& net_params)
425-
: session_impl(net_params)
424+
ga_session::ga_session(const nlohmann::json& net_params, nlohmann::json& defaults)
425+
: session_impl(net_params, defaults)
426426
, m_proxy(socksify(net_params.value("proxy", std::string{})))
427427
, m_has_network_proxy(!m_proxy.empty())
428428
, m_io()

src/ga_session.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ namespace sdk {
4040
using heartbeat_t = websocketpp::pong_timeout_handler;
4141
using nlocktime_t = std::map<std::string, nlohmann::json>; // txhash:pt_idx -> lock info
4242

43-
explicit ga_session(const nlohmann::json& net_params);
43+
ga_session(const nlohmann::json& net_params, nlohmann::json& defaults);
4444
~ga_session();
4545

4646
void connect();

src/session.cpp

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
#include "autobahn_wrapper.hpp"
1212
#include "exception.hpp"
13-
#ifdef BUILD_GDK_RUST
14-
#include "ga_rust.hpp"
15-
#endif
1613
#include "ga_session.hpp"
1714
#include "logging.hpp"
1815
#include "network_parameters.hpp"
@@ -125,20 +122,7 @@ namespace sdk {
125122
GDK_RUNTIME_ASSERT_MSG(init_done, "You must call GA_init first");
126123
GDK_RUNTIME_ASSERT_MSG(!get_impl(), "session already connected");
127124

128-
const auto network = network_parameters::get(net_params.value("name", std::string()));
129-
const auto type = net_params.value("server_type", network.value("server_type", std::string()));
130-
131-
session_ptr session_p;
132-
133-
if (type == "green") {
134-
session_p = boost::make_shared<ga_session>(net_params);
135-
#ifdef BUILD_GDK_RUST
136-
} else if (type == "electrum") {
137-
session_p = boost::make_shared<ga_rust>(net_params);
138-
#endif
139-
} else {
140-
GDK_RUNTIME_ASSERT_MSG(false, "server_type field unknown value");
141-
}
125+
session_ptr session_p = session_impl::create(net_params);
142126

143127
boost::weak_ptr<session_impl> weak_session = session_p;
144128
session_p->set_ping_fail_handler([weak_session] {

src/session_impl.cpp

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "session_impl.hpp"
22
#include "exception.hpp"
3+
#include "ga_rust.hpp"
4+
#include "ga_session.hpp"
35
#include "logging.hpp"
46
#include "session.hpp"
57

@@ -14,24 +16,22 @@ namespace sdk {
1416
ret[key] = src.value(key, ret.value(key, default_));
1517
}
1618

17-
static network_parameters get_network_overrides(const nlohmann::json& user_params)
19+
static network_parameters get_network_overrides(const nlohmann::json& user_params, nlohmann::json& defaults)
1820
{
19-
// Get the registered network parameters the passed in parameters are based on
20-
auto ret = network_parameters::get(user_params.at("name"));
2121
// Set override-able settings from the users parameters
22-
set_override(ret, "electrum_tls", user_params, false);
23-
set_override(ret, "electrum_url", user_params, std::string());
24-
set_override(ret, "log_level", user_params, "none");
25-
set_override(ret, "spv_multi", user_params, false);
26-
set_override(ret, "spv_servers", user_params, nlohmann::json::array());
27-
set_override(ret, "spv_enabled", user_params, false);
28-
set_override(ret, "use_tor", user_params, false);
29-
set_override(ret, "user_agent", user_params, std::string());
22+
set_override(defaults, "electrum_tls", user_params, false);
23+
set_override(defaults, "electrum_url", user_params, std::string());
24+
set_override(defaults, "log_level", user_params, "none");
25+
set_override(defaults, "spv_multi", user_params, false);
26+
set_override(defaults, "spv_servers", user_params, nlohmann::json::array());
27+
set_override(defaults, "spv_enabled", user_params, false);
28+
set_override(defaults, "use_tor", user_params, false);
29+
set_override(defaults, "user_agent", user_params, std::string());
3030
// FIXME: Remove this by fetching it directly where needed
3131
const std::string datadir = gdk_config().value("datadir", std::string());
3232
GDK_RUNTIME_ASSERT(!datadir.empty());
33-
ret["state_dir"] = datadir + "/state";
34-
return network_parameters{ ret };
33+
defaults["state_dir"] = datadir + "/state";
34+
return network_parameters{ defaults };
3535
}
3636

3737
static void configure_logging(const network_parameters& net_params)
@@ -52,8 +52,24 @@ namespace sdk {
5252
}
5353
} // namespace
5454

55-
session_impl::session_impl(const nlohmann::json& net_params)
56-
: m_net_params(get_network_overrides(net_params))
55+
boost::shared_ptr<session_impl> session_impl::create(const nlohmann::json& net_params)
56+
{
57+
auto defaults = network_parameters::get(net_params.value("name", std::string()));
58+
const auto type = net_params.value("server_type", defaults.value("server_type", std::string()));
59+
60+
if (type == "green") {
61+
return boost::make_shared<ga_session>(net_params, defaults);
62+
}
63+
#ifdef BUILD_GDK_RUST
64+
if (type == "electrum") {
65+
return boost::make_shared<ga_rust>(net_params, defaults);
66+
}
67+
#endif
68+
throw user_error("Unknown server_type");
69+
}
70+
71+
session_impl::session_impl(const nlohmann::json& net_params, nlohmann::json& defaults)
72+
: m_net_params(get_network_overrides(net_params, defaults))
5773
, m_debug_logging(m_net_params.log_level() == "debug")
5874
{
5975
configure_logging(m_net_params);

src/session_impl.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@ namespace sdk {
1818

1919
class session_impl {
2020
public:
21-
explicit session_impl(const nlohmann::json& net_params);
21+
explicit session_impl(const nlohmann::json& net_params, nlohmann::json& defaults);
2222
session_impl(const session_impl& other) = delete;
2323
session_impl(session_impl&& other) noexcept = delete;
2424
session_impl& operator=(const session_impl& other) = delete;
2525
session_impl& operator=(session_impl&& other) noexcept = delete;
2626

2727
virtual ~session_impl();
2828

29+
// Factory method
30+
static boost::shared_ptr<session_impl> create(const nlohmann::json& net_params);
31+
2932
virtual void on_failed_login() = 0;
3033
virtual bool is_connected() const = 0;
3134
virtual void set_ping_fail_handler(ping_fail_t handler) = 0;

0 commit comments

Comments
 (0)