diff --git a/src/blade/CMakeLists.txt b/src/blade/CMakeLists.txt index 0fa0578..19b106b 100644 --- a/src/blade/CMakeLists.txt +++ b/src/blade/CMakeLists.txt @@ -1,55 +1,55 @@ include_directories ( ${CMAKE_CURRENT_BUILD_DIR} ) set ( BLADE_COMMON_SRCS - Protocol.cpp + transport/Serialization.cpp ui/UiBackend.cpp ${VOY_SOURCES} main.cpp ) if (NOT EXISTS "${CAPNP_EXECUTABLE}") # Workaround for some CMake problems with CapnProto set(CAPNP_EXECUTABLE "/usr/bin/capnp") set(CAPNPC_CXX_EXECUTABLE "/usr/bin/capnpc-c++") set(CAPNP_INCLUDE_DIRECTORY "/usr/include") set(CAPNP_INCLUDE_DIRS "/usr/include") endif() capnp_generate_cpp ( BLADE_CAPNP_SRCS BLADE_CAPNP_HEADERS - protocol/ping_message.capnp - protocol/query_message.capnp - protocol/error_message.capnp + transport/protocol/ping_message.capnp + transport/protocol/query_message.capnp + transport/protocol/error_message.capnp - protocol/controller_message.capnp + transport/protocol/controller_message.capnp ) add_executable ( bladed ${BLADE_COMMON_SRCS} ${BLADE_CAPNP_SRCS} ) target_link_libraries ( bladed Qt5::Core Qt5::Quick ${capnp_LIBRARIES} ${Boost_SYSTEM_LIBRARY} ${Boost_FILESYSTEM_LIBRARY} ${Boost_REGEX_LIBRARY} ${Boost_THREAD_LIBRARY} ${ZMQ_LIBRARIES} -pthread ) diff --git a/src/blade/blade-features.h.cmake b/src/blade/blade-features.h.cmake new file mode 100644 index 0000000..f13a8b0 --- /dev/null +++ b/src/blade/blade-features.h.cmake @@ -0,0 +1,11 @@ +#ifndef CONFIG_FEATURES_H_ +#define CONFIG_FEATURES_H_ + +#cmakedefine BLADE_DATA_DIR "@BLADE_DATA_DIR@" + +#cmakedefine BLADE_PLUGIN_DIR "@BLADE_PLUGIN_DIR@" +#cmakedefine BLADE_FULL_PLUGIN_DIR "@BLADE_FULL_PLUGIN_DIR@" + +#cmakedefine BLADE_INSTALL_PREFIX "@BLADE_INSTALL_PREFIX@" + +#endif diff --git a/src/blade/main.cpp b/src/blade/main.cpp index 13116df..e31db6c 100644 --- a/src/blade/main.cpp +++ b/src/blade/main.cpp @@ -1,126 +1,127 @@ /* * Copyright (C) 2018, 2019 Ivan Čukić * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. * If not, see . */ // std, boost #include #include #include // Voy #include #include #include #include #include #include #include #include // Qt #include #include #include #include #include // Blade -#include "Protocol.h" -#include "ControllerMessage.h" +#include "transport/Serialization.h" +namespace s11n = blade::serialization; +#include "transport/ControllerMessage.h" #include "ui/UiBackend.h" // Utils #include #include #include #include using std_ex::overloaded; using std_ex::bind_front; using namespace std::literals::string_literals; using namespace std::literals::chrono_literals; using namespace voy; class QueryGenerator { public: template blade::QueryMessage operator() (T&& value) const { return { m_id, voy_fwd(value) }; } private: mutable std::uint64_t m_id = 0; }; int main(int argc, char *argv[]) { QGuiApplication app(argc, argv); QQmlApplicationEngine engine; UiBackend backend; auto context = engine.rootContext(); context->setContextProperty("BladeUiBackend", &backend); engine.load(app.arguments()[1]); if (engine.rootObjects().isEmpty()) throw "QML not loaded properly"; boost::thread voy_thread([&backend] { auto cout = [] (auto&& value) { qDebug() << "SINK: " << value; }; using voy::dsl::operator|; auto pipeline = qt::signal(&backend, &UiBackend::searchRequested) | remove_if(&QString::isEmpty) | debounce(200ms) | transform(QueryGenerator{}) | transform(bind_front(initialize{}, "42", "0")) - | transform(blade::serialization::asStdString) - | transform(blade::serialization::readControllerMessage) + | transform(s11n::asStdString) + | transform(s11n::readControllerMessage) | transform([] (auto&& cm) { qDebug() << cm; return std::visit( overloaded { [] (const blade::PingMessage& msg) { return "PING"_qs; }, [] (const blade::QueryMessage& msg) { return msg.text; }, [] (const blade::ErrorMessage& msg) { return QString::fromLatin1(msg.message); } }, cm.message); }) | qt::slot(&backend, &UiBackend::searchFinished); voy::event_loop::run(); }); return app.exec(); } diff --git a/src/blade/ControllerMessage.cpp b/src/blade/transport/ControllerMessage.cpp similarity index 100% rename from src/blade/ControllerMessage.cpp rename to src/blade/transport/ControllerMessage.cpp diff --git a/src/blade/ControllerMessage.h b/src/blade/transport/ControllerMessage.h similarity index 100% rename from src/blade/ControllerMessage.h rename to src/blade/transport/ControllerMessage.h diff --git a/src/blade/Protocol.cpp b/src/blade/transport/Serialization.cpp similarity index 100% rename from src/blade/Protocol.cpp rename to src/blade/transport/Serialization.cpp diff --git a/src/blade/Protocol.h b/src/blade/transport/Serialization.h similarity index 98% rename from src/blade/Protocol.h rename to src/blade/transport/Serialization.h index 676b4e9..ea56459 100644 --- a/src/blade/Protocol.h +++ b/src/blade/transport/Serialization.h @@ -1,138 +1,138 @@ /* * Copyright (C) 2019 Ivan Čukić * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) version 3, or any * later version accepted by the membership of KDE e.V. (or its * successor approved by the membership of KDE e.V.), which shall * act as a proxy defined in Section 6 of version 3 of the license. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library. * If not, see . */ #ifndef BLADE_TRANSPORT_H #define BLADE_TRANSPORT_H #include "ControllerMessage.h" #include #include #include #include #include #include #include using std_ex::overloaded; -#include "protocol/controller_message.capnp.h" +#include "transport/protocol/controller_message.capnp.h" namespace blade::serialization { inline void writeToStdStream(const ControllerMessage &msg, std::ostream& out_) { capnp::MallocMessageBuilder builder; auto cm = builder.initRoot(); cm.setHost(msg.host.data()); cm.setController(msg.controller.data()); auto message = cm.initMessage(); std::visit( overloaded { [&](const PingMessage &) { message.initPing(); }, [&](const QueryMessage &msg) { auto query = message.initQuery(); query.setId(msg.id); query.setText(msg.text.toUtf8().data()); }, [&](const ErrorMessage &msg) { auto error = message.initError(); error.setCode(msg.code); error.setMessage(msg.message.data()); }, }, msg.message); kj::std::StdOutputStream out(out_); capnp::writeMessage(out, builder); } void writeToStdStream(const ControllerMessage &msg, std::ostream& out); inline std::string asStdString(const ControllerMessage& msg) { std::ostringstream result; writeToStdStream(msg, result); return result.str(); } namespace detail { auto asByteArray(const capnp::Text::Reader& reader) { return QByteArray(reader.cStr(), reader.size()); } } // namespace detail inline blade::ControllerMessage readControllerMessage(const std::string &data) { std::istringstream ss(data); kj::std::StdInputStream is(ss); capnp::InputStreamMessageReader reader(is); auto cm = reader.getRoot(); blade::ControllerMessage result; result.host = detail::asByteArray(cm.getHost()); result.controller = detail::asByteArray(cm.getController()); auto message = cm.getMessage(); if (message.hasQuery()) { auto query = message.getQuery(); result.message = blade::QueryMessage{ query.getId(), QString::fromUtf8(detail::asByteArray(query.getText())) }; } else if (message.hasPing()) { result.message = blade::PingMessage{}; } else if (message.hasError()) { auto error = message.getError(); result.message = blade::ErrorMessage{ error.getCode(), detail::asByteArray(error.getMessage()) }; } else { result.message = blade::ErrorMessage{ static_cast(-1), "unknown error" }; } return result; } } // namespace blade::serialization #endif // include guard diff --git a/src/blade/protocol/controller_message.capnp b/src/blade/transport/protocol/controller_message.capnp similarity index 100% rename from src/blade/protocol/controller_message.capnp rename to src/blade/transport/protocol/controller_message.capnp diff --git a/src/blade/protocol/error_message.capnp b/src/blade/transport/protocol/error_message.capnp similarity index 100% rename from src/blade/protocol/error_message.capnp rename to src/blade/transport/protocol/error_message.capnp diff --git a/src/blade/protocol/ping_message.capnp b/src/blade/transport/protocol/ping_message.capnp similarity index 100% rename from src/blade/protocol/ping_message.capnp rename to src/blade/transport/protocol/ping_message.capnp diff --git a/src/blade/protocol/query_message.capnp b/src/blade/transport/protocol/query_message.capnp similarity index 100% rename from src/blade/protocol/query_message.capnp rename to src/blade/transport/protocol/query_message.capnp