Skip to content

Commit

Permalink
new serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
martinhaefner committed Dec 23, 2017
1 parent b2330b5 commit 4764f5c
Show file tree
Hide file tree
Showing 40 changed files with 808 additions and 1,107 deletions.
1 change: 1 addition & 0 deletions examples/echo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "simppl/stub.h"
#include "simppl/skeleton.h"
#include "simppl/dispatcher.h"
#include "simppl/string.h"


class MyEchoClient : public simppl::dbus::Stub<simppl::example::EchoService>
Expand Down
111 changes: 56 additions & 55 deletions examples/mixed/mixed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "simppl/stub.h"
#include "simppl/skeleton.h"
#include "simppl/string.h"


namespace test
Expand All @@ -25,7 +26,7 @@ namespace test
INTERFACE(clock)
{
Signal<int> tick;

clock()
: INIT(tick)
{
Expand All @@ -49,75 +50,75 @@ namespace test

void the_client()
{
simppl::dbus::Dispatcher d;
simppl::dbus::Stub<test::clock> clock(d, "clock");
simppl::dbus::Stub<test::long_op> op(d, "op");
clock.connected = [&clock, &op](simppl::dbus::ConnectionState st){
clock.tick.attach() >> [&op](int count){
std::cout << "tick: " << count << std::endl;
if (count == 4)
{
// mixing blocking and non-blocking calls is not really a good
// idea, but this is for demonstration that it works only
std::string rc = op.do_it("Hello");
std::cout << "Response: " << rc << std::endl;
}
};
};
d.run();
simppl::dbus::Dispatcher d;
simppl::dbus::Stub<test::clock> clock(d, "clock");
simppl::dbus::Stub<test::long_op> op(d, "op");
clock.connected = [&clock, &op](simppl::dbus::ConnectionState st){
clock.tick.attach() >> [&op](int count){
std::cout << "tick: " << count << std::endl;
if (count == 4)
{
// mixing blocking and non-blocking calls is not really a good
// idea, but this is for demonstration that it works only
std::string rc = op.do_it("Hello");
std::cout << "Response: " << rc << std::endl;
}
};
};
d.run();
}


void the_clock()
{
simppl::dbus::Dispatcher d;
simppl::dbus::Skeleton<test::clock> skel(d, "clock");
std::thread t([&skel](){
int i=0;
while(true)
{
skel.tick.notify(i++);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
d.run();
simppl::dbus::Dispatcher d;
simppl::dbus::Skeleton<test::clock> skel(d, "clock");
std::thread t([&skel](){
int i=0;
while(true)
{
skel.tick.notify(i++);
std::this_thread::sleep_for(std::chrono::seconds(1));
}
});
d.run();
}


void the_long_op()
{
simppl::dbus::Dispatcher d;
simppl::dbus::Skeleton<test::long_op> skel(d, "op");
// just echo with some waiting time
skel.do_it >> [&skel](const std::string& str){
std::this_thread::sleep_for(std::chrono::seconds(5));
skel.respond_with(skel.do_it(str));
};
d.run();
simppl::dbus::Dispatcher d;
simppl::dbus::Skeleton<test::long_op> skel(d, "op");
// just echo with some waiting time
skel.do_it >> [&skel](const std::string& str){
std::this_thread::sleep_for(std::chrono::seconds(5));
skel.respond_with(skel.do_it(str));
};
d.run();
}


int main()
{
simppl::dbus::enable_threads();
std::thread t1(the_long_op);
std::thread t2(the_clock);
std::thread t3(the_client);
while(true)
std::this_thread::sleep_for(std::chrono::seconds(1));
return 0;
simppl::dbus::enable_threads();
std::thread t1(the_long_op);
std::thread t2(the_clock);
std::thread t3(the_client);
while(true)
std::this_thread::sleep_for(std::chrono::seconds(1));
return 0;
}


61 changes: 48 additions & 13 deletions include/simppl/bool.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,60 @@



inline
Serializer& write(bool b)
#ifndef __SIMPPL_DBUS_BOOL_H__
#define __SIMPPL_DBUS_BOOL_H__


#include "simppl/serialization.h"


namespace simppl
{

namespace dbus
{


// forward decl
template<typename T>
struct Codec;


template<>
struct Codec<bool>
{
// FIXME can this be removed?
enum { dbus_type_code = DBUS_TYPE_BOOLEAN };


static inline
void encode(DBusMessageIter& iter, bool b)
{
dbus_bool_t _b = b;
dbus_message_iter_append_basic(iter_, dbus_type_code<bool>::value, &_b);
return *this;
dbus_message_iter_append_basic(&iter, dbus_type_code, &_b);
}



Deserializer& read(bool& t)
// FIXME move to impl file
static inline
void decode(DBusMessageIter& iter, bool& t)
{
dbus_bool_t b;
dbus_message_iter_get_basic(iter_, &b);
dbus_message_iter_next(iter_);
dbus_message_iter_get_basic(&iter, &b);
dbus_message_iter_next(&iter);

t = b;
return *this;
}


static inline
std::ostream& make_type_signature(std::ostream& os)
{
return os << DBUS_TYPE_BOOLEAN_AS_STRING;
}
};


} // namespace dbus

} // namespace simppl


template<> struct dbus_type_code<bool> { enum { value = DBUS_TYPE_BOOLEAN }; };
#endif // __SIMPPL_DBUS_BOOL_H__
39 changes: 16 additions & 23 deletions include/simppl/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <stdlib.h>
#include <cstring>

#include "simppl/serialization.h"


namespace simppl
{
Expand Down Expand Up @@ -47,6 +49,7 @@ struct FixedSizeBuffer
/**
* No copy of data.
*/
explicit
FixedSizeBuffer(void* buf)
: buf((unsigned char*)buf)
, self(false)
Expand Down Expand Up @@ -80,57 +83,47 @@ struct FixedSizeBuffer
unsigned char* buf;
bool self;
};


namespace detail
{


template<size_t len>
struct Codec<FixedSizeBuffer<len>>
{
static
void encode(Serializer& s, const FixedSizeBuffer<len>& b)
void encode(DBusMessageIter& iter, const FixedSizeBuffer<len>& b)
{
DBusMessageIter iter;
DBusMessageIter _iter;

dbus_message_iter_open_container(s.iter_, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &iter);
dbus_message_iter_append_fixed_array(&iter, DBUS_TYPE_BYTE, &b.buf, len);
dbus_message_iter_close_container(s.iter_, &iter);
dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE_AS_STRING, &_iter);
dbus_message_iter_append_fixed_array(&_iter, DBUS_TYPE_BYTE, &b.buf, len);
dbus_message_iter_close_container(&iter, &_iter);
}


static
void decode(Deserializer& s, FixedSizeBuffer<len>& b)
void decode(DBusMessageIter& iter, FixedSizeBuffer<len>& b)
{
DBusMessageIter iter;
dbus_message_iter_recurse(s.iter_, &iter);
DBusMessageIter _iter;
dbus_message_iter_recurse(&iter, &_iter);

unsigned char* buf;
int _len = len;
dbus_message_iter_get_fixed_array(&iter, &buf, &_len);
dbus_message_iter_get_fixed_array(&_iter, &buf, &_len);

b.assign(buf);

// advance to next element
dbus_message_iter_next(s.iter_);
dbus_message_iter_next(&iter);
}
};


template<size_t len>
struct make_type_signature<FixedSizeBuffer<len>>
{


static inline
std::ostream& eval(std::ostream& os)
std::ostream& make_type_signature(std::ostream& os)
{
return os << DBUS_TYPE_ARRAY_AS_STRING;
}
};


} // namespace detail

} // namespace dbus

} // namespace simppl
Expand Down
23 changes: 13 additions & 10 deletions include/simppl/clientside.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,10 @@ struct ClientSignal : ClientSignalBase
{
ClientSignal* that = (ClientSignal*)(obj);

detail::Deserializer d(msg);
detail::GetCaller<std::tuple<T...>>::type::template eval(d, that->f_);
DBusMessageIter iter;
dbus_message_iter_init(msg, &iter);

detail::GetCaller<std::tuple<T...>>::type::template eval(iter, that->f_);
}
};

Expand All @@ -142,7 +144,7 @@ struct ClientPropertyWritableMixin

Variant<data_type> vt(t);

that->stub().set_property(that->name(), [&vt](detail::Serializer& s){
that->stub().set_property(that->name(), [&vt](DBusMessageIter& s){
simppl::dbus::detail::serialize(s, vt);
});
}
Expand All @@ -155,7 +157,7 @@ struct ClientPropertyWritableMixin

Variant<data_type> vt(t);

return detail::InterimCallbackHolder<holder_type>(that->stub().set_property_async(that->name(), [&vt](detail::Serializer& s){
return detail::InterimCallbackHolder<holder_type>(that->stub().set_property_async(that->name(), [&vt](DBusMessageIter& s){
simppl::dbus::detail::serialize(s, vt);
}));
}
Expand Down Expand Up @@ -201,10 +203,11 @@ struct ClientProperty
{
message_ptr_t msg = stub().get_property(name_);

detail::Deserializer ds(msg.get());
DBusMessageIter iter;
dbus_message_iter_init(msg.get(), &iter);

Variant<DataT> v;
ds.read(v);
Codec<Variant<DataT>>::decode(iter, v);

return std::move(*v.template get<DataT>());
}
Expand Down Expand Up @@ -253,10 +256,10 @@ struct ClientProperty
template<typename DataT, int Flags>
ClientProperty<DataT, Flags>& ClientProperty<DataT, Flags>::attach()
{
stub().attach_property(name_, [this](detail::Deserializer& s){
stub().attach_property(name_, [this](DBusMessageIter& iter){

Variant<data_type> d;
s.read(d);
Codec<Variant<data_type>>::decode(iter, d);

if (this->f_)
this->f_(CallState(42), *d.template get<data_type>());
Expand Down Expand Up @@ -321,7 +324,7 @@ struct ClientMethod
static_assert(std::is_convertible<typename detail::canonify<std::tuple<T...>>::type,
args_type>::value, "args mismatch");

auto msg = parent_->send_request_and_block(method_name_, [&](detail::Serializer& s){
auto msg = parent_->send_request_and_block(method_name_, [&](DBusMessageIter& s){
serializer_type::eval(s, t...);
}, is_oneway);

Expand All @@ -338,7 +341,7 @@ struct ClientMethod
static_assert(std::is_convertible<typename detail::canonify<std::tuple<typename std::decay<T>::type...>>::type,
args_type>::value, "args mismatch");

return detail::InterimCallbackHolder<holder_type>(parent_->send_request(method_name_, [&](detail::Serializer& s){
return detail::InterimCallbackHolder<holder_type>(parent_->send_request(method_name_, [&](DBusMessageIter& s){
serializer_type::eval(s, t...);
}, false));
}
Expand Down
Loading

0 comments on commit 4764f5c

Please sign in to comment.