Recently, we are working on a project to use Android Push technology. Currently, Android push technology is usually used in AndroidPn. Stress testing is required for the Push server. Of course, it is impossible to find thousands of mobile phones for testing. Therefore, you can only simulate the AndroidPN client on the PC side. Each thread represents an AndroidPN client. If you want to simulate the AndroidPN client on a PC, you cannot understand the source code. Google, you can find the relevant source code analysis. This article is based on the relevant, add some of your own opinions. Androidpn contains two packages: server and client. The server can be run independently as a server, embedded into the web project servlet, and interacted with other parts of the web project in the tomcat environment. The main package structure of Server is as follows: org. androidpn. server. dao, org. androidpn. server. model and org. androidpn. server. to use hibernate to connect to a database and implement simple user logon authentication, the service can be replaced by our own authentication module during development. The remaining package is the main implementation of the push. Next, let's look at the packages one by one: 1. the classes in the util package are used to load the configuration file in resources. In the configuration file, you can specify attributes such as the listening port and ssl Certificate directory. The 2.org. androidpn. server. xmpp package defines some exception types, mainly including the entry class XmppServer, which is used to start and stop the server program. 3.org. androidpn. server. xmpp. auth package contains some Authentication Classes. Our own authentication module can be combined with androidpn here. 4.org. androidpn. server. xmpp. codec is an XML file parsing package of XMPP protocol. The server uses this package to encode and decode messages received and sent by xmpp. 5.org. androidpn. server. xmpp. the handler package mainly processes messages. We can define our own handler for different message types. The 6.org.androidpn.server.xmpp.net package is responsible for maintaining persistent connections with clients, some transmission methods are implemented for xmpp message sending. 7.org. androidpn. server. xmpp. presence contains only the PresenceManager class to maintain the online status of the client. The icationicationmanager class in the 8.org. androidpn. server. xmpp. push package contains an interface for sending messages to the client. The 9.org. androidpn. server. xmpp. router package sends the received packets to the corresponding handler for processing. It is a routing package. The 10.org. androidpn. server. xmpp. session package defines the session used to represent persistent links. Each session contains the status information of a connection. 11.org. androidpn. server. xmpp. ssl is a toolkit for ssl authentication on connections. (Currently, the server uses NonSASLAuthentication authentication.) classes related to the XMPP protocol: IQ Presence messages indicate <iq>, <presence>, and, <message> section Packet indicates the abstract class of the XMPP section, and IQ Presence Message is the Element of the subclass of Packet. It indicates the elements in the XML section. If you do not know much about the XMPP protocol, you can get a general idea of the meaning of Various xml sections in XMPP. The whole process of sending messages from the server is as follows: 1. The push interface of icationicationmanager is called. 2. Use SessionManager to find the corresponding client link in the current session set. 3. define your own XMPP Message format and assemble it. 4. Send a message to the client through the corresponding session. In this process, we need to modify step 3, that is, to define and assemble our own xmpp message, so as to transmit the appropriate information to the client and facilitate client resolution. An example of a simple message assembly is as follows: 1: private IQ createCustomizeIQ (String apiKey, String title, 2: String message, String uri) {3: Random random = new Random (); 4: String id = Integer. toHexString (random. nextInt (); 5: // String id = String. valueOf (System. currentTimeMillis (); 6: 7: Element notification = descrienthelper. createElement (QName. get (8: "notification", icationication_namespace); 9: notification. addElement ("I D "). setText (id); 10: notification. addElement ("title "). setText (title); 11: notification. addElement ("message "). setText (message); 12: notification. addElement ("uri "). setText (uri); 13: // attributes of the custom IQ 14: notification. addElement ("attribute name "). setText (attribute); 15: IQ iq = new IQ (); 16: iq. setType (IQ. type. set); 17: iq. setChildElement (notification); 18: 19: return iq; 20:} note that when creating an element, the incoming namespace must be resolved with the name used by the client. Space matches. The process for the server to receive and process messages is as follows: 1. connection receives packet and uses tsc. push. server. xmpp. codec to decode it. 2. the router routes packet to the corresponding handler Based on the namespace and other information of packet. 3. handler. The corresponding router and handler classes have examples in androidpn for reference. No code will be posted here. In development, you only need to define your own router and handler classes based on the format in which the client sends messages, register the router in PacketRouter, and register the handler in IQHandler. Supplement: Step 1 for PacketRouter to register a router. that is, add a member variable to PacketRouter, whose type is custom XXXRouter (custom message router) 2. initialize XXXRouter 3 in the constructor of PackRouter. call the corresponding vro according to different message types. 4. add the route Method route (XXX xxx) of XXXRouter to PacketRouter. Note: XXX (custom XMPP Section) it is an abstract class in the <iq> section of IQHandler, a subclass of Packet, which is used to register a handler. The registered IQHandler inherits IQHandler and overwrites the handleIQ (IQ) <iq> section of the response returned by the method. IQHandler's process (IQ) is to process various IQ values. In actual process, IQHandler handler = new IQXXXHandler (). Call handler. process () will call the handleIQ (IQ) method of the subclass. The main package structure of the Client is as follows: the Client side contains message sending and receiving, parsing, and persistent connection initiation, reconnection and other functions are very powerful. We don't have to worry about the underlying connection during development, or worry about disconnection. we can focus on the development of the business part. The code structure is also very simple. Remove the android Service and BroadCast classes and some tool classes and constant classes: 1. icationicationiq, notifiqiqprovider, and icationpackpacketlistener are responsible for parsing and processing received Notification messages. xmppManager is the master controller. icationicationservice maintains the androidpn connection in the background through this class. 3. PersistentConnectionListener, PhoneStateChangeListener, and ReconnectionThread. java are responsible for listening to the status of the mobile phone and reconnection. When customizing a message, we need to define three classes: Define the message entity in *** IQ, and convert the message to *** IQ entity in *** IQProvider, the entity is processed in *** PacketListener. For specific implementation, see NotificationIQ, icationiqiqprovider, and icationpackpacketlistener. After defining these classes, you also need to register these three classes in connection in XmppManager. The Code is as follows: // ConnectTask class in XmppManager Class 1: Log. I (LOGTAG, "XMPP connected successfully"); 2: // packet provider 3: ProviderManager. getInstance (). addIQProvider ("notification", 4: "androidpn: iq: notification", new notifiqiqprovider (); // The LoginTask class is in the XmppManager class. 1: // packet filter 2: packetFilter packetFilter = new PacketTypeFilter (icationicationiq. class); 3: // Packet listener 4: PacketListener packetListener = xmppManager. geticationicationpacketlistener (); 5: connection. addPacketListener (packetListener, packetFilter); note that when you register *** IQProvider, the incoming namespace must be consistent with the namespace used by the server to assemble the message. The above red part is added when I read this article. It is a supplement to this article... If not, please correct me.