first commit
This commit is contained in:
23
test/CMakeLists.txt
Normal file
23
test/CMakeLists.txt
Normal file
@@ -0,0 +1,23 @@
|
||||
add_executable(test_ipc
|
||||
test_ipc.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(test_ipc
|
||||
fifo_ipc
|
||||
)
|
||||
|
||||
add_executable(test_ipc_c_api
|
||||
test_ipc_c_api.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(test_ipc_c_api
|
||||
fifo_ipc
|
||||
)
|
||||
|
||||
add_executable(test_ipc_python
|
||||
test_ipc_python.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(test_ipc_python
|
||||
fifo_ipc
|
||||
)
|
||||
77
test/test_ipc.cpp
Normal file
77
test/test_ipc.cpp
Normal file
@@ -0,0 +1,77 @@
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <fmt/core.h>
|
||||
#include <thread>
|
||||
#include <unistd.h>
|
||||
|
||||
#include "ipc/api.hpp"
|
||||
|
||||
int
|
||||
main(int /* argc */, char** /* argv */)
|
||||
{
|
||||
using ipc::Ipc;
|
||||
using fmt::format;
|
||||
using fmt::print;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
int verbose = 0;
|
||||
|
||||
// custom name
|
||||
#if defined(_WIN32)
|
||||
auto name = format("custom_fifo_ipc_{}", getpid());
|
||||
#else
|
||||
auto name = format("/tmp/custom_fifo_ipc_{}", getpid());
|
||||
#endif
|
||||
print("\n{}\n\n", name);
|
||||
ipc::init(name, 0600);
|
||||
|
||||
std::thread server_thread([name, verbose] {
|
||||
print("[server] pre-start\n");
|
||||
std::this_thread::sleep_for(1s);
|
||||
|
||||
auto server = Ipc(name, true, verbose);
|
||||
print("[server] start\n");
|
||||
|
||||
int value;
|
||||
double result;
|
||||
while(1) {
|
||||
{
|
||||
server.read(value);
|
||||
print("[server] read int({})\n", value);
|
||||
}
|
||||
|
||||
{
|
||||
// result = value * value;
|
||||
result = std::sqrt(value);
|
||||
server.write(result);
|
||||
print("[server] write double({:.2e})\n", result);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
print("[client] pre-start\n");
|
||||
auto client = Ipc(name, false, verbose);
|
||||
print("[client] start\n");
|
||||
|
||||
double answer;
|
||||
auto hline = "\n------------------------\n";
|
||||
for (int i = 2; i <= 5; ++i) {
|
||||
std::this_thread::sleep_for(500ms);
|
||||
print(hline);
|
||||
|
||||
{
|
||||
client.write(i);
|
||||
print("[client] write int({})\n", i);
|
||||
}
|
||||
|
||||
{
|
||||
client.read(answer);
|
||||
print("[client] read double({:.2e})\n", answer);
|
||||
}
|
||||
}
|
||||
|
||||
print(hline);
|
||||
print("[client] finish the work\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
67
test/test_ipc_c_api.cpp
Normal file
67
test/test_ipc_c_api.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <thread>
|
||||
|
||||
#include "ipc/c_api.h"
|
||||
|
||||
int
|
||||
main(int /* argc */, char** /* argv */)
|
||||
{
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
int verbose = 0;
|
||||
|
||||
printf("\n");
|
||||
ipc_init_default(); // default name
|
||||
|
||||
std::thread server_thread([verbose] {
|
||||
printf("[c-server] pre-start\n");
|
||||
std::this_thread::sleep_for(1s);
|
||||
|
||||
auto server = ipc_create_default(1, verbose);
|
||||
printf("[c-server] start\n");
|
||||
|
||||
int value;
|
||||
double result;
|
||||
while(1) {
|
||||
{
|
||||
ipc_read_int(server, &value, 1);
|
||||
printf("[c-server] read int(%d)\n", value);
|
||||
}
|
||||
|
||||
{
|
||||
result = value * value;
|
||||
result = std::sqrt(value);
|
||||
ipc_write_double(server, &result, 1);
|
||||
printf("[c-server] write double(%.2e)\n", result);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
printf("[c-client] pre-start\n");
|
||||
auto client = ipc_create_default(0, verbose);
|
||||
printf("[c-client] start\n");
|
||||
|
||||
double answer;
|
||||
auto hline = "\n------------------------\n";
|
||||
for (int i = 2; i <= 5; ++i) {
|
||||
std::this_thread::sleep_for(500ms);
|
||||
printf(hline);
|
||||
|
||||
{
|
||||
ipc_write_int(client, &i, 1);
|
||||
printf("[c-client] write int(%d)\n", i);
|
||||
}
|
||||
|
||||
{
|
||||
ipc_read_double(client, &answer, 1);
|
||||
printf("[c-client] read double(%.2e)\n", answer);
|
||||
}
|
||||
}
|
||||
|
||||
printf(hline);
|
||||
printf("[c-client] finish the work\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
47
test/test_ipc_python.cpp
Normal file
47
test/test_ipc_python.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <thread>
|
||||
|
||||
#include <fmt/core.h>
|
||||
|
||||
#include "ipc/api.hpp"
|
||||
|
||||
int
|
||||
main(int /* argc */, char** /* argv */)
|
||||
{
|
||||
using ipc::Ipc;
|
||||
using fmt::format;
|
||||
using fmt::print;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
int verbose = 0;
|
||||
ipc::init(); // default name
|
||||
|
||||
ipc::start_external("python test_server.py", true);
|
||||
|
||||
print("[client] pre-start\n");
|
||||
auto client = Ipc(false, verbose);
|
||||
print("[client] start\n");
|
||||
|
||||
double answer;
|
||||
auto hline = "\n------------------------\n";
|
||||
for (int i = 2; i <= 5; ++i) {
|
||||
std::this_thread::sleep_for(500ms);
|
||||
print(hline);
|
||||
|
||||
{
|
||||
client.write(i);
|
||||
print("[client] write int({})\n", i);
|
||||
}
|
||||
|
||||
{
|
||||
client.read(answer);
|
||||
print("[client] read double({:.2e})\n", answer);
|
||||
}
|
||||
}
|
||||
|
||||
print(hline);
|
||||
print("[client] finish the work\n\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user