[Ext] The XMPP protocol-based Android IM system

Source: Internet
Author: User
Tags md5 encryption sqlite database xml parser

Transferred from: http://blog.csdn.net/lnb333666/article/details/7471292

Previously done a chat social software based on XMPP protocol, summed up a bit. Sent out.

Based on the open source XMPP im Protocol, the C/S architecture is designed to connect to the server with the TCP protocol via GPRS wireless network to erect the open source Openfn ' E server as an instant communication platform.

The system mainly consists of the following parts: First, the server is responsible for the management of the connection or the session with other entities, receiving or forwarding XML (extensiblemarkup Language) stream elements to the authorized clients, servers, etc., and the client terminal. It is connected to the server and obtains all the functionality provided by the server or any other related services through XMPP. The third is the Protocol gateway. Complete the translation between the information transmitted by the XMPP protocol and the information that the external message system can recognize. And then the XMPP network. The connection between each server and client is realized. The system uses the client/server architecture architecture.

Client:

The client is developed based on the Android platform. The client is responsible for initiating the creation of a connection request to the server when it initiates the communication process for instant communication. The system connects with Internet network through GPRS wireless network, and realizes instant communication with Android client through the server.


Server-side:

The server side uses OpenFire as the server. Allows multiple clients to log on concurrently and connect to a server concurrently. The server authenticates each client's connection, and the client-to-server communication is performed in the context of that session.

1.1 Server-side design (this piece can almost be said to have finished.) Not much to tangle with)

ANDROIDPN server-side is implemented in the Java language, based on the OpenFire Open source project, the Web Part uses the spring framework, which is different from OpenFire. The ANDROIDPN server consists of two sections, one that listens to the XMPP service on a specific port and is responsible for communicating with the client's Xmppconnection class, which is user registration and authentication, and sends a push notification message. The other part is the Web server, which uses a lightweight HTTP server that is responsible for receiving Web requests from users.

The main four components are Sessionmanager,auth Manager,presencemanager and notification Manager respectively. SessionManager is responsible for managing the client-server session, Auth Manager is responsible for client user authentication management, and Presence Manager is responsible for managing the client user's login status. The Notificationmanager is responsible for implementing the server push message function to the client.

The system client is based on the Android phone platform. Use XMPP as an Instant messaging protocol. XMPP is an XML-based communication protocol that realizes quasi-real-time exchange of structured information for any two network terminals. Parse the XML using the XML parsing package provided by the Android platform. Because the app activity is running on the main thread. Therefore, multithreading technology is used to solve the problem of system communication. For communication security issues. The user information and chat information of the system are stored in the SQLite database of the Android platform itself, and the multimedia files and picture files are stored in the SD card of the Android platform virtual file storage device.

Communication module is responsible for building communication with the server old. Processing is done by creating 3 threads. It is responsible for sending, receiving and transmitting heartbeat information, and parsing module is mainly used to parse the XML data stream. Different types of data objects are encapsulated according to the parsing elements: The data module defines most of the data types and objects throughout the client, and application modules include instant messaging, picture browsing, and music playback. Is the interface between the client and the user, and the encryption module decrypts the messages sent and received. To ensure the security of your communications data.

The client of the system is divided into 5 modules for design and development, 2 shows.

Communication module is responsible for building communication with the server old. Processing is done by creating 3 threads. It is responsible for sending, receiving and transmitting heartbeat information, and parsing module is mainly used to parse the XML data stream. Different types of data objects are encapsulated according to the parsing elements: The data module defines most of the data types and objects throughout the client, and application modules include instant messaging, picture browsing, and music playback. Is the interface between the client and the user, and the encryption module decrypts the messages sent and received. To ensure the security of your communications data.

Encryption (binary code is converted to BASE64 code first, after conversion to BASE64 code, then MD5 encryption,)

The TCP connection between the XMPP server and between the client and the server is ignored. TCP provides a tile-to-connect, reliable byte-stream service. Maintain a real-time, bidirectional transmission channel. TCP Packages the user data to form a message segment. It sends the data and starts a timer, waits for the peer data to confirm, the other end confirms the received data, re-sorts the unordered data, and discards the duplicate data; TCP provides end-to-end traffic control. Calculate and validate a mandatory end-to-end inspection. However, the GPRS network has a limitation on the TCP link. When the TCP link is not having data traffic for a long time. The priority of this link is automatically lowered until the link is forcibly disconnected. So in the application. This link is maintained by sending a heartbeat.

