Muduo Network Programming Example (ix) Simple message Broadcast service

Source: Internet
Author: User

This article introduces the use of Muduo to implement a simple topic-based message broadcast service, which is actually a "chat room" a simple extension, but chat is not a person, but a distributed system in the program.

The code for this article is shown in Http://code.google.com/p/muduo/source/browse/trunk/examples/hub

In distributed systems, in addition to the commonly used end-to-end communications, there are a one-to-many number of broadcast communications. A reference to "broadcast" may be reminiscent of IP multicast or IP multicast, which is not the subject of this article. This article is going to talk about the application layer broadcast based on TCP protocol. The schematic diagram is as follows:

In the figure above the rounded rectangle represents the program, "Hub" is a service program, not a network hub, it plays a similar role in the hub, so named. 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. That is, Publisher uses the hub to broadcast messages to multiple subscribers. The advantage of this pub/sub structure is that you can add multiple subscriber without modifying Publisher to a certain extent to achieve decoupling (or as distributed observer pattern). Because of the TCP protocol, broadcasting is basically reliable, where the "reliable" refers to "more reliable than UDP," is not "completely reliable." (thinking: How to avoid the Hub becoming single point of failure?) )

In order to avoid crosstalk (cross-talk), each topic at the same time should have only one publisher,hub does not provide compare-and-swap operation.

("Reliable broadcast, Atomic broadcasting" in the distributed system is of great significance, is the replicated state machine way to achieve reliable distributed services, "reliable broadcast" involving consensus algorithm, beyond the scope of this article. )

Application-tier broadcasting is useful in distributed systems, with a few examples:

1. Sports scores broadcast. There are 8 venues playing badminton, and each venue's scoring program sends the current score to their respective topic (venue 1th is sent to Court1, number 2nd is sent to Court2, and so on). Need to use the score of the program (the stadium's big screen display, online score broadcast, etc.) subscribe to the topic of interest, you can receive the latest score data. Since this article does not implement 100% reliable broadcasts, the message should be snapshot rather than incremental. (In other words, the content of the message is "now a few", not "who scored." )

2. Load monitoring. Running a monitor on each machine periodically publish the current load (CPU, network, disk, temperature) of the machine to the hostname-named topic, so that the program that needs to use the data can get the data at the hub by subscribing to the corresponding topic, without having to go straight with multiple machines Contact. (For the sake of reliability, the monitoring program sends a message that should contain a timestamp, which prevents stale data or, to a certain extent, a heartbeat.) Along the way, the service program in the distributed system can also publish its current load to the hub for load balancer and monitor access.

Agreement

For simplicity, the Muduo hub example uses a text protocol with a '/r/n ' demarcation so that the hub can be tested with Telnet. The agreement has only three commands:

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.