"Article Summary"
Simple Object Access Protocol (SOAP) is a protocol specification for exchanging data and is a lightweight, simple, XML-based protocol that is designed to exchange structured and solidified information on the web.
In the actual software development projects, SOAP is mostly used to process user data, that is, to realize the functions of opening accounts, selling accounts, changing households and checking households.
In this paper, the SOAP protocol and SOAP-based development process are described in detail according to the SOAP-based project development that the author has actually worked on. I hope you can have a comprehensive understanding of the development of soap-based protocols through this article.
Introduction to the SOAPUI and SOAP code generation framework
"工欲善其事, its prerequisite", in order to develop based on the SOAP protocol, we need to prepare the relevant tool software beforehand. When I first started to develop, and did not take into account the importance of the tool, and so on after the code to find no self-test tools, this time only rushed to find colleagues to the relevant software to install. This has affected the efficiency of the work. Therefore, "The horses are not moved, the fodder first", the code is not written, the tool should be installed.
Here are two software related to soap: SOAPUI and the SOAP code generation framework.
1.soapUI
SOAPUI is a SOAP-based simulation test tool that simulates a Web client sending a message to a SOAP message processing module (the module we are developing). We can use this software to perform unit and integration tests on the programs we write.
Figure 1 is the SOAPUI software interface.
Figure 1 SOAPUI's software interface
2.SOAP code Generation framework
At the time of soap-based development, we will use many of the functions that have been encapsulated by soap. To avoid duplication of effort, the designer of soap provided us with a framework of auto-generated code called gSOAP. As soon as we have the WSDL file (described later) ready, we can generate the relevant function files that we need to use in the development process with two simple names.
This framework can be used to generate code regardless of which platform (Windows, Linux, or Mac OS) The program is developed under. The auto-generated code is shown in tool layout 2.
Figure 2 tool layout for automatically generated code
We can generate a code framework by putting the corresponding WSDL file into the corresponding folder.
second, the software module architecture based on SOAP development
In general, the sending of SOAP messages is an external module that deals directly with the user, which needs to be transformed into an internal message before it can be used to perform specific processing. Therefore, the SOAP-based development of the software module architecture is shown in 3.
Figure 3 software module architecture based on SOAP development
As can be seen from Figure 3, the system generally does not directly use the SOAP message to execute the specific business logic, but instead uses an intermediate module to transform the SOAP message into a contract protocol within the system, and then the specific business execution module performs the specific operation according to the internal protocol.
The benefits of doing this are:
First, separating the message transformation from the business Execution facilitates program implementation and troubleshooting.
Second, the security of the system can be higher without exposing the specific execution process of the business.
Thirdly, if the external SOAP protocol changes frequently, only the intermediate SOAP protocol conversion module needs to be modified, so that the execution of the normal business is not affected by the frequent changes of the specific business processing modules.
third, the specific development process based on soap
After all the preparation is done, we are going to write the program next. The following to achieve user data management (ie: adding and deleting) as an example, the entire development process.
1. Writing a WSDL file
The WSDL file, which is also the SOAP protocol file, is used to contract which specific actions to perform. For example, we need to carry out an account opening operation in the system, including the user's phone number, user name and password. So, we can write the following file named "ISOAPBOSS.WSDL":
<?xml version= "1.0" encoding= "UTF-8"?> <wsdl:definitions targetnamespace="Soap" Xmlns:tns="Soap" Xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" XMLNS:SOAP12="Http://www.w3.org/2003/05/soap-envelope" xmlns:ns1="Http://mail.dto.op.web.ngmail.zte.com" xmlns:xsd="Http://www.w3.org/2001/XMLSchema" Xmlns:soapenc11="http://schemas.xmlsoap.org/soap/encoding/" XMLNS:SOAPENC12="Http://www.w3.org/2003/05/soap-encoding" XMLNS:SOAP11="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"> <wsdl:types> <xsd:schema attributeformdefault= "qualified" elementformdefault=" Qualified " targetnamespace=" Soap " xmlns:xsd=" http://www.w3.org/2001/ XmlSchema "> <xsd:element name="CREATEUSERREQT"> <xsd:complextype> <xsd:sequence> <xsd:element maxOccurs = "1" minoccurs = "1" name = "Userphone" nillable = "true" type = "xsd:string" /> <xsd:element maxOccurs = "1" minoccurs = "0" name = "UserName" nillable = "true" type = "xsd:string" /> <xsd:element maxOccurs="1" minOccurs="0" name=" Password "nillable=" true " type=" xsd:string "/> </xsd:sequence> </xsd:complextype> </xsd:element> <xsd:element name="Createuserresponse"> <xsd:complextype> <xsd:sequence> <xsd:element maxOccurs="1" minOccurs="1" name= "ResultCode" type="Xsd:int"/> <xsd:element maxOccurs="1" minOccurs="1" name=" Description "type=" xsd:string"/> </xsd:sequence> </xsd:complextype> </xsd:element> </Xsd:schema> </wsdl:types> <wsdl:message name="Createuserrequest"> <wsdl:part name="parameters" element="TNS:CREATEUSERREQT"/ > </wsdl:message> <wsdl:message name="Createuserresponse"> <wsdl:part name="parameters" element="Tns:createuserresponse" /> </wsdl:message> <wsdl:porttype name="Isoapbossporttype"> <wsdl:operation name="CreateUser"> <wsdl:input name="createuserrequest" message="TNS: Createuserrequest "/> <wsdl:output name="Createuserresponse" message="TNS: Createuserresponse "/> </wsdl:operation> </wsdl:porttype> <wsdl:binding name="isoapbosshttpbinding" type="tns:i Soapbossporttype "> <wsdlsoap:binding style="Document" transport="http// Schemas.xmlsoap.org/soap/http "/> <wsdl:operation name="CreateUser"> <wsdlsoap:operation soapaction=""/> <wsdl:input name="Createuserrequest"> <wsdlsoap:body use ="literal"/> </wsdl:input> <wsdl:output name="Createuserresponse"> <wsdlsoap:body use ="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="Isoapboss"> <wsdl:port name= "isoapbosshttpport" binding="tns:i Soapbosshttpbinding "> <wsdlsoap:address location ="Http://10.10.10.10:9999/soapboss/Isoapboss" /> </wsdl:port> </wsdl:service> </wsdl:definitions>
For the above document content, the explanation is as follows:
First, the message fields to be included when opening an account include Userphone, username, and password, where Userphone is required (both maxoccurs and minoccurs have values of 1), and username and password are optional fields. Response messages include ResultCode (result code) and description (Result description) fields.
Second, "10.10.10.10" and "9999" in the final "Http://10.10.10.10:9999/soapboss/Isoapboss" of the file represent the IP address of the machine where the SOAP protocol conversion module is located in Figure 3 and the port number that the module is bound to. These two values can be filled in the configuration file of the SOAP protocol transformation module.
2. Generating a SOAP code framework with a WSDL file
Because we are programs written under Windows, put the "isoapboss.wsdl" file that was written in the previous step in the "Win32" folder in Figure 2, as shown in 4.
Figure 4 Location of WSDL file placement
As you can see from Figure 4, in addition to the "isoapboss.wsdl" file, there are also "Soapcpp2.exe" and "Wsdl2h.exe" two files under this folder, and these two files are used to generate a code framework.
In the cmd window, execute the "wsdl2h.exe-c isoapboss.wsdl" and the "Soapcpp2.exe Isoapboss.h" command sequentially, as shown in 5.
Figure 5 Generating command execution for the code framework
After executing the command, the relevant code file is generated under the "Win32" folder, as shown in 6.
Figure 6 File storage after code file generation
We just need to copy the ". C" and ". h" files in Figure 6 to our code project.
3. Writing the specific SOAP message processing code
The SOAP protocol transformation module in Figure 3 is used to receive, parse, and then convert a SOAP message into an internally contracted message structure.
The main features of this module are three: Receive SOAP messages, process SOAP messages, parse SOAP messages, and assemble them. The following are explained separately.
(1) Receiving SOAP messages
The pseudo-code for this process is as follows:
soap_init(&soap); // 初始化soapm = soap_bind(&soap, IpAdr, Port, BACKLOG); // 绑定IP和端口号while (!soap_valid_socket(m)) // 循环直至服务套接字合法{ m = soap_bind(&soap, IpAdr, Port, BACKLOG);}……for (;;) // 用一个死循环来不停地接收SOAP消息{ s = soap_accept(&soap); // 接收到SOAP消息 if (!soap_valid_socket(s)) // 对错误消息的处理 { …… } // 将接收到的SOAP消息放入队列供处理线程处理 EnQueue(data); }
(2) Handling SOAP messages
The pseudo-code for this process is as follows:
for (;;) // 用一个死循环来不停地处理SOAP消息{ DeQueue(&data); // 将SOAP消息从队列中取出来处理 …… soap_set_namespaces(tsoap, namespaces); // 设置命名空间 soap_serve(tsoap); // 具体处理SOAP消息的函数 soap_destroy(tsoap); // 处理完成之后,销毁该SOAP消息 soap_end(tsoap); // 结束本次处理}
(3) Parsing SOAP messages and assembling them
This step requires us to implement the __ns1__createuser function in the SoapStub.h file.
This process is different from the specific business and is no longer specified here.
The functions used in the above three processes are the encapsulated functions in the SOAP code framework that we generated using the WSDL file.
4. Test the code you have written
By convention, when the code is well written, we have to test it rigorously.
For the testing of SOAP messages, our first step was to create a new project in the SOAPUI software and import the WSDL file into it. For example, we set up a project called Testsoap, shown in 7.
Figure 7 New Testsoap Project
We fill in the specific values for Userphone, username, and password, and then click the button that sends the message to begin a formal test of the entire program. The test executes after the software interface 8 is shown.
Figure 8 The software interface after the test execution
As we can see from Figure 8, the account opening process has been successfully executed.
To fully test the program, you can set up multiple sets of test cases.
The above is the overall development process based on the SOAP protocol, the specific programming details depend on the actual situation of the project that everyone participates in, but the basic process is consistent.
Iv. Summary
In this paper, based on the author's actual development experience, the SOAP protocol and SOAP-based development process are described in detail for the relevant development of friends to reference.
Throughout the development process, we should pay attention to the following points:
First, it is important to write WSDL files, which is equivalent to the protocol between the internal system and the user. Before writing the code, we must make sure that the agreement is clear, so as to avoid the inconvenience caused by the late modification.
Second, when testing the code, it is important to read the logs generated by the program and related data in the database to ensure that each process is executed correctly.
Third, be sure to agree with the user after the execution of the relevant result code (that is, the value of ResultCode), which values indicate the success of the execution, which values indicate that the execution failed, all should be considered well. This also makes it easy to find program problems.
Welcome to the attention and support of my new book "C Programmer from the campus to the workplace."
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
SOAP protocol and SOAP-based development process details