“At present, the street lamp monitoring system in many cities across the country is restricted by regions and still stays in a small-scale monitoring mode, which makes the monitoring standards in various regions inconsistent and chaotic in management. At the same time, it also takes up a lot of human and material resources. Therefore, the unified management of street lamp monitoring systems in various regions to form a large-scale unified monitoring system has become the trend of future street lamp monitoring development.The traditional SOCKET communication model has a limit on the number of clients. When the actual client exceeds the limit, data will be blocked and lost, and even the server software will crash. The communication model of the completion port technology is introduced.
At present, the street lamp monitoring system in many cities across the country is restricted by regions and still stays in a small-scale monitoring mode, which makes the monitoring standards in various regions inconsistent and chaotic in management. At the same time, it also takes up a lot of human and material resources. Therefore, the unified management of street lamp monitoring systems in various regions to form a large-scale unified monitoring system has become the trend of future street lamp monitoring development. The traditional SOCKET communication model has a limit on the number of clients. When the actual client exceeds the limit, there will be data blockage and loss, and even server software crashes. The communication model that introduces the completion port technology does not have the number of clients. It is limited, and has efficient data processing capabilities, which can give full play to its advantages in large-scale street lamp monitoring systems, ensuring the efficiency and reliability of data transmission.
Under the Visual C++++ 2008 programming environment, by completing the application of port technology, the original C/S-based street lamp monitoring system software is optimized, so that the entire system can be applied to a large number of client occasions, and the communication system can still be maintained Higher stability.
2. The overall structure of the monitoring system software
The street lamp monitoring system is divided into two parts: remote terminal equipment and monitoring software. The remote terminal equipment is installed at the street lamp control site and is the main hardware equipment that realizes the monitoring function. The remote terminal is connected to the server through the GPRS wireless communication network, and according to the user’s setting parameters, it realizes functions such as timing switch lights, data collection and accident alarm.
Depending on the situation in different regions, the number may be very large, and the amount of data transmitted to the server will also be very large. The monitoring software is a set of network communication software based on the Client/Server mode under the Visual C + + 2008 development platform. It consists of two parts: server software and client software. The back-end database uses MS SQLServer 2005. The structure diagram of the monitoring system is shown in Figure 1.
The server of the monitoring software is installed and working on the server, and is responsible for receiving the data transmitted from the monitoring terminal device, analyzing the data, and storing it in the database; at the same time, it communicates with the client of the software and transfers the instruction data of the software client. Forward to the corresponding monitoring terminal equipment to manage and control the monitored objects.
The client of the monitoring software works on the user’s computer and is connected to the server and database through the network to provide services for a few specific street lamp monitoring administrators. The client provides a full-featured graphical interface for these administrator users. The user can query data and send control instructions through the client, or understand the working status of each remote terminal in real time through the client’s Electronic map function and cabinet monitoring animation.
3. The server completes the realization of the port communication model
3. 1 Completion port principle
3. 1. 1 Complete port introduction
The network communication module is the core part of the entire system. Because it is responsible for large-scale data transmission and processing, it poses a challenge to the efficiency of software performance, and the application of port communication technology solves this problem.
The completion port (I/O Completion Port) is a core object of the Windows NT execution subsystem. By associating the completion port with any I/O handle (file or socket, etc.), the user can asynchronously obtain and process the I/O result through the completion port.
The completion port is directly supported by the system to provide parallel optimization. Several parallel service threads are established on the completion port, generally the number of which is the number of CPUs, and they provide services for service requests that arrive at the completion port. When a service request arrives, if there is a service thread available, the thread is activated. If there is no service thread available, the service request is added to the request queue. The queue uses a first-in-first-out (FIFO) strategy to ensure that these requests are received Fair service. The establishment of service threads and the FIFO strategy of request queues reduce the number of CPU switching between different threads and reduce the overhead caused by thread context switching.
3. 1. 2 Overlapping I/O
The design principle of the completion port is to allow applications to use overlapping data structures to deliver one or more I/O requests at a time. When these requests are completed, the application can provide services for them. This requires us to use overlapped I/O when using the completion port.
Overlapping I/O, that is, when the I/O function is called, regardless of whether the I/O is completed, the function returns immediately, and the bottom layer of the operating system handles the actual work of the I/O, and the application (process) can continue to do other things. Therefore, the completion port is an efficient mechanism for handling the completion of overlapped I/O.
3. 1. 3 worker thread
In addition to the service thread working on the completion port, before associating the socket, one or more worker threads must be created to provide services for the completion port after the I/O request is delivered to the completion port object. The number of worker threads depends on the overall design of the application. The created worker thread is managed by the completion port. When an I/O completion notification arrives, the completion port wakes up a worker thread to receive the I/O completion notification and process it. The completion port automatically schedules the worker threads, and the completion port determines which worker thread to wake up. If there is no I/O completion notification, all worker threads are waiting. According to experience, the number of worker threads is generally twice the number of CPUs plus 2.
3. 2 Complete the program realization of the port
The network communication module creates the completion port object through the CreateIoCompletionPort function, associates the received SOCKET object with the completion port, starts a certain number of working threads, obtains the current status of the SOCKET on the completion port through the GetQueuedCompletionStatus function, and stores the received data from the cache Take out. The main work flow chart of the completion port is shown in Figure 2.
1) When the program starts, initialize the network and create the port handle:
CompletionPort = CreateIoCompletionPort (INVALID_ HANDLE_ VALUE, NULL, 0, 0);
2) Start 2* N + 2 worker threads, where N is the number of CPUs:
3) Enter a *loop, start* client connection request;
4) Bind the received client SOCKET with the completion port object;
5) Send an asynchronous WSARecv or WSASend operation, the actual operation of receiving and sending data will be completed by the operating system.
6) Repeat the operations from 3) to 5) above.
1) Enter the loop and get the operation result of WSASend /WSARecv from the completion port through the GetQueuedCompletionStatus function:
2) Perform data processing according to the I/O status on the completion port;
3) Submit a new WSASend /WSARecv operation request;
4) Repeat the operations from 1) to 4) above.
3. 3 Communication protocol design
The entire monitoring system uses TCP (Transmission Control Protocol, Transmission Control Protocol) for data transmission. Based on this, a set of monitoring system protocols are designed to complete the communication between the server and the remote terminal, and between the server and the client. According to the actual needs of street lamp monitoring, the data message includes the following forms.
1) The connection authentication data message that the remote terminal actively sends to the software server is shown in Table 1.
2) The field data messages sent by the remote terminal to the software server regularly include the current, voltage, temperature, switch status, alarm information and other data information collected by the street lamp monitoring site, as shown in Table 2.
3) The parameter setting message sent by the software client to the server and forwarded by the server to the corresponding remote terminal. According to different function numbers, the message sends different parameter information, including lamp switching time, alarm threshold, and data The acquisition cycle is shown in Table 3.
3. 4 Complete the optimization of port communication
3. 4. 1 The design of the memory pool
The completion port model adopts the asynchronous communication mode. Each time the WSASend and WSARecv functions are called, a structure space must be created in the memory. After the function is called, the structure space is destroyed. Frequent creation and destruction of memory space takes up a lot of system resources. Therefore, when designing and completing the port program, create a certain amount of structure space according to requirements and put it into a unified free queue. When calling the WSASend and WSARecv functions When, take a structure space from the queue, use it, and then put it back into the queue.
3. 4. 2 Design of connection pool
When receiving the client with the traditional accept function, the accept function will create a socket as the return value and assign it to the client.
When the client disconnects, the created socket will be destroyed. The process of creating and destroying sockets consumes a lot of system resources. Therefore, when receiving a client, the acceptEx function is used instead of accept. This function can assign a pre-created socket object to the receiving client. First, create a certain number of socket objects to form a connection pool. When a client connection request is received, the idle socket object is taken out of the connection pool and assigned to the client. When the connection is disconnected, the socket is put back into the connection. Pool queue. The design of the connection pool reduces the constant creation and destruction of the client SOCKET, saving a lot of system resources.
3. 4. 3 The design of thread pool
The completion port itself applies thread pool technology. The threads in the thread pool not only include worker threads, but also service threads on the completion port of the work. Effective management of these threads can reduce frequent CPU switching between different threads and reduce the time spent switching thread contexts.
3. 4. 4 Data pool design
The data received by the completion port module must be processed and analyzed according to the communication protocol and stored in the corresponding database.
Because the data transmission of the completion port network communication is always uneven, it often happens that a large amount of data is received in a short period of time, and only a small amount of data is received in another period of time. In order to prevent the server from overloading in a short period of time, causing accidental data loss or program crashes, during data processing, a data storage queue is established in advance to form a data pool, and unprocessed data is added to the queue. The FIFO strategy is used to allocate CPU time, which makes full use of CPU resources and improves the safety and reliability of data processing.
4 Client software design
The client software is connected to the server through the general SOCKET communication method, and its main function is to provide users with a simple and convenient user interface. The map Display module intuitively displays the city map and the distribution map of the street light system to the user by drawing the GIS electronic map, so that the user can roughly understand the operating status of the entire street light system. The animation Display module uses FLASH programming technology to show the schematic diagram of the power distribution cabinet controlled by a single remote terminal to the user. The user can understand the real-time data on the spot and set specific monitoring points, switch lights and other operations. The data display module is connected to the database, and the user can query the historical data of each monitoring point and the current setting parameters, and understand the specific working status of the street light system. The main interface of the software client is shown in Figure 3.
5. Complete the performance test of the port server software
5.1 Test object
Compared with the traditional communication model, the completion port communication model has a larger data throughput and the number of clients. Through the design and application of thread pool, connection pool, and memory pool, system resources are saved and the data processing of server software is improved. efficient. In the performance test and comparison of the traditional communication model and the completion port communication model, two important indicators, the hungry client and the number of thread context switches per second, are selected as the test objects. Hungry clients are defined as the number of clients that are not affected by the server among the clients that apply for connection and send data to the server at the same time.
5.2 Test environment
Choose two Intel Core2 1. 9GHz dual-core CPUs, 2G memory desktop computers, one as a server computer and one as a client computer. Install the old street light monitoring software of the traditional communication model and the new version of the street light monitoring software that completes the port model on the server computer, and add test code to the software program to calculate the number of hungry clients and the number of thread context switching; on the client computer Use the test software to simulate a certain number of terminal equipment clients, and simultaneously connect and send data to the server.
5.3 Test results and analysis
Constantly change the number of simulated clients, test the two communication models, respectively record the number of hungry clients and the number of thread context switches of the two models under different numbers of clients, repeat the test many times, and obtain multiple sets of data , Take the average value.
As shown in Table 4, when the number of simulated clients gradually increases, the number of hungry clients in the traditional communication model is also increasing, which makes a large number of clients unable to receive a response from the server, and a large number of client data cannot be transmitted, resulting in data congestion And lost. The completion port communication model adopts a series of optimization strategies, and there is no situation that the client cannot get the service.
As shown in Table 5, when the number of simulated clients is small, the thread context switching times of the two communication models are equivalent; when the number of simulated clients increases, the switching times of the traditional communication model increase sharply, and each switching will cause The extra cost of system resources makes the data processing efficiency of the traditional communication model very low. When using the completion port communication model, the number of thread context switching does not change with the increase of simulated clients. Therefore, the completion port model is more suitable for a large number of client applications, and the reliability and data communication can still be maintained. High efficiency.
6. Concluding remarks
The introduction of port technology has been completed, and the advantages of the server’s multiple CPUs have been fully utilized, so that the data communication performance of the entire monitoring system has been greatly optimized. After the stress test, when the number of monitoring terminal equipment reaches 5000, the system can still maintain efficient and stable operation. At present, the system is applied to the street lamp control of Xiamen Road and Bridge Company, Longyan Changting and other places, and has achieved good results.