Jiguang SMS is a business of Jiguang Push. The biggest advantage of Jiguang SMS is to re-send SMS messages for users whose push messages have not been delivered, which not only ensures the reach of messages but also saves costs, and the SMS function is required for authentication SMS or other business scenarios. .
The bottom layer of the Jiguang SMS service is the real docking operator, which uses the cmpp protocol to send SMS messages. In the test environment, it is impossible to verify the SMS function in the real environment. The reasons are as follows:
1. The test needs to send a large number of text messages, and the cost will be high
2. It is necessary to simulate various abnormal messages, and real operators have many rules to trigger exceptions
3. It is necessary to quickly return various errors to the operator, and the real operator may be delayed, which is not conducive to testing
4. Various scenarios such as speed limit settings are also required for large amounts of data
Therefore, considering the above 4 points, we finally choose to “create” an operator to process the data sent by the data client to meet the daily SMS service test.
Before the simulation, we need to confirm the underlying protocol, interaction method, and data processing method used by the operator in order to better establish our own “operator”.
1. Understand the cmpp protocol
1.1 Network structure
As shown in Figure 1, the Internet SMS Gateway (ISMG) is an intermediary entity between the external information resource station entity (SP) and the SMS center in the mobile network. On the one hand, the Internet SMS Gateway is responsible for receiving the information sent by SP to mobile users and submitting it to SMS center. On the other hand, the information that the mobile user requests the SP service will be sent to the SP by the short message center through the Internet short message gateway.
1.2 CMPP function overview
The following content comes from the introduction of the cmpp2.0 document
Figure 3 shows an example of a typical service operation of SMS receiving (Short Message Mobile Terminated, SM MT):
Figure 3 Schematic diagram of MT that needs to be forwarded
1. The SP sends a data request (may be a short message notification or a mobile phone ringtone, etc.), which is received by the source ISMG;
2. The source ISMG returns a response to the received information;
3. The source ISMG cannot find the gateway code corresponding to the mobile phone number segment in the local database, and sends routing request information to GNS (tandem gateway);
4. The tandem gateway returns the routing information;
5. The source ISMG forwards the request to the destination ISMG according to the routing information;
6. The destination ISMG returns a response to the received information;
7. The destination ISMG sends the request information to the SMC;
8. SMC returns a response to the destination ISMG;
2. Protocol stack
The CMPP protocol uses TCP/IP as the underlying communication bearer. The specific structure is shown in Figure 2:
Figure 2.1 CMPP protocol stack
ICP and ISMG establish a TCP connection in Clientmdash;Server mode for mutual submission of information between the two parties. After the TCP/IP connection is established, the Client initiates the establishment of the connection at the application layer. At this time, if the ICP or ISMG thinks that a TLS connection needs to be established, the TLS field is set in the transmitted data packet to establish a TLS connection between the two parties.
In the data transmission process after the application layer connection is established, if the ICP or ISMG needs to send encrypted information to the opposite end, a TLS connection can also be established. In this case, only the Tls_available (whether TLS layer is used) attribute field needs to be set in the corresponding message body. Other attributes in the message body of this message are not sent.
Messages are sent concurrently, with sliding window flow control, that is, if the receiver receives more messages than the window size bar before replying, it will be rejected. The flowchart is shown in Figure 3:
2.1. Message Definition
The message definition is the “way of communication” between the client and the server The following is the definition of the content of the cmpp protocol (one of the authentication protocols)
basic data type
Message Header Format (Message Header)
CMPP_CONNECT message definition (SPàISMG)
CMPP_CONNECT message definition
CMPP_CONNECT_RESP message definition (ISMG à SP)
3. Build the server
3.1 Structure Analysis of SMS Test Pile
business process flow. As shown in Figure 3.2
Processing flow description
1. The server receives a request data with a length of 39 bytes. Since the header is fixed at 12 bytes, the remainder is 27 bytes.
2. According to the 27-byte length of the above protocol, it is judged that a connection needs to be established
3. Obtain the user name and password of the client’s connection, and parse the AuthenticatorISMG field to verify whether the authentication is successful.
4. Successfully proceed to the next step (need to respond to the authentication success), process the acquired data according to the established connection, and return the content
We extract its main code with the authentication part: take out the header packet
Get the main content, authenticate the content
3.2 Effect display
The SMS test pile has been running in the test environment for more than 2 years, which has helped the company save a lot of test costs. The following is the content of the effect it shows
Display on iportal, send successful status.
Displayed on iportal, sending failed status.
Displayed on iportal, sent status.
Displayed on Iportal, the status of insufficient balance
3.3 Problems encountered by SMS simulation piles
1. The amount of request data sent has exceeded the window size
When the number sent by the client exceeds the processing size, the server will limit the current, queue the task and return the response code to reduce the pressure on the server
2. The code creates invalid links and wastes resources
When a connection is established, a connection is stored. When the connection is invalid, the connection needs to be deleted. How to determine that the connection is invalid? The heartbeat connection is not established, or after the connection is established, no message is sent within 20 minutes (test environment), and the server is responsible for the broken chain. deal with
3. Language options, and program design
You can choose both java and Python languages. Considering that Python is widely used in the company, choose Python
The class of choice for Python is the struct module, the user parses the byte data, uses gevent to solve the coroutine, or the multi-process provided by Python
4. Message sticky package
When reading data, read the data in the ioArgs buffer in sequence according to the length information of the header. If the length requirement is not met, continue to read the next ioArgs to solve the problem of sticky packets and half packets.
We have gradually improved the test pile. It not only has the function of parsing data, sending reports, keeping heartbeats, and handling different error codes, but also improving the processing speed. It used to process 10 in 1 minute, but now it can process 300 in 1 minute, satisfying our needs. An improvement in the speed of large batches of data, perfecting our testing of various scenarios.