Verified Commit f4a0a067 authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

api: Add basic library for server communication

parent 14d106cd
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -302,8 +302,37 @@ else ()
endif ()

add_subdirectory(etc)
add_subdirectory(lib)
add_subdirectory(src)

if(CARGO_BUILD_TESTS)
  add_subdirectory(tests)
endif()


# ##############################################################################
# Generate and install CMake config files so that the project can be consumed
# using find_package()
# ##############################################################################

include(CMakePackageConfigHelpers)

configure_package_config_file(
  "${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}-config.cmake.in"
  "${PROJECT_BINARY_DIR}/${PROJECT_NAME}-config.cmake"
  INSTALL_DESTINATION
  "${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}-${PROJECT_VERSION}"
)

write_basic_package_version_file(
  "${PROJECT_NAME}-config-version.cmake"
  VERSION ${PROJECT_VERSION}
  COMPATIBILITY SameMajorVersion
)

install(FILES
  ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config.cmake
  ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}-config-version.cmake
  DESTINATION
  ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}-${PROJECT_VERSION}
  )
+31 −0
Original line number Diff line number Diff line
################################################################################
# Copyright 2022-2023, Barcelona Supercomputing Center (BSC), Spain            #
#                                                                              #
# This software was partially supported by the EuroHPC-funded project ADMIRE   #
#   (Project ID: 956748, https://www.admire-eurohpc.eu).                       #
#                                                                              #
# This file is part of Cargo.                                                  #
#                                                                              #
# Cargo is free software: you can redistribute it and/or modify                #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# Cargo 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 General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with Cargo.  If not, see <https://www.gnu.org/licenses/>.              #
#                                                                              #
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

@PACKAGE_INIT@

check_required_components("@PROJECT_NAME@")

include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@Exports.cmake")

check_required_components("@PROJECT_NAME@")
 No newline at end of file

lib/CMakeLists.txt

0 → 100644
+62 −0
Original line number Diff line number Diff line
################################################################################
# Copyright 2022-2023, Barcelona Supercomputing Center (BSC), Spain            #
#                                                                              #
# This software was partially supported by the EuroHPC-funded project ADMIRE   #
#   (Project ID: 956748, https://www.admire-eurohpc.eu).                       #
#                                                                              #
# This file is part of Cargo.                                                  #
#                                                                              #
# Cargo is free software: you can redistribute it and/or modify                #
# it under the terms of the GNU General Public License as published by         #
# the Free Software Foundation, either version 3 of the License, or            #
# (at your option) any later version.                                          #
#                                                                              #
# Cargo 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 General Public License for more details.                                 #
#                                                                              #
# You should have received a copy of the GNU General Public License            #
# along with Cargo.  If not, see <https://www.gnu.org/licenses/>.              #
#                                                                              #
# SPDX-License-Identifier: GPL-3.0-or-later                                    #
################################################################################

add_library(cargo SHARED)

target_sources(cargo PRIVATE cargo.hpp fmt_formatters.hpp libcargo.cpp)

list(APPEND public_headers "cargo.hpp")
list(APPEND public_headers "fmt_formatters.hpp")

target_include_directories(
  cargo PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>
               $<INSTALL_INTERFACE:include/${PROJECT_NAME}>
)

set_target_properties(cargo PROPERTIES PUBLIC_HEADER "${public_headers}")

target_link_libraries(cargo PRIVATE logger fmt::fmt thallium)

## Install library + targets ###################################################

set_target_properties(
  cargo PROPERTIES VERSION ${PROJECT_VERSION} SOVERSION
                                              ${PROJECT_VERSION_MAJOR}
)

install(
  TARGETS cargo
  EXPORT cargoExports
  LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
  RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
  ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
  PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PROJECT_NAME}
)

install(
  EXPORT cargoExports
  DESTINATION
    ${CMAKE_INSTALL_LIBDIR}/cmake/${CMAKE_PROJECT_NAME}-${PROJECT_VERSION}
  NAMESPACE cargo::
)

lib/cargo.hpp

