use of com.automatak.dnp3.Channel in project jboss-remoting by jboss-remoting.
the class ChannelTestBase method testSimpleWriteMethodTwoWay.
@Test
public void testSimpleWriteMethodTwoWay() throws Exception {
Byte[] bytes = new Byte[] { 1, 2, 3 };
Byte[] manipulatedBytes = new Byte[] { 2, 4, 6 };
MessageOutputStream out = sendChannel.writeMessage();
for (int i = 0; i < bytes.length; i++) {
out.write(bytes[i]);
}
out.close();
final CountDownLatch latch = new CountDownLatch(2);
final ArrayList<Byte> senderResult = new ArrayList<Byte>();
final ArrayList<Byte> receiverResult = new ArrayList<Byte>();
final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
recvChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
error.printStackTrace();
latch.countDown();
}
public void handleEnd(final Channel channel) {
System.out.println("End of channel");
latch.countDown();
}
public void handleMessage(final Channel channel, final MessageInputStream message) {
System.out.println("Message received on receiver");
try {
int i = message.read();
while (i != -1) {
receiverResult.add((byte) i);
System.out.println("read " + i);
i = message.read();
}
message.close();
MessageOutputStream out = channel.writeMessage();
try {
for (Byte b : receiverResult) {
byte send = (byte) (b * 2);
System.out.println("Sending back " + send);
out.write(send);
}
} finally {
out.close();
// close should be idempotent
out.close();
// no effect expected, since message is closed
out.flush();
}
System.out.println("Done writing");
} catch (IOException e) {
exRef.set(e);
} finally {
IoUtils.safeClose(message);
latch.countDown();
}
}
});
sendChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
error.printStackTrace();
latch.countDown();
}
public void handleEnd(final Channel channel) {
System.out.println("End of channel");
latch.countDown();
}
public void handleMessage(final Channel channel, final MessageInputStream message) {
System.out.println("Message received on sender");
try {
int i = message.read();
while (i != -1) {
senderResult.add((byte) i);
i = message.read();
}
message.close();
} catch (IOException e) {
exRef.set(e);
} finally {
IoUtils.safeClose(message);
latch.countDown();
}
}
});
latch.await();
assertNull(exRef.get());
Byte[] receiverBytes = receiverResult.toArray(new Byte[receiverResult.size()]);
assertArrayEquals(bytes, receiverBytes);
Byte[] senderBytes = senderResult.toArray(new Byte[senderResult.size()]);
assertArrayEquals(manipulatedBytes, senderBytes);
}
use of com.automatak.dnp3.Channel in project jboss-remoting by jboss-remoting.
the class ChannelTestBase method testSimpleWriteMethodWithWrappedOuputStream.
@Test
public void testSimpleWriteMethodWithWrappedOuputStream() throws Exception {
Byte[] bytes = new Byte[] { 1, 2, 3 };
FilterOutputStream out = new FilterOutputStream(sendChannel.writeMessage());
for (int i = 0; i < bytes.length; i++) {
out.write(bytes[i]);
}
// The close() method of FilterOutputStream will flush the underlying output stream before closing it,
// so we end up with two messages
out.close();
final CountDownLatch latch = new CountDownLatch(1);
final ArrayList<Byte> result = new ArrayList<Byte>();
final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
recvChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
error.printStackTrace();
latch.countDown();
}
public void handleEnd(final Channel channel) {
System.out.println("End of channel");
latch.countDown();
}
public void handleMessage(final Channel channel, final MessageInputStream message) {
System.out.println("Message received");
try {
int i = message.read();
while (i != -1) {
result.add((byte) i);
i = message.read();
}
message.close();
} catch (IOException e) {
exRef.set(e);
} finally {
IoUtils.safeClose(message);
latch.countDown();
}
}
});
latch.await();
assertNull(exRef.get());
Byte[] resultBytes = result.toArray(new Byte[result.size()]);
assertArrayEquals(bytes, resultBytes);
}
use of com.automatak.dnp3.Channel in project jboss-remoting by jboss-remoting.
the class ChannelTestBase method testEmptyMessage.
@Test
public void testEmptyMessage() throws IOException, InterruptedException {
final AtomicBoolean wasEmpty = new AtomicBoolean();
final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
final CountDownLatch latch = new CountDownLatch(1);
recvChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
error.printStackTrace();
exRef.set(error);
latch.countDown();
}
public void handleEnd(final Channel channel) {
System.out.println("End of channel");
latch.countDown();
}
public void handleMessage(final Channel channel, final MessageInputStream message) {
System.out.println("Message received");
try {
if (message.read() == -1) {
wasEmpty.set(true);
}
message.close();
} catch (IOException e) {
exRef.set(e);
} finally {
IoUtils.safeClose(message);
latch.countDown();
}
}
});
MessageOutputStream messageOutputStream = sendChannel.writeMessage();
messageOutputStream.close();
latch.await();
IOException exception = exRef.get();
if (exception != null) {
throw exception;
}
assertTrue(wasEmpty.get());
}
use of com.automatak.dnp3.Channel in project jboss-remoting by jboss-remoting.
the class ChannelTestBase method testLotsOfContent.
@Test
public void testLotsOfContent() throws IOException, InterruptedException {
final AtomicBoolean wasOk = new AtomicBoolean();
final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
final CountDownLatch latch = new CountDownLatch(1);
InputStream stream = ChannelTestBase.class.getResourceAsStream("/test-content.bin");
assertNotNull(stream);
final byte[] data;
try {
data = new byte[TEST_FILE_LENGTH];
int c = 0;
do {
int r = stream.read(data, c, TEST_FILE_LENGTH - c);
if (r == -1) {
break;
}
c += r;
} while (c < TEST_FILE_LENGTH);
stream.close();
} finally {
IoUtils.safeClose(stream);
}
recvChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
error.printStackTrace();
exRef.set(error);
latch.countDown();
}
public void handleEnd(final Channel channel) {
System.out.println("End of channel");
latch.countDown();
}
public void handleMessage(final Channel channel, final MessageInputStream message) {
new Thread(new Runnable() {
public void run() {
try {
System.out.println("Message received");
final byte[] received = new byte[TEST_FILE_LENGTH];
int c = 0;
do {
int r = message.read(received, c, TEST_FILE_LENGTH - c);
if (r == -1) {
break;
}
c += r;
} while (c < TEST_FILE_LENGTH);
message.close();
assertArrayEquals(data, received);
wasOk.set(true);
} catch (IOException e) {
exRef.set(e);
} finally {
IoUtils.safeClose(message);
latch.countDown();
}
}
}).start();
}
});
MessageOutputStream messageOutputStream = sendChannel.writeMessage();
messageOutputStream.write(data);
messageOutputStream.close();
// close should be idempotent
messageOutputStream.close();
// no effect expected, since message is closed
messageOutputStream.flush();
messageOutputStream.flush();
messageOutputStream.flush();
latch.await();
IOException exception = exRef.get();
if (exception != null) {
throw exception;
}
assertTrue(wasOk.get());
}
use of com.automatak.dnp3.Channel in project jboss-remoting by jboss-remoting.
the class ChannelTestBase method testSimpleWriteMethodFromNonInitiatingSide.
@Test
public void testSimpleWriteMethodFromNonInitiatingSide() throws Exception {
Byte[] bytes = new Byte[] { 1, 2, 3 };
MessageOutputStream out = recvChannel.writeMessage();
for (int i = 0; i < bytes.length; i++) {
out.write(bytes[i]);
}
out.close();
final CountDownLatch latch = new CountDownLatch(1);
final ArrayList<Byte> result = new ArrayList<Byte>();
final AtomicReference<IOException> exRef = new AtomicReference<IOException>();
sendChannel.receiveMessage(new Channel.Receiver() {
public void handleError(final Channel channel, final IOException error) {
error.printStackTrace();
latch.countDown();
}
public void handleEnd(final Channel channel) {
System.out.println("End of channel");
latch.countDown();
}
public void handleMessage(final Channel channel, final MessageInputStream message) {
System.out.println("Message received");
try {
int i = message.read();
while (i != -1) {
result.add((byte) i);
i = message.read();
}
message.close();
} catch (IOException e) {
exRef.set(e);
} finally {
IoUtils.safeClose(message);
latch.countDown();
}
}
});
latch.await();
assertNull(exRef.get());
Byte[] resultBytes = result.toArray(new Byte[result.size()]);
assertArrayEquals(bytes, resultBytes);
}
Aggregations