Skip to content

Commit

Permalink
Merge from new_serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Haefner committed Apr 25, 2022
2 parents db12144 + eeb6610 commit 9d4a665
Show file tree
Hide file tree
Showing 46 changed files with 1,765 additions and 963 deletions.
26 changes: 19 additions & 7 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,39 @@
# or an add_subdirectory.
#
cmake_minimum_required(VERSION 3.5)
project(simppl VERSION 0.3.0)
project(simppl VERSION 0.4.0)

# Configuration specific to being the main project
if(PROJECT_NAME STREQUAL CMAKE_PROJECT_NAME)
set(CMAKE_VERBOSE_MAKEFILE ON)
set(SIMPPL_NOT_SUBPROJECT ON)

include(CTest)
endif()

# Reference the FindDBus module until an upstream one is available
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)

set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)

if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()

# enable introspection by default
option(HAVE_INTROSPECTION "Have introspection" ON)
include(CMakeDependentOption)

cmake_dependent_option(SIMPPL_BUILD_TESTS "Build tests"
ON "SIMPPL_NOT_SUBPROJECT" OFF
)

cmake_dependent_option(SIMPPL_BUILD_EXAMPLES "Build examples"
ON "SIMPPL_NOT_SUBPROJECT" OFF
)

if(SIMPPL_BUILD_TESTS)
include(CTest)
endif()

# enable threading
set(CMAKE_THREAD_PREFER_PTHREAD TRUE)
set(THREADS_PREFER_PTHREAD_FLAG TRUE)
Expand All @@ -39,8 +44,8 @@ find_package(Threads REQUIRED)
find_package(DBus REQUIRED)
find_package(Boost)


