Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

libcppa actor that spawns actors on socket connections

I'm looking for example code for a libcppa actor that listens on a port for new connection and then spawn new actors to handle the new connection.

Any help would be appreciated.

Thanks

like image 531
monkey_p Avatar asked Nov 02 '22 20:11

monkey_p


1 Answers

Do you want the actors to read/write directly from/to the socket? A proper actor-based IO abstraction is not implemented yet, but it is a planned feature for the next version of libcppa (stay tuned). If you only want to distribute your actors via the network, take a look at the publish/remote_actor function pair.

/edit:

For now, you can use some of libcppa's utility to get a blocking version up and running:

using namespace cppa;
spawn<detached>([] {
    auto ack = network::ipv4_acceptor::create(4242);
    for (;;) {
        auto spair = ack->accept_connection();
        spawn<detached>([spair] {
            // spair.first is the input stream
            // spair.second is the output stream
            // please see http://neverlord.github.io/libcppa/namespacecppa_1_1network.html
        });
    }
);

This does not scale well, because you will create a thread per connection, and it's not very elegant. Plus, it's hard to multiplex between messages received via the socket and messages received from other actors. A more elegant solution is on its way. ;)

like image 161
neverlord Avatar answered Nov 08 '22 05:11

neverlord