Skip to content

Commit

Permalink
Implement SocketUTransport for mock testing. (eclipse-uprotocol#64)
Browse files Browse the repository at this point in the history
This PR implements a socket transport to the python dispacther script,
and necessary data structures for listener callbacks and callback
removal. The high level code is a private implementation in
SocketUTransport.cpp. The unordered_map supporting callbacks is in
SafeTupleMap.h. The details of this is std::hash extensions to hash
tuples of optionals for filtering with wildcards.
  • Loading branch information
debruce committed Jul 30, 2024
1 parent f59656a commit 0395b91
Show file tree
Hide file tree
Showing 11 changed files with 834 additions and 531 deletions.
28 changes: 21 additions & 7 deletions up_client_socket/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ cmake_minimum_required(VERSION 3.20.1)
project(up_client_socket VERSION 0.1.0 LANGUAGES CXX DESCRIPTION "C++ socket transport")

find_package(up-cpp REQUIRED)
find_package(protobuf REQUIRED)
find_package(up-core-api REQUIRED)
find_package(spdlog REQUIRED)
add_definitions(-DSPDLOG_FMT_EXTERNAL)
find_package(fmt REQUIRED CONFIG)
find_package(RapidJSON CONFIG REQUIRED)

# This is the root CMakeLists.txt file; We can set project wide settings here
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
Expand All @@ -27,18 +28,18 @@ if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
endif()

file(GLOB_RECURSE SRC_FILES "${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp")
add_library(${PROJECT_NAME} src/SocketUTransport.cpp)

add_library(${PROJECT_NAME} ${SRC_FILES})
add_library(up_client_socket::${PROJECT_NAME} ALIAS ${PROJECT_NAME})

target_include_directories(${PROJECT_NAME}
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
${rapidjson_INCLUDE_DIRS}
${up-cpp_INCLUDE_DIR}
${up-core-api_INCLUDE_DIR}
${protobuf_INCLUDE_DIR}
${spdlog_INCLUDE_DIR})

set_property(TARGET ${PROJECT_NAME} PROPERTY POSITION_INDEPENDENT_CODE ON)
Expand All @@ -50,9 +51,22 @@ target_link_libraries(${PROJECT_NAME}
dl
spdlog::spdlog
up-cpp::up-cpp
fmt::fmt
rapidjson)
up-core-api::up-core-api
protobuf::libprotobuf)

add_executable(myTest src/test.cpp)
target_include_directories(myTest
PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}>
${up-cpp_INCLUDE_DIR}
${up-core-api_INCLUDE_DIR}
${protobuf_INCLUDE_DIR}
${spdlog_INCLUDE_DIR})
target_link_libraries(myTest ${PROJECT_NAME} spdlog::spdlog)

# Specify the install location for the library
INSTALL(TARGETS ${PROJECT_NAME})
INSTALL(DIRECTORY include DESTINATION .)
INSTALL(DIRECTORY include DESTINATION .)

5 changes: 5 additions & 0 deletions up_client_socket/cpp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Local build steps for SocketUTransport
*From the up_client_socket/cpp directory, run the following commands*
1. conan install --build=missing .
2. cmake --preset conan-release
3. (cd build/Release; cmake --build . -- -j)
73 changes: 0 additions & 73 deletions up_client_socket/cpp/conanfile.py

This file was deleted.

7 changes: 3 additions & 4 deletions up_client_socket/cpp/conanfile.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
[requires]
up-core-api/1.6.0
protobuf/3.21.12
up-cpp/0.1.2-dev
rapidjson/cci.20230929
up-cpp/1.0.1-rc1
spdlog/1.13.0
fmt/10.2.1
openssl/1.1.1w

[generators]
CMakeDeps
CMakeToolchain

[layout]
cmake_layout
cmake_layout
24 changes: 24 additions & 0 deletions up_client_socket/cpp/include/Demangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache License Version 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <cxxabi.h>

#include <string>

template <typename T>
std::string demangle(const T& t) {
auto info = typeid(t).name();
int status;
auto realname = abi::__cxa_demangle(info, NULL, NULL, &status);
return std::string(realname);
}
54 changes: 54 additions & 0 deletions up_client_socket/cpp/include/SafeTupleMap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// SPDX-FileCopyrightText: 2024 Contributors to the Eclipse Foundation
//
// See the NOTICE file(s) distributed with this work for additional
// information regarding copyright ownership.
//
// This program and the accompanying materials are made available under the
// terms of the Apache License Version 2.0 which is available at
// https://www.apache.org/licenses/LICENSE-2.0
//
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <functional>
#include <memory>
#include <mutex>
#include <unordered_map>
#include <vector>

#include "TupleOfOptionals.h"

template <typename KEY, typename VALUE>
class SafeTupleMap {
std::unordered_map<KEY, std::shared_ptr<VALUE>,
tuple_of_optionals::hash<KEY>>
map_;
std::mutex mtx;

public:
using Key = KEY;

SafeTupleMap() = default;

std::shared_ptr<VALUE> find(const KEY& key, bool create = false) {
std::unique_lock<std::mutex> lock(mtx);
auto it = map_.find(key);
if (!create) {
return (it != map_.end()) ? it->second : nullptr;
} else {
if (it != map_.end())
return it->second;
auto ptr = std::make_shared<VALUE>();
map_.emplace(key, ptr);
return ptr;
}
}

void erase(std::function<void(std::shared_ptr<VALUE>)> fn) {
std::unique_lock<std::mutex> lock(mtx);
for (auto [key, ptr] : map_) {
fn(ptr);
}
}
};
Loading

0 comments on commit 0395b91

Please sign in to comment.