summaryrefslogtreecommitdiff
path: root/lib/block_props.cpp
blob: 172282d0b518a317a9ac75fa2990eeb506c1f6fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (C) by Josh Blum. See LICENSE.txt for licensing information.

#include "element_impl.hpp"
#include <gras/block.hpp>

using namespace gras;

/***********************************************************************
 * The actual thread-safe implementation of property handling
 **********************************************************************/
void BlockActor::handle_callable(
    const CallableMessage &message,
    const Theron::Address from
)
{
    //setup reply
    CallableMessage reply;
    reply.key = message.key;
    reply.args = NULL;

    //call into the handler overload to do the property access
    try
    {
        reply.ret = data->block->_handle_call_ts(message.key, message.args);
    }
    catch (const std::exception &e)
    {
        reply.error = e.what();
    }
    catch (...)
    {
        reply.error = "unknown error";
    }

    //send the reply
    this->Send(reply, from); //ACK

    //work could have been skipped by a high prio msg
    //forcefully kick the task to recheck in a new call
    this->Send(SelfKickMessage(), this->GetAddress());
}

PMCC Block::_handle_call_ts(const std::string &key, const PMCC *args)
{
    //got here, so we call the base class unless this got overloaded
    return Callable::_handle_call(key, args);
}

/***********************************************************************
 * A special receiver to handle the property access result
 **********************************************************************/
struct CallableReceiver : Theron::Receiver
{
    CallableReceiver(void)
    {
        this->RegisterHandler(this, &CallableReceiver::handle_callable);
    }

    void handle_callable(const CallableMessage &msg, const Theron::Address)
    {
        this->message = msg;
    }

    CallableMessage message;
};

/***********************************************************************
 * Handle the get and set calls from the user's call-stack
 **********************************************************************/
PMCC Block::_handle_call(const std::string &key, const PMCC *args)
{
    CallableReceiver receiver;
    CallableMessage message;
    boost::shared_ptr<BlockActor> actor = (*this)->block_actor;
    message.prio_token = actor->prio_token;
    message.key = key;
    message.args = (PMCC *)args;
    actor->GetFramework().Send(message, receiver.GetAddress(), actor->GetAddress());
    receiver.Wait();
    if (not receiver.message.error.empty())
    {
        throw std::runtime_error(receiver.message.error);
    }
    return receiver.message.ret;
}