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

Add a plugin mechanism to resource_retriever #103

Open
wants to merge 6 commits into
base: rolling
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
8 changes: 6 additions & 2 deletions resource_retriever/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ find_package(libcurl_vendor REQUIRED)
find_package(CURL REQUIRED)

# TODO(wjwwood): split cpp and python apis into separate packages

add_library(${PROJECT_NAME} src/retriever.cpp)
add_library(${PROJECT_NAME}
src/retriever.cpp
src/plugins/retriever_plugin.cpp
src/plugins/filesystem_retriever.cpp
src/plugins/curl_retriever.cpp
)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
Expand Down
49 changes: 49 additions & 0 deletions resource_retriever/include/resource_retriever/exception.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright 2009, Willow Garage, Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Willow Garage, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef RESOURCE_RETRIEVER__EXCEPTION_HPP_
#define RESOURCE_RETRIEVER__EXCEPTION_HPP_

#include <stdexcept>
#include <string>

#include "resource_retriever/visibility_control.hpp"

namespace resource_retriever
{
class RESOURCE_RETRIEVER_PUBLIC Exception : public std::runtime_error
{
public:
Exception(const std::string & file, const std::string & error_msg)
: std::runtime_error("Error retrieving file [" + file + "]: " + error_msg)
{
}
};
} // namespace resource_retriever

#endif // RESOURCE_RETRIEVER__EXCEPTION_HPP_
64 changes: 64 additions & 0 deletions resource_retriever/include/resource_retriever/memory_resource.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2009, Willow Garage, Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Willow Garage, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef RESOURCE_RETRIEVER__MEMORY_RESOURCE_HPP_
#define RESOURCE_RETRIEVER__MEMORY_RESOURCE_HPP_

#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "resource_retriever/visibility_control.hpp"

namespace resource_retriever
{
/**
* \brief A combination of a pointer to data in memory along with the data's size.
*/
struct RESOURCE_RETRIEVER_PUBLIC MemoryResource
{
explicit MemoryResource(
std::string url_in,
std::string expanded_url_in,
std::vector<uint8_t> data_in)
:url(std::move(url_in)),
expanded_url(std::move(expanded_url_in)),
data(std::move(data_in))
{
}
const std::string url;
const std::string expanded_url;
const std::vector<uint8_t> data;
};

using MemoryResourcePtr = std::shared_ptr<MemoryResource>;

} // namespace resource_retriever

#endif // RESOURCE_RETRIEVER__MEMORY_RESOURCE_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2009, Willow Garage, Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Willow Garage, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef RESOURCE_RETRIEVER__PLUGINS__CURL_RETRIEVER_HPP_
#define RESOURCE_RETRIEVER__PLUGINS__CURL_RETRIEVER_HPP_

#include <string>

#include "resource_retriever/plugins/retriever_plugin.hpp"
#include "resource_retriever/visibility_control.hpp"

using CURL = void;

namespace resource_retriever::plugins
{

class RESOURCE_RETRIEVER_PUBLIC CurlRetriever : public RetrieverPlugin
{
public:
CurlRetriever();
~CurlRetriever() override;

CurlRetriever(const CurlRetriever & ret) = delete;
CurlRetriever & operator=(const CurlRetriever & other) = delete;

CurlRetriever(CurlRetriever && other) noexcept;
CurlRetriever & operator=(CurlRetriever && other) noexcept;

bool can_handle(const std::string & url) override;
std::string name() override;
MemoryResourcePtr get(const std::string & url) override;

private:
CURL * curl_handle_ {nullptr};
mjcarroll marked this conversation as resolved.
Show resolved Hide resolved
};

} // namespace resource_retriever::plugins

#endif // RESOURCE_RETRIEVER__PLUGINS__CURL_RETRIEVER_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Copyright 2009, Willow Garage, Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Willow Garage, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef RESOURCE_RETRIEVER__PLUGINS__FILESYSTEM_RETRIEVER_HPP_
#define RESOURCE_RETRIEVER__PLUGINS__FILESYSTEM_RETRIEVER_HPP_