0 → 100644
+116 −0
Original line number Diff line number Diff line
/******************************************************************************
 * Copyright 2022-2023, Barcelona Supercomputing Center (BSC), Spain
 *
 * This software was partially supported by the EuroHPC-funded project ADMIRE
 *   (Project ID: 956748, https://www.admire-eurohpc.eu).
 *
 * This file is part of Cargo.
 *
 * Cargo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cargo 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cargo.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *****************************************************************************/


#ifndef CARGO_HPP
#define CARGO_HPP

#include <string>
#include <vector>

namespace cargo {

using transfer_id = std::uint64_t;
using error_code = std::int32_t;

/**
 * A Cargo server
 */
struct server {

    explicit server(std::string address) noexcept;

    [[nodiscard]] std::string
    protocol() const noexcept;

    [[nodiscard]] std::string
    address() const noexcept;

private:
    std::string m_protocol;
    std::string m_address;
};


/**
 * A dataset
 */
class dataset {

public:
    dataset() noexcept = default;
    explicit dataset(std::string id) noexcept;

    [[nodiscard]] std::string
    id() const noexcept;

    template <typename Archive>
    void
    serialize(Archive& ar) {
        ar& m_id;
    }

private:
    std::string m_id;
};


/**
 * A transfer handler
 */
class transfer {

public:
    transfer() noexcept = default;
    explicit transfer(transfer_id id) noexcept;

    [[nodiscard]] transfer_id
    id() const noexcept;

    template <typename Archive>
    void
    serialize(Archive& ar) {
        ar& m_id;
    }

private:
    transfer_id m_id;
};


/**
 * Request the transfer of a dataset collection.
 *
 * @param srv The Cargo server that should execute the transfer.
 * @param sources The input datasets that should be transferred.
 * @param targets The output datasets that should be generated.
 * @return A transfer
 */
cargo::transfer
transfer_datasets(const server& srv, const std::vector<dataset>& sources,
                  const std::vector<dataset>& targets);

} // namespace cargo

#endif // CARGO_HPP

lib/fmt_formatters.hpp

0 → 100644
+72 −0
Original line number Diff line number Diff line
/******************************************************************************
 * Copyright 2022-2023, Barcelona Supercomputing Center (BSC), Spain
 *
 * This software was partially supported by the EuroHPC-funded project ADMIRE
 *   (Project ID: 956748, https://www.admire-eurohpc.eu).
 *
 * This file is part of Cargo.
 *
 * Cargo is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cargo 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Cargo.  If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *****************************************************************************/

#ifndef CARGO_FMT_FORMATTERS_HPP
#define CARGO_FMT_FORMATTERS_HPP

#include <iomanip>
#include <string_view>
#include <fmt/format.h>

namespace cargo {

class dataset;

} // namespace cargo

template <>
struct fmt::formatter<cargo::dataset> : formatter<std::string_view> {
    // parse is inherited from formatter<string_view>.
    template <typename FormatContext>
    auto
    format(const cargo::dataset& d, FormatContext& ctx) const {
        const auto str = fmt::format("{{id: {}}}", std::quoted(d.id()));
        return formatter<std::string_view>::format(str, ctx);
    }
};

template <>
struct fmt::formatter<std::vector<cargo::dataset>>
    : formatter<std::string_view> {
    // parse is inherited from formatter<string_view>.
    template <typename FormatContext>
    auto
    format(const std::vector<cargo::dataset>& v, FormatContext& ctx) const {
        const auto str = fmt::format("[{}]", fmt::join(v, ", "));
        return formatter<std::string_view>::format(str, ctx);
    }
};

template <>
struct fmt::formatter<cargo::transfer> : formatter<std::string_view> {
    // parse is inherited from formatter<string_view>.
    template <typename FormatContext>
    auto
    format(const cargo::transfer& tx, FormatContext& ctx) const {
        const auto str = fmt::format("{{id: {}}}", tx.id());
        return formatter<std::string_view>::format(str, ctx);
    }
};

#endif // CARGO_FMT_FORMATTERS_HPP
Loading