summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJosh Blum2013-03-17 12:32:43 -0700
committerJosh Blum2013-03-17 12:32:43 -0700
commit4e19b066d27b90355657688f67b6e2f208390a82 (patch)
treef72809037a62f883f1a60acddb3115e376da889a /lib
parentb142e0e75f8ab4e7b2bb70c3fc71b61fd5f71651 (diff)
downloadsandhi-4e19b066d27b90355657688f67b6e2f208390a82.tar.gz
sandhi-4e19b066d27b90355657688f67b6e2f208390a82.tar.bz2
sandhi-4e19b066d27b90355657688f67b6e2f208390a82.zip
gras: property implementation in lib
Diffstat (limited to 'lib')
-rw-r--r--lib/block_props.cpp83
-rw-r--r--lib/gras_impl/block_actor.hpp7
-rw-r--r--lib/gras_impl/messages.hpp9
-rw-r--r--lib/register_messages.cpp1
4 files changed, 100 insertions, 0 deletions
diff --git a/lib/block_props.cpp b/lib/block_props.cpp
index 641ecbb..bba9441 100644
--- a/lib/block_props.cpp
+++ b/lib/block_props.cpp
@@ -2,3 +2,86 @@
#include "element_impl.hpp"
#include <gras/block.hpp>
+
+using namespace gras;
+
+PropertyRegistry::PropertyRegistry(void){}
+PropertyRegistry::~PropertyRegistry(void){}
+
+void BlockActor::handle_prop_access(
+ const PropAccessMessage &message,
+ const Theron::Address from
+)
+{
+ ASSERT(this->prio_count.Load() != 0);
+ this->prio_count.Decrement();
+
+ PropAccessMessage reply;
+ reply.set = not message.set;
+ reply.key = message.key;
+
+ PropertyRegistrySptr pr = prop_registry[message.key];
+ if (not pr) reply.error = "no property registered for key: " + message.key;
+ else try
+ {
+ if (message.set) pr->set(message.value);
+ else reply.value = pr->get();
+ }
+ catch (const std::exception &e)
+ {
+ reply.error = e.what();
+ }
+ catch (...)
+ {
+ reply.error = "unknown error";
+ }
+
+ this->Send(reply, from); //ACK
+}
+
+void Block::_register_property(const std::string &key, PropertyRegistrySptr pr)
+{
+ (*this)->block->prop_registry[key] = pr;
+}
+
+struct PropAccessReceiver : Theron::Receiver
+{
+ PropAccessReceiver(void)
+ {
+ this->RegisterHandler(this, &PropAccessReceiver::handle_prop_access);
+ }
+
+ void handle_prop_access(const PropAccessMessage &msg, const Theron::Address)
+ {
+ this->message = msg;
+ }
+
+ PropAccessMessage message;
+};
+
+PMCC BlockActor::prop_access_dispatcher(const std::string &key, const PMCC &value, const bool set)
+{
+ PropAccessReceiver receiver;
+ PropAccessMessage message;
+ message.set = set;
+ message.key = key;
+ message.value = value;
+ this->Push(message, receiver.GetAddress());
+ this->prio_count.Increment();
+ receiver.Wait();
+ if (not receiver.message.error.empty())
+ {
+ throw std::runtime_error(receiver.message.error);
+ }
+ return receiver.message.value;
+}
+
+void Block::_set_property(const std::string &key, const PMCC &value)
+{
+ (*this)->block->prop_access_dispatcher(key, value, true);
+}
+
+PMCC Block::_get_property(const std::string &key)
+{
+ return (*this)->block->prop_access_dispatcher(key, PMCC(), false);
+}
diff --git a/lib/gras_impl/block_actor.hpp b/lib/gras_impl/block_actor.hpp
index 935c87d..d7429f1 100644
--- a/lib/gras_impl/block_actor.hpp
+++ b/lib/gras_impl/block_actor.hpp
@@ -19,6 +19,7 @@
#include <Theron/Detail/Threading/Atomic.h>
#include <vector>
#include <set>
+#include <map>
namespace gras
{
@@ -61,6 +62,7 @@ struct BlockActor : Apology::Worker
this->RegisterHandler(this, &BlockActor::handle_output_alloc);
this->RegisterHandler(this, &BlockActor::handle_output_update);
+ this->RegisterHandler(this, &BlockActor::handle_prop_access);
this->RegisterHandler(this, &BlockActor::handle_self_kick);
this->RegisterHandler(this, &BlockActor::handle_get_stats);
}
@@ -90,6 +92,7 @@ struct BlockActor : Apology::Worker
void handle_output_alloc(const OutputAllocMessage &, const Theron::Address);
void handle_output_update(const OutputUpdateMessage &, const Theron::Address);
+ void handle_prop_access(const PropAccessMessage &, const Theron::Address);
void handle_self_kick(const SelfKickMessage &, const Theron::Address);
void handle_get_stats(const GetStatsMessage &, const Theron::Address);
@@ -180,6 +183,10 @@ struct BlockActor : Apology::Worker
std::vector<std::vector<OutputHintMessage> > output_allocation_hints;
+ //property stuff
+ PMCC prop_access_dispatcher(const std::string &key, const PMCC &value, const bool set);
+ std::map<std::string, PropertyRegistrySptr> prop_registry;
+
BlockStats stats;
};
diff --git a/lib/gras_impl/messages.hpp b/lib/gras_impl/messages.hpp
index 6cc685b..15a7c2e 100644
--- a/lib/gras_impl/messages.hpp
+++ b/lib/gras_impl/messages.hpp
@@ -131,6 +131,14 @@ struct OutputUpdateMessage
//-- do not ack
//----------------------------------------------------------------------
+struct PropAccessMessage
+{
+ bool set;
+ std::string key;
+ PMCC value;
+ std::string error;
+};
+
struct SelfKickMessage
{
//empty
@@ -172,6 +180,7 @@ THERON_DECLARE_REGISTERED_MESSAGE(gras::OutputHintMessage);
THERON_DECLARE_REGISTERED_MESSAGE(gras::OutputAllocMessage);
THERON_DECLARE_REGISTERED_MESSAGE(gras::OutputUpdateMessage);
+THERON_DECLARE_REGISTERED_MESSAGE(gras::PropAccessMessage);
THERON_DECLARE_REGISTERED_MESSAGE(gras::SelfKickMessage);
THERON_DECLARE_REGISTERED_MESSAGE(gras::GetStatsMessage);
diff --git a/lib/register_messages.cpp b/lib/register_messages.cpp
index e502e5b..058f308 100644
--- a/lib/register_messages.cpp
+++ b/lib/register_messages.cpp
@@ -26,5 +26,6 @@ THERON_DEFINE_REGISTERED_MESSAGE(gras::OutputHintMessage);
THERON_DEFINE_REGISTERED_MESSAGE(gras::OutputAllocMessage);
THERON_DEFINE_REGISTERED_MESSAGE(gras::OutputUpdateMessage);
+THERON_DEFINE_REGISTERED_MESSAGE(gras::PropAccessMessage);
THERON_DEFINE_REGISTERED_MESSAGE(gras::SelfKickMessage);
THERON_DEFINE_REGISTERED_MESSAGE(gras::GetStatsMessage);