Skip to content

Commit 0da80eb

Browse files
committed
shared libs for env_server
1 parent 37ce310 commit 0da80eb

18 files changed

+1734
-28
lines changed

.gitignore

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ build/
22
.vscode/
33
.vs/
44
.pytest_cache/
5-
*.pyc
5+
*.pyc
6+
7+
/scratch.sh

.gitmodules

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
[submodule "example/lib/spdlog"]
2-
path = example/lib/spdlog
1+
[submodule "deps/lib/spdlog"]
2+
path = deps/lib/spdlog
33
url = [email protected]:gabime/spdlog.git
4-
[submodule "example/lib/msgpack-c"]
5-
path = example/lib/msgpack-c
4+
[submodule "deps/lib/msgpack-c"]
5+
path = deps/lib/msgpack-c
66
url = [email protected]:msgpack/msgpack-c.git
7-
[submodule "example/lib/libzmq"]
8-
path = example/lib/libzmq
7+
[submodule "deps/lib/libzmq"]
8+
path = deps/lib/libzmq
99
url = [email protected]:zeromq/libzmq.git

CMakeLists.txt

+10
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ if (CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
3838
endif(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
3939

4040
# Dependencies
41+
42+
# Spdlog
43+
add_subdirectory(deps/lib/spdlog)
44+
# ZMQ
45+
option(ZMQ_BUILD_TESTS "" OFF)
46+
add_subdirectory(deps/lib/libzmq)
47+
4148
## PyTorch
49+
set(Torch_DIR /home/rozgo/deeprl/libtorch/libtorch-cxx11-abi-shared-with-deps-1.3.1/libtorch/share/cmake/Torch)
4250
if (NOT TORCH_FOUND)
4351
find_package(Torch REQUIRED)
4452
if (TORCH_CXX_FLAGS)
@@ -85,5 +93,7 @@ if (CPPRL_BUILD_EXAMPLE)
8593
add_subdirectory(example)
8694
endif(CPPRL_BUILD_EXAMPLE)
8795

96+
add_subdirectory(env_server)
97+
8898
# Recurse into source tree
8999
add_subdirectory(src)

deps/lib/libzmq

Submodule libzmq added at 85df755

deps/lib/msgpack-c

Submodule msgpack-c added at fbf5b9d

deps/lib/spdlog

Submodule spdlog added at 2d26485
File renamed without changes.

env_server/CMakeLists.txt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
add_executable(env_server env_server.cpp communicator.cpp)
2+
3+
target_include_directories(env_server
4+
PRIVATE
5+
.
6+
../include
7+
../deps
8+
../deps/lib/spdlog/include
9+
../deps/lib/msgpack-c/include
10+
../deps/lib/spdlog/include
11+
../deps/lib/libzmq/include
12+
)
13+
14+
target_link_libraries(env_server PRIVATE libzmq-static cpprl)

env_server/communicator.cpp

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <memory>
2+
#include <string>
3+
4+
#include <spdlog/spdlog.h>
5+
6+
#include "communicator.h"
7+
#include "requests.h"
8+
#include "third_party/zmq.hpp"
9+
10+
namespace gym_client
11+
{
12+
Communicator::Communicator(const std::string &url)
13+
{
14+
context = std::make_unique<zmq::context_t>(1);
15+
socket = std::make_unique<zmq::socket_t>(*context, ZMQ_PAIR);
16+
17+
socket->connect(url.c_str());
18+
spdlog::info(get_raw_response());
19+
}
20+
21+
Communicator::~Communicator() {}
22+
23+
std::string Communicator::get_raw_response()
24+
{
25+
// Receive message
26+
zmq::message_t zmq_msg;
27+
socket->recv(&zmq_msg);
28+
29+
// Cast message to string
30+
std::string response = std::string(static_cast<char *>(zmq_msg.data()), zmq_msg.size());
31+
32+
return response;
33+
}
34+
}

env_server/communicator.h

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <memory>
5+
#include <sstream>
6+
#include <string>
7+
8+
#include <msgpack.hpp>
9+
#include <spdlog/spdlog.h>
10+
#include <spdlog/fmt/bundled/ostream.h>
11+
12+
#include "requests.h"
13+
#include "third_party/zmq.hpp"
14+
15+
namespace gym_client
16+
{
17+
class Communicator
18+
{
19+
public:
20+
Communicator(const std::string &url);
21+
~Communicator();
22+
23+
std::string get_raw_response();
24+
25+
template <class T>
26+
std::unique_ptr<T> get_response()
27+
{
28+
// Receive message
29+
zmq::message_t packed_msg;
30+
socket->recv(&packed_msg);
31+
32+
// Desrialize message
33+
msgpack::object_handle object_handle = msgpack::unpack(static_cast<char *>(packed_msg.data()), packed_msg.size());
34+
msgpack::object object = object_handle.get();
35+
36+
// Fill out response object
37+
std::unique_ptr<T> response = std::make_unique<T>();
38+
try
39+
{
40+
object.convert(response);
41+
}
42+
catch (...)
43+
{
44+
spdlog::error("Communication error: {}", object);
45+
}
46+
47+
return response;
48+
}
49+
50+
template <class T>
51+
void send_request(const Request<T> &request)
52+
{
53+
msgpack::sbuffer buffer;
54+
msgpack::pack(buffer, request);
55+
56+
zmq::message_t message(buffer.size());
57+
std::memcpy(message.data(), buffer.data(), buffer.size());
58+
socket->send(message);
59+
}
60+
61+
private:
62+
std::unique_ptr<zmq::context_t> context;
63+
std::unique_ptr<zmq::socket_t> socket;
64+
};
65+
}

0 commit comments

Comments
 (0)