diff options
author | Vitaly Takmazov | 2016-01-26 20:09:27 +0300 |
---|---|---|
committer | Vitaly Takmazov | 2016-01-26 20:09:27 +0300 |
commit | 75d80dc433348435c1f8e11fafb0d1deda1ca793 (patch) | |
tree | c06a67e2bcf6e10bf12f7caabcd43280df2d91ab /src/main/java/com/juick/xmpp/s2s/ConnectionOut.java | |
parent | bace08c34252bffd3a575ca86b2b2f420165a81b (diff) |
import s2s component
Diffstat (limited to 'src/main/java/com/juick/xmpp/s2s/ConnectionOut.java')
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/ConnectionOut.java | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java new file mode 100644 index 00000000..2626b926 --- /dev/null +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionOut.java @@ -0,0 +1,122 @@ +package com.juick.xmpp.s2s; + +import com.juick.xmpp.utils.XmlUtils; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStreamWriter; +import java.net.Socket; +import org.xmlpull.v1.XmlPullParser; + +/** + * + * @author ugnich + */ +public class ConnectionOut extends Connection implements Runnable { + + public boolean streamReady = false; + public String to; + String checkSID = null; + String dbKey = null; + + public ConnectionOut(String hostname) { + super(); + to = hostname; + } + + public ConnectionOut(String hostname, String checkSID, String dbKey) { + super(); + to = hostname; + this.checkSID = checkSID; + this.dbKey = dbKey; + } + + @Override + public void run() { + System.out.println("STREAM TO " + to + " START"); + + try { + HostnamePort addr = DNSQueries.getServerAddress(to); + socket = new Socket(addr.hostname, addr.port); + + parser.setInput(new InputStreamReader(socket.getInputStream())); + writer = new OutputStreamWriter(socket.getOutputStream()); + + sendStanza("<?xml version='1.0'?><stream:stream xmlns='jabber:server' " + + "xmlns:stream='http://etherx.jabber.org/streams' xmlns:db='jabber:server:dialback' from='" + + XMPPComponent.HOSTNAME + "' to='" + to + "' version='1.0'>"); + + parser.next(); // stream:stream + streamID = parser.getAttributeValue(null, "id"); + if (streamID == null || streamID.isEmpty()) { + throw new Exception("STREAM TO " + to + " INVALID FIRST PACKET"); + } + + System.out.println("STREAM TO " + to + " " + streamID + " OPEN"); + XMPPComponent.addConnectionOut(this); + + if (checkSID != null) { + sendDialbackVerify(checkSID, dbKey); + } + + sendStanza("<db:result from='" + XMPPComponent.HOSTNAME + "' to='" + to + "'>" + generateDialbackKey(to, XMPPComponent.HOSTNAME, streamID) + "</db:result>"); + + while (parser.next() != XmlPullParser.END_DOCUMENT) { + if (parser.getEventType() != XmlPullParser.START_TAG) { + continue; + } + + if (XMPPComponent.LOGFILE != null) { + logParser(); + } + + String tag = parser.getName(); + if (tag.equals("db:result")) { + String type = parser.getAttributeValue(null, "type"); + if (type != null && type.equals("valid")) { + streamReady = true; + System.out.println("STREAM TO " + to + " " + streamID + " READY"); + + String cache = XMPPComponent.getFromCache(to); + if (cache != null) { + System.out.println("STREAM TO " + to + " " + streamID + " SENDING CACHE"); + sendStanza(cache); + } + + } else { + System.out.println("STREAM TO " + to + " " + streamID + " DIALBACK FAIL"); + } + XmlUtils.skip(parser); + } else if (tag.equals("db:verify")) { + String from = parser.getAttributeValue(null, "from"); + String type = parser.getAttributeValue(null, "type"); + String sid = parser.getAttributeValue(null, "id"); + if (from != null && from.equals(to) && sid != null && !sid.isEmpty() && type != null) { + ConnectionIn c = XMPPComponent.getConnectionIn(sid); + if (c != null) { + c.sendDialbackResult(from, type); + } + } + XmlUtils.skip(parser); + } else { + System.out.println("STREAM TO " + to + " " + streamID + ": " + XmlUtils.parseToString(parser, true)); + } + } + + System.err.println("STREAM TO " + to + " " + streamID + " FINISHED"); + XMPPComponent.removeConnectionOut(this); + closeConnection(); + } catch (Exception e) { + System.err.println(e.toString()); + XMPPComponent.removeConnectionOut(this); + closeConnection(); + } + } + + public void sendDialbackVerify(String sid, String key) { + try { + sendStanza("<db:verify from='" + XMPPComponent.HOSTNAME + "' to='" + to + "' id='" + sid + "'>" + key + "</db:verify>"); + } catch (IOException e) { + System.err.println("STREAM TO " + to + " " + streamID + " ERROR: " + e.toString()); + } + } +} |