Best practice for forwarding messages between typed members in the C ++ Actors Framework?

I am trying to transfer some work from one typed actor to another. The CAF User Guide indicates that this can be done using the method forward_to. This method looks as if it is available only to members who are explicitly of type event_based_actor. However, it forward_toappears to be a thin shell according to the method forward_current_messagethat is defined for all participants of the type local_actor. Therefore, I suppose it's okay to call forward_current_messagedirectly?

In addition, in order to get forwarded messages working with typed participants, I still had to return a response from an intermediate actor. This acting answer seems to be ignored, which is good, but am I doing something wrong? Or is it really necessary to pay the (usually minimal) cost of creating an answer that will not be used?

Here is an example working code example that demonstrates my attempt to redirect messages with typed participants:

#include <iostream>
#include "caf/all.hpp"

using namespace caf;
using namespace std;

using a_type = typed_actor<replies_to<int>::with<bool>>;
using b_type = typed_actor<replies_to<int>::with<bool>>;

actor worker()
{
    return spawn(
        [](event_based_actor *self) -> behavior
        {
            return
            {
                [self](int index)
                {
                    aout(self) << "Worker: " << index << endl;
                    return index;
                }
            };
        });
}

b_type::behavior_type bBehavior(b_type::pointer self)
{
    return
    {
        [self](int value)
        {
            // Create blocking actor
            scoped_actor blockingActor;

            // Spawn pool workers and send each a message
            auto pool = actor_pool::make(value, worker, actor_pool::round_robin());
            for(int i = 0; i < value; ++i)
            {
                blockingActor->send(pool, i);
            }

            // Wait for completion
            vector<int> results;
            int i = 0;
            blockingActor->receive_for(i, value) (
                [&results](int value)
                {
                    results.push_back(value);
                });

            blockingActor->send_exit(pool, exit_reason::user_shutdown);
            self->quit();
            return (value == results.size());
        }
    };
}

class A : public a_type::base
{
protected:
    behavior_type make_behavior() override
    {
        return
        {
            [this](int value) -> bool
            {
                aout(this) << "Number of tasks: " << value << endl;
                b_type forwardDestination = spawn(bBehavior);
                auto castDestination = actor_cast<actor>(forwardDestination);
                this->forward_current_message(castDestination);
                this->quit();
                return false;
            }
        };
    }
};


void tester()
{
    a_type testeeActor = spawn<A>();
    scoped_actor self;
    self->sync_send(testeeActor, 5).await(
        [testeeActor, &self](bool success)
        {
            aout(self) << "All workers completed? " << (success ? "Yes!" : "No :(") << endl;
        });
}

int main()
{
    tester();
    await_all_actors_done();
    shutdown();
    cout << "Press Enter to continue" << endl;
    cin.get();
}
+4
source share
1 answer

So I guess you can directly call forward_current_message?

, forward_current_message API CAF ( Doxygen). , - , protected/private .

delegate. ( 0.14.1) , , . "" unit test .

: delegate send, . delegated<T> T , , T .

A :

class A : public a_type::base
{
protected:
    behavior_type make_behavior() override {
        return {
            [this](int value) {
                aout(this) << "Number of tasks: " << value << endl;
                auto forwardDestination = spawn(bBehavior);
                this->quit();
                return delegate(forwardDestination, value);
            }
        };
    }
};
+4

Source: https://habr.com/ru/post/1618643/


All Articles