diff --git a/.gitignore b/.gitignore index f60a73705..e990e3f83 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,8 @@ nohup.out build/ cmake-build-debug/ +.cache/ +.ipynb_checkpoints/ .idea/ .dir-locals.el *.pyc diff --git a/externals b/externals index 10da2cde6..6b71f4149 160000 --- a/externals +++ b/externals @@ -1 +1 @@ -Subproject commit 10da2cde64499ba382446f4ee88e31300b822091 +Subproject commit 6b71f4149cd4f28632d7f9e80a4926a5698b10e4 diff --git a/src/lib/config/tests/HTTPServer.h b/src/lib/config/tests/HTTPServer.h index 88d1c0aac..90dd98368 100644 --- a/src/lib/config/tests/HTTPServer.h +++ b/src/lib/config/tests/HTTPServer.h @@ -8,6 +8,7 @@ #include #include #include +#define BOOST_BIND_GLOBAL_PLACEHOLDERS #include #include #include diff --git a/src/lib/plugin/plugin.cpp b/src/lib/plugin/plugin.cpp index cf2977f81..1cefa5748 100644 --- a/src/lib/plugin/plugin.cpp +++ b/src/lib/plugin/plugin.cpp @@ -18,7 +18,7 @@ using namespace bts::orderman; void init_plugins(SparkServices &services) { - static std::vector( + static std::vector( boost::filesystem::path(), ""))> keep_alive; @@ -54,7 +54,7 @@ void init_plugins(SparkServices &services) { LOG(Plugin, Info, "Attempting to initialize plugin {}", pl); try { - auto init_plugin = boost::dll::import( + auto init_plugin = boost::dll::import_symbol( f.path(), "init_spark_plugin"); init_plugin(services); keep_alive.push_back(init_plugin); diff --git a/src/lib/quickbus/network/QBConnection.cpp b/src/lib/quickbus/network/QBConnection.cpp index 426b01d2c..ec8ae99fd 100644 --- a/src/lib/quickbus/network/QBConnection.cpp +++ b/src/lib/quickbus/network/QBConnection.cpp @@ -22,6 +22,13 @@ namespace qbus { using boost::asio::ip::tcp; using namespace boost; +#if BOOST_VERSION >= 107000 +#define GET_IO_SERVICE(s) \ + ((boost::asio::io_context &)(s).get_executor().context()) +#else +#define GET_IO_SERVICE(s) ((s).get_io_service()) +#endif + struct QBConnection::PImpl { PImpl(tcp::socket &s, Router &router, config::QuickbusNodeCfg const &cfg) : socket(std::move(s)), router(router), pending_send(), write_buffer(), @@ -30,7 +37,7 @@ struct QBConnection::PImpl { my_desc(local_endpoint_description(socket)), remote_desc(remote_endpoint_description(socket)), cfg(cfg), last_received_ts(datetime::now()), last_sent_ts(datetime::now()), - hb_timer(socket.get_io_service()) { + hb_timer(GET_IO_SERVICE(socket)) { write_buffer.reserve(100 * 1024); read_buffer.reserve(100 * 1024); } diff --git a/src/lib/quickbus/network/QBConnectionManager.h b/src/lib/quickbus/network/QBConnectionManager.h index 67aeffe06..784974ad4 100644 --- a/src/lib/quickbus/network/QBConnectionManager.h +++ b/src/lib/quickbus/network/QBConnectionManager.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -13,11 +14,6 @@ namespace config { class QuickbusNodeCfg; } } // namespace bts -namespace boost { -namespace asio { -class io_service; -} -} // namespace boost // end fwds namespace bts { diff --git a/src/lib/utils/Dispatcher.cpp b/src/lib/utils/Dispatcher.cpp index 07f279b93..b6e791362 100644 --- a/src/lib/utils/Dispatcher.cpp +++ b/src/lib/utils/Dispatcher.cpp @@ -152,8 +152,8 @@ Dispatcher &Dispatcher::create_new(const char *name) { } }); t.detach(); - auto result = - c.wait_for(lock, chrono::seconds(5)); // releases the lock temporarily + auto result = c.wait_for( + lock, boost::asio::chrono::seconds(5)); // releases the lock temporarily bts_assert_msg( result == cv_status::no_timeout, "Dispatcher failed to start."); bts_assert(new_dispatcher); // Could happen to to spurious wake diff --git a/src/lib/utils/Dispatcher.h b/src/lib/utils/Dispatcher.h index 82124094a..483418459 100644 --- a/src/lib/utils/Dispatcher.h +++ b/src/lib/utils/Dispatcher.h @@ -4,6 +4,7 @@ #include "lib/utils/bts_export.h" #include "lib/utils/bts_no_copy.h" +#include #include #include #include @@ -53,12 +54,6 @@ * */ -namespace boost { -namespace asio { -class io_service; -} -} // namespace boost - namespace bts { namespace utils { diff --git a/src/lib/utils/datetime.cpp b/src/lib/utils/datetime.cpp index 706f3b93c..3951b4a23 100644 --- a/src/lib/utils/datetime.cpp +++ b/src/lib/utils/datetime.cpp @@ -49,8 +49,8 @@ long unix_now() { (long)1E6; } -std::chrono::system_clock::time_point -utc_ll_to_timepoint(unsigned long long ll) { +std::chrono::system_clock::time_point utc_ll_to_timepoint( + unsigned long long ll) { auto us_since_epoch = ((ll & ticks_mask) - anchor_ticks) / ticks_per_micro; return std::chrono::system_clock::time_point( std::chrono::microseconds(us_since_epoch)); @@ -149,8 +149,8 @@ PTime ll_to_ptime(LLDateTime ll) { boost::posix_time::microseconds(ticks_part / ticks_per_micro))); } -extern "C" BTS_EXPORT int -ll_to_cstr(LLDateTime ll, char *buffer, int buffer_sz) { +extern "C" BTS_EXPORT int ll_to_cstr( + LLDateTime ll, char *buffer, int buffer_sz) { const int str_sz = sizeof("20121204-14:44:02.000"); bts_assert(buffer_sz >= str_sz); auto str = FIX_timestamp(ll_to_ptime(ll)); @@ -218,8 +218,8 @@ std::string FIX_date_time_string(PTime const &t) { sprintf( &result[0], "%d%02d%02d-%02d:%02d:%02d", (unsigned short)t.date().year(), t.date().month().as_number(), t.date().day().as_number(), - t.time_of_day().hours(), t.time_of_day().minutes(), - t.time_of_day().seconds()); + (int)t.time_of_day().hours(), (int)t.time_of_day().minutes(), + (int)t.time_of_day().seconds()); return result; } diff --git a/src/server/sparkd/cmds/Run.cpp b/src/server/sparkd/cmds/Run.cpp index 28a00fd7e..e3368b145 100644 --- a/src/server/sparkd/cmds/Run.cpp +++ b/src/server/sparkd/cmds/Run.cpp @@ -59,7 +59,7 @@ static void execute() { trace::PerfLog perf_log; metrics::MetricsContainer stats; metrics::publish_system_metrics(stats); - http::SparkWebServer webserver(sc, stats, perf_log, ports.config_port()); + // http::SparkWebServer webserver(sc, stats, perf_log, ports.config_port()); persistence::PersistenceServiceServer settings_saver(sc, "settings"); user::AuthServiceServer login_server(sc); diff --git a/src/server/sparkd/cmds/Web.cpp b/src/server/sparkd/cmds/Web.cpp index 8deb5b400..9003c715c 100644 --- a/src/server/sparkd/cmds/Web.cpp +++ b/src/server/sparkd/cmds/Web.cpp @@ -1,74 +1,74 @@ -#include "Web.h" -#include "lib/api/public/ServiceContext.h" -#include "lib/api/services/MarketDataService.pb.h" -#include "lib/api/services/SyntheticSecurityService.pb.h" -#include "lib/config/config.h" -#include "lib/config/protos/MarketData.pb.h" -#include "lib/config/protos/Ports.pb.h" -#include "lib/config/protos/quickbus_cfg.pb.h" -#include "lib/md/MBOProcessor.h" -#include "lib/md/MDClient.h" -#include "lib/md/SyntheticSecurityService.h" -#include "lib/md/relay/QueryServer.h" -#include "lib/metrics/metrics.h" -#include "lib/metrics/system_metrics.h" -#include "lib/quickbus/quickbus.h" -#include "lib/tracing/PerfLog.h" -#include "lib/utils/Dispatcher.h" -#include "lib/utils/thread_manager.h" -#include "md/cmds/Cleanup.h" -#include "md/cmds/Run.h" -#include "server/sparkd/exchange_sim/ExchangeSim.h" -#include "server/sparkd/http/WebServer.h" -#include "server/sparkd/md/platforms/mdp3_onixs/TradeSummarySignalDispatcher.h" -#include "server/sparkd/user/AdminServiceServer.h" -#include "server/sparkd/user/AuthServiceServer.h" -#include -#include -#include +// #include "Web.h" +// #include "lib/api/public/ServiceContext.h" +// #include "lib/api/services/MarketDataService.pb.h" +// #include "lib/api/services/SyntheticSecurityService.pb.h" +// #include "lib/config/config.h" +// #include "lib/config/protos/MarketData.pb.h" +// #include "lib/config/protos/Ports.pb.h" +// #include "lib/config/protos/quickbus_cfg.pb.h" +// #include "lib/md/MBOProcessor.h" +// #include "lib/md/MDClient.h" +// #include "lib/md/SyntheticSecurityService.h" +// #include "lib/md/relay/QueryServer.h" +// #include "lib/metrics/metrics.h" +// #include "lib/metrics/system_metrics.h" +// #include "lib/quickbus/quickbus.h" +// #include "lib/tracing/PerfLog.h" +// #include "lib/utils/Dispatcher.h" +// #include "lib/utils/thread_manager.h" +// #include "md/cmds/Cleanup.h" +// #include "md/cmds/Run.h" +// #include "server/sparkd/exchange_sim/ExchangeSim.h" +// #include "server/sparkd/http/WebServer.h" +// #include "server/sparkd/md/platforms/mdp3_onixs/TradeSummarySignalDispatcher.h" +// #include "server/sparkd/user/AdminServiceServer.h" +// #include "server/sparkd/user/AuthServiceServer.h" +// #include +// #include +// #include -namespace bts::cmds { +// namespace bts::cmds { -struct Web::Args { - bool md; -}; +// struct Web::Args { +// bool md; +// }; -Web::Web() - : CLI::App( - "Run only the services required for the web admin. This cannot be " - "run concurrently with 'sparkd run.'", - "web"), - _args(new Web::Args()) { - add_flag("--md", _args->md, "Start market data"); - callback([&] { - config::QuickbusNodeCfg qb_cfg; - auto &ports = config::get(); - qb_cfg.set_my_port(ports.qbus_port()); - qbus::Quickbus quickbus(qb_cfg); - bts::api::ServiceContext sc(&quickbus); +// Web::Web() +// : CLI::App( +// "Run only the services required for the web admin. This cannot be " +// "run concurrently with 'sparkd run.'", +// "web"), +// _args(new Web::Args()) { +// add_flag("--md", _args->md, "Start market data"); +// callback([&] { +// config::QuickbusNodeCfg qb_cfg; +// auto &ports = config::get(); +// qb_cfg.set_my_port(ports.qbus_port()); +// qbus::Quickbus quickbus(qb_cfg); +// bts::api::ServiceContext sc(&quickbus); - bts::metrics::MetricsContainer stats; - metrics::publish_system_metrics(stats); - trace::PerfLog perf_log; - http::SparkWebServer webserver(sc, stats, perf_log, ports.config_port()); +// bts::metrics::MetricsContainer stats; +// metrics::publish_system_metrics(stats); +// trace::PerfLog perf_log; +// http::SparkWebServer webserver(sc, stats, perf_log, ports.config_port()); - user::AuthServiceServer login_server(sc); - if (_args->md) - md::cmds::Run::run( - nullptr, nullptr, md::cmds::Run::MDType::Configured, true, true); +// user::AuthServiceServer login_server(sc); +// if (_args->md) +// md::cmds::Run::run( +// nullptr, nullptr, md::cmds::Run::MDType::Configured, true, true); - bts::api::synthetic::SyntheticSecurityService synthetic_service(sc); - bts::admin::AdminServiceServer admin_service(sc); - bts::md::MDClient mdclient( - &bts::utils::Dispatcher::current(), &synthetic_service); +// bts::api::synthetic::SyntheticSecurityService synthetic_service(sc); +// bts::admin::AdminServiceServer admin_service(sc); +// bts::md::MDClient mdclient( +// &bts::utils::Dispatcher::current(), &synthetic_service); - bts::md::QueryServerPtr md_query_server( - bts::md::run_query_server(sc, mdclient, stats)); +// bts::md::QueryServerPtr md_query_server( +// bts::md::run_query_server(sc, mdclient, stats)); - bts::utils::Dispatcher::current().run(); - }); -} +// bts::utils::Dispatcher::current().run(); +// }); +// } -Web::~Web() {} +// Web::~Web() {} -} // namespace bts::cmds +// } // namespace bts::cmds diff --git a/src/server/sparkd/http/WebServer.cpp b/src/server/sparkd/http/WebServer.cpp index 901122afb..e5b83a05a 100644 --- a/src/server/sparkd/http/WebServer.cpp +++ b/src/server/sparkd/http/WebServer.cpp @@ -1,421 +1,421 @@ -#include "WebServer.h" -#include "ApiGateway.h" -#include "MultipartFormDecoder.h" -#include "lib/api/apigateway.pb.h" -#include "lib/api/core/ApiClientInterop.h" -#include "lib/logging/logging.h" -#include "lib/os/os.h" -#include "lib/tracing/PerfLog.h" -#include "lib/utils/Dispatcher.h" -#include "lib/utils/TimeStamp.h" +// #include "WebServer.h" +// #include "ApiGateway.h" +// #include "MultipartFormDecoder.h" +// #include "lib/api/apigateway.pb.h" +// #include "lib/api/core/ApiClientInterop.h" +// #include "lib/logging/logging.h" +// #include "lib/os/os.h" +// #include "lib/tracing/PerfLog.h" +// #include "lib/utils/Dispatcher.h" +// #include "lib/utils/TimeStamp.h" -#include -#include +// #include +// #include -#include -#include -#include +// #include +// #include +// #include -#include -#include -#include -#include -#include -#include -#include -#include +// #include +// #include +// #include +// #include +// #include +// #include +// #include +// #include -typedef websocketpp::server WsServer; -typedef WsServer::endpoint::connection_ptr connection_ptr; -using websocketpp::connection_hdl; -using websocketpp::lib::bind; -using websocketpp::lib::placeholders::_1; -using websocketpp::lib::placeholders::_2; +// typedef websocketpp::server WsServer; +// typedef WsServer::endpoint::connection_ptr connection_ptr; +// using websocketpp::connection_hdl; +// using websocketpp::lib::bind; +// using websocketpp::lib::placeholders::_1; +// using websocketpp::lib::placeholders::_2; -using namespace bts::api::gateway; +// using namespace bts::api::gateway; -std::string read_file(const std::string &path) { - std::ifstream f{path}; - auto sz = boost::filesystem::file_size(path); - std::string contents(sz, ' '); - f.read(&contents[0], sz); - return contents; -} +// std::string read_file(const std::string &path) { +// std::ifstream f{path}; +// auto sz = boost::filesystem::file_size(path); +// std::string contents(sz, ' '); +// f.read(&contents[0], sz); +// return contents; +// } -namespace bts { -namespace http { +// namespace bts { +// namespace http { -struct ServerState { - std::mutex _m; - utils::Dispatcher &_dispatcher; - std::map> - _session; - WsServer _server; - int _port; - std::string _www; - std::string _conf; - const api::ServiceContext &_ctx; - metrics::MetricsContainer &_metrics; - std::shared_ptr> _mtimer; - trace::PerfLog &_perf_log; +// struct ServerState { +// std::mutex _m; +// utils::Dispatcher &_dispatcher; +// std::map> +// _session; +// WsServer _server; +// int _port; +// std::string _www; +// std::string _conf; +// const api::ServiceContext &_ctx; +// metrics::MetricsContainer &_metrics; +// std::shared_ptr> _mtimer; +// trace::PerfLog &_perf_log; - ServerState( - const api::ServiceContext &ctx, - metrics::MetricsContainer &metrics, - trace::PerfLog &l, - int port) - : _m(), _dispatcher(utils::Dispatcher::get_named("Webserver")), - _session(), _server(), _port(port), _www(), _ctx(ctx), - _metrics(metrics), - _mtimer(std::make_shared>( - "spark_metrics_query_duration_ms", - std::array({0.5, 1.0, 5.0, 10.0}))), - _perf_log(l) { - _www = os::sparkdata_path("www"); - _conf = os::sparkdata_path("conf"); +// ServerState( +// const api::ServiceContext &ctx, +// metrics::MetricsContainer &metrics, +// trace::PerfLog &l, +// int port) +// : _m(), _dispatcher(utils::Dispatcher::get_named("Webserver")), +// _session(), _server(), _port(port), _www(), _ctx(ctx), +// _metrics(metrics), +// _mtimer(std::make_shared>( +// "spark_metrics_query_duration_ms", +// std::array({0.5, 1.0, 5.0, 10.0}))), +// _perf_log(l) { +// _www = os::sparkdata_path("www"); +// _conf = os::sparkdata_path("conf"); - _metrics.publish(_mtimer); +// _metrics.publish(_mtimer); - _server.init_asio(_dispatcher.io_service()); - _server.set_open_handler(bind(&ServerState::on_open, this, ::_1)); - _server.set_close_handler(bind(&ServerState::on_close, this, ::_1)); - _server.set_message_handler( - bind(&ServerState::on_message, this, ::_1, ::_2)); - _server.set_http_handler(bind(&ServerState::on_http, this, ::_1)); - // Turn off logging - _server.clear_access_channels(websocketpp::log::alevel::all); - _server.clear_error_channels(websocketpp::log::alevel::all); - _server.set_open_handshake_timeout(0); - run(); - } +// _server.init_asio(_dispatcher.io_service()); +// _server.set_open_handler(bind(&ServerState::on_open, this, ::_1)); +// _server.set_close_handler(bind(&ServerState::on_close, this, ::_1)); +// _server.set_message_handler( +// bind(&ServerState::on_message, this, ::_1, ::_2)); +// _server.set_http_handler(bind(&ServerState::on_http, this, ::_1)); +// // Turn off logging +// _server.clear_access_channels(websocketpp::log::alevel::all); +// _server.clear_error_channels(websocketpp::log::alevel::all); +// _server.set_open_handshake_timeout(0); +// run(); +// } - // starts listening - void run() { - _server.set_reuse_addr(true); - _server.listen(_port); - _server.start_accept(); - } +// // starts listening +// void run() { +// _server.set_reuse_addr(true); +// _server.listen(_port); +// _server.start_accept(); +// } - void on_open(connection_hdl hdl) { - auto con = _server.get_con_from_hdl(hdl); - if (con) { - LOG(Http, Info, "Web client connection from {}", - con->get_remote_endpoint()); - std::lock_guard lock(_m); - _session[hdl] = ApiGatewaySession(_ctx); - } - } +// void on_open(connection_hdl hdl) { +// auto con = _server.get_con_from_hdl(hdl); +// if (con) { +// LOG(Http, Info, "Web client connection from {}", +// con->get_remote_endpoint()); +// std::lock_guard lock(_m); +// _session[hdl] = ApiGatewaySession(_ctx); +// } +// } - void on_close(connection_hdl hdl) { - auto con = _server.get_con_from_hdl(hdl); - if (con) { - LOG(Http, Info, "Web client disconnected: {}", - con->get_remote_endpoint()); - std::lock_guard lock(_m); - _session.erase(hdl); - } - } +// void on_close(connection_hdl hdl) { +// auto con = _server.get_con_from_hdl(hdl); +// if (con) { +// LOG(Http, Info, "Web client disconnected: {}", +// con->get_remote_endpoint()); +// std::lock_guard lock(_m); +// _session.erase(hdl); +// } +// } - void on_message(connection_hdl hdl, WsServer::message_ptr msg) { - std::lock_guard lock(_m); - auto iter = _session.find(hdl); - if (iter != _session.end()) { - GatewayRequest request; - if (request.ParseFromString(msg->get_payload())) { - iter->second.handle( - request, [this, hdl](const GatewayResponse &response) { - websocketpp::lib::error_code ec; - _server.send( - hdl, response.SerializeAsString(), - websocketpp::frame::opcode::binary, ec); - if (ec) { - LOG(Http, Warn, "Error sending from websocket: '{}'", - ec.message()); - } - }); - } else { - LOG(Http, Info, "Could not parse request as GatewayRequest: {}", - msg->get_payload()); - } - } - } +// void on_message(connection_hdl hdl, WsServer::message_ptr msg) { +// std::lock_guard lock(_m); +// auto iter = _session.find(hdl); +// if (iter != _session.end()) { +// GatewayRequest request; +// if (request.ParseFromString(msg->get_payload())) { +// iter->second.handle( +// request, [this, hdl](const GatewayResponse &response) { +// websocketpp::lib::error_code ec; +// _server.send( +// hdl, response.SerializeAsString(), +// websocketpp::frame::opcode::binary, ec); +// if (ec) { +// LOG(Http, Warn, "Error sending from websocket: '{}'", +// ec.message()); +// } +// }); +// } else { +// LOG(Http, Info, "Could not parse request as GatewayRequest: {}", +// msg->get_payload()); +// } +// } +// } - void return_dotall(connection_ptr &con) { - std::vector files; - auto iter = boost::filesystem::directory_iterator(_conf); - auto end = boost::filesystem::directory_iterator(); - // Collect all .conf file - for (; iter != end; ++iter) { - auto ext = iter->path().filename().string().substr( - iter->path().filename().stem().size()); - if (boost::iequals(ext, ".conf")) { - files.push_back(iter->path()); - } - } - // Sort them so we at least it's stable - std::sort(files.begin(), files.end()); - // Concatanate - std::string body; - for (const auto &p : files) { - body += "##########################\n"; - body += "## " + p.filename().string(); - body += "\n##########################\n"; - body += read_file(p.string()); - } +// void return_dotall(connection_ptr &con) { +// std::vector files; +// auto iter = boost::filesystem::directory_iterator(_conf); +// auto end = boost::filesystem::directory_iterator(); +// // Collect all .conf file +// for (; iter != end; ++iter) { +// auto ext = iter->path().filename().string().substr( +// iter->path().filename().stem().size()); +// if (boost::iequals(ext, ".conf")) { +// files.push_back(iter->path()); +// } +// } +// // Sort them so we at least it's stable +// std::sort(files.begin(), files.end()); +// // Concatanate +// std::string body; +// for (const auto &p : files) { +// body += "##########################\n"; +// body += "## " + p.filename().string(); +// body += "\n##########################\n"; +// body += read_file(p.string()); +// } - con->set_body(body); - con->set_status(websocketpp::http::status_code::ok); - } +// con->set_body(body); +// con->set_status(websocketpp::http::status_code::ok); +// } - void return_metrics(connection_ptr &con) { - utils::TimeStamp start = utils::TimeStamp::now(); - std::stringstream ss; - _metrics.print(ss); - con->set_body(ss.str()); - con->set_status(websocketpp::http::status_code::ok); - _mtimer->observe( - (utils::TimeStamp::now() - start).microseconds() / 1000.0); - } +// void return_metrics(connection_ptr &con) { +// utils::TimeStamp start = utils::TimeStamp::now(); +// std::stringstream ss; +// _metrics.print(ss); +// con->set_body(ss.str()); +// con->set_status(websocketpp::http::status_code::ok); +// _mtimer->observe( +// (utils::TimeStamp::now() - start).microseconds() / 1000.0); +// } - void return_perf_trace_data(connection_ptr &con, size_t max_rows) { - utils::TimeStamp start = utils::TimeStamp::now(); - std::stringstream ss; - _perf_log.print_latest_traces(ss, max_rows); - con->set_body(ss.str()); - con->set_status(websocketpp::http::status_code::ok); - _mtimer->observe( - (utils::TimeStamp::now() - start).microseconds() / 1000.0); - } +// void return_perf_trace_data(connection_ptr &con, size_t max_rows) { +// utils::TimeStamp start = utils::TimeStamp::now(); +// std::stringstream ss; +// _perf_log.print_latest_traces(ss, max_rows); +// con->set_body(ss.str()); +// con->set_status(websocketpp::http::status_code::ok); +// _mtimer->observe( +// (utils::TimeStamp::now() - start).microseconds() / 1000.0); +// } - void bad_request(connection_ptr &con, const std::string &message) { - con->set_body(message); - con->set_status(websocketpp::http::status_code::bad_request); - } +// void bad_request(connection_ptr &con, const std::string &message) { +// con->set_body(message); +// con->set_status(websocketpp::http::status_code::bad_request); +// } - std::vector - split_string(std::string body, std::string &deliniator) { - std::vector result; - size_t i = body.find(deliniator); - while (i != std::string::npos) { - result.push_back(body.substr(0, i)); - body = body.substr(i + deliniator.size()); - i = body.find(deliniator); - } - return result; - } +// std::vector +// split_string(std::string body, std::string &deliniator) { +// std::vector result; +// size_t i = body.find(deliniator); +// while (i != std::string::npos) { +// result.push_back(body.substr(0, i)); +// body = body.substr(i + deliniator.size()); +// i = body.find(deliniator); +// } +// return result; +// } - void handle_file_upload(connection_ptr &con) { - auto content_type = con->get_request_header("Content-Type"); - auto data = decode_form_data(content_type, con->get_request_body()); - if (data) { - for (const auto &part : data.value().parts) { - if (!part.filename.empty()) { - LOG(Http, Info, "Received file upload: {}", part.filename); - } - } - } else { - LOG(Http, Warn, - "Failed to decode form data. Ensure you are using " - "'enctype=\"multipart/form-data\"'"); - } +// void handle_file_upload(connection_ptr &con) { +// auto content_type = con->get_request_header("Content-Type"); +// auto data = decode_form_data(content_type, con->get_request_body()); +// if (data) { +// for (const auto &part : data.value().parts) { +// if (!part.filename.empty()) { +// LOG(Http, Info, "Received file upload: {}", part.filename); +// } +// } +// } else { +// LOG(Http, Warn, +// "Failed to decode form data. Ensure you are using " +// "'enctype=\"multipart/form-data\"'"); +// } - con->set_body("got it"); - con->set_status(websocketpp::http::status_code::ok); - } +// con->set_body("got it"); +// con->set_status(websocketpp::http::status_code::ok); +// } - bool require_authentication( - connection_ptr &con, - const std::string &username, - const std::string &password) { - auto auth = con->get_request_header("Authorization"); - if (!auth.empty() && auth.size() >= 7 && - boost::iequals(auth.substr(0, 6), "Basic ")) { - auto b64 = auth.substr(6); - auto decoded = websocketpp::base64_decode(b64); - auto colon = decoded.find(':'); - if (colon != std::string::npos) { - auto user = decoded.substr(0, colon); - auto pass = decoded.substr(colon + 1); - if (user == username && pass == password) - return true; - } - } +// bool require_authentication( +// connection_ptr &con, +// const std::string &username, +// const std::string &password) { +// auto auth = con->get_request_header("Authorization"); +// if (!auth.empty() && auth.size() >= 7 && +// boost::iequals(auth.substr(0, 6), "Basic ")) { +// auto b64 = auth.substr(6); +// auto decoded = websocketpp::base64_decode(b64); +// auto colon = decoded.find(':'); +// if (colon != std::string::npos) { +// auto user = decoded.substr(0, colon); +// auto pass = decoded.substr(colon + 1); +// if (user == username && pass == password) +// return true; +// } +// } - // Failed - con->append_header("WWW-Authenticate", "Basic realm=\"BTS\""); - con->set_status(websocketpp::http::status_code::unauthorized); - con->set_body("Not Authorized"); - return false; - } +// // Failed +// con->append_header("WWW-Authenticate", "Basic realm=\"BTS\""); +// con->set_status(websocketpp::http::status_code::unauthorized); +// con->set_body("Not Authorized"); +// return false; +// } - void check_auth_and_return_info(connection_ptr &con) { - if (require_authentication( - con, "bts", "bbb835af-b1ea-42d3-8be1-fd9d5555530d")) { - auto horrible_json = get_server_info(_ctx); - con->set_body(horrible_json); - con->set_status(websocketpp::http::status_code::ok); - } - } +// void check_auth_and_return_info(connection_ptr &con) { +// if (require_authentication( +// con, "bts", "bbb835af-b1ea-42d3-8be1-fd9d5555530d")) { +// auto horrible_json = get_server_info(_ctx); +// con->set_body(horrible_json); +// con->set_status(websocketpp::http::status_code::ok); +// } +// } - void directory_index( - connection_ptr &con, - const std::string &resource, - const boost::filesystem::path &path) { - // directory listing - std::string body = "\n"; - body += ""; - body += "\n"; - body += "\n"; - body += "\n"; - body += "

