Muduo network programming example 9: simple message broadcast service

Source: Internet
Author: User

Muduo network programming example 9: simple message broadcast service

Chen Shuo (giantchen_at_gmail)

Blog.csdn.net/solstice t.sina.com.cn/giantchen

This is the ninth article in The muduo network programming example series.ArticleTo use muduo to implement a simple pub/sub service.

Muduo full article list: http://blog.csdn.net/Solstice/category/779646.aspx

This article introduces how to use muduo to implement a simple topic-based message broadcast service, which is actually a simple extension of "chat room", but chat is not a human, but a distributed systemProgram.

TheCodeSee http://code.google.com/p/muduo/source/browse/trunk/examples/hub

In a distributed system, in addition to common end-to-end communication, there is also one-to-many broadcast communication. When it comes to broadcast, it may be reminiscent of IP multicast or IP multicast. This is not the topic of this article. This article will talk about TCP-based application layer broadcast. As follows:

The rounded rectangle represents a program. "Hub" is a service program, not a network hub. It acts like a hub and hence its name. Publisher and subscriper communicate with the hub program through the TCP protocol. Publisher sends the message to a topic, subscribers subscribes to the topic, and then receives the message. Publisher uses hub to broadcast messages to multiple subscribers. The advantage of this pub/sub structure is that it can add multiple subscriber without having to modify publisher. To a certain extent, it achieves "decoupling" (which can also be seen as a distributed observer pattern ). Because of the TCP protocol, broadcast is basically reliable. Here "reliability" refers to "more reliable than UDP", not "completely reliable ". (THINKING: How to Avoid hub from becoming a single point of failure ?)

To avoid cross-talk, each topic must have only one publisher at a time, and hub does not provide compare-and-swap operations.

(Reliable broadcast and atomic broadcast are of great significance in distributed systems. It is based on the reliable distributed service implemented using the replicated state machine method. "reliable broadcast" involves consensus.Algorithm, Beyond the scope of this article .)

Application Layer broadcast is very useful in Distributed Systems. Here are some examples:

1. sports score broadcast. There are 8 sets of venues in the badminton competition. The scoring program of each venue sends the current score to their respective topics (venue 1st is sent to Court1, venue 2nd is sent to Court2, and so on ). You need to subscribe to topics that you are interested in using the SCORE program (Large Screen Display of the game field, online score broadcasting, etc.) to receive the latest Score data in a timely manner. Because this article does not implement 100% reliable broadcastThe message should be snapshot, not incremental.. (In other words, the message content is "What is the ratio of today", rather than "who scored just now ".)

2. load monitoring. Run a monitoring program on each machine and periodically publish the current load (CPU, network, disk, temperature) of the machine to the topic named hostname, in this way, the program that needs to use the data can obtain data by subscribing to the corresponding topic in the hub, without directly dealing with multiple machines. (For the sake of reliability, the messages sent by the monitoring program should contain timestamps, which can prevent stale data from even playing a heartbeat role to a certain extent .) In this way, the service programs in the distributed system can also publish their current load to the hub for Load balancer and monitor to use.

Protocol

For the sake of simplicity, muduo's hub example uses the text protocol separated by '\ r \ n', so that Telnet can be used to test the hub. The Protocol has only three commands:

    • Sub <topic> \ r \ n

      • This command indicates to subscribe to the <topic>. Any new topic will be sent to this TCP connection in the future. The Hub sends the latest message on the <topic> to the subscriber during sub.
    • Unsub <topic> \ r \ n
      • This command indicates unsubscribing <topic>
    • Pub <topic> \ r \ n <content> \ r \ n
      • Send a message to <topic> with the content <content>. All subscribers that have subscribed to this topic will receive the same message "Pub <topic> \ r \ n <content> \ r \ n"
Code

In the muduo example, the hub consists of the following parts:

    • The Hub service program is responsible for one-to-many message distribution. It remembers the topics subscribed by each client and only sends messages to specific subscribers. Code see http://code.google.com/p/muduo/source/browse/trunk/examples/hub/hub.cc
    • Pubsub library. To facilitate the compilation of applications using the Hub service, I wrote a simple client library to deal with the hub. This library can subscribe to topics, UNSUBSCRIBE topics, and publish messages to specified topics. For the code, see http://code.google.com/p/muduo/source/browse/trunk/examples/hub/pubsub.h and http://code.google.com/p/muduo/source/browse/trunk/examples/hub/pubsub.cc.
    • Sub sample program. The command line program subscribes to one or more topics and waits for Hub data. Code http://code.google.com/p/muduo/source/browse/trunk/examples/hub/sub.cc
    • Pub sample program. This command line program publishes a message to a topic. The message content is specified by the command line parameter. Code http://code.google.com/p/muduo/source/browse/trunk/examples/hub/pub.cc

A program can be both publisher and subscriber, And the pubsub library uses only one TCP connection (this makes failover easier ).

Example:

    1. Open four command line windows
    2. Run $ hub 9999 In the first window
    3. Run $ sub 127.0.0.1: 9999 mytopic In the second window
    4. Run $ sub 127.0.0.1: 9999 mytopic court in the third window
    5. Run $ pub 127.0.0.1: 9999 mytopic "Hello world. "," mytopic: Hello world. ", indicating that the message on the topic" mytopic "is received.
    6. Run $ pub 127.0.0.1: 9999 court "13:11" in the Fourth window. Then, the third window will print "court:", indicating that you have received the message on the subject of court. The second window does not subscribe to this message, so there is no output.

With this simple pub/sub mechanism, you can do a lot of interesting things. For example, change the end-to-end communication of some programs in the distributed system to pub/SUB (for example, a sent a SOAP request to B, B sends response back through the same TCP connection (the communication between the two can only be checked by log or intercepted by tcpdump); now a publishes a request to topic_a_to_ B, and B sends a response to topic_ B _to_a ), in this way, one more monitoring subscriber can easily view the communication between the two parties, and it is easy to perform status monitoring and trouble shooting.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.