Revision: 16022
Author: [email protected]
Date: Thu Dec 23 00:13:37 2010
Log: Added transceiver package and client/server code with unit test
http://code.google.com/p/mobicents/source/detail?r=16022

Added:
 /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver /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/main/java/org/mobicents/ss7/management/transceiver/ShellSelectableChannel.java /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellSelectionKey.java /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellSelector.java /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellServerChannel.java
 /trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7
/trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management /trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management/transceiver /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/Message.java Thu Dec 23 00:13:37 2010
@@ -0,0 +1,21 @@
+package org.mobicents.ss7.management.transceiver;
+
+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);
+       }
+}
=======================================
--- /dev/null
+++ /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellChannel.java Thu Dec 23 00:13:37 2010
@@ -0,0 +1,135 @@
+package org.mobicents.ss7.management.transceiver;
+
+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();
+       }
+
+}
=======================================
--- /dev/null
+++ /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellProvider.java Thu Dec 23 00:13:37 2010
@@ -0,0 +1,22 @@
+package org.mobicents.ss7.management.transceiver;
+
+import java.io.IOException;
+
+public class ShellProvider {
+
+    public static ShellProvider open() {
+        return new ShellProvider();
+    }
+
+    public ShellChannel openChannel() throws IOException {
+        return ShellChannel.open(this);
+    }
+
+    public ShellServerChannel openServerChannel() throws IOException {
+        return ShellServerChannel.open(this);
+    }
+
+    public ShellSelector openSelector() throws IOException {
+        return ShellSelector.open();
+    }
+}
=======================================
--- /dev/null
+++ /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellSelectableChannel.java Thu Dec 23 00:13:37 2010
@@ -0,0 +1,23 @@
+package org.mobicents.ss7.management.transceiver;
+
+import java.nio.channels.ClosedChannelException;
+import java.nio.channels.SelectableChannel;
+import java.nio.channels.SelectionKey;
+
+public abstract class ShellSelectableChannel {
+
+    //underlying network channel
+    protected SelectableChannel channel;
+
+    /**
+     * (Non Java-doc.)
+     *
+ * @see org.mobicents.protocols.ss7.m3ua.M3UASelectableChannel#register(org.mobicents.protocols.ss7.m3ua.M3UASelector, int);
+     */
+ public ShellSelectionKey register(ShellSelector selector, int ops) throws ClosedChannelException {
+        SelectionKey k = channel.register(selector.selector, ops);
+        ShellSelectionKey key = new ShellSelectionKey(this, k);
+        k.attach(key);
+        return key;
+    }
+}
=======================================
--- /dev/null
+++ /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellSelectionKey.java Thu Dec 23 00:13:37 2010
@@ -0,0 +1,34 @@
+package org.mobicents.ss7.management.transceiver;
+
+import java.nio.channels.SelectionKey;
+
+public class ShellSelectionKey {
+       private ShellSelectableChannel shellSelectableChannel = null;
+       private SelectionKey key = null;
+
+       protected ShellSelectionKey(ShellSelectableChannel 
shellSelectableChannel,
+                       SelectionKey k) {
+               this.shellSelectableChannel = shellSelectableChannel;
+               this.key = k;
+       }
+
+       public ShellSelectableChannel channel() {
+               return shellSelectableChannel;
+       }
+
+       public boolean isAcceptable() {
+               return key.isAcceptable();
+       }
+
+       public boolean isReadable() {
+               return ((ShellChannel) shellSelectableChannel).isReadable();
+       }
+
+       public boolean isWritable() {
+               return ((ShellChannel) shellSelectableChannel).isWritable();
+       }
+
+       public void cancel() {
+               key.cancel();
+       }
+}
=======================================
--- /dev/null
+++ /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellSelector.java Thu Dec 23 00:13:37 2010
@@ -0,0 +1,54 @@
+package org.mobicents.ss7.management.transceiver;
+
+import java.io.IOException;
+import java.nio.channels.SelectionKey;
+import java.nio.channels.Selector;
+import java.nio.channels.spi.SelectorProvider;
+import java.util.Set;
+
+import javolution.util.FastList;
+
+public class ShellSelector {
+
+       protected Selector selector;
+
+ private FastList<ShellSelectionKey> list = new FastList<ShellSelectionKey>();
+
+       protected ShellSelector(Selector selector) {
+               this.selector = selector;
+       }
+
+       public static ShellSelector open() throws IOException {
+               return new 
ShellSelector(SelectorProvider.provider().openSelector());
+       }
+
+       public FastList<ShellSelectionKey> selectNow() throws IOException {
+               list.clear();
+               selector.selectNow();
+               Set<SelectionKey> selection = selector.selectedKeys();
+               for (SelectionKey key : selection) {
+                       if (key.isAcceptable()) {
+                               ShellSelectionKey k = (ShellSelectionKey) 
key.attachment();
+                               list.add(k);
+                       } else if (key.isReadable()) {
+                               ShellSelectionKey k = (ShellSelectionKey) 
key.attachment();
+                               ((ShellChannel) k.channel()).doRead();
+                               if (k.isReadable()) {
+                                       list.add(k);
+                               }
+                       } else {
+                               ShellSelectionKey k = (ShellSelectionKey) 
key.attachment();
+                               ((ShellChannel) k.channel()).doWrite();
+                               if (k.isWritable()) {
+                                       list.add(k);
+                               }
+                       }
+               }
+               selection.clear();
+               return list;
+       }
+
+       public void close() throws IOException {
+               selector.close();
+       }
+}
=======================================
--- /dev/null
+++ /trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/ss7/management/transceiver/ShellServerChannel.java Thu Dec 23 00:13:37 2010
@@ -0,0 +1,41 @@
+package org.mobicents.ss7.management.transceiver;
+
+import java.io.IOException;
+import java.net.SocketAddress;
+import java.nio.channels.ServerSocketChannel;
+import java.nio.channels.spi.AbstractSelectableChannel;
+
+import org.apache.log4j.Logger;
+
+public class ShellServerChannel extends ShellSelectableChannel {
+       private static final Logger logger = Logger
+                       .getLogger(ShellServerChannel.class);
+
+       private ShellProvider shellProvider = null;
+
+       protected ShellServerChannel(ShellProvider shellProvider,
+                       AbstractSelectableChannel channel) throws IOException {
+               this.channel = channel;
+               this.channel.configureBlocking(false);
+               this.shellProvider = shellProvider;
+       }
+
+       public static ShellServerChannel open(ShellProvider shellProvider)
+                       throws IOException {
+               return new ShellServerChannel(shellProvider, 
ServerSocketChannel.open());
+       }
+
+       public ShellChannel accept() throws IOException {
+               return new ShellChannel(shellProvider, ((ServerSocketChannel) 
channel)
+                               .accept());
+       }
+
+       public void bind(SocketAddress address) throws IOException {
+               ((ServerSocketChannel) channel).socket().bind(address);
+       }
+
+       public void close() throws IOException {
+               ((ServerSocketChannel) channel).close();
+               ((ServerSocketChannel) channel).socket().close();
+       }
+}
=======================================
--- /dev/null
+++ /trunk/protocols/ss7/management/shell/src/test/java/org/mobicents/ss7/management/transceiver/ShellTransceiverTest.java Thu Dec 23 00:13:37 2010
@@ -0,0 +1,245 @@
+package org.mobicents.ss7.management.transceiver;
+
+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;
+
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+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();
+                       }
+               }
+
+       }
+}

Reply via email to