From f3a6650cb71068bb7a01c19597eab57f50c2ab44 Mon Sep 17 00:00:00 2001 From: Stepan Usatiuk Date: Wed, 25 Jun 2025 08:09:19 +0200 Subject: [PATCH] dump 2 --- launcher/CMakeLists.txt | 3 +- launcher/backend/CMakeLists.txt | 2 + .../backend/include_public/DhfsInstance.hpp | 21 ++++++++- launcher/backend/src/DhfsInstance.cpp | 45 +++++++++++++++++++ launcher/gui/CMakeLists.txt | 1 + launcher/gui/src/LauncherAppMainFrame.cpp | 1 + launcher/gui/src/LauncherAppMainFrame.h | 5 +++ launcher/libjvm_wrapper/CMakeLists.txt | 10 +++++ .../include_public/LibjvmWrapper.hpp | 25 +++++++++++ launcher/libjvm_wrapper/src/LibjvmWrapper.cpp | 25 +++++++++++ 10 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 launcher/libjvm_wrapper/CMakeLists.txt create mode 100644 launcher/libjvm_wrapper/include_public/LibjvmWrapper.hpp create mode 100644 launcher/libjvm_wrapper/src/LibjvmWrapper.cpp diff --git a/launcher/CMakeLists.txt b/launcher/CMakeLists.txt index 853a2ff2..5749d4f6 100644 --- a/launcher/CMakeLists.txt +++ b/launcher/CMakeLists.txt @@ -28,5 +28,6 @@ if (NOT CMAKE_BUILD_TYPE STREQUAL "Debug") add_link_options(-O3) endif () +add_subdirectory(libjvm_wrapper) +add_subdirectory(backend) add_subdirectory(gui) -add_subdirectory(backend) \ No newline at end of file diff --git a/launcher/backend/CMakeLists.txt b/launcher/backend/CMakeLists.txt index b30de832..878b84a8 100644 --- a/launcher/backend/CMakeLists.txt +++ b/launcher/backend/CMakeLists.txt @@ -5,3 +5,5 @@ add_library(backend target_include_directories(backend PRIVATE include) target_include_directories(backend PUBLIC include_public) + +target_link_libraries(backend PUBLIC libjvm_wrapper) diff --git a/launcher/backend/include_public/DhfsInstance.hpp b/launcher/backend/include_public/DhfsInstance.hpp index f06082ed..6001df50 100644 --- a/launcher/backend/include_public/DhfsInstance.hpp +++ b/launcher/backend/include_public/DhfsInstance.hpp @@ -5,12 +5,31 @@ #ifndef DHFSINSTANCE_HPP #define DHFSINSTANCE_HPP +#include +enum class DhfsInstanceState { + RUNNING, + STOPPED, +}; class DhfsInstance { +public: + DhfsInstance(); + ~DhfsInstance(); + + DhfsInstanceState state(); + + void start(); + + void stop(); + +private: + DhfsInstanceState _state = DhfsInstanceState::STOPPED; + + JavaVM* _jvm = nullptr; + JNIEnv *_env = nullptr; }; - #endif //DHFSINSTANCE_HPP diff --git a/launcher/backend/src/DhfsInstance.cpp b/launcher/backend/src/DhfsInstance.cpp index b3f12939..394db4a6 100644 --- a/launcher/backend/src/DhfsInstance.cpp +++ b/launcher/backend/src/DhfsInstance.cpp @@ -3,3 +3,48 @@ // #include "DhfsInstance.hpp" + +#include +#include + +#include "LibjvmWrapper.hpp" + +DhfsInstance::DhfsInstance() { +} + +DhfsInstance::~DhfsInstance() { +} + +DhfsInstanceState DhfsInstance::state() { +} + +void DhfsInstance::start() { + switch (_state) { + case DhfsInstanceState::RUNNING: + return; + case DhfsInstanceState::STOPPED: + break; + default: + throw std::runtime_error("Unknown DhfsInstanceState"); + } + + JavaVMInitArgs args; + std::vector options; + args.version = JNI_VERSION_21; + args.nOptions = 0; + args.options = options.data(); + args.ignoreUnrecognized = false; + + LibjvmWrapper::instance().JNI_CreateJavaVM(&_jvm, (void**) &_env, &args); +} + +void DhfsInstance::stop() { + switch (_state) { + case DhfsInstanceState::RUNNING: + break; + case DhfsInstanceState::STOPPED: + return; + default: + throw std::runtime_error("Unknown DhfsInstanceState"); + } +} diff --git a/launcher/gui/CMakeLists.txt b/launcher/gui/CMakeLists.txt index 615bd98e..3cb68c77 100644 --- a/launcher/gui/CMakeLists.txt +++ b/launcher/gui/CMakeLists.txt @@ -11,3 +11,4 @@ add_executable(launcher ) target_link_libraries(launcher ${wxWidgets_LIBRARIES}) +target_link_libraries(launcher backend) \ No newline at end of file diff --git a/launcher/gui/src/LauncherAppMainFrame.cpp b/launcher/gui/src/LauncherAppMainFrame.cpp index b7b14548..f4eea2f7 100644 --- a/launcher/gui/src/LauncherAppMainFrame.cpp +++ b/launcher/gui/src/LauncherAppMainFrame.cpp @@ -8,5 +8,6 @@ LauncherAppMainFrame::LauncherAppMainFrame(wxWindow* parent) void LauncherAppMainFrame::OnStartStopButtonClick(wxCommandEvent& event) { std::cout << "Hi!" << std::endl; + _dhfsInstance.start(); // TODO: Implement OnStartStopButtonClick } diff --git a/launcher/gui/src/LauncherAppMainFrame.h b/launcher/gui/src/LauncherAppMainFrame.h index 72e27642..00bff7ae 100644 --- a/launcher/gui/src/LauncherAppMainFrame.h +++ b/launcher/gui/src/LauncherAppMainFrame.h @@ -10,6 +10,8 @@ Subclass of MainFrame, which is generated by wxFormBuilder. //// end generated include +#include "DhfsInstance.hpp" + /** Implementing MainFrame */ class LauncherAppMainFrame : public MainFrame { protected: @@ -21,6 +23,9 @@ public: LauncherAppMainFrame(wxWindow* parent); //// end generated class members + +private: + DhfsInstance _dhfsInstance; }; #endif // __LauncherAppMainFrame__ diff --git a/launcher/libjvm_wrapper/CMakeLists.txt b/launcher/libjvm_wrapper/CMakeLists.txt new file mode 100644 index 00000000..6c90549c --- /dev/null +++ b/launcher/libjvm_wrapper/CMakeLists.txt @@ -0,0 +1,10 @@ +add_library(libjvm_wrapper + src/LibjvmWrapper.cpp + include_public/LibjvmWrapper.hpp +) + +target_include_directories(libjvm_wrapper PRIVATE include) +target_include_directories(libjvm_wrapper PUBLIC include_public) + +find_package(JNI REQUIRED) +target_include_directories(libjvm_wrapper PUBLIC ${JNI_INCLUDE_DIRS}) \ No newline at end of file diff --git a/launcher/libjvm_wrapper/include_public/LibjvmWrapper.hpp b/launcher/libjvm_wrapper/include_public/LibjvmWrapper.hpp new file mode 100644 index 00000000..727f915a --- /dev/null +++ b/launcher/libjvm_wrapper/include_public/LibjvmWrapper.hpp @@ -0,0 +1,25 @@ +// +// Created by Stepan Usatiuk on 24.06.2025. +// + +#ifndef LIBJVMWRAPPER_HPP +#define LIBJVMWRAPPER_HPP +#include + + +class LibjvmWrapper { +public: + static LibjvmWrapper& instance(); + + decltype(JNI_CreateJavaVM)* JNI_CreateJavaVM; + +private: + LibjvmWrapper(); + + ~LibjvmWrapper(); + + void* _lib_handle; +}; + + +#endif //LIBJVMWRAPPER_HPP diff --git a/launcher/libjvm_wrapper/src/LibjvmWrapper.cpp b/launcher/libjvm_wrapper/src/LibjvmWrapper.cpp new file mode 100644 index 00000000..00fe6778 --- /dev/null +++ b/launcher/libjvm_wrapper/src/LibjvmWrapper.cpp @@ -0,0 +1,25 @@ +// +// Created by Stepan Usatiuk on 24.06.2025. +// + +#include "LibjvmWrapper.hpp" +#include +#include + +static constexpr auto LIBJVM_PATH = + "/Library/Java/JavaVirtualMachines/zulu-21.jdk/Contents/Home/lib/server/libjvm.dylib"; + +LibjvmWrapper& LibjvmWrapper::instance() { + static LibjvmWrapper instance; + return instance; +} + +LibjvmWrapper::LibjvmWrapper() { + _lib_handle = dlopen(LIBJVM_PATH, RTLD_NOW | RTLD_GLOBAL); + JNI_CreateJavaVM = reinterpret_cast( + dlsym(_lib_handle, "JNI_CreateJavaVM")); +} + +LibjvmWrapper::~LibjvmWrapper() { + dlclose(_lib_handle); +}