Data format

XML is at the heart of the XMPP system architecture. It can represent almost any kind of structured data. In particular, XMPP makes use of XML data streams for client-side, server-side, one-server-side communication. XML data flow is typically initiated by the client to the server, and the effective time of the XML data stream is directly associated with the user's online session validity time.

Protocol Message Format

The XMPP protocol consists of 3 top-level XML elements: Message, Presence, and IQM. The message is used to represent the transmitted messages when the user sends a message. A message element is inserted in the context of the stream with relevant information sent by the user; presence is used to represent the state of the user. When the user changes their state. A presence element is inserted in the context of the data flow to represent the user's current state; IQ is used to represent a request, a response mechanism, a request is sent from one entity, and another entity accepts the request and responds.

Backstage Servic:

From the level of the class to see this structure is relatively simple, let it become complex, it has three threads: the main thread, the XMPP communication thread, connection error retry thread.

Description of the diagram:

1. Create a single thread in Notificationservice to connect to the server, because using XMPP to connect the server is divided into three steps: Connect, register, log in. So use a stack to save the task Tasks (connecttask,registertask,logintask) to be executed, and then execute them in that order.

2. The thread connecting the XMPP server is using Executors.newsinglethreadexecutor (), which itself can not stop the submit task. Why do I have to use a stack to save a task?

3. Connection thread in the process of connection, registration, login, there may be errors, may fail, then I will have a reconnection mechanism, in the ANDROIDPN opened another thread to retry, and its retry is not each time by how many seconds to retry, but has its own rules.

4. In Logintask, if the server is logged in, it registers a listener to listen for server push packets (Packet), and then notifies the program to be displayed by sending a broadcast.

5. After landing the server, there may be errors, so after landing, will be set up a connectionlistener, for monitoring the connection error, and then appropriate reconnection process, to re-connect

6. During the landing process, there is a mistake to deal with alone, that is, when the account and password is invalid, this time its return status code is 401, this situation should be the local saved account and password are cleared, and then re-connect, or will never land on the server side.

Since all the functions of the system are based on the communication of the XML stream between the networks, a module is dedicated to the processing of the network and the XML stream, and the main functions include the processing of TCP sockets, the parsing of XML streams, and the storage functions of the server and client.

The data module is responsible for parsing XML stream and encapsulating XML module, the main function is: parse the XML stream into Java object, encapsulate the Iava object into XML stream;

The process is that after the XMPP server receives the XML stream, there will be an accessor to read it out and pass it as an entry parameter into the XML parser, the XML parser through the parsing of its namespace, to determine the remaining XML elements to parse out and pass in the corresponding; Ava object, This eventually translates the XML into an Iava object, then passes the Iava object into the application module, implements its request completion function and returns the Iava object, but the Iava object cannot be transmitted directly in the network and must first be converted to an XML section, and the Iava object is passed into the XML wrapper. is encapsulated into an XML section that is sent to the destination node through the send port of the XMPP server.

The process of processing the Java Object Processing module is as follows: When the module receives the Iava object, it decrypts the object through the decryption algorithm and decryption key into the Base64 code, then the F-ear converts the Base64 code into binary code, thus realizing the parsing of the Java object. When the business logic is processed, the module converts the returned Java object from binary code to Base64 code, and then encrypts it with an encryption algorithm, where the cryptographic algorithm is negotiated by the two parties through three handshake protocols when establishing the session.

When the XML section is encapsulated as a Java object, it must be forwarded to the order: correct module to be processed, which requires a routing forwarding module, 3-3. The module is implemented by loading the routing module at system startup, creating a routing module in memory, recording the correspondence between named NULL and functional modules, and when the Iava object is encapsulated, the system reads its namespace and then finds its corresponding module in the routing table, which dynamically loads the module , and forwards the Java object to the module, thus realizing the function of road-Hill forwarding.

[to] the XMPP protocol-based Android Instant messaging system

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.