Search in sources :

Example 1 with InBandBytestreamListener

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

the class InBandBytestreamTest method testInBandBytestreamWithMessageStanzas.

/**
 * An In-Band Bytestream should be successfully established using message stanzas.
 *
 * @throws Exception should not happen
 */
public void testInBandBytestreamWithMessageStanzas() throws Exception {
    XMPPConnection initiatorConnection = getConnection(0);
    XMPPConnection targetConnection = getConnection(1);
    // test data
    Random rand = new Random();
    final byte[] data = new byte[dataSize];
    rand.nextBytes(data);
    final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
    InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection);
    InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() {

        public void incomingBytestreamRequest(InBandBytestreamRequest request) {
            InputStream inputStream;
            try {
                inputStream = request.accept().getInputStream();
                byte[] receivedData = new byte[dataSize];
                int totalRead = 0;
                while (totalRead < dataSize) {
                    int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
                    totalRead += read;
                }
                queue.put(receivedData);
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
    InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection);
    initiatorByteStreamManager.setStanza(StanzaType.MESSAGE);
    OutputStream outputStream = initiatorByteStreamManager.establishSession(targetConnection.getUser()).getOutputStream();
    // verify stream
    outputStream.write(data);
    outputStream.flush();
    outputStream.close();
    assertEquals("received data not equal to sent data", data, queue.take());
}
Also used : Random(java.util.Random) InputStream(java.io.InputStream) SynchronousQueue(java.util.concurrent.SynchronousQueue) InBandBytestreamRequest(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest) OutputStream(java.io.OutputStream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) InBandBytestreamListener(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamListener) XMPPException(org.jivesoftware.smack.XMPPException) InBandBytestreamManager(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager)

Example 2 with InBandBytestreamListener

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

the class InBandBytestreamTest method testBiDirectionalInBandBytestream.

/**
 * An In-Band Bytestream should be successfully established using IQ stanzas. The established
 * session should transfer data bidirectional.
 *
 * @throws Exception should not happen
 */
public void testBiDirectionalInBandBytestream() throws Exception {
    XMPPConnection initiatorConnection = getConnection(0);
    XMPPConnection targetConnection = getConnection(1);
    // test data
    Random rand = new Random();
    final byte[] data = new byte[dataSize];
    rand.nextBytes(data);
    final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
    InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection);
    InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() {

        public void incomingBytestreamRequest(InBandBytestreamRequest request) {
            try {
                InBandBytestreamSession session = request.accept();
                OutputStream outputStream = session.getOutputStream();
                outputStream.write(data);
                outputStream.flush();
                InputStream inputStream = session.getInputStream();
                byte[] receivedData = new byte[dataSize];
                int totalRead = 0;
                while (totalRead < dataSize) {
                    int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
                    totalRead += read;
                }
                queue.put(receivedData);
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
    InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection);
    InBandBytestreamSession session = initiatorByteStreamManager.establishSession(targetConnection.getUser());
    // verify stream
    byte[] receivedData = new byte[dataSize];
    InputStream inputStream = session.getInputStream();
    int totalRead = 0;
    while (totalRead < dataSize) {
        int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
        totalRead += read;
    }
    assertEquals("sent data not equal to received data", data, receivedData);
    OutputStream outputStream = session.getOutputStream();
    outputStream.write(data);
    outputStream.flush();
    outputStream.close();
    assertEquals("received data not equal to sent data", data, queue.take());
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) InBandBytestreamListener(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamListener) XMPPException(org.jivesoftware.smack.XMPPException) InBandBytestreamSession(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession) Random(java.util.Random) SynchronousQueue(java.util.concurrent.SynchronousQueue) InBandBytestreamRequest(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest) InBandBytestreamManager(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager)

Example 3 with InBandBytestreamListener

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

the class InBandBytestreamTest method testInBandBytestreamWithIQStanzas.

/**
 * An In-Band Bytestream should be successfully established using IQ stanzas.
 *
 * @throws Exception should not happen
 */
public void testInBandBytestreamWithIQStanzas() throws Exception {
    XMPPConnection initiatorConnection = getConnection(0);
    XMPPConnection targetConnection = getConnection(1);
    // test data
    Random rand = new Random();
    final byte[] data = new byte[dataSize];
    rand.nextBytes(data);
    final SynchronousQueue<byte[]> queue = new SynchronousQueue<byte[]>();
    InBandBytestreamManager targetByteStreamManager = InBandBytestreamManager.getByteStreamManager(targetConnection);
    InBandBytestreamListener incomingByteStreamListener = new InBandBytestreamListener() {

        public void incomingBytestreamRequest(InBandBytestreamRequest request) {
            InputStream inputStream;
            try {
                inputStream = request.accept().getInputStream();
                byte[] receivedData = new byte[dataSize];
                int totalRead = 0;
                while (totalRead < dataSize) {
                    int read = inputStream.read(receivedData, totalRead, dataSize - totalRead);
                    totalRead += read;
                }
                queue.put(receivedData);
            } catch (Exception e) {
                fail(e.getMessage());
            }
        }
    };
    targetByteStreamManager.addIncomingBytestreamListener(incomingByteStreamListener);
    InBandBytestreamManager initiatorByteStreamManager = InBandBytestreamManager.getByteStreamManager(initiatorConnection);
    OutputStream outputStream = initiatorByteStreamManager.establishSession(targetConnection.getUser()).getOutputStream();
    // verify stream
    outputStream.write(data);
    outputStream.flush();
    outputStream.close();
    assertEquals("received data not equal to sent data", data, queue.take());
}
Also used : Random(java.util.Random) InputStream(java.io.InputStream) SynchronousQueue(java.util.concurrent.SynchronousQueue) InBandBytestreamRequest(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest) OutputStream(java.io.OutputStream) XMPPConnection(org.jivesoftware.smack.XMPPConnection) InBandBytestreamListener(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamListener) XMPPException(org.jivesoftware.smack.XMPPException) InBandBytestreamManager(org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager)

Aggregations

InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 Random (java.util.Random)3 SynchronousQueue (java.util.concurrent.SynchronousQueue)3 XMPPConnection (org.jivesoftware.smack.XMPPConnection)3 XMPPException (org.jivesoftware.smack.XMPPException)3 InBandBytestreamListener (org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamListener)3 InBandBytestreamManager (org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamManager)3 InBandBytestreamRequest (org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamRequest)3 InBandBytestreamSession (org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession)1