How does Android heartbeat packet Heartbeat connect to Android and server long connections? The principle of Push message

Source: Internet
Author: User

Most mobile applications now have the ability to get messages in real-time, in short, with the initiative to send messages and the passive right to receive messages. For example:, QQ, weather forecast and so on, believe that the benefits and user experience I believe we all know it.

Ask the question: This functionality must involve the client (clients) and server (server), so how does the client implement real-time connection communication with the server?

analysis of the problem: This function is actually data synchronization, but also to consider the mobile phone itself, power, network traffic and other constraints, so usually on the mobile side of the two solutions:
1. One is to periodically go to the server to query data, usually using the HTTP protocol to access the Web server, called polling (polling);
2. There is also a long connection between the mobile side and the server, using the XMPP long connection, called push (push). (As I understand it: when the client is implemented:

1  while (true) {23  request (timeout); 4 5 request (timeout); 6 7 }

The client makes a "long" request, and if the server sends a message or the time is out, the client disconnects the request and establishes a long request



Push has a distinct advantage in terms of the amount of power, traffic, and latency of the data being consumed. However, the disadvantage of using push is:
For clients: Relatively high cost of implementation and maintenance, the maintenance of long connections under the mobile wireless network, relative to some technical development difficulty.
For the server: How to achieve multi-core concurrency, CPU job scheduling, a large number of long-connected concurrent maintenance and other technologies, there are still development difficulties.

Before describing the principles of the push scenario, let's look at the features of the mobile wireless network.
Features of mobile wireless networks:
Because IP v4 IP is limited, the operator assigned to the mobile phone terminal IP is the operator intranet IP, mobile phone to connect to the Internet, you need to do a network address translation through the operator's Gateway ("Networking Translation,nat"). Simply said that the operator's gateway needs to maintain an external network IP, port to the network IP, port correspondence, to ensure that the intranet phone can communicate with the Internet server

Schematic diagram is as follows:

the GGSN (Gateway GPRS Support node Gateway GPRS supporting node) module implements the NAT function.
because most mobile wireless network operators are designed to reduce the load on the gateway's NAT mapping table, if it is found that there is no data traffic in the link, its corresponding table is deleted, causing the link to break. (about the role of NAT and its principles you can see my other blog post on the use of UDP (TCP) cross-LAN, NAT traversal experience)
 
push on Android platform for long-connected implementations:
Now that we know that our mobile is going to communicate with the Internet, we have to go through the operator's gateway, so in order not to invalidate the NAT mapping table, we need to send the data to the Internet on a timed basis, because otherwise the NAT mapping table fails, so we only need to send the data with a length of 0.

This is the time to use the timer, on the Android system, timers usually have two kinds:
1.java.util.timer
2.android.app.alarmmanager

Analysis:
Timer: You can perform related tasks according to the schedule or time period. However, the timer needs to use Wakelock to keep the CPU awake to ensure the execution of the task, which consumes a lot of traffic, and when the CPU is dormant, it cannot wake up the task, so it is obviously inappropriate to apply it to the mobile side.

The Alarmmanager:alarmmanager class is a management class that belongs to the Android system packaged to manage the RTC module. This involves the RTC module, to better understand the difference between the two, you need to understand the real difference between the two.
RTC (Real-time clock) real-time alarms in an embedded system, usually using RTC to provide reliable system time, including hours, minutes and months of the day and so on, and requires that the system is in the shutdown state it can also work (usually with backup battery power), Its periphery also does not need too many auxiliary circuit, the typical is only needs a high precision 32.768KHz crystal and the resistor capacitance and so on. (If you are interested in this, you can check the relevant information, here to say a ballpark)
Okay, back to the chase. So, Alarmmanager also called global timing alarm. This means that when I use Alarmmanager to perform tasks on a regular basis, the CPU can hibernate normally, and the CPU is awakened only when the task is performed, which is a very short process.


The following is a simple example of using:


1. Similar to the timer function:
//Get alarm manager
Alarmmanager am = (alarmmanager) getsystemservice (alarm_service);
//Set task execution plan
am.setrepeating (Alarmmanager.elapsed_realtime, Firsttime, 5*1000, sender);//start execution from Firsttime and execute every 5 seconds

2. Implement the global timer function:
//Get alarm manager
Alarmmanager am = (alarmmanager) getsystemservice (alarm_service);
//Set task execution plan
am.setrepeating (Alarmmanager.elapsed_realtime_wakeup, Firsttime, 5*1000, sender);//start execution from Firsttime and execute every 5 seconds

Summary: when using push push on an Android client, you should use Alarmmanager to implement the heartbeat feature to make it truly long-connected.


on the server side of the implementation:
on the server side, can be implemented in many languages, such as C/c++,java,erlang and so on, such as our domestic relatively good aurora push (C development), OpenFire (Java development) and so on.
recently I saw the introduction and principle of the Aurora push, so I'll talk about what kind of problems they are having, and then use any technology or solution to solve them.

when a large number of mobile terminals need to maintain a long connection with the server, the design of the server will be a big challenge.


Assuming that a server maintains 100,000 long connections, when there are 10 million users, you need up to 100 servers to maintain these users ' long connections, and this is not the server for backup, which can be a huge cost issue. That will require us to maximize the number of single-server access to users, that is, the industry has been discussing for a long time c10k problem.
c2000k


In response to this problem, they specifically set up a project, named c2000k, as the name implies, their goal is to maintain a single machine 2 million long connections. In the end, they used a multi-message loop, asynchronous, non-blocking model to achieve spikes of over 3 million long connections on a dual-core, 24G-memory server.




Final Summary:
because I recently used Java to do a PC, server, Android Instant messaging system (in plain imitation of QQ, behind the hope that there are different features). My principle is to use other people's principles, self-realization, so as to better in-depth understanding of some frameworks. Therefore, it is necessary to have a deep understanding of the multi-message loop, asynchronous non-blocking model in the development of communication and server. I will then make a statement about this.
on today's Android platform, it is not the world of Android standalone (I'm not saying there is no future for a single game). Now rely on the development of the Internet to support the entire IT system, so, to become an Android application development experts, must move towards the Android, hardware, cloud services this system to develop.
Reference Document: http://blog.csdn.net/sunmenggmail/article/details/12008075

How does Android heartbeat packet Heartbeat connect to Android and server long connections? The principle of Push message

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.