DICOM: Profiling the "sequence of events triggered by connection requests" in Web Server,mongoose in Orthanc (ii)

Source: Internet
Author: User
Tags sqlite database

background:

Orthanc is a new DICOM server introduced in this column, with a lightweight, rest-enabled feature that turns any computer running Windows and Linux systems into a dicom server, or Minipacs. Orthanc embedded in a variety of modules, database management is simple, and does not rely on third-party software. So through the analysis of Orthanc source code can learn to build a dicom system in various aspects, such as SQLite embedded database, googlelog Log library, DCMTK Medical Dicom Library, as well as the recent open source Web Server,mongoose to introduce.

Preface:

Recent plans to refer to the official website Analysis Mongoose source code at the same time to illustrate. Open source software is one of the main advantage is the disclosure of sources, through the analysis of the source code can learn its excellent design architecture and implementation mode, but for the code tens of thousands of lines, or even a hundred thousand of lines of open-source software, simply delve into the source code may be trapped in it and cannot extricate themselves. Often in the exclamation of the author's ingenious design, lost in the direction of the root of all kinds of logic, lost confidence in the end, in the regret of their limited ability, but also give up efforts. Many good architectures and patterns are not necessarily designed, at least not "in advance" design . Any open source projects are from scratch, from the start to maturity, the first to read the mature source, enjoy the other people's thought fruit will naturally "no blessing." Want to figure out why this data structure is designed? Why is this run logic designed? -- from the context, consider the origin and application of the project, and remember that any design is service to the application . So follow-up blog will use the source code Analysis + examples to explain the way to analyze and learn, or that sentence " see the Source tired debugging debugging instance, debugging tired to delve into the source code", labor (source) Yi (example) combination, down-to-earth, Keep Running ...

HTTP Request:

Today's internet and mobile internet has been known, usually we use the Internet is the most used HTTP request, that is, from the client (PC, mobile phone) to the server request message. As for the specific format of the HTTP request is not introduced, since it is the protocol is used to contract the rules of mutual interaction, let the server know what the client wants to do, and let the client understand what the server gave.

HTTP is a TCP/IP-based application layer protocol, so a TCP connection is first established before the server and client exchange information. There are many ways to establish a connection, such as creating a new connection for each request, a different part of the page requesting a different connection, and so on. Borrowing a blog post tornado an HTTP flowchart in the basic process of an HTTP server, most of the HTTP service side of the process is basically similar, mongoose is the same, as shown in:

Mongoose and fossa:

The previous column describes Mongoose as a small, embeddable http Server. As long as you introduce mongoose.h and mongosoe.c source files in your project, you can quickly open an HTTP Server. Mongoose supports rest and JSON-RPC services ( This is also the root cause of my intention to analyze mongoose, and I hope to embed mongoose into traditional PACs systems in the same orthanc, and distribute the traditional PACs systems ).

Open the Mongoose Warehouse on GitHub and you will find that mongoose is built on the NS skeleton frame, and the NS skeleton is fossa. The official description of fossa is:fossa is a C language to support the multi-Protocol network programming library. The use of event-driven mode, user-friendly implementation of various network protocols, the development of extensible network applications.

Up to now we can think of fossa is the bottom network development library, Mongoose is a package for fossa, enhanced event-driven , convenient for users to get started quickly, and Orthanc is the mongoose is once again packaged, enhanced Rest functionality and provides interfaces related to the SQLite database and the DICOM protocol .

Mongoose and fossa events:

As described above, fossa is event-driven and mongoose is the encapsulation of fossa, so the same event-driven approach is used. However, the events are slightly different, as shown in the following table:

Fossa Mongoose

Ns_connect

Mg_connect

Ns_recv

Mg_request

Ns_send

Mg_poll

Ns_poll

Mg_reply

Ns_accept

Mg_close

Ns_close

Mg_http_error

The Fossa website provides a more detailed description of the events in the HTTP request, such as the following table. May be mongoose on the fossa package is not a small change in the reason, in the Mongoose official website did not find a detailed description of the Mg_xxx event, the document API.MD only describes the use of the various events.

Ns_accept: Triggering the Ns_accept event when the listening port receives a new request connection
Ns_connect: Triggers a ns_connect event whenever a new connection is created using Ns_connect, regardless of whether the new join creation fails or succeeds
NS_RECV: Triggers the NS_RECV event when new data is received and written to the receive buffer
Ns_send: Trigger Ns_send event when data is written to a remote node and purged from the output buffer
The ns_poll:ns_poll is sent to each connection in the Ns_mgr_poll connection list for cleanup (housekeeping), such as checking for timeouts, sending ECG packets, and so on.
Ns_close: Triggered when the connection is disconnected. "Note": The specific Ns_close event is not given in the official documentation, followed by multiple sets of instance testing to detail the event trigger conditions.
Example Explanation:Example test:

After a brief introduction of the theory, the process of the HTTP request has been roughly understood.         First, you need to bind the port (the socket operation under Windows), set up a TCP connection, and then handle the different events accordingly. To provide a more intuitive understanding of the specific process of HTTP requests in Mongoose, we add ns_endalbe_debug predefined in VS, based on the example in the installation instructions document EMBBED.MD. Open the Mongoose source code in the original debugging information; In addition, modify the Mg_ev_handler function in the MONGOOSE.C, add debug statements for each event of fossa, and output the current event type.

1) Rebuild the mongooseembbed project, open the debugging State;

2) Open Google Chrome, enter: http://localhost:8080 , the debug results are as follows:


By default Mongoose is the same as other HTTP servers, and the files in the current directory are displayed by default. and added a hyperlink. On the right is the index of all the files in the home directory where the Mongooseembbed project is located, and a hyperlink to each index is given.

Mongoose Event Trigger process debug:

The information from the console output shows that the program is in a for loop within the main function and loops through the debug information before starting the browser before initiating any request;


When http://localhost:8080 is entered in the browser address bar, the Mg_poll_server function outputs the Ns_accept debug information in the next for loop, indicating that the Ns_accept event is triggered;


In the next loop, debugging information of two kinds of events of Ns_poll and NS_RECV are also output.


Then the Ns_poll, Ns_send, ns_close three kinds of events debugging information are also output.


Finally enter the polling state of the Ns_poll,



Judging from the current debug information, Mongoose's basic process for browser-initiated connection request processing follows:ns_accept->ns_recv->ns_send->ns_poll->ns_close. This is basically the same as the Fossa official note, but there are multiple ns_poll and ns_accept throughout the process, what is the reason? To figure out the whole problem, you need to have a detailed understanding of the overall response process of the HTTP WEB server and need to analyze the specific processes in the Ns_poll_server source code. At present, this part of the train of thought has not been completed, intends to put in the next blog post, please pay attention.




[email protected]

time: 2015-02-07

DICOM: Anatomy of a Web Server,mongoose in Orthanc "event sequence triggered by a connection request" (ii)

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.