XMPP Protocol Learning Note Five (OpenFire message processing process)

Source: Internet
Author: User
Tags sessions

XMPP protocol as an IM, its core is the message delivery, in the OpenFire server XMPP implementation, the message is encapsulated as packet object, so OpenFire server's core code is the client packet object monitoring and processing flow, We're going to take a look at OpenFire's message packet acceptance process today.

First, the OpenFire server needs to start a TCP/IP based listening service to receive the XML stream files from the client. This process is done in the start () method of the Xmppserver class, which is loaded with LoadModule (ConnectionManagerImpl.class.getName ()). Invoke the Createclientlisteners () method of the Connectionmanagerimpl class

    private void Createclientlisteners () {//Start clients plain socket unless it ' s been disabled. if (isclientlistenerenabled ()) {//Create Socketacceptor with correct number of processors socket
            Acceptor = Buildsocketacceptor (); Customize Executor that'll be used by the processors to process incoming the stanzas Executorthreadmodel THREADMO
            del = executorthreadmodel.getinstance ("client");
            int eventthreads = Jiveglobals.getintproperty ("xmpp.client.processing.threads", 16);
            Threadpoolexecutor eventexecutor = (threadpoolexecutor) threadmodel.getexecutor ();
            Eventexecutor.setcorepoolsize (eventthreads + 1);
            Eventexecutor.setmaximumpoolsize (eventthreads + 1);

            Eventexecutor.setkeepalivetime (Timeunit.seconds);
            Socketacceptor.getdefaultconfig (). Setthreadmodel (ThreadModel); Add the XMPP codec filter socketacceptor.getfilterchain (). adDFirst ("XMPP", New Protocolcodecfilter (new xmppcodecfactory)); Kill sessions whose outgoing queues keep growing and fail to send traffic (). A
        Ddafter ("XMPP", "Outcap", New Stalledsessionsfilter ()); }
    }
The socketacceptor is defined in the Buildsocektacceptor () method as a receiver of a server, a socketserver that the Mina framework encapsulates for us, in the above method, We added a filter for Socketacceptor, Xmppcodefactory, this class will filter XMPP related requests, and we'll look at another method of the same class Startclientlistener ()
     private void Startclientlisteners (String localipaddress) {//Start clients plain socket unless it ' s been dis
        abled.
            if (isclientlistenerenabled ()) {int port = Getclientlistenerport ();
                try {//Listen on a specific network interface if it has been set.
                String InterfaceName = Jiveglobals.getxmlproperty ("Network.interface");
                InetAddress bindinterface = null; if (InterfaceName!= null) {if (Interfacename.trim (). Length () > 0) {Bindi
                    Nterface = Inetaddress.getbyname (InterfaceName);
                        }//Start accepting connections Socketacceptor

                . Bind (New Inetsocketaddress (Bindinterface, Port), new Clientconnectionhandler (ServerName));

           Ports.add (New ServerPort (port, ServerName, localipaddress, false, NULL, ServerPort.Type.client));     list<string> params = new arraylist<string> ();
                Params.add (port) (integer.tostring);
            Log.info (localeutils.getlocalizedstring ("Startup.plain", params));  catch (Exception e) {System.err.println ("Error starting XMPP Listener on port" + Port + ":
                "+ e.getmessage ());
            Log.error (localeutils.getlocalizedstring ("Admin.error.socket-setup"), e); }
        }
    }
The Socketacceptor.bind () method starts the listening server to listen for all data sent to server 5222 ports and is processed using the Clientconnetionhandler class. Clinetconnectionhandler inherits from the Connectionhandler class, which implements the Iohandleradaptor interface of Mina, where the messagereceived () method is critical.
        public void messagereceived (iosession sessions, Object message) throws Exception {//Get the stanza handle
        R for this session Stanzahandler handler = (Stanzahandler) session.getattribute (handler); Get the "parser to" use to process stanza. For optimization there are going//To be a parser for each running thread. Each filter is executed//by the Executor placed as the "the".
        So we can have a parser associated//to each Thread int hashcode = Thread.CurrentThread (). Hashcode ();
        Xmpppacketreader parser = Parsers.get (hashcode);
            if (parser = = NULL) {parser = new Xmpppacketreader ();
            Parser.setxppfactory (Factory);
        Parsers.put (hashcode, parser);
        }//Update counter of Read Btyes Updatereadbytescounter (session);
        System.out.println ("RCVD:" + message);
       Let the stanza handler process the received stanza try {     Handler.process ((String) message, parser);
            catch (Exception e) {log.error ("Closing connection due to error while processing message:" + message, E);
            Connection Connection = (Connection) session.getattribute (Connection);
        Connection.close (); }
    }
You can see that the information received is parsed in XML by the Stanzahandler process method and encapsulated as a packet object, and then the next step is done, so that the packet delivery from the client to the server end ends.


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.