Android development based on XMPP protocol

Source: Internet
Author: User
Tags xml parser

Android development based on XMPP protocol

A software based on the open-source XMPP instant messaging protocol was designed, using the C/S Protocol, throughGPRS wireless networkTCP protocol to server to build open source Openfire server as the instant messaging platform

The system is mainly composed of the following parts: 1. The server is responsible for managing the sessions of sent links or other entities, receiving or forwarding XML data to the client, and connecting it to the server, the XMPP protocol is used to obtain all the functions provided by the server or any other related service locks. The third is the translation between the Protocol Gateway information and the external message system but not between information, and the XMPP network, connect various service clients. The system uses the client server architecture.

 

Client:

The client is developed based on the android platform and is responsible for initializing the communication process and performing instant communication. The client creates a connection request from the server,Implement instant communication with the Android client through the server.

Server:

The server uses Openfire as the server, allowing multiple clients to log on concurrently 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.

 

Android Server Design

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 contains two parts, one is to listen to the XMPP service on a specific port and communicate with the XMPPConnection class of the client. The function is to register a user and authenticate the user and send a push notification message, sessionManager Auth Manager PresenceManager and Notification Manager SessionManager manage sessions between the client and the server. They use lightweight HTTP servers to receive web requests from users,

The main four components are SessionManager AuthManager PresenceManager and Notification Manager.

SessionManager manages sessions between clients and servers, AuthManager manages client user authentication, Presence Manager manages Auth Manager manages client user authentication, and Presence Manager manages the logon status of client users, icationicationmanager is responsible for pushing messages from the server 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. 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 in the SD Card of the Virtual File Storage Device of the Android platform.

 

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 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 keep submit tasks. 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 logging on to the server. Therefore, a ConnectionListener will be set up after logging on to the server to listen for connection errors, so it is appropriate to re-connect the thread for re-connection.

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.