diff options
Diffstat (limited to 'src/main/java/com/juick/xmpp/s2s/ConnectionIn.java')
-rw-r--r-- | src/main/java/com/juick/xmpp/s2s/ConnectionIn.java | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java index 8150cc27..7b9483f7 100644 --- a/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java +++ b/src/main/java/com/juick/xmpp/s2s/ConnectionIn.java @@ -5,13 +5,18 @@ import com.juick.xmpp.JID; import com.juick.xmpp.Message; import com.juick.xmpp.Presence; import com.juick.xmpp.utils.XmlUtils; +import org.xmlpull.v1.XmlPullParser; + +import java.io.EOFException; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; -import java.net.Socket; +import java.nio.channels.AsynchronousSocketChannel; +import java.nio.channels.Channels; import java.util.ArrayList; +import java.util.List; import java.util.UUID; -import org.xmlpull.v1.XmlPullParser; +import java.util.logging.Logger; /** * @@ -19,11 +24,13 @@ import org.xmlpull.v1.XmlPullParser; */ public class ConnectionIn extends Connection implements Runnable { - final public ArrayList<String> from = new ArrayList<String>(); + private static final Logger LOGGER = Logger.getLogger(ConnectionIn.class.getName()); + + final public List<String> from = new ArrayList<>(); public long tsRemoteData = 0; public long packetsRemote = 0; - public ConnectionIn(Socket socket) { + public ConnectionIn(AsynchronousSocketChannel socket) { super(); this.socket = socket; streamID = UUID.randomUUID().toString(); @@ -31,10 +38,10 @@ public class ConnectionIn extends Connection implements Runnable { @Override public void run() { - System.out.println("STREAM FROM ? " + streamID + " START"); + LOGGER.info("STREAM FROM ? " + streamID + " START"); try { - parser.setInput(new InputStreamReader(socket.getInputStream())); - writer = new OutputStreamWriter(socket.getOutputStream()); + parser.setInput(new InputStreamReader(Channels.newInputStream(socket))); + writer = new OutputStreamWriter(Channels.newOutputStream(socket)); parser.next(); // stream:stream updateTsRemoteData(); @@ -61,10 +68,7 @@ public class ConnectionIn extends Connection implements Runnable { if (parser.getEventType() != XmlPullParser.START_TAG) { continue; } - - if (XMPPComponent.LOGFILE != null) { - logParser(); - } + logParser(); packetsRemote++; @@ -72,7 +76,7 @@ public class ConnectionIn extends Connection implements Runnable { if (tag.equals("db:result")) { String dfrom = parser.getAttributeValue(null, "from"); String to = parser.getAttributeValue(null, "to"); - System.out.println("STREAM FROM " + dfrom + " TO " + to + " " + streamID + " ASKING FOR DIALBACK"); + LOGGER.info("STREAM FROM " + dfrom + " TO " + to + " " + streamID + " ASKING FOR DIALBACK"); if (dfrom.endsWith(XMPPComponent.HOSTNAME) && (dfrom.equals(XMPPComponent.HOSTNAME) || dfrom.endsWith("." + XMPPComponent.HOSTNAME))) { break; } @@ -103,10 +107,10 @@ public class ConnectionIn extends Connection implements Runnable { } if (valid) { sendStanza("<db:verify from='" + vto + "' to='" + vfrom + "' id='" + vid + "' type='valid'/>"); - System.out.println("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY VALID"); + LOGGER.info("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY VALID"); } else { sendStanza("<db:verify from='" + vto + "' to='" + vfrom + "' id='" + vid + "' type='invalid'/>"); - System.err.println("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY INVALID"); + LOGGER.warning("STREAM FROM " + vfrom + " " + streamID + " DIALBACK VERIFY INVALID"); } } else if (tag.equals("presence") && checkFromTo(parser)) { Presence p = Presence.parse(parser, null); @@ -117,7 +121,7 @@ public class ConnectionIn extends Connection implements Runnable { updateTsRemoteData(); Message msg = Message.parse(parser, XMPPComponent.childParsers); if (msg != null && (msg.type == null || !msg.type.equals(Message.Type.error))) { - System.out.println("STREAM " + streamID + ": " + msg.toString()); + LOGGER.info("STREAM " + streamID + ": " + msg.toString()); if (!JuickBot.incomingMessage(msg)) { XMPPComponent.connRouter.sendStanza(msg.toString()); } @@ -127,18 +131,22 @@ public class ConnectionIn extends Connection implements Runnable { String type = parser.getAttributeValue(null, "type"); String xml = XmlUtils.parseToString(parser, true); if (type == null || !type.equals(Iq.Type.error)) { - System.out.println("STREAM " + streamID + ": " + xml); + LOGGER.info("STREAM " + streamID + ": " + xml); XMPPComponent.connRouter.sendStanza(xml); } } else { - System.out.println("STREAM " + streamID + ": " + XmlUtils.parseToString(parser, true)); + LOGGER.info("STREAM " + streamID + ": " + XmlUtils.parseToString(parser, true)); } } - System.err.println("STREAM " + streamID + " FINISHED"); + LOGGER.warning("STREAM " + streamID + " FINISHED"); + XMPPComponent.removeConnectionIn(this); + closeConnection(); + } catch (EOFException ex) { + LOGGER.info(String.format("STREAM %s CLOSED (dirty)", streamID)); XMPPComponent.removeConnectionIn(this); closeConnection(); } catch (Exception e) { - System.err.println("STREAM " + streamID + " ERROR:" + e.toString()); + LOGGER.warning("STREAM " + streamID + " ERROR:" + e.toString()); e.printStackTrace(); XMPPComponent.removeConnectionIn(this); closeConnection(); @@ -154,10 +162,10 @@ public class ConnectionIn extends Connection implements Runnable { sendStanza("<db:result from='" + XMPPComponent.HOSTNAME + "' to='" + sfrom + "' type='" + type + "'/>"); if (type.equals("valid")) { from.add(sfrom); - System.out.println("STREAM FROM " + sfrom + " " + streamID + " READY"); + LOGGER.info("STREAM FROM " + sfrom + " " + streamID + " READY"); } } catch (IOException e) { - System.err.println("STREAM FROM " + sfrom + " " + streamID + " ERROR: " + e.toString()); + LOGGER.warning("STREAM FROM " + sfrom + " " + streamID + " ERROR: " + e.toString()); } } |