Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Sofa.Http] Introduction of a latest release checker #4702

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Sofa/GUI/Qt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ endif()


find_package(Sofa.GL QUIET)
sofa_find_package(Sofa.Http REQUIRED)

# QtViewer and QGLViewer
if(Sofa.GL_FOUND)
Expand Down Expand Up @@ -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 "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>")

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)
Expand Down
5 changes: 5 additions & 0 deletions Sofa/GUI/Qt/src/sofa/gui/qt/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
* *
* Contact information: [email protected] *
******************************************************************************/
#include <sofa/http/init.h>

#include <sofa/gui/qt/init.h>

#include <sofa/gui/common/GUIManager.h>
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Sofa/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions Sofa/framework/Http/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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

${SOFAHTTP_SRC}/VersionChecker.h
)

set(SOURCE_FILES
${SOFAHTTP_SRC}/init.cpp

${SOFAHTTP_SRC}/VersionChecker.cpp
)

find_package(Sofa.Helper 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
)

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.Helper)
target_link_libraries(${PROJECT_NAME} PUBLIC httplib)

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}"
)
89 changes: 89 additions & 0 deletions Sofa/framework/Http/src/sofa/http/VersionChecker.cpp
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#include <sofa/http/VersionChecker.h>
#include <httplib.h>
#include <json.h>
#include <sofa/helper/logging/Messaging.h>


namespace sofa::http
{

std::optional<std::string> 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<double, std::milli> 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}";
}
}


}
34 changes: 34 additions & 0 deletions Sofa/framework/Http/src/sofa/http/VersionChecker.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#pragma once
#include <optional>
#include <string>
#include <sofa/http/config.h>

namespace sofa::http
{

SOFA_HTTP_API std::optional<std::string> getLatestSOFARelease();
SOFA_HTTP_API void checkLatestSOFARelease();


}
40 changes: 40 additions & 0 deletions Sofa/framework/Http/src/sofa/http/config.h.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/******************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#pragma once

#include <sofa/config.h>
#include <sofa/config/sharedlibrary_defines.h>

#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

namespace sofa::http
{
constexpr const char* MODULE_NAME = "@PROJECT_NAME@";
constexpr const char* MODULE_VERSION = "@PROJECT_VERSION@";
} // namespace sofa::http
39 changes: 39 additions & 0 deletions Sofa/framework/Http/src/sofa/http/init.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/******************************************************************************
* 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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#include <sofa/http/init.h>
#include <sofa/http/Server.h>
#include <sofa/http/VersionChecker.h>


namespace sofa::http
{

SOFA_HTTP_API void init()
{
static bool first = true;
if (first)
{
checkLatestSOFARelease();
}
}

} // namespace sofa::http
33 changes: 33 additions & 0 deletions Sofa/framework/Http/src/sofa/http/init.h
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>. *
*******************************************************************************
* Authors: The SOFA Team and external contributors (see Authors.txt) *
* *
* Contact information: [email protected] *
******************************************************************************/
#pragma once
#include <sofa/http/config.h>

namespace sofa::http
{

/// @brief Initialize the Sofa.Http library, as well as its
/// dependencies.
SOFA_HTTP_API void init();


} // namespace sofa::http
Loading