在C++ Actors Framework中在类型化actor之间转发消息的最佳实践?

Ste*_*lph 5 c++ c++-actor-framework

我正试图将一些作品从一个打字的演员交给另一个演员.CAF用户手册表明可以使用该forward_to方法完成此操作.该方法看起来只适用于明确属于该event_based_actor类型的actor .但是,forward_to似乎是方法的一个薄包装器forward_current_message,它是为该local_actor类型的所有actor定义的.因此,我认为forward_current_message直接打电话是可以的吗?

此外,为了使消息转发与类型的actor一起工作,我仍然必须返回来自中间actor的响应.那个演员的反应似乎被忽略了,这很好,但我做错了吗?或者是否真的有必要支付构建不会被使用的响应的(通常是最小的)成本?

这是一些工作示例代码,演示了我尝试使用类型化actor进行消息转发:

#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();
}
Run Code Online (Sandbox Code Playgroud)

nev*_*ord 5

因此,我认为直接调用forward_current_message是可以的吗?

不,forward_current_message不是CAF中公共API的一部分(因此未在Doxygen中列出).这意味着成员函数可以被重命名,删除,或制成protected/ private随时.

将消息转发给类型化actor的最佳实践是delegate.这是一个新功能(0.14.1引入),遗憾的是手册中没有提到.目前可用的最佳"文档"是它在类型化演员单元测试中的使用.

简短版本是:delegate替代send它转发请求的责任.在类型化的actor中,您可以返回delegated<T>而不是T从消息处理程序返回,以指示其他actor将使用a响应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);
            }
        };
    }
};
Run Code Online (Sandbox Code Playgroud)