public void shouldBlockAndUnblockTest() throws InterruptedException, BrokenBarrierException, NotConnectedException, XmppStringprepException { XMPPTCPConnection connection = new XMPPTCPConnection("user", "pass", "example.org"); final PacketWriter pw = connection.new PacketWriter(); connection.packetWriter = pw; connection.packetReader = connection.new PacketReader(); connection.setWriter(new BlockingStringWriter()); pw.init(); pw.sendStreamElement(new Message()); pw.shutdown(false); shutdown = true; barrier.await();
initialOpenStreamSend.reportSuccess(); while (!done()) { Element element = nextStreamElement(); if (element == null) { continue; maybeAddToUnacknowledgedStanzas(packet); if (packet instanceof Stanza) { Stanza stanza = (Stanza) packet; maybeAddToUnacknowledgedStanzas(stanza); drainWriterQueueToUnacknowledgedStanzas(); if (!(done() || queue.isShutdown())) { writerException = e; } else {
packetWriter.shutdown(instant);
/** * Initializes the connection by creating a stanza reader and writer and opening a * XMPP stream to the server. * * @throws XMPPException if establishing a connection to the server fails. * @throws SmackException if the server fails to respond back or if there is anther error. * @throws IOException */ private void initConnection() throws IOException { boolean isFirstInitialization = packetReader == null || packetWriter == null; compressionHandler = null; // Set the reader and writer instance variables initReaderAndWriter(); if (isFirstInitialization) { packetWriter = new PacketWriter(); packetReader = new PacketReader(); } // Start the writer thread. This will open an XMPP stream to the server packetWriter.init(); // Start the reader thread. The startup() method will block until we // get an opening stream packet back from server packetReader.init(); }
/** * Sends the specified element to the server. * * @param element the element to send. * @throws NotConnectedException * @throws InterruptedException */ protected void sendStreamElement(Element element) throws NotConnectedException, InterruptedException { throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); try { queue.put(element); } catch (InterruptedException e) { // put() may throw an InterruptedException for two reasons: // 1. If the queue was shut down // 2. If the thread was interrupted // so we have to check which is the case throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); // If the method above did not throw, then the sending thread was interrupted throw e; } }
/** * Initializes the writer in order to be used. It is called at the first connection and also * is invoked if the connection is disconnected by an error. */ void init() { shutdownDone.init(); shutdownTimestamp = null; if (unacknowledgedStanzas != null) { // It's possible that there are new stanzas in the writer queue that // came in while we were disconnected but resumable, drain those into // the unacknowledged queue so that they get resent now drainWriterQueueToUnacknowledgedStanzas(); } queue.start(); Async.go(new Runnable() { @Override public void run() { writePackets(); } }, "Smack Writer (" + getConnectionCounter() + ")"); }
@Override public void run() { try { barrier.await(); pw.sendStreamElement(new Message()); // should only return after the pw was interrupted if (!shutdown) { prematureUnblocked = true; } } catch (Exception e) { } try { barrier.await(); } catch (InterruptedException | BrokenBarrierException e) { } } });
private void requestSmAcknowledgementInternal() throws NotConnectedException, InterruptedException { packetWriter.sendStreamElement(AckRequest.INSTANCE); }
@Override public void sendNonza(Nonza element) throws NotConnectedException, InterruptedException { packetWriter.sendStreamElement(element); }
private void sendSmAcknowledgementInternal() throws NotConnectedException, InterruptedException { packetWriter.sendStreamElement(new AckAnswer(clientHandledStanzasCount)); }
@Override protected void sendStanzaInternal(Stanza packet) throws NotConnectedException, InterruptedException { packetWriter.sendStreamElement(packet); if (isSmEnabled()) { for (StanzaFilter requestAckPredicate : requestAckPredicates) { if (requestAckPredicate.accept(packet)) { requestSmAcknowledgementInternal(); break; } } } }
protected void throwNotConnectedExceptionIfDoneAndResumptionNotPossible() throws NotConnectedException { final boolean done = done(); if (done) { final boolean smResumptionPossible = isSmResumptionPossible(); // Don't throw a NotConnectedException is there is an resumable stream available if (!smResumptionPossible) { throw new NotConnectedException(XMPPTCPConnection.this, "done=" + done + " smResumptionPossible=" + smResumptionPossible); } } }
@Override protected void throwNotConnectedExceptionIfAppropriate() throws NotConnectedException { if (packetWriter == null) { throw new NotConnectedException(); } packetWriter.throwNotConnectedExceptionIfDoneAndResumptionNotPossible(); }