#include <string>

#include "resource_retriever/plugins/retriever_plugin.hpp"
#include "resource_retriever/visibility_control.hpp"

namespace resource_retriever::plugins
{

class RESOURCE_RETRIEVER_PUBLIC FilesystemRetriever : public RetrieverPlugin
{
public:
FilesystemRetriever();
~FilesystemRetriever() override;

bool can_handle(const std::string & url) override;
std::string name() override;
MemoryResourcePtr get(const std::string & url) override;
};

} // namespace resource_retriever::plugins

#endif // RESOURCE_RETRIEVER__PLUGINS__FILESYSTEM_RETRIEVER_HPP_
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2009, Willow Garage, Inc. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of the Willow Garage, Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.

#ifndef RESOURCE_RETRIEVER__PLUGINS__RETRIEVER_PLUGIN_HPP_
#define RESOURCE_RETRIEVER__PLUGINS__RETRIEVER_PLUGIN_HPP_

#include <string>

#include <resource_retriever/memory_resource.hpp>
#include "resource_retriever/visibility_control.hpp"

namespace resource_retriever::plugins
{
std::string expand_package_url(const std::string & url);
std::string escape_spaces(const std::string & url);

class RESOURCE_RETRIEVER_PUBLIC RetrieverPlugin
{
public:
RetrieverPlugin();
virtual ~RetrieverPlugin() = 0;

virtual std::string name() = 0;
virtual bool can_handle(const std::string & url) = 0;
virtual MemoryResourcePtr get(const std::string & url) = 0;
};

} // namespace resource_retriever::plugins

#endif // RESOURCE_RETRIEVER__PLUGINS__RETRIEVER_PLUGIN_HPP_
36 changes: 9 additions & 27 deletions resource_retriever/include/resource_retriever/retriever.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,18 @@
#include <memory>
#include <stdexcept>
#include <string>
#include <vector>

#include "resource_retriever/plugins/retriever_plugin.hpp"
#include "resource_retriever/visibility_control.hpp"

using CURL = void;

namespace resource_retriever
{
class Exception : public std::runtime_error
{
public:
Exception(const std::string & file, const std::string & error_msg)
: std::runtime_error("Error retrieving file [" + file + "]: " + error_msg)
{
}
};

/**
* \brief A combination of a pointer to data in memory along with the data's size.
*/
struct MemoryResource
{
std::shared_ptr<uint8_t> data;
size_t size {0};
};
using RetrieverPluginPtr = std::shared_ptr<plugins::RetrieverPlugin>;
using RetrieverVec = std::vector<RetrieverPluginPtr>;

RetrieverVec RESOURCE_RETRIEVER_PUBLIC default_plugins();

/**
* \brief Retrieves files from from a url. Caches a CURL handle so multiple accesses to a single url
Expand All @@ -65,26 +53,20 @@ struct MemoryResource
class RESOURCE_RETRIEVER_PUBLIC Retriever
{
public:
Retriever();
explicit Retriever(RetrieverVec plugins = default_plugins());

~Retriever();

Retriever(const Retriever & ret) = delete;
Retriever & operator=(const Retriever & other) = delete;

Retriever(Retriever && other) noexcept;
Retriever & operator=(Retriever && other) noexcept;

/**
* \brief Get a file and store it in memory
* \param url The url to retrieve. package://package/file will be turned into the correct file:// invocation
* \return The file, loaded into memory
* \throws resource_retriever::Exception if anything goes wrong.
*/
MemoryResource get(const std::string & url);
MemoryResourcePtr get(const std::string & url);

private:
CURL * curl_handle_ {nullptr};
RetrieverVec plugins;
};

} // namespace resource_retriever
Expand Down
Loading