Skip to content

Commit

Permalink
added SDK library
Browse files Browse the repository at this point in the history
  • Loading branch information
serges147 committed Dec 20, 2024
1 parent 2a1f04d commit 0ce543c
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 34 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ if (NOT clang_format)
message(STATUS "Could not locate clang-format")
else ()
file(GLOB_RECURSE format_files
${include_dir}/**/*.[ch]pp
${include_dir}/**/*.hpp
${src_dir}/**/*.[ch]
${src_dir}/**/*.[ch]pp
${test_dir}/**/*.[ch]pp
Expand Down
40 changes: 40 additions & 0 deletions include/ocvsmd/sdk/daemon.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
//

#ifndef OCVSMD_SDK_DAEMON_HPP_INCLUDED
#define OCVSMD_SDK_DAEMON_HPP_INCLUDED

#include <memory>

namespace ocvsmd
{
namespace sdk
{

/// An abstract factory for the specialized interfaces.
///
class Daemon
{
public:
static std::unique_ptr<Daemon> make();

Daemon(Daemon&&) = delete;
Daemon(const Daemon&) = delete;
Daemon& operator=(Daemon&&) = delete;
Daemon& operator=(const Daemon&) = delete;

virtual ~Daemon() = default;

virtual void send_messages() const = 0;

protected:
Daemon() = default;

}; // Daemon

} // namespace sdk
} // namespace ocvsmd

#endif // OCVSMD_SDK_DAEMON_HPP_INCLUDED
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ cmake_minimum_required(VERSION 3.27)

add_subdirectory(common)
add_subdirectory(daemon)
add_subdirectory(sdk)
add_subdirectory(cli)
2 changes: 1 addition & 1 deletion src/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ add_executable(ocvsmd-cli
main.cpp
)
target_link_libraries(ocvsmd-cli
PRIVATE ocvsmd_common
PUBLIC ocvsmd_sdk
)
add_dependencies(ocvsmd-cli
ocvsmd
Expand Down
15 changes: 3 additions & 12 deletions src/cli/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,17 @@
// SPDX-License-Identifier: MIT
//

#include "ipc/unix_socket_client.hpp"

#include <unistd.h>
#include <ocvsmd/sdk/daemon.hpp>

int main(const int argc, const char** const argv)
{
(void) argc;
(void) argv;

ocvsmd::common::ipc::UnixSocketClient client{"/var/run/ocvsmd/local.sock"};

if (!client.connect_to_server())
if (auto daemon = ocvsmd::sdk::Daemon::make())
{
return 1;
daemon->send_messages();
}

client.send_message("Hello, world!");
::sleep(10);
client.send_message("Goodbye, world!");
::sleep(10);

return 0;
}
2 changes: 1 addition & 1 deletion src/common/ipc/unix_socket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace

constexpr int MaxConnections = 5;

class ClientContextImpl final : public UnixSocketServer::ClientContext
class ClientContextImpl final : public detail::ClientContext
{
public:
explicit ClientContextImpl(const int client_fd)
Expand Down
42 changes: 23 additions & 19 deletions src/common/ipc/unix_socket_server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@ namespace common
{
namespace ipc
{
namespace detail
{

class ClientContext
{
public:
ClientContext() = default;

ClientContext(ClientContext&&) = delete;
ClientContext(const ClientContext&) = delete;
ClientContext& operator=(ClientContext&&) = delete;
ClientContext& operator=(const ClientContext&) = delete;

virtual ~ClientContext() = default;

}; // ClientContext

} // namespace detail

class UnixSocketServer final
{
Expand All @@ -41,29 +59,15 @@ class UnixSocketServer final

void accept();

class ClientContext
{
public:
ClientContext() = default;

ClientContext(ClientContext&&) = delete;
ClientContext(const ClientContext&) = delete;
ClientContext& operator=(ClientContext&&) = delete;
ClientContext& operator=(const ClientContext&) = delete;

virtual ~ClientContext() = default;

}; // ClientContext

private:
void handle_client_connection(const int client_fd);
void handle_client_request(const int client_fd);

libcyphal::IExecutor& executor_;
const std::string socket_path_;
int server_fd_;
platform::IPosixExecutorExtension* const posix_executor_ext_;
std::unordered_map<int, std::unique_ptr<ClientContext>> client_contexts_;
libcyphal::IExecutor& executor_;
const std::string socket_path_;
int server_fd_;
platform::IPosixExecutorExtension* const posix_executor_ext_;
std::unordered_map<int, std::unique_ptr<detail::ClientContext>> client_contexts_;

}; // UnixSocketServer

Expand Down
16 changes: 16 additions & 0 deletions src/sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT
#

cmake_minimum_required(VERSION 3.27)

add_library(ocvsmd_sdk
daemon.cpp
)
target_link_libraries(ocvsmd_sdk
PRIVATE ocvsmd_common
)
target_include_directories(ocvsmd_sdk
PUBLIC ${include_dir}
)
56 changes: 56 additions & 0 deletions src/sdk/daemon.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
//

#include <ocvsmd/sdk/daemon.hpp>

#include "ipc/unix_socket_client.hpp"

#include <memory>
#include <utility>
#include <unistd.h>

namespace ocvsmd
{
namespace sdk
{
namespace
{

class DaemonImpl final : public Daemon
{
public:
bool connect()
{
return ipc_client_.connect_to_server();
}

void send_messages() const override
{
ipc_client_.send_message("Hello, world!");
::sleep(1);
ipc_client_.send_message("Goodbye, world!");
::sleep(1);
}

private:
common::ipc::UnixSocketClient ipc_client_{"/var/run/ocvsmd/local.sock"};

}; // DaemonImpl

} // namespace

std::unique_ptr<Daemon> Daemon::make()
{
auto daemon = std::make_unique<DaemonImpl>();
if (!daemon->connect())
{
return nullptr;
}

return std::move(daemon);
}

} // namespace sdk
} // namespace ocvsmd

0 comments on commit 0ce543c

Please sign in to comment.