Skip to content

Commit

Permalink
Lagom: Add a simple IPC client and server for host-side testing
Browse files Browse the repository at this point in the history
Add LibIPC to the Lagom build, and also implement two little test apps
so we can run IPC tests on other systems.
  • Loading branch information
awesomekling committed Aug 3, 2019
1 parent 8e684f0 commit 0f0b00d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 2 deletions.
13 changes: 11 additions & 2 deletions Lagom/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required (VERSION 3.0)
project (Lagom)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -Wextra -Werror -std=c++17 -fPIC")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O2 -Wall -Wextra -Werror -std=c++17 -fPIC -g")

if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wconsumed")
Expand All @@ -11,8 +11,9 @@ endif()

file(GLOB AK_SOURCES "../AK/*.cpp")
file(GLOB LIBCORE_SOURCES "../Libraries/LibCore/*.cpp")
file(GLOB LIBIPC_SOURCES "../Libraries/LibIPC/*.cpp")

set(SOURCES ${AK_SOURCES} ${LIBCORE_SOURCES})
set(SOURCES ${AK_SOURCES} ${LIBCORE_SOURCES} ${LIBIPC_SOURCES})

include_directories (../)
include_directories (../Libraries/)
Expand All @@ -21,3 +22,11 @@ add_library(lagom ${SOURCES})
add_executable(TestApp TestApp.cpp)
target_link_libraries(TestApp lagom)
target_link_libraries(TestApp stdc++)

add_executable(SimpleIPCClient SimpleIPCClient.cpp)
target_link_libraries(SimpleIPCClient lagom)
target_link_libraries(SimpleIPCClient stdc++)

add_executable(SimpleIPCServer SimpleIPCServer.cpp)
target_link_libraries(SimpleIPCServer lagom)
target_link_libraries(SimpleIPCServer stdc++)
3 changes: 3 additions & 0 deletions Lagom/SimpleIPC.ipc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
endpoint Simple {
ComputeSum(i32 a, i32 b, i32 c) => (i32 sum)
}
39 changes: 39 additions & 0 deletions Lagom/SimpleIPCClient.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <LibCore/CEventLoop.h>
#include <LibCore/CTimer.h>
#include <LibCore/CoreIPCClient.h>
#include <stdio.h>
#include "SimpleEndpoint.h"

class SimpleIPCClient : public IPC::Client::ConnectionNG<SimpleEndpoint> {
C_OBJECT(SimpleIPCClient)
public:
SimpleIPCClient()
: ConnectionNG("/tmp/simple-ipc")
{}

virtual void handshake() override {}

i32 compute_sum(i32 a, i32 b, i32 c)
{
return send_sync<Simple::ComputeSum>(a, b, c)->sum();
}
};

int main(int, char**)
{
CEventLoop event_loop;

SimpleIPCClient client;

CTimer timer(100, [&] {
i32 sum = client.compute_sum(1, 2, 3);
dbg() << "Sum: " << sum;
});

CTimer kill_timer(5000, [&] {
dbg() << "Timer fired, good-bye! :^)";
event_loop.quit(0);
});

return event_loop.exec();
}
40 changes: 40 additions & 0 deletions Lagom/SimpleIPCServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include <LibCore/CEventLoop.h>
#include <LibCore/CTimer.h>
#include <LibCore/CoreIPCServer.h>
#include <LibCore/CLocalServer.h>
#include <stdio.h>
#include "SimpleEndpoint.h"

class SimpleIPCServer final :
public IPC::Server::ConnectionNG<SimpleEndpoint>,
public SimpleEndpoint {

C_OBJECT(SimpleIPCServer)
public:
SimpleIPCServer(CLocalSocket& socket, int client_id)
: ConnectionNG(*this, socket, client_id)
{
}

virtual OwnPtr<Simple::ComputeSumResponse> handle(const Simple::ComputeSum& message)
{
return make<Simple::ComputeSumResponse>(message.a() + message.b() + message.c());
}
};

int main(int, char**)
{
CEventLoop event_loop;

unlink("/tmp/simple-ipc");
CLocalServer server_sock;
server_sock.listen("/tmp/simple-ipc");
server_sock.on_ready_to_accept = [&] {
auto* client_socket = server_sock.accept();
ASSERT(client_socket);
static int next_client_id = 0;
IPC::Server::new_connection_ng_for_client<SimpleIPCServer>(*client_socket, ++next_client_id);
};

return event_loop.exec();
}

0 comments on commit 0f0b00d

Please sign in to comment.