concept message in category c++

appears as: message, messages, messages, message, The messages
Functional Programming in C++

This is an excerpt from Manning's book Functional Programming in C++.

Listing 5.4 Moving around the maze
while (1) {
    - draw the maze and the player
    - read user input
    - if we should move (the user pressed one of the arrow keys)
      check whether the destination cell is a wall,
      and move the player to the destination cell if it isn't
    - check whether we have found the exit,
      show a message if we have,
      and exit the loop
}

Figure 12.1 We can’t set the attributes of real-life objects. We can send them messages and let the objects react to those messages.

c12_01.png
Listing 12.1 Minimal interface for an actor
template <typename SourceMessageType,    #1  
          typename MessageType>    #1  
class actor {
public:
    using value_type = MessageType;    #4  

    void process_message(SourceMessageType&& message);    #2  

    template <typename EmitFunction>
    void set_message_handler(EmitFunction emit);    #3  

private:
    std::function<void(MessageType&&)> m_emit;    #3  
};

#1   An actor can receive messages of one type and send messages of another.
#4   Defines the type of the message the actor is sending, so you can check it later when you need to connect actors
#2   Handles a new message arriving
#3   Sets the m_emit handler the actor calls when it wants to send a message

This interface clearly states that an actor only knows how to receive a message and how to send a message onward. It can have as much private data as it needs to perform its work, but none of it should ever be available to the outside world. Because the data can’t be shared, you have no need for any synchronization on it.

It’s important to note that there can be actors that just receive messages (usually called sinks), actors that just send messages (usually called sources), and general actors that do both.

You’ll first create an actor that listens for incoming network connections, and collect the messages clients send to the service. To make the protocol as simple as possible, messages will be line-based; each message needs to have only one newline character in it, at the end of the message.

This actor is the source actor. It receives messages from the outside world (entities that aren’t part of your service) and is the source of those messages, as far as your service is concerned. The rest of the system doesn’t need to be concerned about where messages come from, so you can consider the client connections to be an integral part of this source actor.

The service (see figure 12.4) will have an interface similar to actor, with the exception that you don’t need the process_message function because this is a source actor. It doesn’t receive messages from other actors in the system (as I said, it gets the messages from external entities—the clients—which aren’t considered actors in this service); the service actor just sends the messages.

Figure 12.4 The service actor listens for client connections and for their messages. This part is hidden from the rest of the program, which just knows a stream of strings is arriving from somewhere.

c12_04.png
Listing 12.2 Service to listen for client connections
class service {
public:
    using value_type = std::string;    #1  

    explicit service(
            boost::asio::io_service& service,
            unsigned short port = 42042)
        : m_acceptor(service,    #2  
            tcp::endpoint(tcp::v4(), port))    #2  
        , m_socket(service)    #2  
    {
    }

    service(const service& other) = delete;    #3  
    service(service&& other) = default;    #3  

    template <typename EmitFunction>
    void set_message_handler(EmitFunction emit)    #4  
    {    #4  
        m_emit = emit;    #4  
        do_accept();    #4  
    }

private:
    void do_accept()
    {
        m_acceptor.async_accept(
            m_socket,
            [this](const error_code& error) {
                if (!error) {
                    make_shared_session(    #5  
                            std::move(m_socket),    #5  
                            m_emit    #5  
                        )->start();    #5  

                } else {
                    std::cerr << error.message() << std::endl;

                }

                // Listening to another client
                do_accept();
            });
    }

    tcp::acceptor m_acceptor;
    tcp::socket m_socket;
    std::function<void(std::string&&)> m_emit;
};

#1   Reads client input line by line, so sent messages are strings
#2   Creates the service that listens at the specified port (by default, 42042)
#3   Disables copying, but allows moves
#4   No point accepting connections from clients until someone registers to listen to messages from message_service.
#5   Creates and starts the session for the incoming client. When the session object reads a message from a client, it’s passed to m_emit. make_shared_session creates a shared pointer to a session object instance.