Skip to content
Snippets Groups Projects
Commit 88f18e6d authored by Jerome Royan's avatar Jerome Royan
Browse files

remove implementation sample

parent 0edf06fa
No related branches found
No related tags found
3 merge requests!6Correction of many fixes,!5force the use of version 6.6.0 of openapi-generator-cli due to a bug since...,!4fix: uniqueItem for array reactivated: Work around found to use std::vector...
#
# ARF - Augmented Reality Framework (ETSI ISG ARF)
#
# Copyright 2024 ETSI
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Last change: May 2024
#
cmake_minimum_required(VERSION 3.23)
project(RestBedImpl VERSION 2.1.0)
set(TARGET_NAME RestbedImpl)
find_package(Boost REQUIRED)
find_package(restbed REQUIRED)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -pg -g3" )
file(GLOB SRCS
${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp
)
add_executable(${TARGET_NAME} ${SRCS})
include_directories(src)
target_link_libraries(${TARGET_NAME} boost::boost restbed::restbed)
# WORLD STORAGE CPP SERVER (RESTBED)
## Description
This folder contains the source files for a basic implementation of the library you just created
## Requirements
- Ubuntu 22.04
- CMake > 2.22
- conan V2
## Code usage
Once you are in this directory, to build and execute the server, use the following commands:
conan install . --build=missing
cmake . --preset conan-release
cmake --build build/Release
[requires]
boost/1.78.0
restbed/4.8
[generators]
CMakeDeps
CMakeToolchain
[layout]
cmake_layout
\ No newline at end of file
//
// ARF - Augmented Reality Framework (ETSI ISG ARF)
//
// Copyright 2022 ETSI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Last change: June 2022
//
#include "DefaultApiEtsi.h"
namespace etsi {
DefaultApiEtsi::DefaultApiEtsi(const std::shared_ptr<Pistache::Rest::Router>& rtr)
: DefaultApi(rtr)
{
}
void DefaultApiEtsi::get_version(Pistache::Http::ResponseWriter &response) {
response.send(Pistache::Http::Code::Ok, "Version 0.0.1\n");
}
void DefaultApiEtsi::get_ping(Pistache::Http::ResponseWriter &response) {
response.send(Pistache::Http::Code::Ok, "Server online !\n Have fun\n");
}
}
//
// ARF - Augmented Reality Framework (ETSI ISG ARF)
//
// Copyright 2022 ETSI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Last change: June 2022
//
#ifndef DefaultApiEtsi_H_
#define DefaultApiEtsi_H_
#include <pistache/endpoint.h>
#include <pistache/http.h>
#include <pistache/router.h>
#include <memory>
#include <optional>
#include <DefaultApi.h>
namespace etsi
{
class DefaultApiEtsi : public org::openapitools::server::api::DefaultApi {
public:
explicit DefaultApiEtsi(const std::shared_ptr<Pistache::Rest::Router>& rtr);
~DefaultApiEtsi() override = default;
void get_version(Pistache::Http::ResponseWriter &response);
void get_ping(Pistache::Http::ResponseWriter &response);
};
}
#endif
//
// ARF - Augmented Reality Framework (ETSI ISG ARF)
//
// Copyright 2022 ETSI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Last change: June 2022
//
#include "TrackableApiEtsi.h"
#include <boost/uuid/uuid.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/uuid/uuid_io.hpp>
#include <pistache/http.h>
#include <nlohmann/json.hpp>
namespace etsi {
using json = nlohmann::json;
using namespace org::openapitools::server::model;
TrackableApiEtsi::TrackableApiEtsi(const std::shared_ptr<Pistache::Rest::Router>& rtr)
: TrackablesApi(rtr)
{
m_manager = WorldStorageManager::GetInstance();
}
void TrackableApiEtsi::add_trackable(const Trackable &trackable, Pistache::Http::ResponseWriter &response) {
boost::uuids::uuid trackableId = m_manager->addTrackable(trackable);
const std::string tmp = boost::lexical_cast<std::string>(trackableId);
const char * ret = tmp.c_str();
response.send(Pistache::Http::Code::Ok, ret);
}
void TrackableApiEtsi::delete_trackable(const std::string &trackableId, Pistache::Http::ResponseWriter &response) {
const boost::uuids::uuid id = boost::lexical_cast<boost::uuids::uuid>(trackableId);
m_manager->deleteTrackable(id);
response.send(Pistache::Http::Code::Ok, "Trackable deleted\n");
}
void TrackableApiEtsi::get_trackable_by_id(const std::string &trackableId, Pistache::Http::ResponseWriter &response) {
auto jsonObjects = json::array();
const boost::uuids::uuid id = boost::lexical_cast<boost::uuids::uuid>(trackableId);
Trackable trackable = m_manager->getTrackableById(id);
to_json(jsonObjects, trackable);
response.headers().add<Pistache::Http::Header::ContentType>(MIME(Application, Json));
response.send(Pistache::Http::Code::Ok, jsonObjects.dump());
}
void TrackableApiEtsi::get_trackables(Pistache::Http::ResponseWriter &response) {
auto jsonObjects = json::array();
json toAdd;
for (Trackable t : m_manager->getTrackables()){
to_json(toAdd, t);
jsonObjects.push_back(toAdd);
}
response.headers().add<Pistache::Http::Header::ContentType>(MIME(Application, Json));
response.send(Pistache::Http::Code::Ok, jsonObjects.dump());
}
}
//
// ARF - Augmented Reality Framework (ETSI ISG ARF)
//
// Copyright 2022 ETSI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Last change: June 2022
//
#ifndef TrackableApiEtsi_H_
#define TrackableApiEtsi_H_
#include <pistache/endpoint.h>
#include <pistache/http.h>
#include <pistache/router.h>
#include <memory>
#include <optional>
#include <WorldStorageManager.h>
#include <TrackablesApi.h>
namespace etsi
{
using namespace org::openapitools::server::model;
class TrackableApiEtsi : public org::openapitools::server::api::TrackablesApi {
public:
explicit TrackableApiEtsi(const std::shared_ptr<Pistache::Rest::Router>& rtr);
~TrackableApiEtsi() override = default;
void add_trackable(const Trackable &trackable, Pistache::Http::ResponseWriter &response);
void delete_trackable(const std::string &trackableId, Pistache::Http::ResponseWriter &response);
void get_trackable_by_id(const std::string &trackableId, Pistache::Http::ResponseWriter &response);
void get_trackables(Pistache::Http::ResponseWriter &response);
private:
WorldStorageManager *m_manager;
};
}
#endif
//
// ARF - Augmented Reality Framework (ETSI ISG ARF)
//
// Copyright 2022 ETSI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Last change: June 2022
//
#include "WorldStorageManager.h"
#include <thread>
#include <iostream>
/**
* The first time we call GetInstance we will lock the storage location
* and then we make sure again that the variable is null and then we
* set the value. RU:
*/
WorldStorageManager *WorldStorageManager::GetInstance()
{
std::lock_guard<std::mutex> lock(mutex_);
if (pinstance_ == nullptr)
{
pinstance_ = new WorldStorageManager();
}
return pinstance_;
}
boost::uuids::uuid WorldStorageManager::addTrackable(org::openapitools::server::model::Trackable trackable){
boost::uuids::uuid trackableId = boost::uuids::random_generator()();
m_trackableMap.insert(std::make_pair(trackableId, trackable));
return trackableId;
}
void WorldStorageManager::deleteTrackable(boost::uuids::uuid trackableId){
m_trackableMap.erase(trackableId);
}
std::list<org::openapitools::server::model::Trackable> WorldStorageManager::getTrackables(){
std::list<org::openapitools::server::model::Trackable> ret;
std::map<boost::uuids::uuid, org::openapitools::server::model::Trackable>::iterator it;
for (it = m_trackableMap.begin(); it != m_trackableMap.end(); it++)
{
ret.push_back(it->second);
}
return ret;
}
org::openapitools::server::model::Trackable WorldStorageManager::getTrackableById(boost::uuids::uuid trackableId){
auto ret = m_trackableMap.find(trackableId);
return ret->second;
}
/**
* Static methods should be defined outside the class.
*/
WorldStorageManager* WorldStorageManager::pinstance_{nullptr};
std::mutex WorldStorageManager::mutex_;
//
// ARF - Augmented Reality Framework (ETSI ISG ARF)
//
// Copyright 2022 ETSI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Last change: June 2022
//
#ifndef WORLDSTORAGEMANAGER_H_
#define WORLDSTORAGEMANAGER_H_
#include <string>
#include <list>
#include <mutex>
#include <map>
#include <Trackable.h>
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
/**
* The WorldStorageManager class defines the `GetInstance` method that serves as an
* alternative to constructor and lets clients access the same instance of this
* class over and over.
*/
class WorldStorageManager
{
/**
* The WorldStorageManager's constructor/destructor should always be private to
* prevent direct construction/desctruction calls with the `new`/`delete`
* operator.
*/
private:
static WorldStorageManager * pinstance_;
static std::mutex mutex_;
std::map<boost::uuids::uuid, org::openapitools::server::model::Trackable> m_trackableMap;
protected:
WorldStorageManager() {}
~WorldStorageManager() {}
public:
/**
* WorldStorageManagers should not be cloneable.
*/
WorldStorageManager(WorldStorageManager &other) = delete;
/**
* WorldStorageManagers should not be assignable.
*/
void operator=(const WorldStorageManager &) = delete;
/**
* This is the static method that controls the access to the singleton
* instance. On the first run, it creates a singleton object and places it
* into the static field. On subsequent runs, it returns the client existing
* object stored in the static field.
*/
static WorldStorageManager *GetInstance();
boost::uuids::uuid addTrackable(org::openapitools::server::model::Trackable trackable);
void deleteTrackable(boost::uuids::uuid trackableId);
std::list<org::openapitools::server::model::Trackable> getTrackables();
org::openapitools::server::model::Trackable getTrackableById(boost::uuids::uuid trackableId);
};
#endif
//
// ARF - Augmented Reality Framework (ETSI ISG ARF)
//
// Copyright 2022 ETSI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Last change: June 2022
//
#include "pistache/endpoint.h"
#include "pistache/http.h"
#include "pistache/router.h"
#ifdef __linux__
#include <vector>
#include <signal.h>
#include <unistd.h>
#endif
#include <TrackableApiEtsi.h>
#include <DefaultApiEtsi.h>
#define PISTACHE_SERVER_THREADS 2
#define PISTACHE_SERVER_MAX_REQUEST_SIZE 32768
#define PISTACHE_SERVER_MAX_RESPONSE_SIZE 32768
static Pistache::Http::Endpoint *httpEndpoint;
#ifdef __linux__
static void sigHandler [[noreturn]] (int sig){
switch(sig){
case SIGINT:
case SIGQUIT:
case SIGTERM:
case SIGHUP:
default:
httpEndpoint->shutdown();
break;
}
exit(0);
}
static void setUpUnixSignals(std::vector<int> quitSignals) {
sigset_t blocking_mask;
sigemptyset(&blocking_mask);
for (auto sig : quitSignals)
sigaddset(&blocking_mask, sig);
struct sigaction sa;
sa.sa_handler = sigHandler;
sa.sa_mask = blocking_mask;
sa.sa_flags = 0;
for (auto sig : quitSignals)
sigaction(sig, &sa, nullptr);
}
#endif
using namespace org::openapitools::server::api;
using namespace etsi;
int main() {
#ifdef __linux__
std::vector<int> sigs{SIGQUIT, SIGINT, SIGTERM, SIGHUP};
setUpUnixSignals(sigs);
#endif
Pistache::Address addr(Pistache::Ipv4::any(), Pistache::Port(8080));
httpEndpoint = new Pistache::Http::Endpoint((addr));
auto router = std::make_shared<Pistache::Rest::Router>();
auto opts = Pistache::Http::Endpoint::options()
.threads(PISTACHE_SERVER_THREADS);
opts.flags(Pistache::Tcp::Options::ReuseAddr);
opts.maxRequestSize(PISTACHE_SERVER_MAX_REQUEST_SIZE);
opts.maxResponseSize(PISTACHE_SERVER_MAX_RESPONSE_SIZE);
httpEndpoint->init(opts);
DefaultApiEtsi DefaultApiserver(router);
DefaultApiserver.init();
TrackableApiEtsi TrackablesApiserver(router);
TrackablesApiserver.init();
httpEndpoint->setHandler(router->handler());
httpEndpoint->serve();
httpEndpoint->shutdown();
}
Source diff could not be displayed: it is too large. Options to address this: view the blob.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment