From c3e4f0eaacf8ab32ceab32b8f46e8b7d85c71cfb Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Fri, 31 Jan 2014 00:33:01 +0100 Subject: parser works but some stuff still needs some refactoring --- src/de/gultsch/chat/xml/XmlReader.java | 5 ++ src/de/gultsch/chat/xmpp/IqPacket.java | 11 ++++ src/de/gultsch/chat/xmpp/MessagePacket.java | 13 +++++ src/de/gultsch/chat/xmpp/PresencePacket.java | 13 +++++ src/de/gultsch/chat/xmpp/XmppConnection.java | 80 +++++++++++++++++++++++----- 5 files changed, 109 insertions(+), 13 deletions(-) create mode 100644 src/de/gultsch/chat/xmpp/MessagePacket.java create mode 100644 src/de/gultsch/chat/xmpp/PresencePacket.java (limited to 'src/de/gultsch') diff --git a/src/de/gultsch/chat/xml/XmlReader.java b/src/de/gultsch/chat/xml/XmlReader.java index e086c8ae..8963f8d7 100644 --- a/src/de/gultsch/chat/xml/XmlReader.java +++ b/src/de/gultsch/chat/xml/XmlReader.java @@ -50,6 +50,7 @@ public class XmlReader { //Log.d(LOGTAG,"there was a wake lock. releasing it till next event"); wakeLock.release(); //release wake look while waiting on next parser event } + //Log.d(LOGTAG,"waiting for new event..."); while(parser.next() != XmlPullParser.END_DOCUMENT) { //Log.d(LOGTAG,"found new event. acquiring wake lock"); wakeLock.acquire(); @@ -75,17 +76,21 @@ public class XmlReader { public Element readElement(Tag currentTag) throws XmlPullParserException, IOException { Element element = new Element(currentTag.getName()); + //Log.d(LOGTAG,"trying to read element "+element.getName()); element.setAttributes(currentTag.getAttributes()); Tag nextTag = this.readTag(); + //Log.d(LOGTAG,"next Tag is: "+nextTag.toString()); if(nextTag.isNo()) { element.setContent(nextTag.getName()); nextTag = this.readTag(); } + //Log.d(LOGTAG,"reading till the end of "+element.getName()); while(!nextTag.isEnd(element.getName())) { Element child = this.readElement(nextTag); element.addChild(child); nextTag = this.readTag(); } + //Log.d(LOGTAG,"return with element"+element); return element; } } diff --git a/src/de/gultsch/chat/xmpp/IqPacket.java b/src/de/gultsch/chat/xmpp/IqPacket.java index 062bf1c0..eec66a64 100644 --- a/src/de/gultsch/chat/xmpp/IqPacket.java +++ b/src/de/gultsch/chat/xmpp/IqPacket.java @@ -6,6 +6,7 @@ public class IqPacket extends Element { public static final int TYPE_SET = 0; public static final int TYPE_RESULT = 1; + public static final int TYPE_GET = 2; private IqPacket(String name) { super(name); @@ -18,9 +19,19 @@ public class IqPacket extends Element { case TYPE_SET: this.setAttribute("type", "set"); break; + case TYPE_GET: + this.setAttribute("type", "get"); + break; + case TYPE_RESULT: + this.setAttribute("type", "result"); + break; default: break; } } + + public IqPacket() { + super("iq"); + } } diff --git a/src/de/gultsch/chat/xmpp/MessagePacket.java b/src/de/gultsch/chat/xmpp/MessagePacket.java new file mode 100644 index 00000000..de6e0234 --- /dev/null +++ b/src/de/gultsch/chat/xmpp/MessagePacket.java @@ -0,0 +1,13 @@ +package de.gultsch.chat.xmpp; + +import de.gultsch.chat.xml.Element; + +public class MessagePacket extends Element { + private MessagePacket(String name) { + super(name); + } + + public MessagePacket() { + super("message"); + } +} diff --git a/src/de/gultsch/chat/xmpp/PresencePacket.java b/src/de/gultsch/chat/xmpp/PresencePacket.java new file mode 100644 index 00000000..947afe7a --- /dev/null +++ b/src/de/gultsch/chat/xmpp/PresencePacket.java @@ -0,0 +1,13 @@ +package de.gultsch.chat.xmpp; + +import de.gultsch.chat.xml.Element; + +public class PresencePacket extends Element { + private PresencePacket(String name) { + super("presence"); + } + + public PresencePacket() { + super("presence"); + } +} diff --git a/src/de/gultsch/chat/xmpp/XmppConnection.java b/src/de/gultsch/chat/xmpp/XmppConnection.java index 942033a1..de35a0ea 100644 --- a/src/de/gultsch/chat/xmpp/XmppConnection.java +++ b/src/de/gultsch/chat/xmpp/XmppConnection.java @@ -35,8 +35,12 @@ public class XmppConnection implements Runnable { private XmlReader tagReader; private TagWriter tagWriter; - private boolean isTlsEncrypted = false; + private boolean isTlsEncrypted = true; private boolean isAuthenticated = false; + + private static final int PACKET_IQ = 0; + private static final int PACKET_MESSAGE = 1; + private static final int PACKET_PRESENCE = 2; public XmppConnection(Account account, PowerManager pm) { this.account = account; @@ -112,7 +116,11 @@ public class XmppConnection implements Runnable { sendStartStream(); processStream(tagReader.readTag()); } else if (nextTag.isStart("iq")) { - processIq(nextTag); + Log.d(LOGTAG,processIq(nextTag).toString()); + } else if (nextTag.isStart("message")) { + Log.d(LOGTAG,processMessage(nextTag).toString()); + } else if (nextTag.isStart("presence")) { + Log.d(LOGTAG,processPresence(nextTag).toString()); } else if (nextTag.isEnd("stream")) { break; } else { @@ -121,20 +129,45 @@ public class XmppConnection implements Runnable { } } } - - private void processIq(Tag currentTag) throws XmlPullParserException, IOException { - int typ = -1; - if (currentTag.getAttribute("type").equals("result")) { - typ = IqPacket.TYPE_RESULT; + + private Element processPacket(Tag currentTag, int packetType) throws XmlPullParserException, IOException { + Element element; + switch (packetType) { + case PACKET_IQ: + element = new IqPacket(); + break; + case PACKET_MESSAGE: + element = new MessagePacket(); + break; + case PACKET_PRESENCE: + element = new PresencePacket(); + break; + default: + return null; } - IqPacket iq = new IqPacket(currentTag.getAttribute("id"),typ); + element.setAttributes(currentTag.getAttributes()); Tag nextTag = tagReader.readTag(); - while(!nextTag.isEnd("iq")) { - Element element = tagReader.readElement(nextTag); - iq.addChild(element); + while(!nextTag.isEnd(element.getName())) { + if (!nextTag.isNo()) { + Element child = tagReader.readElement(nextTag); + element.addChild(child); + } nextTag = tagReader.readTag(); } - Log.d(LOGTAG,"this is what i understood: "+iq.toString()); + return element; + } + + + private IqPacket processIq(Tag currentTag) throws XmlPullParserException, IOException { + return (IqPacket) processPacket(currentTag,PACKET_IQ); + } + + private MessagePacket processMessage(Tag currentTag) throws XmlPullParserException, IOException { + return (MessagePacket) processPacket(currentTag, PACKET_MESSAGE); + } + + private PresencePacket processPresence(Tag currentTag) throws XmlPullParserException, IOException { + return (PresencePacket) processPacket(currentTag, PACKET_PRESENCE); } private void sendStartTLS() throws XmlPullParserException, IOException { @@ -188,7 +221,8 @@ public class XmppConnection implements Runnable { Element element = tagReader.readElement(nextTag); streamFeatures.addChild(element); nextTag = tagReader.readTag(); - } + } + Log.d(LOGTAG,streamFeatures.toString()); } private void sendBindRequest() throws IOException { @@ -196,9 +230,29 @@ public class XmppConnection implements Runnable { Element bind = new Element("bind"); bind.setAttribute("xmlns","urn:ietf:params:xml:ns:xmpp-bind"); iq.addChild(bind); + //Element resource = new Element("resource"); + //resource.setContent("mobile"); + //bind.addChild(resource); Log.d(LOGTAG,"sending bind request: "+iq.toString()); tagWriter.writeElement(iq); tagWriter.flush(); + + + //technically not bind stuff + IqPacket startSession = new IqPacket(this.nextRandomId(), IqPacket.TYPE_SET); + Element session = new Element("session"); + session.setAttribute("xmlns","urn:ietf:params:xml:ns:xmpp-session"); + session.setContent(""); + startSession.addChild(session); + + tagWriter.writeElement(startSession); + tagWriter.flush(); + + Element presence = new Element("presence"); + + tagWriter.writeElement(presence); + tagWriter.flush(); + } private void processStreamError(Tag currentTag) { -- cgit v1.2.3