A Sudoku server evolution of Muduo multithreaded model

Source: Internet
Author: User
Tags port number

Taking a Sudoku solver as an example, this paper reviews a variety of design schemes of concurrent network service programs, and introduces two common techniques for writing multithreaded servers using Muduo network library. The previous examples show Muduo's ability and convenience in writing single-threaded concurrent Web service programs, and today we look at its performance in multithreading.

See code for this article: http://code.google.com/p/muduo/source/browse/trunk/examples/sudoku/

Download: http://muduo.googlecode.com/files/muduo-0.2.5-alpha.tar.gz

Sudoku Solver

Suppose there is such a network programming task: Write a Sudoku program (Sudoku solver) and make it into a network service.

Sudoku Solver is my favorite example of network programming, it has appeared in the "Distributed system deployment, monitoring and process management," a few areas, "Muduo design and implementation of one: Buffer class Design", "Multi-threaded server application for example interpretation and answer questions" and so on, it can also be seen as A variant of echo service ("Talking about network programming learning experience", the Echo is listed as one of the three major TCP network programming cases).

Write such a program in the network programming is not very difficult, and write echo service almost (from the network connection read a Sudoku topic, calculate the answer, and then send back to the customer), the challenge is how to do to play the current multi-core hardware capabilities? Before we talk about this, let's write a basic single-threaded version first.

Agreement

A simple,/r/n-delimited line of text protocol that uses a TCP long connection to actively disconnect clients when no service is required.

Request: [id:]〈81digits〉/r/n

Response: [id:]〈81digits〉/r/n or [ID:] nosolution/r/n

Where [ID:] Represents an optional ID, which is used to differentiate between successive requests to support Parallel pipelining, and the ID in the request is echoed back in the response. The significance of Parallel pipelining is seen in Rai Yonghao's small--protobuf-wide variety of RPC (2), or the introduction to Out-of-order RPC on page 54th of the "Engineering development method for distributed Systems", as I wrote.

〈81digits〉 is Sudoku's chessboard, 9x9 number, unknown number in 0.

If the Sudoku has a solution, then the response is a chessboard filled with numbers, and if there is no solution, return nosolution.

Example 1:

Request: 000000010400000000020000000000050407008000300001090000300400200050100000000806000/r/n

< p> response: 693784512487512936125963874932651487568247391741398625319475268856129743274836159/r/n

< p> Example 2:

Request: a:000000010400000000020000000000050407008000300001090000300400200050100000000806000/r/n

Response: a:693784512487512936125963874932651487568247391741398625319475268856129743274836159/r/n

Example 3:

Request: b:000000010400000000020000000000050407008000300001090000300400200050100000000806005/r/n

Response: b:nosolution/r/n

Based on this text protocol, we can use Telnet simulation client to test Sudoku Solver, do not need to write Sudoku client separately. The default port number for SudokuSolver is 9981 because it has 9x9=81 lattices.

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.