parser works but some stuff still needs some refactoring

This commit is contained in:
Daniel Gultsch 2014-01-31 00:33:01 +01:00
parent 6c5c3ac2de
commit c3e4f0eaac
5 changed files with 109 additions and 13 deletions

View file

@ -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;
}
}

View file

@ -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");
}
}

View file

@ -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");
}
}

View file

@ -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");
}
}

View file

@ -35,9 +35,13 @@ 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;
wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
@ -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 {
@ -122,19 +130,44 @@ 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 {
@ -189,6 +222,7 @@ public class XmppConnection implements Runnable {
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) {