Revision: 16023
Author: [email protected]
Date: Thu Dec 23 02:39:53 2010
Log: Taking care of Message stream
http://code.google.com/p/mobicents/source/detail?r=16023
Added:
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/MessageFactory.java
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management/transceiver/MessageTest.java
Modified:
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/Message.java
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellChannel.java
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellProvider.java
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management/transceiver/ShellTransceiverTest.java
=======================================
--- /dev/null
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/MessageFactory.java
Thu Dec 23 02:39:53 2010
@@ -0,0 +1,84 @@
+package org.mobicents.ss7.management.transceiver;
+
+import java.nio.ByteBuffer;
+
+public class MessageFactory {
+
+ private byte[] header = new byte[4];
+ private boolean isHeaderReady = false;
+
+ private int pos = 0;
+ private int length;
+
+ private byte[] params;
+
+ private Message message;
+
+ public Message createMessage(String message) {
+ return new Message(message.getBytes());
+ }
+
+ public Message createMessage(ByteBuffer buffer) {
+ if (!isHeaderReady) {
+ int len = Math.min(header.length - pos, buffer.remaining());
+ buffer.get(header, pos, len);
+
+ // update cursor postion in the header's buffer
+ pos += len;
+
+ // header completed?
+ isHeaderReady = pos == header.length;
+
+ if (!isHeaderReady) {
+ // no more data available
+ return null;
+ }
+
+ // obtain remaining length of the message and prepare buffer
+ length = ((header[0] & 0xff << 24) | (header[1] & 0xff << 16)
+ | (header[2] & 0xff << 8) | (header[3] & 0xff)) - 4;
+
+ params = new byte[length];
+
+ // finally switch cursor position
+ pos = 0;
+
+ message = new Message();
+ }
+
+ // at this point we must recheck remainder of the input buffer
+ // because possible case when input buffer fits exactly to the
header
+ if (!buffer.hasRemaining()) {
+ return null;
+ }
+
+ // again, reading all parameters before parsing
+
+ // compute available or required data
+ int len = Math.min(params.length, buffer.remaining());
+ buffer.get(params, pos, len);
+
+ // update cursor position
+ pos += len;
+
+ // end of message not reached
+ if (pos < params.length) {
+ return null;
+ }
+
+ // end of message reached and most probably some data remains in
buffer
+ // do not touch remainder of the input buffer, next call to this
method
+ // will proceed remainder
+
+ // parsing params of this message
+ message.decode(params);
+
+ // switch factory for receiving new message
+ this.isHeaderReady = false;
+ this.pos = 0;
+
+ // return
+ return message;
+ }
+
+}
=======================================
--- /dev/null
+++
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management/transceiver/MessageTest.java
Thu Dec 23 02:39:53 2010
@@ -0,0 +1,53 @@
+package org.mobicents.ss7.management.transceiver;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class MessageTest {
+ private MessageFactory messageFactory = new MessageFactory();
+
+ public MessageTest() {
+ }
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ }
+
+ @AfterClass
+ public static void tearDownClass() throws Exception {
+ }
+
+ @Before
+ public void setUp() {
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ /**
+ * Test of getOpc method, of class ProtocolDataImpl.
+ */
+ @Test
+ public void testMessage() throws IOException {
+ ByteBuffer buffer = ByteBuffer.allocate(8192);
+
+ Message msg = messageFactory.createMessage("Some message");
+ msg.encode(buffer);
+
+ buffer.flip();
+
+ Message msg1 = messageFactory.createMessage(buffer);
+
+
+ assertEquals(msg.toString(), msg1.toString());
+ }
+}
=======================================
---
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/Message.java
Thu Dec 23 00:13:37 2010
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/Message.java
Thu Dec 23 02:39:53 2010
@@ -3,19 +3,37 @@
import java.nio.ByteBuffer;
public class Message {
- private byte[] data = null;
-
- protected Message(byte[] data) {
- this.data = data;
-
- }
-
- protected void encode(ByteBuffer txBuffer) {
- txBuffer.put(data);
- }
-
- @Override
- public String toString() {
- return new String(data);
- }
-}
+ private byte[] data = null;
+
+
+ protected Message() {
+
+ }
+
+ protected Message(byte[] data) {
+ this.data = data;
+ }
+
+
+ protected void decode(byte[] data) {
+ this.data = data;
+ }
+
+ protected void encode(ByteBuffer txBuffer) {
+ txBuffer.position(4); // Length int
+ txBuffer.put(data);
+
+ int length = txBuffer.position();
+
+ txBuffer.rewind();
+
+ txBuffer.putInt(length);
+
+ txBuffer.position(length);
+ }
+
+ @Override
+ public String toString() {
+ return new String(data);
+ }
+}
=======================================
---
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellChannel.java
Thu Dec 23 00:13:37 2010
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellChannel.java
Thu Dec 23 02:39:53 2010
@@ -3,133 +3,128 @@
import java.io.IOException;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
-import java.nio.channels.SelectableChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.AbstractSelectableChannel;
import java.util.concurrent.ConcurrentLinkedQueue;
public class ShellChannel extends ShellSelectableChannel {
- // Queue for incoming messages
- protected ConcurrentLinkedQueue<Message> rxQueue = new
ConcurrentLinkedQueue<Message>();
- // Queue for outgoing messages
- protected ConcurrentLinkedQueue<Message> txQueue = new
ConcurrentLinkedQueue<Message>();
-
- // receiver buffer
- private ByteBuffer rxBuffer = ByteBuffer.allocateDirect(8192);
- // transmittor buffer
- private ByteBuffer txBuffer = ByteBuffer.allocateDirect(8192);
-
- // provider instance
- private ShellProvider provider;
-
- protected ShellChannel(ShellProvider provider,
- AbstractSelectableChannel channel) throws IOException {
- this.channel = channel;
- this.channel.configureBlocking(false);
- this.provider = provider;
-
- // clean transmission buffer
- txBuffer.clear();
- txBuffer.rewind();
- txBuffer.flip();
-
- // clean receiver buffer
- rxBuffer.clear();
- rxBuffer.rewind();
- rxBuffer.flip();
- }
-
- public static ShellChannel open(ShellProvider provider) throws
IOException {
- return new ShellChannel(provider, SocketChannel.open());
- }
-
- public Message receive() throws IOException {
- return rxQueue.poll();
- }
-
- public void send(Message message) throws IOException {
- txQueue.offer(message);
- }
-
- protected boolean isReadable() {
- return !rxQueue.isEmpty();
- }
-
- protected boolean isWritable() {
- return txQueue.isEmpty();
- }
-
- protected void doRead() throws IOException {
- // clean rx buffer
- rxBuffer.clear();
-
- // reading data from socketChannel
- int len = ((SocketChannel) channel).read(rxBuffer);
- if (len == -1) {
- // socketChannel closed by remote peer
- ((SocketChannel) channel).close();
- rxQueue.clear();
-
- return;
- }
-
- rxBuffer.flip();
-
- // split stream on to the messages
- //while (rxBuffer.position() < rxBuffer.limit()) {
- // try to read message
-
- //TODO : we may not have read all data. Take care of this
- int readLength = rxBuffer.limit();
- byte[] buff = new byte[readLength];
- rxBuffer.get(buff, 0, readLength);
-
- Message message = new Message(buff);
- if (message != null) {
- rxQueue.offer(message);
- }
- //}
- }
-
- protected void doWrite() throws IOException {
- if (txBuffer.hasRemaining()) {
- ((SocketChannel) channel).write(txBuffer);
- } else if (!txQueue.isEmpty()) {
- Message msg = txQueue.poll();
-
- txBuffer.clear();
- txBuffer.rewind();
-
- msg.encode(txBuffer);
- txBuffer.flip();
- ((SocketChannel) channel).write(txBuffer);
- }
- }
-
- public void bind(SocketAddress address) throws IOException {
- ((SocketChannel) channel).socket().bind(address);
- }
-
- public boolean connect(SocketAddress remote) throws IOException {
- return ((SocketChannel) channel).connect(remote);
- }
-
- public boolean finishConnect() throws IOException {
- return ((SocketChannel) channel).finishConnect();
- }
-
- public boolean isConnected() {
- return ((SocketChannel) channel).isConnected();
- }
-
- public boolean isConnectionPending() {
- return ((SocketChannel) channel).isConnectionPending();
- }
-
- public void close() throws IOException {
- ((SocketChannel) channel).close();
- ((SocketChannel) channel).socket().close();
- }
+ // Queue for incoming messages
+ protected ConcurrentLinkedQueue<Message> rxQueue = new
ConcurrentLinkedQueue<Message>();
+ // Queue for outgoing messages
+ protected ConcurrentLinkedQueue<Message> txQueue = new
ConcurrentLinkedQueue<Message>();
+
+ // receiver buffer
+ private ByteBuffer rxBuffer = ByteBuffer.allocateDirect(8192);
+ // transmittor buffer
+ private ByteBuffer txBuffer = ByteBuffer.allocateDirect(8192);
+
+ // provider instance
+ private ShellProvider provider;
+
+ protected ShellChannel(ShellProvider provider,
+ AbstractSelectableChannel channel) throws IOException {
+ this.channel = channel;
+ this.channel.configureBlocking(false);
+ this.provider = provider;
+
+ // clean transmission buffer
+ txBuffer.clear();
+ txBuffer.rewind();
+ txBuffer.flip();
+
+ // clean receiver buffer
+ rxBuffer.clear();
+ rxBuffer.rewind();
+ rxBuffer.flip();
+ }
+
+ public static ShellChannel open(ShellProvider provider) throws
IOException {
+ return new ShellChannel(provider, SocketChannel.open());
+ }
+
+ public Message receive() throws IOException {
+ return rxQueue.poll();
+ }
+
+ public void send(Message message) throws IOException {
+ txQueue.offer(message);
+ }
+
+ protected boolean isReadable() {
+ return !rxQueue.isEmpty();
+ }
+
+ protected boolean isWritable() {
+ return txQueue.isEmpty();
+ }
+
+ protected void doRead() throws IOException {
+ // clean rx buffer
+ rxBuffer.clear();
+
+ // reading data from socketChannel
+ int len = ((SocketChannel) channel).read(rxBuffer);
+ if (len == -1) {
+ // socketChannel closed by remote peer
+ ((SocketChannel) channel).close();
+ rxQueue.clear();
+
+ return;
+ }
+
+ rxBuffer.flip();
+
+ // split stream on to the messages
+ while (rxBuffer.position() < rxBuffer.limit()) {
+ // try to read message
+
+ Message message =
this.provider.getMessageFactory().createMessage(
+ rxBuffer);
+ if (message != null) {
+ rxQueue.offer(message);
+ }
+ }
+ }
+
+ protected void doWrite() throws IOException {
+ if (txBuffer.hasRemaining()) {
+ ((SocketChannel) channel).write(txBuffer);
+ } else if (!txQueue.isEmpty()) {
+ Message msg = txQueue.poll();
+
+ txBuffer.clear();
+ txBuffer.rewind();
+
+ msg.encode(txBuffer);
+ txBuffer.flip();
+ ((SocketChannel) channel).write(txBuffer);
+ }
+ }
+
+ public void bind(SocketAddress address) throws IOException {
+ ((SocketChannel) channel).socket().bind(address);
+ }
+
+ public boolean connect(SocketAddress remote) throws IOException {
+ return ((SocketChannel) channel).connect(remote);
+ }
+
+ public boolean finishConnect() throws IOException {
+ return ((SocketChannel) channel).finishConnect();
+ }
+
+ public boolean isConnected() {
+ return ((SocketChannel) channel).isConnected();
+ }
+
+ public boolean isConnectionPending() {
+ return ((SocketChannel) channel).isConnectionPending();
+ }
+
+ public void close() throws IOException {
+ ((SocketChannel) channel).close();
+ ((SocketChannel) channel).socket().close();
+ }
}
=======================================
---
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellProvider.java
Thu Dec 23 00:13:37 2010
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellProvider.java
Thu Dec 23 02:39:53 2010
@@ -3,6 +3,8 @@
import java.io.IOException;
public class ShellProvider {
+
+ private MessageFactory messageFactory = new MessageFactory();
public static ShellProvider open() {
return new ShellProvider();
@@ -19,4 +21,8 @@
public ShellSelector openSelector() throws IOException {
return ShellSelector.open();
}
-}
+
+ public MessageFactory getMessageFactory(){
+ return messageFactory;
+ }
+}
=======================================
---
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management/transceiver/ShellTransceiverTest.java
Thu Dec 23 00:13:37 2010
+++
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management/transceiver/ShellTransceiverTest.java
Thu Dec 23 02:39:53 2010
@@ -1,11 +1,12 @@
package org.mobicents.ss7.management.transceiver;
+import static org.junit.Assert.assertEquals;
+
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.nio.channels.SelectionKey;
-import java.util.Collection;
import javolution.util.FastList;
@@ -15,231 +16,236 @@
import org.junit.BeforeClass;
import org.junit.Test;
-import static org.junit.Assert.*;
-
public class ShellTransceiverTest {
- private InetAddress localhost;
-
- private int serverPort = 9082;
- private int clientPort = 9083;
-
- private Server server;
- private Client client;
-
- public ShellTransceiverTest() {
- }
-
- @BeforeClass
- public static void setUpClass() throws Exception {
- }
-
- @AfterClass
- public static void tearDownClass() throws Exception {
- }
-
- @Before
- public void setUp() throws UnknownHostException, IOException {
- localhost = InetAddress.getLocalHost();
-
- client = new Client(localhost, clientPort);
- server = new Server(localhost, serverPort);
-
- }
-
- @After
- public void tearDown() {
- }
-
- /**
- * Test of connect method, of class M3UASocket.
- */
- @Test
- @SuppressWarnings("static-access")
- public void testSockets() throws Exception {
- System.out.println("Starting server");
- server.start();
- Thread.currentThread().sleep(100);
-
- System.out.println("Connecting to server");
- client.connect(localhost, serverPort);
- System.out.println("Client connected");
-
- client.start();
-
- Thread.currentThread().sleep(3000);
-
- client.stop();
- server.stop();
-
- assertEquals("Hello world", server.getReceivedMessage());
- assertEquals("Hello world", client.getReceivedMessage());
- }
-
- private class Client implements Runnable {
- private ShellProvider provider;
- private ShellChannel channel;
- private ShellSelector selector;
- private ShellSelectionKey skey;
- private volatile boolean started = false;
-
- private String rxMessage = "";
- private String[] txMessage = new String[] { "Hello", " ",
"world" };
-
- public Client(InetAddress address, int port) throws IOException
{
- provider = ShellProvider.open();
- channel = provider.openChannel();
- channel.bind(new InetSocketAddress(address, port));
-
- selector = provider.openSelector();
- skey = channel.register(selector, SelectionKey.OP_READ
- | SelectionKey.OP_WRITE);
- }
-
- @SuppressWarnings("static-access")
- public void connect(InetAddress endpoint, int port) throws
IOException {
- channel.connect(new InetSocketAddress(endpoint, port));
- if (channel.isConnectionPending()) {
- while (!channel.isConnected()) {
- channel.finishConnect();
- try {
-
Thread.currentThread().sleep(10);
- } catch (Exception e) {
- }
- }
- }
- }
-
- public void start() {
- started = true;
- new Thread(this).start();
- }
-
- public void stop() throws IOException {
- started = false;
- }
-
- public String getReceivedMessage() {
- return rxMessage;
- }
-
- public void run() {
- int i = 0;
- while (started) {
- try {
- FastList<ShellSelectionKey> keys =
selector.selectNow();
-
- for (FastList.Node<ShellSelectionKey> n
= keys.head(), end = keys
- .tail(); (n =
n.getNext()) != end;) {
- ShellSelectionKey key =
n.getValue();
- ShellChannel chan =
(ShellChannel) key.channel();
- if (key.isReadable()) {
- Message msg = (Message)
chan.receive();
- System.out.println("Receive
message " + msg);
- rxMessage +=
msg.toString();
- }
-
- if (key.isWritable() && i <
txMessage.length) {
- chan.send(new
Message(txMessage[i++].getBytes()));
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- try {
- skey.cancel();
- selector.close();
- channel.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
-
- private class Server implements Runnable {
-
- private ShellProvider provider;
- private ShellServerChannel serverChannel;
- private ShellChannel channel;
- private ShellSelector selector;
- private ShellSelectionKey skey;
-
- private volatile boolean started = false;
-
- private String rxMessage = "";
- private String[] txMessage = new String[] { "Hello", " ",
"world" };
-
- public Server(InetAddress address, int port) throws IOException
{
- provider = ShellProvider.open();
- serverChannel = provider.openServerChannel();
- serverChannel.bind(new InetSocketAddress(address,
port));
-
- selector = provider.openSelector();
- skey = serverChannel.register(selector,
SelectionKey.OP_ACCEPT);
- }
-
- public void start() {
- started = true;
- new Thread(this).start();
- }
-
- public void stop() throws IOException {
- started = false;
- }
-
- public String getReceivedMessage() {
- return rxMessage;
- }
-
- private void accept() throws IOException {
- channel = serverChannel.accept();
- skey.cancel();
- skey = channel.register(selector, SelectionKey.OP_READ
- | SelectionKey.OP_WRITE);
- }
-
- public void run() {
- int i = 0;
- while (started) {
- try {
- FastList<ShellSelectionKey> keys =
selector.selectNow();
-
- for (FastList.Node<ShellSelectionKey> n
= keys.head(), end = keys
- .tail(); (n =
n.getNext()) != end;) {
-
- ShellSelectionKey key =
n.getValue();
-
- if (key.isAcceptable()) {
- ShellServerChannel chan
= (ShellServerChannel) key
-
.channel();
- accept();
- } else if (key.isReadable()) {
- ShellChannel chan =
(ShellChannel) key.channel();
- Message msg = (Message)
chan.receive();
- System.out.println("Receive
" + msg);
- rxMessage +=
msg.toString();
- } else if (key.isWritable() && i
< txMessage.length) {
- ShellChannel chan =
(ShellChannel) key.channel();
- chan.send(new
Message(txMessage[i++].getBytes()));
- }
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- try {
- skey.cancel();
- if (channel != null) {
- channel.close();
- }
- serverChannel.close();
- selector.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
-}
+ private InetAddress localhost;
+
+ private int serverPort = 9082;
+ private int clientPort = 9083;
+
+ private Server server;
+ private Client client;
+
+ public ShellTransceiverTest() {
+ }
+
+ @BeforeClass
+ public static void setUpClass() throws Exception {
+ }
+
+ @AfterClass
+ public static void tearDownClass() throws Exception {
+ }
+
+ @Before
+ public void setUp() throws UnknownHostException, IOException {
+ localhost = InetAddress.getLocalHost();
+
+ client = new Client(localhost, clientPort);
+ server = new Server(localhost, serverPort);
+
+ }
+
+ @After
+ public void tearDown() {
+ }
+
+ private Message createMessage(ShellProvider provider, String text) {
+ Message msg = provider.getMessageFactory().createMessage(text);
+ return msg;
+ }
+
+ /**
+ * Test of connect method, of class M3UASocket.
+ */
+ @Test
+ @SuppressWarnings("static-access")
+ public void testSockets() throws Exception {
+ System.out.println("Starting server");
+ server.start();
+ Thread.currentThread().sleep(100);
+
+ System.out.println("Connecting to server");
+ client.connect(localhost, serverPort);
+ System.out.println("Client connected");
+
+ client.start();
+
+ Thread.currentThread().sleep(3000);
+
+ client.stop();
+ server.stop();
+
+ assertEquals("Hello world", server.getReceivedMessage());
+ assertEquals("Hello world", client.getReceivedMessage());
+ }
+
+ private class Client implements Runnable {
+ private ShellProvider provider;
+ private ShellChannel channel;
+ private ShellSelector selector;
+ private ShellSelectionKey skey;
+ private volatile boolean started = false;
+
+ private String rxMessage = "";
+ private String[] txMessage = new String[] { "Hello", " ", "world"
};
+
+ public Client(InetAddress address, int port) throws IOException {
+ provider = ShellProvider.open();
+ channel = provider.openChannel();
+ channel.bind(new InetSocketAddress(address, port));
+
+ selector = provider.openSelector();
+ skey = channel.register(selector, SelectionKey.OP_READ
+ | SelectionKey.OP_WRITE);
+ }
+
+ @SuppressWarnings("static-access")
+ public void connect(InetAddress endpoint, int port) throws
IOException {
+ channel.connect(new InetSocketAddress(endpoint, port));
+ if (channel.isConnectionPending()) {
+ while (!channel.isConnected()) {
+ channel.finishConnect();
+ try {
+ Thread.currentThread().sleep(10);
+ } catch (Exception e) {
+ }
+ }
+ }
+ }
+
+ public void start() {
+ started = true;
+ new Thread(this).start();
+ }
+
+ public void stop() throws IOException {
+ started = false;
+ }
+
+ public String getReceivedMessage() {
+ return rxMessage;
+ }
+
+ public void run() {
+ int i = 0;
+ while (started) {
+ try {
+ FastList<ShellSelectionKey> keys =
selector.selectNow();
+
+ for (FastList.Node<ShellSelectionKey> n = keys.head(),
end = keys
+ .tail(); (n = n.getNext()) != end;) {
+ ShellSelectionKey key = n.getValue();
+ ShellChannel chan = (ShellChannel) key.channel();
+ if (key.isReadable()) {
+ Message msg = (Message) chan.receive();
+ System.out.println("Receive message " + msg);
+ rxMessage += msg.toString();
+ }
+
+ if (key.isWritable() && i < txMessage.length) {
+ chan.send(createMessage(this.provider,
+ txMessage[i++]));
+ }
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ try {
+ skey.cancel();
+ selector.close();
+ channel.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+ }
+
+ private class Server implements Runnable {
+
+ private ShellProvider provider;
+ private ShellServerChannel serverChannel;
+ private ShellChannel channel;
+ private ShellSelector selector;
+ private ShellSelectionKey skey;
+
+ private volatile boolean started = false;
+
+ private String rxMessage = "";
+ private String[] txMessage = new String[] { "Hello", " ", "world"
};
+
+ public Server(InetAddress address, int port) throws IOException {
+ provider = ShellProvider.open();
+ serverChannel = provider.openServerChannel();
+ serverChannel.bind(new InetSocketAddress(address, port));
+
+ selector = provider.openSelector();
+ skey = serverChannel.register(selector,
SelectionKey.OP_ACCEPT);
+ }
+
+ public void start() {
+ started = true;
+ new Thread(this).start();
+ }
+
+ public void stop() throws IOException {
+ started = false;
+ }
+
+ public String getReceivedMessage() {
+ return rxMessage;
+ }
+
+ private void accept() throws IOException {
+ channel = serverChannel.accept();
+ skey.cancel();
+ skey = channel.register(selector, SelectionKey.OP_READ
+ | SelectionKey.OP_WRITE);
+ }
+
+ public void run() {
+ int i = 0;
+ while (started) {
+ try {
+ FastList<ShellSelectionKey> keys =
selector.selectNow();
+
+ for (FastList.Node<ShellSelectionKey> n = keys.head(),
end = keys
+ .tail(); (n = n.getNext()) != end;) {
+
+ ShellSelectionKey key = n.getValue();
+
+ if (key.isAcceptable()) {
+ ShellServerChannel chan = (ShellServerChannel)
key
+ .channel();
+ accept();
+ } else if (key.isReadable()) {
+ ShellChannel chan = (ShellChannel)
key.channel();
+ Message msg = (Message) chan.receive();
+ System.out.println("Receive " + msg);
+ rxMessage += msg.toString();
+ } else if (key.isWritable() && i <
txMessage.length) {
+ ShellChannel chan = (ShellChannel)
key.channel();
+ chan.send(createMessage(this.provider,
+ txMessage[i++]));
+ }
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ try {
+ skey.cancel();
+ if (channel != null) {
+ channel.close();
+ }
+ serverChannel.close();
+ selector.close();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ }
+}