/protocols/ss7/m3ua/impl/src/test/java/org/mobicents/protocols/ss7/m3ua/impl/sctp/M3UATransferTest.java

http://mobicents.googlecode.com/ · Java · 278 lines · 202 code · 48 blank · 28 comment · 17 complexity · 8f79f4f5d3a9f1e8ded91e060b7a8044 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source
  3. * Copyright 2011, Red Hat, Inc. and individual contributors
  4. * by the @authors tag. See the copyright.txt in the distribution for a
  5. * full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.mobicents.protocols.ss7.m3ua.impl.sctp;
  23. import static org.junit.Assert.assertEquals;
  24. import java.io.IOException;
  25. import java.net.InetAddress;
  26. import java.net.InetSocketAddress;
  27. import java.net.UnknownHostException;
  28. import java.nio.channels.SelectionKey;
  29. import java.util.Collection;
  30. import org.junit.After;
  31. import org.junit.AfterClass;
  32. import org.junit.Before;
  33. import org.junit.BeforeClass;
  34. import org.junit.Test;
  35. import org.mobicents.protocols.ss7.m3ua.M3UAChannel;
  36. import org.mobicents.protocols.ss7.m3ua.M3UAProvider;
  37. import org.mobicents.protocols.ss7.m3ua.M3UASelectionKey;
  38. import org.mobicents.protocols.ss7.m3ua.M3UASelector;
  39. import org.mobicents.protocols.ss7.m3ua.M3UAServerChannel;
  40. import org.mobicents.protocols.ss7.m3ua.message.M3UAMessage;
  41. import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
  42. import org.mobicents.protocols.ss7.m3ua.message.MessageType;
  43. import org.mobicents.protocols.ss7.m3ua.message.transfer.PayloadData;
  44. import org.mobicents.protocols.ss7.m3ua.parameter.ProtocolData;
  45. /**
  46. * @author amit bhayani
  47. *
  48. */
  49. public class M3UATransferTest {
  50. private InetAddress localhost;
  51. private int serverPort = 6092;
  52. private int clientPort = 6093;
  53. private Server server;
  54. private Client client;
  55. public M3UATransferTest() {
  56. }
  57. @BeforeClass
  58. public static void setUpClass() throws Exception {
  59. }
  60. @AfterClass
  61. public static void tearDownClass() throws Exception {
  62. }
  63. @Before
  64. public void setUp() throws UnknownHostException, IOException {
  65. localhost = InetAddress.getLocalHost();
  66. client = new Client(localhost, clientPort);
  67. server = new Server(localhost, serverPort);
  68. }
  69. @After
  70. public void tearDown() {
  71. }
  72. /**
  73. * Test of connect method, of class M3UASocket.
  74. */
  75. @Test
  76. @SuppressWarnings("static-access")
  77. public void testSockets() throws Exception {
  78. System.out.println("Starting server");
  79. server.start();
  80. Thread.currentThread().sleep(100);
  81. System.out.println("Connecting to server");
  82. client.connect(localhost, serverPort);
  83. System.out.println("Client connected");
  84. client.start();
  85. Thread.currentThread().sleep(3000);
  86. client.stop();
  87. server.stop();
  88. assertEquals("Hello world", server.getReceivedMessage());
  89. assertEquals("Hello world", client.getReceivedMessage());
  90. }
  91. private M3UAMessage createMessage(M3UAProvider provider, String text) {
  92. PayloadData msg = (PayloadData) provider.getMessageFactory().createMessage(MessageClass.TRANSFER_MESSAGES, MessageType.PAYLOAD);
  93. ProtocolData protocolData = provider.getParameterFactory().createProtocolData(1408, 14150, 0, 0, 0, 1, text.getBytes());
  94. msg.setData(protocolData);
  95. return msg;
  96. }
  97. private class Client implements Runnable {
  98. private M3UAProvider provider;
  99. private M3UAChannel channel;
  100. private M3UASelector selector;
  101. private M3UASelectionKey skey;
  102. private volatile boolean started = false;
  103. private String rxMessage = "";
  104. private String[] txMessage = new String[]{"Hello", " ", "world"};
  105. public Client(InetAddress address, int port) throws IOException {
  106. provider = SctpProvider.provider();
  107. channel = provider.openChannel();
  108. channel.bind(new InetSocketAddress(address, port));
  109. selector = provider.openSelector();
  110. skey = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
  111. }
  112. @SuppressWarnings("static-access")
  113. public void connect(InetAddress endpoint, int port) throws IOException {
  114. channel.connect(new InetSocketAddress(endpoint, port));
  115. if (channel.isConnectionPending()) {
  116. while (!channel.isConnected()) {
  117. channel.finishConnect();
  118. try {
  119. Thread.currentThread().sleep(10);
  120. } catch (Exception e) {
  121. }
  122. }
  123. }
  124. }
  125. public void start() {
  126. started = true;
  127. new Thread(this).start();
  128. }
  129. public void stop() throws IOException {
  130. started = false;
  131. }
  132. public String getReceivedMessage() {
  133. return rxMessage;
  134. }
  135. public void run() {
  136. int i = 0;
  137. while (started) {
  138. try {
  139. Collection<M3UASelectionKey> keys = selector.selectNow();
  140. for (M3UASelectionKey key : keys) {
  141. M3UAChannel chan = (M3UAChannel) key.channel();
  142. if (key.isReadable()) {
  143. PayloadData msg = (PayloadData) chan.receive();
  144. System.out.println("Receive message " + msg);
  145. rxMessage += new String(msg.getData().getData());
  146. }
  147. if (key.isWritable() && i < txMessage.length) {
  148. chan.send(createMessage(provider, txMessage[i++]));
  149. }
  150. }
  151. } catch (Exception e) {
  152. e.printStackTrace();
  153. }
  154. }
  155. try {
  156. skey.cancel();
  157. selector.close();
  158. channel.close();
  159. } catch (IOException e) {
  160. e.printStackTrace();
  161. }
  162. }
  163. }
  164. private class Server implements Runnable {
  165. private M3UAProvider provider;
  166. private M3UAServerChannel serverChannel;
  167. private M3UAChannel channel;
  168. private M3UASelector selector;
  169. private M3UASelectionKey skey;
  170. private volatile boolean started = false;
  171. private String rxMessage = "";
  172. private String[] txMessage = new String[]{"Hello", " ", "world"};
  173. public Server(InetAddress address, int port) throws IOException {
  174. provider = SctpProvider.provider();
  175. serverChannel = provider.openServerChannel();
  176. serverChannel.bind(new InetSocketAddress(address, port));
  177. selector = provider.openSelector();
  178. skey = serverChannel.register(selector, SelectionKey.OP_ACCEPT);
  179. }
  180. public void start() {
  181. started = true;
  182. new Thread(this).start();
  183. }
  184. public void stop() throws IOException {
  185. started = false;
  186. }
  187. public String getReceivedMessage() {
  188. return rxMessage;
  189. }
  190. private void accept() throws IOException {
  191. channel = serverChannel.accept();
  192. skey.cancel();
  193. skey = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
  194. }
  195. public void run() {
  196. int i = 0;
  197. while (started) {
  198. try {
  199. Collection<M3UASelectionKey> keys = selector.selectNow();
  200. for (M3UASelectionKey key : keys) {
  201. if (key.isAcceptable()) {
  202. M3UAServerChannel chan = (M3UAServerChannel) key.channel();
  203. accept();
  204. } else if (key.isReadable()) {
  205. M3UAChannel chan = (M3UAChannel) key.channel();
  206. PayloadData msg = (PayloadData) chan.receive();
  207. System.out.println("Receive " + msg);
  208. rxMessage += new String(msg.getData().getData());
  209. } else if (key.isWritable() && i < txMessage.length) {
  210. M3UAChannel chan = (M3UAChannel) key.channel();
  211. chan.send(createMessage(provider, txMessage[i++]));
  212. }
  213. }
  214. } catch (IOException e) {
  215. e.printStackTrace();
  216. }
  217. }
  218. try {
  219. skey.cancel();
  220. if (channel != null) {
  221. channel.close();
  222. }
  223. serverChannel.close();
  224. selector.close();
  225. } catch (IOException e) {
  226. e.printStackTrace();
  227. }
  228. }
  229. }
  230. }