Index of " + resource + "

\n"; - auto iter = boost::filesystem::directory_iterator(path); - auto end = boost::filesystem::directory_iterator(); - body += "\n"; - body += "\n"; - con->set_status(websocketpp::http::status_code::ok); - con->set_body(body); - } +// void directory_index( +// connection_ptr &con, +// const std::string &resource, +// const boost::filesystem::path &path) { +// // directory listing +// std::string body = "\n"; +// body += ""; +// body += "\n"; +// body += "\n"; +// body += "\n"; +// body += "

Index of " + resource + "

\n"; +// auto iter = boost::filesystem::directory_iterator(path); +// auto end = boost::filesystem::directory_iterator(); +// body += "\n"; +// body += "\n"; +// con->set_status(websocketpp::http::status_code::ok); +// con->set_body(body); +// } - boost::filesystem::path web_ui_path(const boost::filesystem::path &root) { - auto reldir = root / "web/ui"; - if (boost::filesystem::exists(reldir)) - return reldir; - reldir = root / "ui"; - if (boost::filesystem::exists(reldir)) - return reldir; - if (root.has_parent_path()) - return web_ui_path(root.parent_path()); - return _www; - } +// boost::filesystem::path web_ui_path(const boost::filesystem::path &root) { +// auto reldir = root / "web/ui"; +// if (boost::filesystem::exists(reldir)) +// return reldir; +// reldir = root / "ui"; +// if (boost::filesystem::exists(reldir)) +// return reldir; +// if (root.has_parent_path()) +// return web_ui_path(root.parent_path()); +// return _www; +// } - boost::filesystem::path map_path_to_filesystem(const std::string &resource) { - std::string config = "/config"; - std::string files = "/files"; - std::string perf_trace = "/perf_trace"; - std::string docs = "/docs"; +// boost::filesystem::path map_path_to_filesystem(const std::string &resource) { +// std::string config = "/config"; +// std::string files = "/files"; +// std::string perf_trace = "/perf_trace"; +// std::string docs = "/docs"; - if (resource.compare(0, config.size(), config) == 0) { - return _conf + resource.substr(config.size()); - } else if (resource.compare(0, files.size(), files) == 0) { - return _www + resource.substr(files.size()); - } else if (resource.compare(0, perf_trace.size(), perf_trace) == 0) { - return os::sparkd_wd() + "/data_files" + resource; - } else if (resource.compare(0, docs.size(), docs) == 0) { - return os::sparkd_wd() + "/web" + resource; - } - return web_ui_path(os::get_install_path()) / resource; - } +// if (resource.compare(0, config.size(), config) == 0) { +// return _conf + resource.substr(config.size()); +// } else if (resource.compare(0, files.size(), files) == 0) { +// return _www + resource.substr(files.size()); +// } else if (resource.compare(0, perf_trace.size(), perf_trace) == 0) { +// return os::sparkd_wd() + "/data_files" + resource; +// } else if (resource.compare(0, docs.size(), docs) == 0) { +// return os::sparkd_wd() + "/web" + resource; +// } +// return web_ui_path(os::get_install_path()) / resource; +// } - void return_file(connection_ptr &con, const boost::filesystem::path &path) { - con->set_body(read_file(path.string())); - con->set_status(websocketpp::http::status_code::ok); - } +// void return_file(connection_ptr &con, const boost::filesystem::path &path) { +// con->set_body(read_file(path.string())); +// con->set_status(websocketpp::http::status_code::ok); +// } - void return_bts_installer(connection_ptr &con) { - std::string installer_bits = read_file(_www + "/bts_installer.exe"); - const std::string installer_config = _www + "install_config.json"; - if (boost::filesystem::is_regular_file(installer_config)) { - auto conf = read_file(installer_config); - std::string needle = "$HOST"; - std::size_t pos; - do { - pos = conf.find(needle, 0); - if (pos != std::string::npos) { - auto host = con->get_uri()->get_host(); - conf.replace(pos, needle.size(), host); - } - } while (pos != std::string::npos); - installer_bits += conf; - } - con->set_body(installer_bits); - con->set_status(websocketpp::http::status_code::ok); - } +// void return_bts_installer(connection_ptr &con) { +// std::string installer_bits = read_file(_www + "/bts_installer.exe"); +// const std::string installer_config = _www + "install_config.json"; +// if (boost::filesystem::is_regular_file(installer_config)) { +// auto conf = read_file(installer_config); +// std::string needle = "$HOST"; +// std::size_t pos; +// do { +// pos = conf.find(needle, 0); +// if (pos != std::string::npos) { +// auto host = con->get_uri()->get_host(); +// conf.replace(pos, needle.size(), host); +// } +// } while (pos != std::string::npos); +// installer_bits += conf; +// } +// con->set_body(installer_bits); +// con->set_status(websocketpp::http::status_code::ok); +// } - std::string strip_query_str(const std::string &request) { - auto position = request.find("?"); - if (position != std::string::npos) { - return request.substr(0, position); - } - return request; - } +// std::string strip_query_str(const std::string &request) { +// auto position = request.find("?"); +// if (position != std::string::npos) { +// return request.substr(0, position); +// } +// return request; +// } - void on_http(connection_hdl hdl) { - auto con = _server.get_con_from_hdl(hdl); - auto resource = strip_query_str(con->get_resource()); - LOG(Http, Diag, "HTTP GET: {}", resource); - if (boost::iequals(resource, "/config/.all")) { - return_dotall(con); - } else if (boost::iequals(resource, "/metrics")) { - return_metrics(con); - } else if (boost::iequals(resource, "/perf_trace_data")) { - return_perf_trace_data(con, 1000); - } else if (std::regex_match( - resource, std::regex("/perf_trace_data/(\\d++)"))) { - std::regex rx = std::regex("/perf_trace_data/(\\d++)"); - std::smatch matches; - if (std::regex_match(resource, matches, rx)) { - auto max_rows = std::atoi(matches[1].str().c_str()); - return_perf_trace_data(con, max_rows); - } else - return_perf_trace_data(con, 1000); - } else if (boost::iequals(resource, "/server_info")) { - check_auth_and_return_info(con); - } else if ( - boost::iequals(resource, "/bts_installer.exe") && - boost::filesystem::is_regular_file(_www + "/bts_installer.exe")) { - return_bts_installer(con); - } else if (boost::iequals(resource, "/robots.txt")) { - con->set_status(websocketpp::http::status_code::ok); - std::string body = "User-agent: *\nDisallow: /"; - con->set_body(body); - } else if (boost::iequals(resource, "/upload")) { - handle_file_upload(con); - } else { - auto path = map_path_to_filesystem(resource); - if (boost::filesystem::is_directory(path)) { - auto index = path / "index.html"; - if (boost::filesystem::is_regular_file(index)) { - return_file(con, index); - } else { - directory_index(con, resource, path); - } - } else if (boost::filesystem::is_regular_file(path)) { - return_file(con, path); - } else { - con->set_status(websocketpp::http::status_code::not_found); - con->set_body("Cannot find " + resource); - } - } - } -}; +// void on_http(connection_hdl hdl) { +// auto con = _server.get_con_from_hdl(hdl); +// auto resource = strip_query_str(con->get_resource()); +// LOG(Http, Diag, "HTTP GET: {}", resource); +// if (boost::iequals(resource, "/config/.all")) { +// return_dotall(con); +// } else if (boost::iequals(resource, "/metrics")) { +// return_metrics(con); +// } else if (boost::iequals(resource, "/perf_trace_data")) { +// return_perf_trace_data(con, 1000); +// } else if (std::regex_match( +// resource, std::regex("/perf_trace_data/(\\d++)"))) { +// std::regex rx = std::regex("/perf_trace_data/(\\d++)"); +// std::smatch matches; +// if (std::regex_match(resource, matches, rx)) { +// auto max_rows = std::atoi(matches[1].str().c_str()); +// return_perf_trace_data(con, max_rows); +// } else +// return_perf_trace_data(con, 1000); +// } else if (boost::iequals(resource, "/server_info")) { +// check_auth_and_return_info(con); +// } else if ( +// boost::iequals(resource, "/bts_installer.exe") && +// boost::filesystem::is_regular_file(_www + "/bts_installer.exe")) { +// return_bts_installer(con); +// } else if (boost::iequals(resource, "/robots.txt")) { +// con->set_status(websocketpp::http::status_code::ok); +// std::string body = "User-agent: *\nDisallow: /"; +// con->set_body(body); +// } else if (boost::iequals(resource, "/upload")) { +// handle_file_upload(con); +// } else { +// auto path = map_path_to_filesystem(resource); +// if (boost::filesystem::is_directory(path)) { +// auto index = path / "index.html"; +// if (boost::filesystem::is_regular_file(index)) { +// return_file(con, index); +// } else { +// directory_index(con, resource, path); +// } +// } else if (boost::filesystem::is_regular_file(path)) { +// return_file(con, path); +// } else { +// con->set_status(websocketpp::http::status_code::not_found); +// con->set_body("Cannot find " + resource); +// } +// } +// } +// }; -SparkWebServer::SparkWebServer( - const api::ServiceContext &ctx, - metrics::MetricsContainer &metrics, - trace::PerfLog &l, - int port) - : _me(std::make_shared(ctx, metrics, l, port)) {} +// SparkWebServer::SparkWebServer( +// const api::ServiceContext &ctx, +// metrics::MetricsContainer &metrics, +// trace::PerfLog &l, +// int port) +// : _me(std::make_shared(ctx, metrics, l, port)) {} -} // namespace http -} // namespace bts +// } // namespace http +// } // namespace bts diff --git a/src/server/sparkd/main.cpp b/src/server/sparkd/main.cpp index 1fb98aced..bb2b4e425 100644 --- a/src/server/sparkd/main.cpp +++ b/src/server/sparkd/main.cpp @@ -31,7 +31,7 @@ int main(int argc, char **argv) { app.add_subcommand(CLI::App_p(new cmds::QuickTest())); app.add_subcommand(CLI::App_p(new cmds::Run())); app.add_subcommand(CLI::App_p(new cmds::Spartan())); - app.add_subcommand(CLI::App_p(new cmds::Web())); + // app.add_subcommand(CLI::App_p(new cmds::Web())); app.add_subcommand( "test", "Run unit test(s)"); // so this shows up in help CLI11_PARSE(app, argc, argv);