use of org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession 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());
}
use of org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession in project Smack by igniterealtime.
the class IBBTransferNegotiator method createOutgoingStream.
@Override
public OutputStream createOutgoingStream(String streamID, Jid initiator, Jid target) throws NoResponseException, XMPPErrorException, NotConnectedException, InterruptedException {
InBandBytestreamSession session = this.manager.establishSession(target, streamID);
session.setCloseBothStreamsEnabled(true);
return session.getOutputStream();
}
use of org.jivesoftware.smackx.bytestreams.ibb.InBandBytestreamSession in project Smack by igniterealtime.
the class IBBTransferNegotiator method negotiateIncomingStream.
@Override
InputStream negotiateIncomingStream(Stanza streamInitiation) throws NotConnectedException, InterruptedException {
// build In-Band Bytestream request
InBandBytestreamRequest request = new ByteStreamRequest(this.manager, (Open) streamInitiation);
// always accept the request
InBandBytestreamSession session = request.accept();
session.setCloseBothStreamsEnabled(true);
return session.getInputStream();
}
Aggregations