Search in sources :

Example 1 with Close

use of org.jivesoftware.smackx.bytestreams.ibb.packet.Close in project Smack by igniterealtime.

the class InBandBytestreamSession method closeByLocal.

/**
     * This method is invoked if one of the streams has been closed locally, if an error occurred
     * locally or if the whole session should be closed.
     * 
     * @throws IOException if an error occurs while sending the close request
     */
protected synchronized void closeByLocal(boolean in) throws IOException {
    if (this.isClosed) {
        return;
    }
    if (this.closeBothStreamsEnabled) {
        this.inputStream.closeInternal();
        this.outputStream.closeInternal(true);
    } else {
        if (in) {
            this.inputStream.closeInternal();
        } else {
            // close stream but try to send any data left
            this.outputStream.closeInternal(true);
        }
    }
    if (this.inputStream.isClosed && this.outputStream.isClosed) {
        this.isClosed = true;
        // send close request
        Close close = new Close(this.byteStreamRequest.getSessionID());
        close.setTo(this.remoteJID);
        try {
            connection.createStanzaCollectorAndSend(close).nextResultOrThrow();
        } catch (Exception e) {
            // Sadly we are unable to use the IOException(Throwable) constructor because this
            // constructor is only supported from Android API 9 on.
            IOException ioException = new IOException();
            ioException.initCause(e);
            throw ioException;
        }
        this.inputStream.cleanup();
        // remove session from manager
        // Thanks Google Error Prone for finding the bug where remove() was called with 'this' as argument. Changed
        // now to remove(byteStreamRequest.getSessionID).
        InBandBytestreamManager.getByteStreamManager(this.connection).getSessions().remove(byteStreamRequest.getSessionID());
    }
}
Also used : Close(org.jivesoftware.smackx.bytestreams.ibb.packet.Close) IOException(java.io.IOException) NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) IOException(java.io.IOException) SocketTimeoutException(java.net.SocketTimeoutException)

Example 2 with Close

use of org.jivesoftware.smackx.bytestreams.ibb.packet.Close in project Smack by igniterealtime.

the class CloseListener method handleIQRequest.

@Override
public IQ handleIQRequest(IQ iqRequest) {
    Close closeRequest = (Close) iqRequest;
    InBandBytestreamSession ibbSession = this.manager.getSessions().get(closeRequest.getSessionID());
    if (ibbSession == null) {
        try {
            this.manager.replyItemNotFoundPacket(closeRequest);
        } catch (InterruptedException | NotConnectedException e) {
            return null;
        }
    } else {
        try {
            ibbSession.closeByPeer(closeRequest);
        } catch (InterruptedException | NotConnectedException e) {
            return null;
        }
        this.manager.getSessions().remove(closeRequest.getSessionID());
    }
    return null;
}
Also used : NotConnectedException(org.jivesoftware.smack.SmackException.NotConnectedException) Close(org.jivesoftware.smackx.bytestreams.ibb.packet.Close)

Example 3 with Close

use of org.jivesoftware.smackx.bytestreams.ibb.packet.Close in project Smack by igniterealtime.

the class CloseListenerTest method shouldReplyErrorIfSessionIsUnknown.

/**
     * If a close request to an unknown session is received it should be replied
     * with an <item-not-found/> error.
     * 
     * @throws Exception should not happen
     */
@Test
public void shouldReplyErrorIfSessionIsUnknown() throws Exception {
    // mock connection
    XMPPConnection connection = mock(XMPPConnection.class);
    // initialize InBandBytestreamManager to get the CloseListener
    InBandBytestreamManager byteStreamManager = InBandBytestreamManager.getByteStreamManager(connection);
    // get the CloseListener from InBandByteStreamManager
    CloseListener closeListener = Whitebox.getInternalState(byteStreamManager, CloseListener.class);
    Close close = new Close("unknownSessionId");
    close.setFrom(initiatorJID);
    close.setTo(targetJID);
    closeListener.handleIQRequest(close);
    // wait because packet is processed in an extra thread
    Thread.sleep(200);
    // capture reply to the In-Band Bytestream close request
    ArgumentCaptor<IQ> argument = ArgumentCaptor.forClass(IQ.class);
    verify(connection).sendStanza(argument.capture());
    // assert that reply is the correct error packet
    assertEquals(initiatorJID, argument.getValue().getTo());
    assertEquals(IQ.Type.error, argument.getValue().getType());
    assertEquals(XMPPError.Condition.item_not_found, argument.getValue().getError().getCondition());
}
Also used : IQ(org.jivesoftware.smack.packet.IQ) Close(org.jivesoftware.smackx.bytestreams.ibb.packet.Close) XMPPConnection(org.jivesoftware.smack.XMPPConnection) Test(org.junit.Test)

Aggregations

Close (org.jivesoftware.smackx.bytestreams.ibb.packet.Close)3 NotConnectedException (org.jivesoftware.smack.SmackException.NotConnectedException)2 IOException (java.io.IOException)1 SocketTimeoutException (java.net.SocketTimeoutException)1 XMPPConnection (org.jivesoftware.smack.XMPPConnection)1 IQ (org.jivesoftware.smack.packet.IQ)1 Test (org.junit.Test)1