From f7b4bf7ca0295701cd2140321a1ec849cec74fbe Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Tue, 30 Apr 2024 12:27:38 +0200 Subject: [PATCH 1/3] Add empty module for http --- Sofa/framework/CMakeLists.txt | 1 + Sofa/framework/Http/CMakeLists.txt | 28 +++++++++++++++ Sofa/framework/Http/src/sofa/http/config.h.in | 34 ++++++++++++++++++ Sofa/framework/Http/src/sofa/http/init.cpp | 36 +++++++++++++++++++ Sofa/framework/Http/src/sofa/http/init.h | 33 +++++++++++++++++ 5 files changed, 132 insertions(+) create mode 100644 Sofa/framework/Http/CMakeLists.txt create mode 100644 Sofa/framework/Http/src/sofa/http/config.h.in create mode 100644 Sofa/framework/Http/src/sofa/http/init.cpp create mode 100644 Sofa/framework/Http/src/sofa/http/init.h diff --git a/Sofa/framework/CMakeLists.txt b/Sofa/framework/CMakeLists.txt index 07dc0d9cb5b..f56b89f4e59 100644 --- a/Sofa/framework/CMakeLists.txt +++ b/Sofa/framework/CMakeLists.txt @@ -14,6 +14,7 @@ sofa_add_subdirectory(library DefaultType Sofa.DefaultType ON) sofa_add_subdirectory(library Core Sofa.Core ON) sofa_add_subdirectory(library Simulation Sofa.Simulation ON) sofa_add_subdirectory(library SimpleApi Sofa.SimpleApi ON) +sofa_add_subdirectory(library Http Sofa.Http ON) sofa_add_subdirectory(library Testing Sofa.Testing ON) # Library gathering all framework libraries diff --git a/Sofa/framework/Http/CMakeLists.txt b/Sofa/framework/Http/CMakeLists.txt new file mode 100644 index 00000000000..1403115e00f --- /dev/null +++ b/Sofa/framework/Http/CMakeLists.txt @@ -0,0 +1,28 @@ +cmake_minimum_required(VERSION 3.22) +project(Sofa.Http LANGUAGES CXX) + +set(SOFAHTTP_SRC "src/sofa/http") + +set(HEADER_FILES + ${SOFAHTTP_SRC}/config.h.in + ${SOFAHTTP_SRC}/init.h +) + +set(SOURCE_FILES + ${SOFAHTTP_SRC}/init.cpp +) + +find_package(Sofa.Config REQUIRED) + +add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES}) +target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Config) + +set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER Sofa.Framework) # IDE folder + +sofa_create_package_with_targets( + PACKAGE_NAME ${PROJECT_NAME} + PACKAGE_VERSION ${Sofa_VERSION} + TARGETS ${PROJECT_NAME} AUTO_SET_TARGET_PROPERTIES + INCLUDE_SOURCE_DIR "src" + INCLUDE_INSTALL_DIR "${PROJECT_NAME}" +) diff --git a/Sofa/framework/Http/src/sofa/http/config.h.in b/Sofa/framework/Http/src/sofa/http/config.h.in new file mode 100644 index 00000000000..c40d991f15b --- /dev/null +++ b/Sofa/framework/Http/src/sofa/http/config.h.in @@ -0,0 +1,34 @@ +/****************************************************************************** +* SOFA, Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program 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) any later version. * +* * +* This program 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 program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once + +#include +#include + +#define SOFA_HTTP_VERSION @PROJECT_VERSION@ + +#ifdef SOFA_BUILD_SOFA_HTTP +# define SOFA_TARGET @PROJECT_NAME@ +# define SOFA_HTTP_API SOFA_EXPORT_DYNAMIC_LIBRARY +#else +# define SOFA_HTTP_API SOFA_IMPORT_DYNAMIC_LIBRARY +#endif diff --git a/Sofa/framework/Http/src/sofa/http/init.cpp b/Sofa/framework/Http/src/sofa/http/init.cpp new file mode 100644 index 00000000000..af65aa6a852 --- /dev/null +++ b/Sofa/framework/Http/src/sofa/http/init.cpp @@ -0,0 +1,36 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program 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) any later version. * +* * +* This program 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 program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#include + + +namespace sofa::http +{ + +SOFA_HTTP_API void init() +{ + static bool first = true; + if (first) + { + } +} + +} // namespace sofa::http diff --git a/Sofa/framework/Http/src/sofa/http/init.h b/Sofa/framework/Http/src/sofa/http/init.h new file mode 100644 index 00000000000..0e434fae30e --- /dev/null +++ b/Sofa/framework/Http/src/sofa/http/init.h @@ -0,0 +1,33 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program 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) any later version. * +* * +* This program 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 program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once +#include + +namespace sofa::http +{ + +/// @brief Initialize the Sofa.Http library, as well as its +/// dependencies. +SOFA_HTTP_API void init(); + + +} // namespace sofa::http From e5707cfd8e4ba0801cc3ac1ffbe213f12ca34a38 Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Tue, 30 Apr 2024 13:31:58 +0200 Subject: [PATCH 2/3] dependency to httplib --- Sofa/framework/Http/CMakeLists.txt | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/Sofa/framework/Http/CMakeLists.txt b/Sofa/framework/Http/CMakeLists.txt index 1403115e00f..5cd368b7400 100644 --- a/Sofa/framework/Http/CMakeLists.txt +++ b/Sofa/framework/Http/CMakeLists.txt @@ -14,8 +14,31 @@ set(SOURCE_FILES find_package(Sofa.Config REQUIRED) + + + +find_package(httplib QUIET) +if(NOT httplib_FOUND AND SOFA_ALLOW_FETCH_DEPENDENCIES) + message("${PROJECT_NAME}: DEPENDENCY httplib NOT FOUND. SOFA_ALLOW_FETCH_DEPENDENCIES is ON, fetching httplib...") + + include(FetchContent) + FetchContent_Declare(httplib + GIT_REPOSITORY https://github.com/yhirose/cpp-httplib + GIT_TAG v0.15.3 + GIT_SHALLOW TRUE + ) + + FetchContent_MakeAvailable(httplib) +elseif (NOT httplib_FOUND) + message(FATAL_ERROR "${PROJECT_NAME}: DEPENDENCY httplib NOT FOUND. SOFA_ALLOW_FETCH_DEPENDENCIES is OFF and thus cannot be fetched. Install httplib, or enable SOFA_ALLOW_FETCH_DEPENDENCIES to fix this issue.") +endif() + + + + add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES}) target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Config) +target_link_libraries(${PROJECT_NAME} PUBLIC httplib) set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER Sofa.Framework) # IDE folder From d85326f755ef7aeddff9f3644359def7c8dbc711 Mon Sep 17 00:00:00 2001 From: Alex Bilger Date: Tue, 30 Apr 2024 16:49:19 +0200 Subject: [PATCH 3/3] check version --- Sofa/GUI/Qt/CMakeLists.txt | 2 + Sofa/GUI/Qt/src/sofa/gui/qt/init.cpp | 5 ++ Sofa/framework/Http/CMakeLists.txt | 15 ++-- .../Http/src/sofa/http/VersionChecker.cpp | 89 +++++++++++++++++++ .../Http/src/sofa/http/VersionChecker.h | 34 +++++++ Sofa/framework/Http/src/sofa/http/config.h.in | 6 ++ Sofa/framework/Http/src/sofa/http/init.cpp | 3 + 7 files changed, 147 insertions(+), 7 deletions(-) create mode 100644 Sofa/framework/Http/src/sofa/http/VersionChecker.cpp create mode 100644 Sofa/framework/Http/src/sofa/http/VersionChecker.h diff --git a/Sofa/GUI/Qt/CMakeLists.txt b/Sofa/GUI/Qt/CMakeLists.txt index acc702c7803..cf067810724 100644 --- a/Sofa/GUI/Qt/CMakeLists.txt +++ b/Sofa/GUI/Qt/CMakeLists.txt @@ -70,6 +70,7 @@ endif() find_package(Sofa.GL QUIET) +sofa_find_package(Sofa.Http REQUIRED) # QtViewer and QGLViewer if(Sofa.GL_FOUND) @@ -330,6 +331,7 @@ add_library(${PROJECT_NAME} SHARED ${MOC_HEADER_FILES} ${HEADER_FILES} ${MOC_FIL # For files generated by the moc target_include_directories(${PROJECT_NAME} PUBLIC "$") +target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Http) target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.GUI.Common) target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Component.Visual) target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Component.SceneUtility) diff --git a/Sofa/GUI/Qt/src/sofa/gui/qt/init.cpp b/Sofa/GUI/Qt/src/sofa/gui/qt/init.cpp index 1012b86a10b..f0786fd7148 100644 --- a/Sofa/GUI/Qt/src/sofa/gui/qt/init.cpp +++ b/Sofa/GUI/Qt/src/sofa/gui/qt/init.cpp @@ -19,6 +19,8 @@ * * * Contact information: contact@sofa-framework.org * ******************************************************************************/ +#include + #include #include @@ -54,6 +56,9 @@ namespace sofa::gui::qt static bool first = true; if (first) { + sofa::http::init(); + + #if SOFA_GUI_QT_ENABLE_QGLVIEWER sofa::gui::common::GUIManager::RegisterGUI("qglviewer", &sofa::gui::qt::RealGUI::CreateGUI, nullptr, 3); #endif diff --git a/Sofa/framework/Http/CMakeLists.txt b/Sofa/framework/Http/CMakeLists.txt index 5cd368b7400..1aff15d2d80 100644 --- a/Sofa/framework/Http/CMakeLists.txt +++ b/Sofa/framework/Http/CMakeLists.txt @@ -6,16 +6,17 @@ set(SOFAHTTP_SRC "src/sofa/http") set(HEADER_FILES ${SOFAHTTP_SRC}/config.h.in ${SOFAHTTP_SRC}/init.h + + ${SOFAHTTP_SRC}/VersionChecker.h ) set(SOURCE_FILES ${SOFAHTTP_SRC}/init.cpp -) - -find_package(Sofa.Config REQUIRED) - + ${SOFAHTTP_SRC}/VersionChecker.cpp +) +find_package(Sofa.Helper REQUIRED) find_package(httplib QUIET) if(NOT httplib_FOUND AND SOFA_ALLOW_FETCH_DEPENDENCIES) @@ -28,16 +29,16 @@ if(NOT httplib_FOUND AND SOFA_ALLOW_FETCH_DEPENDENCIES) GIT_SHALLOW TRUE ) + set(HTTPLIB_REQUIRE_OPENSSL ON CACHE INTERNAL "") #This requires OpenSSL >=3.0.0 FetchContent_MakeAvailable(httplib) + elseif (NOT httplib_FOUND) message(FATAL_ERROR "${PROJECT_NAME}: DEPENDENCY httplib NOT FOUND. SOFA_ALLOW_FETCH_DEPENDENCIES is OFF and thus cannot be fetched. Install httplib, or enable SOFA_ALLOW_FETCH_DEPENDENCIES to fix this issue.") endif() - - add_library(${PROJECT_NAME} SHARED ${HEADER_FILES} ${SOURCE_FILES}) -target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Config) +target_link_libraries(${PROJECT_NAME} PUBLIC Sofa.Helper) target_link_libraries(${PROJECT_NAME} PUBLIC httplib) set_target_properties(${PROJECT_NAME} PROPERTIES FOLDER Sofa.Framework) # IDE folder diff --git a/Sofa/framework/Http/src/sofa/http/VersionChecker.cpp b/Sofa/framework/Http/src/sofa/http/VersionChecker.cpp new file mode 100644 index 00000000000..44e02997dcb --- /dev/null +++ b/Sofa/framework/Http/src/sofa/http/VersionChecker.cpp @@ -0,0 +1,89 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program 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) any later version. * +* * +* This program 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 program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#include +#include +#include +#include + + +namespace sofa::http +{ + +std::optional getLatestSOFARelease() +{ + constexpr std::string_view githubURL = "https://api.github.com"; + constexpr std::string_view repoPath = "/repos/sofa-framework/sofa/releases/latest"; + + httplib::Client cli{std::string(githubURL)}; + + auto res = cli.Get(std::string(repoPath)); + if (res && res->status == 200) + { + sofa::helper::json j = sofa::helper::json::parse(res->body); + return j["tag_name"]; + } + + return {}; +} + +void checkLatestSOFARelease() +{ + const auto removeVPrefix = [](std::string version) + { + if (!version.empty() && version[0] == 'v') + { + version = version.substr(1); + } + return version; + }; + + const auto t1 = std::chrono::high_resolution_clock::now(); + const auto latestVersion = getLatestSOFARelease(); + const auto t2 = std::chrono::high_resolution_clock::now(); + + const std::chrono::duration ms = t2 - t1; + + if (latestVersion.value_or("").empty()) + { + msg_warning("VersionChecker") << "Cannot get latest release version {" << ms.count() << " ms}"; + return; + } + + const std::string latestVersionNumber = removeVPrefix(latestVersion.value()); + const std::string currentVersionNumber = removeVPrefix(std::string(MODULE_VERSION)); + + // Compare versions and print the result + if (latestVersionNumber > currentVersionNumber) + { + msg_info("VersionChecker") << "A newer version of SOFA (" << latestVersionNumber + << ") is available! Current version is " << currentVersionNumber + << ". {" << ms.count() << " ms}"; + } + else + { + msg_info("VersionChecker") << "You are using the latest version (" + << currentVersionNumber << ")" << ". {" << ms.count() << " ms}"; + } +} + + +} diff --git a/Sofa/framework/Http/src/sofa/http/VersionChecker.h b/Sofa/framework/Http/src/sofa/http/VersionChecker.h new file mode 100644 index 00000000000..b502e306a35 --- /dev/null +++ b/Sofa/framework/Http/src/sofa/http/VersionChecker.h @@ -0,0 +1,34 @@ +/****************************************************************************** +* SOFA, Simulation Open-Framework Architecture * +* (c) 2006 INRIA, USTL, UJF, CNRS, MGH * +* * +* This program 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) any later version. * +* * +* This program 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 program. If not, see . * +******************************************************************************* +* Authors: The SOFA Team and external contributors (see Authors.txt) * +* * +* Contact information: contact@sofa-framework.org * +******************************************************************************/ +#pragma once +#include +#include +#include + +namespace sofa::http +{ + +SOFA_HTTP_API std::optional getLatestSOFARelease(); +SOFA_HTTP_API void checkLatestSOFARelease(); + + +} diff --git a/Sofa/framework/Http/src/sofa/http/config.h.in b/Sofa/framework/Http/src/sofa/http/config.h.in index c40d991f15b..0ef12a2ed98 100644 --- a/Sofa/framework/Http/src/sofa/http/config.h.in +++ b/Sofa/framework/Http/src/sofa/http/config.h.in @@ -32,3 +32,9 @@ #else # define SOFA_HTTP_API SOFA_IMPORT_DYNAMIC_LIBRARY #endif + +namespace sofa::http +{ + constexpr const char* MODULE_NAME = "@PROJECT_NAME@"; + constexpr const char* MODULE_VERSION = "@PROJECT_VERSION@"; +} // namespace sofa::http diff --git a/Sofa/framework/Http/src/sofa/http/init.cpp b/Sofa/framework/Http/src/sofa/http/init.cpp index af65aa6a852..f32614a6be9 100644 --- a/Sofa/framework/Http/src/sofa/http/init.cpp +++ b/Sofa/framework/Http/src/sofa/http/init.cpp @@ -20,6 +20,8 @@ * Contact information: contact@sofa-framework.org * ******************************************************************************/ #include +#include +#include namespace sofa::http @@ -30,6 +32,7 @@ SOFA_HTTP_API void init() static bool first = true; if (first) { + checkLatestSOFARelease(); } }