package asmack.org.jivesoftware.smack; import asmack.org.jivesoftware.smack.packet.Packet; import java.io.IOException; import java.io.Writer; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; /* access modifiers changed from: package-private */ public class PacketWriter { private XMPPConnection connection; private boolean done; private Thread keepAliveThread; private long lastActive = System.currentTimeMillis(); private final BlockingQueue<Packet> queue = new ArrayBlockingQueue(500, true); private Writer writer; private Thread writerThread; /* access modifiers changed from: private */ public class KeepAliveTask implements Runnable { private int delay; private Thread thread; public KeepAliveTask(int i) { this.delay = i; } public void run() { try { Thread.sleep(15000); } catch (InterruptedException unused) { } while (!PacketWriter.this.done && PacketWriter.this.keepAliveThread == this.thread) { long j = (long) this.delay; synchronized (PacketWriter.this.writer) { if (System.currentTimeMillis() - PacketWriter.this.lastActive >= ((long) this.delay)) { try { PacketWriter.this.writer.write("<p/>"); PacketWriter.this.writer.flush(); } catch (Exception unused2) { } } } Thread.sleep(j); } } /* access modifiers changed from: protected */ public void setThread(Thread thread2) { this.thread = thread2; } } protected PacketWriter(XMPPConnection xMPPConnection) { this.connection = xMPPConnection; init(); } private Packet nextPacket() { Packet packet = null; while (!this.done && (packet = this.queue.poll()) == null) { try { synchronized (this.queue) { this.queue.wait(); } } catch (InterruptedException unused) { } } return packet; } /* access modifiers changed from: private */ /* access modifiers changed from: public */ /* JADX WARNING: Missing exception handler attribute for start block: B:36:0x006a */ private void writePackets(Thread thread) { try { openStream(); while (true) { if (!this.done) { if (this.writerThread != thread) { break; } Packet nextPacket = nextPacket(); if (nextPacket != null) { synchronized (this.writer) { this.writer.write(nextPacket.toXML()); this.writer.flush(); this.lastActive = System.currentTimeMillis(); } } } } try { synchronized (this.writer) { while (!this.queue.isEmpty()) { this.writer.write(this.queue.remove().toXML()); } this.writer.flush(); } } catch (Exception e2) { e2.printStackTrace(); } this.queue.clear(); try { this.writer.write("</stream:stream>"); this.writer.flush(); } catch (Exception unknown) { this.writer.close(); } catch (Throwable th) { try { this.writer.close(); } catch (Exception unused) { } throw th; } try { this.writer.close(); } catch (Exception unused2) { } } catch (IOException e3) { if (!this.done) { this.done = true; this.connection.packetReader.notifyConnectionError(e3); } } } /* access modifiers changed from: package-private */ public void cleanup() { this.connection.interceptors.clear(); this.connection.sendListeners.clear(); } /* access modifiers changed from: protected */ public void init() { this.writer = this.connection.writer; this.done = false; this.writerThread = new Thread() { /* class asmack.org.jivesoftware.smack.PacketWriter.AnonymousClass1 */ public void run() { PacketWriter.this.writePackets(this); } }; Thread thread = this.writerThread; thread.setName("Smack Packet Writer (" + this.connection.connectionCounterValue + ")"); this.writerThread.setDaemon(true); } /* access modifiers changed from: package-private */ public void openStream() throws IOException { this.writer.write("<stream:stream" + " to=\"" + this.connection.getServiceName() + "\"" + " xmlns=\"jabber:client\"" + " xmlns:stream=\"http://etherx.jabber.org/streams\"" + " version=\"1.0\">"); this.writer.flush(); } public void sendPacket(Packet packet) { if (!this.done) { this.connection.firePacketInterceptors(packet); try { this.queue.put(packet); synchronized (this.queue) { this.queue.notifyAll(); } this.connection.firePacketSendingListeners(packet); } catch (InterruptedException e2) { e2.printStackTrace(); } } } /* access modifiers changed from: package-private */ public void setWriter(Writer writer2) { this.writer = writer2; } public void shutdown() { this.done = true; synchronized (this.queue) { this.queue.notifyAll(); } } /* access modifiers changed from: package-private */ public void startKeepAliveProcess() { int keepAliveInterval = SmackConfiguration.getKeepAliveInterval(); if (keepAliveInterval > 0) { KeepAliveTask keepAliveTask = new KeepAliveTask(keepAliveInterval); this.keepAliveThread = new Thread(keepAliveTask); keepAliveTask.setThread(this.keepAliveThread); this.keepAliveThread.setDaemon(true); Thread thread = this.keepAliveThread; thread.setName("Smack Keep Alive (" + this.connection.connectionCounterValue + ")"); this.keepAliveThread.start(); } } public void startup() { this.writerThread.start(); } }