Default handler for reply

61 views
Skip to first unread message

Eric Pederson

unread,
Dec 20, 2016, 9:07:29 PM12/20/16
to actor-framework
Dear all:

I need to send a message and get back a response that I won't know the type of.

If I use send like in the below, the default handler is never triggered.  

I've also tried to use request().then(others >> [](message_view &view) { /* handle */ } but others doesn't appear to work for then().


system.spawn([&](caf::event_based_actor *self) {
    self->set_default_handler([=](caf::scheduled_actor *, caf::message_view &view) -> caf::result<caf::message> {
        std::cout << "DEFAULT: " << view.content().stringify() << std::endl;
        self->quit();
        return caf::message(caf::none);
    });

    self->send(actorPool.actor(), something);
});

Is there a way to do what I'm trying to do?   I can't change the target actor which handles something

Eric Pederson

unread,
Dec 20, 2016, 9:45:06 PM12/20/16
to actor-framework
I figured it out.  I forgot to to return a behavior.

system.spawn([&](caf::event_based_actor *self) {
    self->set_default_handler([=](caf::scheduled_actor *, caf::message_view &view) -> caf::result<caf::message> {
        std::cout << "DEFAULT: " << view.content().stringify() << std::endl;
        self->quit();
        return caf::message(caf::none);
    });

    self->send(actorPool.actor(), something);

    return caf::behavior{ []{} };
});

It would be great if there were some way to fail at compile time if you neglect to return a behavior from a spawn function.

Dominik Charousset

unread,
Dec 22, 2016, 6:05:02 AM12/22/16
to actor-f...@googlegroups.com
Hi,

 would be great if there were some way to fail at compile time if you neglect to return a behavior from a spawn function.
there are many use cases for actors that perform a given task and then terminate (simply by not returning a behavior). Performing all work in the default handler without setting an actual behavior is a niche use case in comparison. However, returning a dummy behavior is not an elegant solution. Maybe CAF could keep an actor alive if the default handler was changed. I'm not sure from the top of my head if that has implications in CAF, but it's probably more intuitive for developers.

    Dominik


On Tuesday, December 20, 2016 at 9:07:29 PM UTC-5, Eric Pederson wrote:
Dear all:

I need to send a message and get back a response that I won't know the type of.

If I use send like in the below, the default handler is never triggered.  

I've also tried to use request().then(others >> [](message_view &view) { /* handle */ } but others doesn't appear to work for then().


system.spawn([&](caf::event_based_actor *self) {
    self->set_default_handler([=](caf::scheduled_actor *, caf::message_view &view) -> caf::result<caf::message> {
        std::cout << "DEFAULT: " << view.content().stringify() << std::endl;
        self->quit();
        return caf::message(caf::none);
    });

    self->send(actorPool.actor(), something);
});

Is there a way to do what I'm trying to do?   I can't change the target actor which handles something

--
You received this message because you are subscribed to the Google Groups "actor-framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to actor-framewo...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Eric Pederson

unread,
Dec 22, 2016, 10:05:41 AM12/22/16
to actor-f...@googlegroups.com
Ah, I didn't think of spawning a behavior-less actor to do some one-time processing.  I'm so used to using other mechanisms for that (like std::async).  But that makes sense.  In our use case we're doing a lot of routing of messages of unknown type (well, we know the base type) so we use the default handler a lot in the middle of the system, and at the ends where we are bridging from other transports like ZeroMQ.

Thanks!


-- Eric

To unsubscribe from this group and stop receiving emails from it, send an email to actor-framework+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "actor-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/actor-framework/QOiGYAV_bY8/unsubscribe.
To unsubscribe from this group and all its topics, send an email to actor-framework+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages