Android Instant Messaging System Based on XMPP protocol

Source: Internet
Author: User
Tags md5 encryption xml parser

During this time, the company had to learn more about XMPP-based chat and social networking software. I searched for some information and summarized it. .

Based on the open-source XMPP instant communication protocol, the C/S architecture is used to connect to the server through the GPRS wireless network using the TCP protocol, and the open-source openfn 'e server is set up as the instant messaging platform.

The system is mainly composed of the following parts: 1. A server is responsible for managing established connections or sessions with other entities, receiving or forwarding XML (extensiblemarkup LANGUAGE) stream elements to authorized clients and servers.

User terminal. It is connected to the server and uses XMPP to obtain all functions provided by the server or any other related service. Third, the Protocol Gateway. The translation between the information transmitted by XMPP protocol and that recognized by the external message system. And then XMPP network. Connect servers and clients. The system adopts the client/server architecture.

 

Client:

The client is developed based on the Android platform. The client initiates a connection creation request to the server during the communication initialization and instant communication. The system establishes a connection with the Internet through the GPRS wireless network, and implements instant communication with the android client through the server.

Server:

The server uses openfire as the server. Allows multiple clients to log on simultaneously and concurrently connect to a server. The server authenticates the connection of each client and creates a session for the authenticated client.

Communication between the client and the server is performed in the context of the session.

 

1.1 Server Design (this is almost finished. No need to tangle)

The androidpn server is implemented in Java. Based on the openfire open source project, the Web part adopts the Spring framework, which is different from openfire. The androidpn server consists of two parts. One is to listen to the XMPP service on a specific port and communicate with the xmppconnection class of the client. The role is to register a user and authenticate the user, and send push notification messages. In addition, a web server uses a lightweight HTTP server to receive users' Web requests.

The main four components are sessionmanager, auth manager, presencemanager, and notification.
Manager. Sessionmanager manages sessions between the client and the server, auth Manager manages client user authentication, and presence
The manager is responsible for managing the logon status of client users, and the icationicationmanager is responsible for implementing the server's message push function to the client.

The System Client is based on the Android mobile phone platform. XMPP is used as the instant messaging protocol. XMPP is a communication protocol based on XML to implement quasi-real-time exchange of structured information between any two network terminals. Use the XML parsing package provided by the Android platform to parse the XML. Because all application activities run on the main thread. Therefore, multithreading technology is used to solve system communication problems. For communication security issues, the user information and chat information of the system are stored on the client in the SQLite database of the Android platform, and multimedia files and image files are stored on the Android platform Virtual File storage device SD.
Card.

 

The communication module establishes old communication with the server. Create three threads for processing. Messages are sent, received, and heartbeat messages respectively. The parsing module is mainly used to parse XML data streams. Different data objects are encapsulated based on different types of parsing elements: the data module defines the data types and objects of the entire client. The application modules include instant messaging, Image Browsing, and music playback. The interface between the client and the user. The encryption module decrypts the messages sent and received. To ensure the security of communication data.

 

The System Client is divided into five modules for design and development, as shown in figure 2.

The communication module establishes old communication with the server. Create three threads for processing. Messages are sent, received, and heartbeat messages respectively. The parsing module is mainly used to parse XML data streams. Different data objects are encapsulated based on different types of parsing elements: the data module defines the data types and objects of the entire client. The application modules include instant messaging, Image Browsing, and music playback. The interface between the client and the user. The encryption module decrypts the messages sent and received. To ensure the security of communication data.

Encryption (first convert the binary code to the base64 code, and then perform MD5 encryption after it is converted to the base64 code ,)

TCP connections between XMPP servers and between customers and servers are used. TCP provides a tile connection and reliable byte stream service. Maintains a real-time two-way transmission channel. TCP packs user data into a packet segment. It starts a timer after sending the data, waits for the peer data to confirm, the other end confirms the received data, sorts the out-of-order data again, and discards the duplicate data; TCP provides end-to-end traffic control. Calculate and verify a mandatory end-to-end test. However, the GPRS network has a limit on the TCP link. When the TCP link has no data traffic for a long time. The priority of this link is automatically lowered until the link is forcibly disconnected. Therefore, in applications, send heartbeat packets to maintain this link.

Data format

XML is the core of the XMPP system architecture. It can express almost any structured data. In particular, XMPP uses XML data streams for client-side, server-side, and server-side communication. XML data streams are generally initiated from the client to the server. The validity period of XML data streams is directly related to the user's online session validity period.

Protocol Message format

The XMPP protocol includes three top-layer XML elements: Message, presence, and iqm. Message is used to indicate transmitted messages when a user sends a message. A message element will be inserted in the context of the stream, with information sent by the user in the middle; presence is used to indicate the user's status. When the user changes his/her status. A presence element is inserted in the context of the data stream to indicate the current state of the user. IQ is used to indicate a request and a response mechanism to send requests from an entity, another entity accepts the request and responds.

Background servic:

From the perspective of the class level, this structure is relatively simple. What makes it more complicated is that there are three threads in it: the main thread, the XMPP communication thread, and the connection error retry thread.

Graph description:

1.
Create a single thread in icationicationservice to connect to the server. Because XMPP is used to connect to the server, there are three steps: connection, registration, and login. Therefore, a stack is used to save the task (connecttask, registertask, logintask) to be executed, and then executed in this order.

2.
The thread connecting to the XMPP server uses executors. newsinglethreadexecutor (), which can be a non-stop submit task. Why do I need to use a stack to save tasks?

3.
Connection threads may encounter errors during connection, registration, and login, and may fail. In this case, I will have a reconnection mechanism, in androidpn, another thread is enabled to retry. Retry is not based on the number of seconds each time, but has its own rules.

4.
In logintask, If you log on to the server, it registers a listener to listen to the packets pushed by the server (packet ), then, the program to be displayed is notified by sending a broadcast.

5.
An error may also occur after you log on to the server. Therefore, a connectionlistener will be set after you log on to the server to listen on connection errors, and re-connect the thread to the server.

6.
During the login process, there is an error to be handled separately, that is, when the account and password are invalid, the status code returned at this time is 401, in this case, the locally saved account and password should be cleared and then connected again. Otherwise, the server will never be logged on.

All functions of the system are implemented based on XML stream communication between networks. Therefore, a module is required to handle network-related communications and XML streams, the main functions include TCP socket processing during communication between the server and the client, XML Stream parsing and storage.

The data module parses and encapsulates XML streams. The main function is to parse XML streams into Java objects and encapsulate iava objects into XML streams;

The process is that after the XMPP server receives the XML stream, a parser will read it and pass it as an entry parameter to the XML parser, by parsing its namespace, the XML Parser determines that the remaining XML elements are parsed and passed into the corresponding; In the Ava object, XML is eventually converted into an iava object, the iava object is then passed into the application module to implement the function of completing the request and return the iava object. However, the iava object cannot be directly transmitted over the network and must be converted to an XML section first, therefore, the iava object will be passed into the XML package and encapsulated into an XML section, and sent to the target node through the XMPP server's sending port.

The processing process of the Java object processing module is as follows: when the module receives an iava object, it first decrypts the object to a base64 code through the decryption algorithm and decryption key, then, the F-er converts the base64 code to the binary code to parse the Java object. After the business logic is processed, the module first converts the returned Java object from the binary code to the base64 code, and then encrypts it using the encryption algorithm, the encryption algorithm is negotiated by both parties through the three-way handshake protocol when a session is established.

After the XML section is encapsulated as a Java object, it must be forwarded to the correct module for processing. This requires a routing forwarding module, as shown in 3-3. The implementation principle of this module is as follows: when the system starts, this routing module is loaded, and a routing module is created in the memory, which records the correspondence between the namespace and the function module, after an iava object is encapsulated, the system reads its namespace and searches for the corresponding module in the routing table to dynamically load the module, and forward the Java object to this module to implement the Lushan forwarding function.

Related Article

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.