Was sind gute Quellen, um die Threading Implementierung einer XMPP-Anwendung zu studieren?

StackOverflow https://stackoverflow.com/questions/107772

  •  01-07-2019
  •  | 
  •  

Frage

Von meinem Verständnis des XMPP-Protokoll basiert auf einem Always-on-Verbindung, wo Sie haben keine unmittelbare Anzeige, wenn eine XML-Nachricht beendet ist.

Dies bedeutet, dass Sie den Stream zu bewerten haben, wie es kommt. Dies bedeutet auch, dass, wahrscheinlich, Sie mit asynchronen Verbindungen zu tun haben, da der Sockel in der Mitte einer XML-Nachricht blockieren kann, entweder aufgrund Nachrichtenlänge oder eine Verbindung ist langsam.

würde ich eine Quelle pro Antwort zu schätzen wissen, so dass wir sie mod und sehen, was ist der Favorit.

War es hilfreich?

Lösung

Are you wanting to deal with multiple connections at once? Good asynch socket processing is a must in that case, to avoid one thread per connection.

Otherwise, you just need an XML parser that can deal with a chunk of bytes at a time. Expat is the canonical example; if you're in Java, try XP. These types of XML parsers will fire events as possible, and buffer partial stanzas until the rest arrives.

Now, to address your assertion that there is no notification when a stanza ends, that's not really true. The important thing is not to process the XML stream as if it is a sequence of documents. Use the following pseudo-code:

stanza = null
while parser has more:
  switch on token type:
     START_TAG:
       elem =  create element from parser state
       if stanza is not null:
         add elem as child of stanza
       stanza = elem
     END_TAG:
       parent = parent of stanza
       if parent is not null:
         fire OnStanza event
       stanza = parent

This approach should work with an event-based or pull parser. It only requires holding on to one pointer worth of state. Obviously, you'll also need to handle attributes, character data, entity references (like & and the like), and special-purpose the stream:stream tag, but this should get you started.

Andere Tipps

Igniterealtime.org provides an open source XMPP-server and client written in java

ejabberd is written in Erlang. I don't know the details of the ejabberd implementation, but one advantage of using Erlang is really inexpensive threads. I'll speculate they start a thread per XMPP connection. In Erlang terminology these would be called processes, but these are not protected-memory address spaces they are lightweight user-space threads.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top