PageRenderTime 30ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/map/map-impl/src/test/java/org/mobicents/protocols/ss7/map/functional/SccpHarness.java

http://mobicents.googlecode.com/
Java | 272 lines | 136 code | 45 blank | 91 comment | 1 complexity | 1d55c9f91a963782ffb5ee61bc7d1109 MD5 | raw file
Possible License(s): LGPL-3.0, GPL-3.0, LGPL-2.1, GPL-2.0, CC-BY-SA-3.0, CC0-1.0, Apache-2.0, BSD-3-Clause
  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.map.functional;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import org.mobicents.protocols.ss7.mtp.Mtp3TransferPrimitive;
  26. import org.mobicents.protocols.ss7.mtp.Mtp3UserPartBaseImpl;
  27. import org.mobicents.protocols.ss7.sccp.SccpProvider;
  28. import org.mobicents.protocols.ss7.sccp.impl.RemoteSignalingPointCode;
  29. import org.mobicents.protocols.ss7.sccp.impl.RemoteSubSystem;
  30. import org.mobicents.protocols.ss7.sccp.impl.SccpResource;
  31. import org.mobicents.protocols.ss7.sccp.impl.SccpStackImpl;
  32. import org.mobicents.protocols.ss7.sccp.impl.router.Router;
  33. /**
  34. * @author amit bhayani
  35. *
  36. */
  37. public abstract class SccpHarness {
  38. protected SccpStackImpl sccpStack1 = new SccpStackImpl();
  39. protected SccpProvider sccpProvider1 = sccpStack1.getSccpProvider();
  40. protected SccpStackImpl sccpStack2 = new SccpStackImpl();
  41. protected SccpProvider sccpProvider2 = sccpStack2.getSccpProvider();
  42. // private ConcurrentLinkedQueue<byte[]> data1 = new ConcurrentLinkedQueue<byte[]>();
  43. // private ConcurrentLinkedQueue<byte[]> data2 = new ConcurrentLinkedQueue<byte[]>();
  44. private Mtp3UserPartImpl mtp3UserPart1 = new Mtp3UserPartImpl();
  45. private Mtp3UserPartImpl mtp3UserPart2 = new Mtp3UserPartImpl();
  46. protected Router router1 = new Router();
  47. protected Router router2 = new Router();
  48. protected SccpResource resource1 = new SccpResource();
  49. protected SccpResource resource2 = new SccpResource();
  50. /**
  51. *
  52. */
  53. public SccpHarness() {
  54. mtp3UserPart1.setOtherPart(mtp3UserPart2);
  55. mtp3UserPart2.setOtherPart(mtp3UserPart1);
  56. }
  57. private void setUpStack1() {
  58. try {
  59. router1.start();
  60. router1.getRules().clear();
  61. router1.getPrimaryAddresses().clear();
  62. router1.getBackupAddresses().clear();
  63. } catch (Exception e) {
  64. // ignore
  65. }
  66. resource1.start();
  67. resource1.getRemoteSpcs().clear();
  68. resource1.getRemoteSsns().clear();
  69. resource1.getRemoteSpcs().put(1, new RemoteSignalingPointCode(2, 0, 0));
  70. resource1.getRemoteSsns().put(1, new RemoteSubSystem(2, 8, 0));
  71. sccpStack1.setRouter(router1);
  72. sccpStack1.setSccpResource(resource1);
  73. sccpStack1.setLocalSpc(1);
  74. sccpStack1.setNi(2);
  75. sccpStack1.setMtp3UserPart(mtp3UserPart1);
  76. sccpStack1.start();
  77. }
  78. private void setUpStack2() {
  79. try {
  80. router2.start();
  81. router2.getRules().clear();
  82. router2.getPrimaryAddresses().clear();
  83. router2.getBackupAddresses().clear();
  84. } catch (Exception e) {
  85. // ignore
  86. }
  87. resource2.start();
  88. resource2.getRemoteSpcs().clear();
  89. resource2.getRemoteSsns().clear();
  90. resource2.getRemoteSpcs().put(1, new RemoteSignalingPointCode(1, 0, 0));
  91. resource2.getRemoteSsns().put(1, new RemoteSubSystem(1, 8, 0));
  92. sccpStack2.setRouter(router2);
  93. sccpStack2.setSccpResource(resource2);
  94. sccpStack2.setLocalSpc(2);
  95. sccpStack2.setNi(2);
  96. sccpStack2.setMtp3UserPart(mtp3UserPart2);
  97. sccpStack2.start();
  98. }
  99. private void tearDownStack1() {
  100. router1.getRules().clear();
  101. router1.getPrimaryAddresses().clear();
  102. router1.getBackupAddresses().clear();
  103. router1.stop();
  104. resource1.getRemoteSpcs().clear();
  105. resource1.getRemoteSsns().clear();
  106. resource1.stop();
  107. sccpStack1.stop();
  108. }
  109. private void tearDownStack2() {
  110. router1.getRules().clear();
  111. router1.getPrimaryAddresses().clear();
  112. router1.getBackupAddresses().clear();
  113. router1.stop();
  114. resource1.getRemoteSpcs().clear();
  115. resource1.getRemoteSsns().clear();
  116. resource1.stop();
  117. sccpStack1.stop();
  118. }
  119. public void setUp() {
  120. this.setUpStack1();
  121. this.setUpStack2();
  122. }
  123. public void tearDown() {
  124. this.tearDownStack1();
  125. this.tearDownStack2();
  126. }
  127. /**
  128. * After this method invoking all MTP traffic will be save into the file "MsgLog.txt"
  129. * file format:
  130. * [message][message]...[message]
  131. * [message] ::= { byte-length low byte, byte-length high byte, byte[] message }
  132. */
  133. public void saveTrafficInFile() {
  134. ((Mtp3UserPartImpl) this.mtp3UserPart1).saveTrafficInFile = true;
  135. ((Mtp3UserPartImpl) this.mtp3UserPart2).saveTrafficInFile = true;
  136. try {
  137. FileOutputStream fs = new FileOutputStream("MsgLog.txt", false);
  138. fs.close();
  139. } catch (Exception e) {
  140. // TODO Auto-generated catch block
  141. e.printStackTrace();
  142. }
  143. }
  144. private class Mtp3UserPartImpl extends Mtp3UserPartBaseImpl {
  145. // private ConcurrentLinkedQueue<byte[]> readFrom;
  146. // private ConcurrentLinkedQueue<byte[]> writeTo;
  147. private Mtp3UserPartImpl otherPart;
  148. private boolean saveTrafficInFile = false;
  149. Mtp3UserPartImpl() {
  150. try {
  151. this.start();
  152. } catch (Exception e) {
  153. // TODO Auto-generated catch block
  154. e.printStackTrace();
  155. }
  156. }
  157. public void setOtherPart(Mtp3UserPartImpl otherPart) {
  158. this.otherPart = otherPart;
  159. }
  160. @Override
  161. public void sendMessage(Mtp3TransferPrimitive msg) throws IOException {
  162. if (saveTrafficInFile) {
  163. try {
  164. byte[] txData = msg.encodeMtp3();
  165. FileOutputStream fs = new FileOutputStream("MsgLog.txt", true);
  166. int ln = txData.length;
  167. fs.write(ln & 0xFF);
  168. fs.write(ln >> 8);
  169. fs.write(txData);
  170. fs.close();
  171. } catch (Exception e) {
  172. // TODO Auto-generated catch block
  173. e.printStackTrace();
  174. }
  175. }
  176. this.otherPart.sendTransferMessageToLocalUser(msg, msg.getSls());
  177. }
  178. // @Override
  179. // public int read(ByteBuffer b) throws IOException {
  180. // int dataRead = 0;
  181. // while (!this.readFrom.isEmpty()) {
  182. // byte[] rxData = this.readFrom.poll();
  183. // System.out.println("Read " + Arrays.toString(rxData));
  184. // dataRead = dataRead + rxData.length;
  185. // b.put(rxData);
  186. // }
  187. // return dataRead;
  188. // }
  189. //
  190. // @Override
  191. // public int write(ByteBuffer b) throws IOException {
  192. // int dataAdded = 0;
  193. // if (b.hasRemaining()) {
  194. // byte[] txData = new byte[b.limit() - b.position()];
  195. // b.get(txData);
  196. // dataAdded = dataAdded + txData.length;
  197. // System.out.println("Write " + Arrays.toString(txData));
  198. // this.writeTo.add(txData);
  199. //
  200. // if (saveTrafficInFile) {
  201. // try {
  202. // FileOutputStream fs = new FileOutputStream("MsgLog.txt", true);
  203. // int ln = txData.length;
  204. // fs.write(ln & 0xFF);
  205. // fs.write(ln >> 8);
  206. // fs.write(txData);
  207. // fs.close();
  208. // } catch (Exception e) {
  209. // // TODO Auto-generated catch block
  210. // e.printStackTrace();
  211. // }
  212. // }
  213. // }
  214. // return dataAdded;
  215. // }
  216. // @Override
  217. // public void execute() throws IOException {
  218. // //We dont use this
  219. //
  220. // try {
  221. // //FIXME: this has to be changed to some MTP change!!!
  222. // Thread.sleep(100);
  223. // } catch( Exception e ) {
  224. // }
  225. // }
  226. }
  227. }