add_library(simppl SHARED
src/callstate.cpp
src/dispatcher.cpp
src/error.cpp
src/serverresponseholder.cpp
Expand All @@ -58,6 +63,8 @@ add_library(simppl SHARED
src/clientside.cpp
src/serialization.cpp
src/bool.cpp
src/holders.cpp
src/properties.cpp
)
# Provide a namespaced alias
add_library(Simppl::simppl ALIAS simppl)
Expand All @@ -74,14 +81,16 @@ target_include_directories(simppl
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)

target_compile_options(simppl PRIVATE -Werror -Wall)
target_compile_options(simppl PRIVATE -Werror -Wall -Wno-error=deprecated-declarations)
target_link_libraries(simppl PUBLIC
DBUS::DBUS
${CMAKE_THREAD_LIBS_INIT}
)

# Build a set of examples
add_subdirectory(examples)
if(SIMPPL_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()

if(SIMPPL_BUILD_TESTS AND BUILD_TESTING)
add_subdirectory(tests)
Expand All @@ -106,6 +115,9 @@ if(SIMPPL_NOT_SUBPROJECT)
NAMESPACE Simppl::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/simppl
)
install(DIRECTORY include/simppl
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

configure_package_config_file(cmake/SimpplConfig.cmake.in
${CMAKE_CURRENT_BINARY_DIR}/SimpplConfig.cmake
Expand Down
113 changes: 94 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Development libraries of libdbus-1 are also needed for a successful build.

## Compilers

I only developed on GCC >=4.9. The code uses features of upto C++14 and
I only developed on GCC >=4.9. The code uses features of upto C++14 and
g++ specific demangling functions for class symbols. Therefore, I do not
expect the code to work on any other compiler family without appropriate
code adaptions.
Expand Down Expand Up @@ -122,7 +122,7 @@ provided under

This mapping is done automatically by simppl, for servers this is currently
fix. But clients may connect to any bus/objectpath layout in order to connect
any DBus service. This also means that only one interface can be provided
any DBus service. This also means that only one interface can be provided
by a distinct objectpath, at least other than the properties interface needed for
providing service properties. But we will ignore signals and
properties for now and continue with our EchoService. Let's instantiate the
Expand Down Expand Up @@ -160,7 +160,7 @@ Let's provide an implementation for the echo method:
```

Now, simppl will receive the echo request, unmarshall the argument and
forward the request to the provided lambda function (of course, you may
forward the request to the provided lambda function (of course, you may
also bind any other function via std::bind). But there is still
no response yet. Let's send a response back to the client:

Expand Down Expand Up @@ -188,7 +188,7 @@ That's simppl, isn't it? Setup the eventloop and the server is finished:
{
simppl::dbus::Dispatcher disp("bus::session");
MyEcho instance(disp);
disp.run();
return EXIT_SUCCESS;
Expand Down Expand Up @@ -259,7 +259,7 @@ Event loop driven clients always get callbacks called from the dbus runtime
when any event occurs. The initial event for a client is the connected event
which will be emitted when the server registers itself on the bus (remember
the busname). After being connected, the client may start an
asynchronous method like in the example above. The method response callbacks
asynchronous method like in the example above. The method response callbacks
can be any function object fullfilling the response signature, the preferred
way nowadays in a C++ lambda. The main program is as simple as in the
blocking example:
Expand All @@ -271,7 +271,7 @@ blocking example:
simppl::dbus::Dispatcher disp("bus:session");

MyEchoClient client(disp);

disp.run();

return EXIT_SUCCESS;
Expand Down Expand Up @@ -327,7 +327,7 @@ the structure, i.e. the structure can be used as any simple data type:
simppl::dbus::Dispatcher disp("bus:session");

simppl::dbus::Stub<ComplexTest> teststub(disp, "test");

Data d({ 42, "Hallo", ... });

int i_ret;
Expand All @@ -346,9 +346,9 @@ this blocking call? Isn't that cool?
The signal in the example above is only sensefully usable
in an event loop driven client. There is a slight difference between
the properties concepts of simppl and dbus for now: the changed notification
will not be part of the Properties interface so only get and set may
currently be used to access properties of non-simppl services. But this is
nothing that cannot be changed in future. See how a client will typically
will not be part of the Properties interface so only get and set may
currently be used to access properties of non-simppl services. But this is
nothing that cannot be changed in future. See how a client will typically
register for update notifications in order to receive changes on the properties
on server side. See the clients connected callback:
Expand All @@ -367,7 +367,7 @@ on server side. See the clients connected callback:
{
...
};
data.attach() >> [](const Data& d)
{
// first callback is due to the attach!
Expand All @@ -385,9 +385,9 @@ on server side. See the clients connected callback:
```

Properties on server side can be either implemented by providing a
callback function to be called whenever the property is requested by a
callback function to be called whenever the property is requested by a
client or the property value can be stored within the server instance.
Therefore, you have to decide and initialize the property within the
Therefore, you have to decide and initialize the property within the
server's constructor. Let's see the difference:

```c++
Expand All @@ -396,22 +396,97 @@ server's constructor. Let's see the difference:
MyComplexServer(simppl::dbus::Dispatcher& disp)
: simppl::dbus::Skeleton<ComplexTest>(disp, "test")
{
// either using the callback version...
// either using the callback version...
data.on_read([](){
return Data({ 42, "Hallo", ... });
});

// ... or keep a copy in the server instance
data = Data({ 42, "Hallo", ... });
}
};
```

The notification of property changes is either done by calling the properties
notify(...) method in case the property is initialized for callback access
or by just assigning a new value to the stored property instance.
notify(...) method in case the property is initialized for callback access
or by just assigning a new value to the stored property instance.
The version of property access to be used in your server is completely
transparent for the client and depends on your use-case.
transparent for the client and depends on your use-case.

Simppl properties also support the GetAll interface. See unittest for an example.

User-defined exceptions may also be transferred. This feature is currently
restricted to method calls. To define an error you have to use the boost
fusion binding:

```c++
namespace test
{

class MyError : simppl::dbus::Error
{
public:

// needed for client side code generation
MyError() = default;

// used to throw error on server side
MyError(int rc)
: simppl::dbus::Error("My.Error") // make a DBus appropriate error name
, result(rc)
{
// NOOP
}

int result;
};

} // namespace

BOOST_FUSION_ADAPT_STRUCT(
test::MyError,
(int, result)
)
```
In the interface definition the exception class has to be added:
```c++
namespace test
{
INTERFACE(HelloService)
{
Method<in<std::string>, _throw<MyError>> hello;
// constructor
HelloService()
: INIT(hello)
{
}
};
} // namespace
```

You may now throw the error in a server's method callback:

```c++
class MyHello : simppl::dbus::Skeleton<HelloService>
{
MyHello(simppl::dbus::Dispatcher& disp)
: simppl::dbus::Skeleton<HelloService>(disp, "myHello")
{
hello >> [](const std::string& hello_string)
{
if (hello_string == "idiot")
respond_with(MyError(EINVAL));

respond_with(hello());
};
}
};
```

This was a short introduction to simppl/dbus. I hope you will like
developing client/server applications with the means of C++ and without
Expand Down
2 changes: 1 addition & 1 deletion cmake/FindDBus.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ find_path(DBUS_ARCH_INCLUDE_DIR
${PC_DBUS_INCLUDE_DIRS}
${_DBUS_LIBRARY_DIR}
${DBUS_INCLUDE_DIR}
PATH_SUFFIXES include
PATH_SUFFIXES include dbus-1.0/include
)

include(FindPackageHandleStandardArgs)
Expand Down
2 changes: 1 addition & 1 deletion cmake/GoogleTest-CMakeLists.txt.in
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.5.0)
cmake_minimum_required(VERSION 3.2)

project(googletest-download NONE)

Expand Down
8 changes: 4 additions & 4 deletions examples/echo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ class MyEchoClient : public simppl::dbus::Stub<simppl::example::EchoService>
{
if (st == simppl::dbus::ConnectionState::Connected)
{
echo.async("Hello World!") >> [this](const simppl::dbus::CallState st, const std::string& echo_string)
echo.async("Hello World!") >> [this](const simppl::dbus::CallState& st, const std::string& echo_string)
{
if (st)
{
std::cout << "Server says '" << echo_string << "'" << std::endl;
}
else
std::cout << "Got error: " << st.what() << std::endl;

disp().stop();
};
}
Expand All @@ -56,7 +56,7 @@ class MyEcho : public simppl::dbus::Skeleton<simppl::example::EchoService>
int main(int argc, char** argv)
{
simppl::dbus::Dispatcher disp("bus:session");

if (argc == 1)
{
MyEchoClient client(disp);
Expand All @@ -73,6 +73,6 @@ int main(int argc, char** argv)
MyEchoClient client(disp);
disp.run();
}

return EXIT_SUCCESS;
}
Loading

0 comments on commit 9d4a665

Please sign in to comment.