mirror of
https://github.com/vanitasvitae/Smack.git
synced 2024-12-24 19:47:58 +01:00
JingleIBBTransportTest works
This commit is contained in:
parent
68a03aeb48
commit
60cb777ea9
6 changed files with 140 additions and 88 deletions
|
@ -266,9 +266,9 @@ public class JingleContent implements JingleTransportCallback {
|
|||
throws SmackException.NotConnectedException, InterruptedException {
|
||||
//Establish transport
|
||||
if (isReceiving()) {
|
||||
getTransport().establishIncomingBytestreamSession(connection, this);
|
||||
getTransport().establishIncomingBytestreamSession(connection, this, getParent());
|
||||
} else if (isSending()) {
|
||||
getTransport().establishOutgoingBytestreamSession(connection, this);
|
||||
getTransport().establishOutgoingBytestreamSession(connection, this, getParent());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -76,10 +76,10 @@ public abstract class JingleTransport<D extends JingleContentTransportElement> e
|
|||
|
||||
public abstract String getNamespace();
|
||||
|
||||
public abstract void establishIncomingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback)
|
||||
public abstract void establishIncomingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback, JingleSession session)
|
||||
throws SmackException.NotConnectedException, InterruptedException;
|
||||
|
||||
public abstract void establishOutgoingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback)
|
||||
public abstract void establishOutgoingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback, JingleSession session)
|
||||
throws SmackException.NotConnectedException, InterruptedException;
|
||||
|
||||
public void setPeersProposal(JingleTransport<?> peersProposal) {
|
||||
|
|
|
@ -72,9 +72,7 @@ public class JingleIBBTransport extends JingleTransport<JingleIBBTransportElemen
|
|||
}
|
||||
|
||||
@Override
|
||||
public void establishIncomingBytestreamSession(final XMPPConnection connection, final JingleTransportCallback callback) {
|
||||
final JingleSession session = getParent().getParent();
|
||||
|
||||
public void establishIncomingBytestreamSession(final XMPPConnection connection, final JingleTransportCallback callback, final JingleSession session) {
|
||||
final InBandBytestreamManager inBandBytestreamManager = InBandBytestreamManager.getByteStreamManager(connection);
|
||||
|
||||
InBandBytestreamListener bytestreamListener = new InBandBytestreamListener() {
|
||||
|
@ -104,8 +102,7 @@ public class JingleIBBTransport extends JingleTransport<JingleIBBTransportElemen
|
|||
}
|
||||
|
||||
@Override
|
||||
public void establishOutgoingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback) {
|
||||
JingleSession session = getParent().getParent();
|
||||
public void establishOutgoingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback, final JingleSession session) {
|
||||
InBandBytestreamManager inBandBytestreamManager = InBandBytestreamManager.getByteStreamManager(connection);
|
||||
inBandBytestreamManager.setDefaultBlockSize(blockSize);
|
||||
try {
|
||||
|
|
|
@ -128,14 +128,14 @@ public class JingleS5BTransport extends JingleTransport<JingleS5BTransportElemen
|
|||
}
|
||||
|
||||
@Override
|
||||
public void establishIncomingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback)
|
||||
public void establishIncomingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback, JingleSession session)
|
||||
throws SmackException.NotConnectedException, InterruptedException {
|
||||
this.callback = callback;
|
||||
establishBytestreamSession(connection);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void establishOutgoingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback)
|
||||
public void establishOutgoingBytestreamSession(XMPPConnection connection, JingleTransportCallback callback, JingleSession session)
|
||||
throws SmackException.NotConnectedException, InterruptedException {
|
||||
this.callback = callback;
|
||||
establishBytestreamSession(connection);
|
||||
|
|
|
@ -1,77 +0,0 @@
|
|||
package org.jivesoftware.smackx.jingle;
|
||||
|
||||
import static junit.framework.TestCase.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Random;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
||||
import org.jivesoftware.smackx.jingle.callbacks.JingleTransportCallback;
|
||||
import org.jivesoftware.smackx.jingle.transport.jingle_ibb.JingleIBBTransport;
|
||||
|
||||
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
|
||||
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
|
||||
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
|
||||
import org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint;
|
||||
import org.junit.Assert;
|
||||
|
||||
/**
|
||||
* Created by vanitas on 27.07.17.
|
||||
*/
|
||||
public class JingleIBBTest extends AbstractSmackIntegrationTest {
|
||||
|
||||
public JingleIBBTest(SmackIntegrationTestEnvironment environment) {
|
||||
super(environment);
|
||||
}
|
||||
|
||||
@SmackIntegrationTest
|
||||
public void testIBBTransport() throws Exception {
|
||||
JingleIBBTransport sender = new JingleIBBTransport();
|
||||
JingleIBBTransport receiver = new JingleIBBTransport();
|
||||
|
||||
final SimpleResultSyncPoint recvPoint = new SimpleResultSyncPoint();
|
||||
|
||||
final byte[] data = new byte[512];
|
||||
new Random().nextBytes(data);
|
||||
final byte[] recv = new byte[512];
|
||||
|
||||
receiver.establishIncomingBytestreamSession(conOne, new JingleTransportCallback() {
|
||||
@Override
|
||||
public void onTransportReady(BytestreamSession bytestreamSession) {
|
||||
try {
|
||||
bytestreamSession.getInputStream().read(recv);
|
||||
bytestreamSession.getInputStream().close();
|
||||
recvPoint.signal();
|
||||
} catch (IOException e) {
|
||||
fail(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTransportFailed(Exception e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString());
|
||||
recvPoint.signal();
|
||||
}
|
||||
});
|
||||
|
||||
sender.establishOutgoingBytestreamSession(conTwo, new JingleTransportCallback() {
|
||||
@Override
|
||||
public void onTransportReady(BytestreamSession bytestreamSession) {
|
||||
try {
|
||||
bytestreamSession.getOutputStream().write(data);
|
||||
} catch (IOException e) {
|
||||
fail(e.toString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTransportFailed(Exception e) {
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
recvPoint.wait(10 * 1000);
|
||||
Assert.assertArrayEquals(data, recv);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,132 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2017 Paul Schaub
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.jivesoftware.smackx.jingle;
|
||||
|
||||
import static junit.framework.TestCase.fail;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.util.Random;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.jivesoftware.smack.util.Async;
|
||||
import org.jivesoftware.smackx.bytestreams.BytestreamSession;
|
||||
import org.jivesoftware.smackx.jingle.callbacks.JingleTransportCallback;
|
||||
import org.jivesoftware.smackx.jingle.components.JingleSession;
|
||||
import org.jivesoftware.smackx.jingle.transport.jingle_ibb.JingleIBBTransport;
|
||||
import org.jivesoftware.smackx.jingle.util.Role;
|
||||
|
||||
import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
|
||||
import org.igniterealtime.smack.inttest.SmackIntegrationTest;
|
||||
import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
|
||||
import org.igniterealtime.smack.inttest.util.SimpleResultSyncPoint;
|
||||
import org.junit.Assert;
|
||||
|
||||
/**
|
||||
* Test the JingleIBBTransport in a very basic case.
|
||||
*/
|
||||
public class JingleIBBTransportTest extends AbstractSmackIntegrationTest {
|
||||
|
||||
public JingleIBBTransportTest(SmackIntegrationTestEnvironment environment) {
|
||||
super(environment);
|
||||
}
|
||||
|
||||
@SmackIntegrationTest
|
||||
public void basicJIBBTTest() throws Exception {
|
||||
JingleIBBTransport sender = new JingleIBBTransport();
|
||||
final JingleIBBTransport receiver = new JingleIBBTransport(sender.getSid(), sender.getBlockSize());
|
||||
|
||||
JingleSession senderSession = new JingleSession(null, conTwo.getUser().asFullJidOrThrow(), conOne.getUser().asFullJidOrThrow(), Role.initiator, "sid");
|
||||
JingleSession receiverSession = new JingleSession(null, conTwo.getUser().asFullJidOrThrow(), conOne.getUser().asFullJidOrThrow(), Role.responder, "sid");
|
||||
|
||||
final SimpleResultSyncPoint recvPoint = new SimpleResultSyncPoint();
|
||||
|
||||
final int size = 16000;
|
||||
final byte[] data = new byte[size];
|
||||
new Random().nextBytes(data);
|
||||
final byte[] recv = new byte[size];
|
||||
|
||||
receiver.establishIncomingBytestreamSession(conOne, new JingleTransportCallback() {
|
||||
@Override
|
||||
public void onTransportReady(final BytestreamSession bytestreamSession) {
|
||||
LOGGER.log(Level.INFO, "Receiving!");
|
||||
Async.go(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
InputStream inputStream = bytestreamSession.getInputStream();
|
||||
|
||||
byte[] buf = new byte[receiver.getBlockSize()];
|
||||
int read = 0;
|
||||
while (read < size) {
|
||||
int r = inputStream.read(buf);
|
||||
if (r >= 0) {
|
||||
System.arraycopy(buf, 0, recv, read, r);
|
||||
read += r;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
LOGGER.log(Level.INFO, "Success!");
|
||||
|
||||
bytestreamSession.getInputStream().close();
|
||||
recvPoint.signal();
|
||||
} catch (IOException e) {
|
||||
fail(e.toString());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTransportFailed(Exception e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString());
|
||||
recvPoint.signal();
|
||||
}
|
||||
}, receiverSession);
|
||||
|
||||
sender.establishOutgoingBytestreamSession(conTwo, new JingleTransportCallback() {
|
||||
@Override
|
||||
public void onTransportReady(final BytestreamSession bytestreamSession) {
|
||||
LOGGER.log(Level.INFO, "Sending!");
|
||||
Async.go(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
OutputStream outputStream = bytestreamSession.getOutputStream();
|
||||
outputStream.write(data);
|
||||
outputStream.flush();
|
||||
|
||||
} catch (IOException e) {
|
||||
fail(e.toString());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTransportFailed(Exception e) {
|
||||
LOGGER.log(Level.SEVERE, e.toString());
|
||||
}
|
||||
}, senderSession);
|
||||
|
||||
recvPoint.waitForResult(60 * 1000);
|
||||
Assert.assertArrayEquals(data, recv);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue