/protocols/ss7/isup/isup-impl/src/main/java/org/mobicents/protocols/ss7/isup/impl/ISUPStackImpl.java

http://mobicents.googlecode.com/ · Java · 347 lines · 120 code · 44 blank · 183 comment · 18 complexity · 50c354ba14f3e15fe2751f7c526837df 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. /**
  23. * Start time:12:14:57 2009-09-04<br>
  24. * Project: mobicents-isup-stack<br>
  25. *
  26. * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski </a>
  27. */
  28. package org.mobicents.protocols.ss7.isup.impl;
  29. import java.io.IOException;
  30. import java.util.Properties;
  31. import org.apache.log4j.Logger;
  32. import org.mobicents.protocols.ss7.isup.CircuitManager;
  33. import org.mobicents.protocols.ss7.isup.ISUPMessageFactory;
  34. import org.mobicents.protocols.ss7.isup.ISUPParameterFactory;
  35. import org.mobicents.protocols.ss7.isup.ISUPProvider;
  36. import org.mobicents.protocols.ss7.isup.ISUPStack;
  37. import org.mobicents.protocols.ss7.isup.ParameterException;
  38. import org.mobicents.protocols.ss7.isup.impl.message.AbstractISUPMessage;
  39. import org.mobicents.protocols.ss7.mtp.Mtp3;
  40. import org.mobicents.protocols.ss7.mtp.Mtp3PausePrimitive;
  41. import org.mobicents.protocols.ss7.mtp.Mtp3ResumePrimitive;
  42. import org.mobicents.protocols.ss7.mtp.Mtp3StatusPrimitive;
  43. import org.mobicents.protocols.ss7.mtp.Mtp3TransferPrimitive;
  44. import org.mobicents.protocols.ss7.mtp.Mtp3UserPart;
  45. import org.mobicents.protocols.ss7.mtp.Mtp3UserPartListener;
  46. /**
  47. * Start time:12:14:57 2009-09-04<br>
  48. * Project: mobicents-isup-stack<br>
  49. *
  50. * @author <a href="mailto:baranowb@gmail.com">Bartosz Baranowski </a>
  51. */
  52. public class ISUPStackImpl implements ISUPStack, Mtp3UserPartListener {
  53. private Logger logger = Logger.getLogger(ISUPStackImpl.class);
  54. private State state = State.IDLE;
  55. // dont quite like the idea of so many threads... but.
  56. // private ExecutorService executor;
  57. // private ExecutorService layer3exec;
  58. // protected ConcurrentLinkedQueue<byte[]> txDataQueue = new
  59. // ConcurrentLinkedQueue<byte[]>();
  60. private Mtp3UserPart mtp3UserPart = null;
  61. private CircuitManager circuitManager = null;
  62. private ISUPProviderImpl provider;
  63. // local vars
  64. private ISUPMessageFactory messageFactory;
  65. private ISUPParameterFactory parameterFactory;
  66. public ISUPStackImpl() {
  67. super();
  68. }
  69. public ISUPProvider getIsupProvider() {
  70. return provider;
  71. }
  72. public void start() throws IllegalStateException {
  73. if (state != State.CONFIGURED) {
  74. throw new IllegalStateException("Stack has not been configured or is already running!");
  75. }
  76. if (state == State.RUNNING) {
  77. // throw new StartFailedException("Can not start stack again!");
  78. throw new IllegalStateException("Can not start stack again!");
  79. }
  80. if (this.mtp3UserPart == null) {
  81. throw new IllegalStateException("No Mtp3UserPart present!");
  82. }
  83. if (this.circuitManager == null) {
  84. throw new IllegalStateException("No CircuitManager present!");
  85. }
  86. // this.executor = Executors.newFixedThreadPool(1);
  87. // this.layer3exec = Executors.newFixedThreadPool(1);
  88. this.provider.start();
  89. // this.layer3exec.execute(new MtpStreamHandler());
  90. this.mtp3UserPart.addMtp3UserPartListener(this);
  91. this.state = State.RUNNING;
  92. }
  93. public void stop() {
  94. if (state != State.RUNNING) {
  95. throw new IllegalStateException("Stack is not running!");
  96. }
  97. // if(state == State.CONFIGURED)
  98. // {
  99. // throw new IllegalStateException("Can not stop stack again!");
  100. // }
  101. this.mtp3UserPart.removeMtp3UserPartListener(this);
  102. // this.executor.shutdown();
  103. // this.layer3exec.shutdown();
  104. this.provider.stop();
  105. this.state = State.CONFIGURED;
  106. }
  107. // ///////////////
  108. // CONF METHOD //
  109. // ///////////////
  110. /**
  111. *
  112. */
  113. public void configure(Properties props) {
  114. if (state != State.IDLE) {
  115. throw new IllegalStateException("Stack already been configured or is already running!");
  116. }
  117. this.provider = new ISUPProviderImpl(this, props);
  118. this.parameterFactory = this.provider.getParameterFactory();
  119. this.messageFactory = this.provider.getMessageFactory();
  120. this.state = State.CONFIGURED;
  121. }
  122. public Mtp3UserPart getMtp3UserPart() {
  123. return mtp3UserPart;
  124. }
  125. public void setMtp3UserPart(Mtp3UserPart mtp3UserPart) {
  126. this.mtp3UserPart = mtp3UserPart;
  127. }
  128. public void setCircuitManager(CircuitManager mgr) {
  129. this.circuitManager = mgr;
  130. }
  131. public CircuitManager getCircuitManager() {
  132. return this.circuitManager;
  133. }
  134. // ---------------- private methods and class defs
  135. /**
  136. * @param message
  137. */
  138. void send(Mtp3TransferPrimitive message) throws IOException {
  139. if (this.state != State.RUNNING)
  140. return;
  141. // here we have encoded msg, nothing more, need to add MTP3 label.
  142. // txDataQueue.add(message);
  143. try {
  144. this.mtp3UserPart.sendMessage(message);
  145. } catch (IOException e) {
  146. // log here Exceptions from MTP3 level
  147. logger.error("IOException when sending the message to MTP3 level: " + e.getMessage(), e);
  148. e.printStackTrace();
  149. throw e;
  150. }
  151. }
  152. private enum State {
  153. IDLE, CONFIGURED, RUNNING;
  154. }
  155. @Override
  156. public void onMtp3PauseMessage(Mtp3PausePrimitive arg0) {
  157. // TODO Auto-generated method stub
  158. }
  159. @Override
  160. public void onMtp3ResumeMessage(Mtp3ResumePrimitive arg0) {
  161. // TODO Auto-generated method stub
  162. }
  163. @Override
  164. public void onMtp3StatusMessage(Mtp3StatusPrimitive arg0) {
  165. // TODO Auto-generated method stub
  166. }
  167. @Override
  168. public void onMtp3TransferMessage(Mtp3TransferPrimitive mtpMsg) {
  169. // int commandCode = msu[7];
  170. // http://pt.com/page/tutorials/ss7-tutorial/mtp
  171. // byte[] payload = new byte[msu.length - 5];
  172. // System.arraycopy(msu, 5, payload, 0, payload.length);
  173. // byte sls = msu[5];
  174. // for post processing
  175. // AbstractISUPMessage msg = (AbstractISUPMessage)
  176. // messageFactory.createCommand(commandCode);
  177. // msg.decode(payload, parameterFactory);
  178. // msg.setSls(sls); // store SLS...
  179. // return msg;
  180. if (this.state != State.RUNNING)
  181. return;
  182. // process only ISUP messages
  183. if (mtpMsg.getSi() != Mtp3._SI_SERVICE_ISUP)
  184. return;
  185. // 1(SIO) + 3(RL) + 1(SLS) + 2(CIC) + 1(CODE)
  186. byte[] payload = mtpMsg.getData();
  187. int commandCode = payload[2];
  188. AbstractISUPMessage msg = (AbstractISUPMessage) messageFactory.createCommand(commandCode);
  189. try {
  190. msg.decode(payload, parameterFactory);
  191. } catch (ParameterException e) {
  192. logger.error("Error decoding of incoming Mtp3TransferPrimitive" + e.getMessage(), e);
  193. e.printStackTrace();
  194. }
  195. msg.setSls(mtpMsg.getSls()); // store SLS...
  196. provider.receive(msg);
  197. }
  198. // private class MtpStreamHandler implements Runnable {
  199. // ByteBuffer rxBuffer = ByteBuffer.allocateDirect(1000);
  200. // ByteBuffer txBuffer = ByteBuffer.allocateDirect(1000);
  201. // int rxBytes = 0;
  202. // @SuppressWarnings("unused")
  203. // int txBytes = 0;
  204. //
  205. // public void run() {
  206. // // Execute only till state is Running
  207. // while (state == State.RUNNING) {
  208. //
  209. // try {
  210. // //Execute the MTP3UserPart
  211. // mtp3UserPart.execute();
  212. //
  213. // rxBytes = 0;
  214. // rxBuffer.clear();
  215. // try {
  216. // rxBytes = mtp3UserPart.read(rxBuffer);
  217. // if (rxBytes != 0) {
  218. // byte[] data = new byte[rxBytes];
  219. // rxBuffer.flip();
  220. // rxBuffer.get(data);
  221. // MessageHandler handler = new MessageHandler(data);
  222. // executor.execute(handler);
  223. // }
  224. // } catch (IOException e) {
  225. // logger.error("Error while readig data from Mtp3UserPart", e);
  226. // }
  227. //
  228. // // Iterate till we send all data
  229. // while (!txDataQueue.isEmpty()) {
  230. // txBuffer.clear();
  231. // txBuffer.put(txDataQueue.poll());
  232. // txBuffer.flip();
  233. // try {
  234. // txBytes = mtp3UserPart.write(txBuffer);
  235. // } catch (IOException e) {
  236. // logger.error("Error while writting data to Mtp3UserPart", e);
  237. // }
  238. // }// while txDataQueue
  239. // } catch (IOException e1) {
  240. // // TODO Auto-generated catch block
  241. // e1.printStackTrace();
  242. // }
  243. // }// end of while
  244. // }
  245. // }
  246. //
  247. // private class MessageHandler implements Runnable {
  248. // // MSU as input stream
  249. // private byte[] msu;
  250. // private ISUPMessage message;
  251. //
  252. // protected MessageHandler(byte[] msu) {
  253. // this.msu = msu;
  254. //
  255. // }
  256. //
  257. // private ISUPMessage parse() throws IOException {
  258. // try {
  259. // // FIXME: change this, dont copy over and over?
  260. //
  261. // int commandCode = msu[7];// 1(SIO) + 3(RL) + 1(SLS) + 2(CIC) + 1(CODE)
  262. // // http://pt.com/page/tutorials/ss7-tutorial/mtp
  263. // byte[] payload = new byte[msu.length - 5];
  264. // System.arraycopy(msu, 5, payload, 0, payload.length);
  265. // byte sls = msu[5];
  266. // // for post processing
  267. // AbstractISUPMessage msg = (AbstractISUPMessage)
  268. // messageFactory.createCommand(commandCode);
  269. // msg.decode(payload, parameterFactory);
  270. // msg.setSls(sls); //store SLS...
  271. // return msg;
  272. //
  273. // } catch (Exception e) {
  274. // // FIXME: what should we do here? send back?
  275. // e.printStackTrace();
  276. // logger.error("Failed on data: " + Utils.hexDump(null, msu));
  277. // }
  278. // return null;
  279. // }
  280. //
  281. // public void run() {
  282. // if (message == null) {
  283. // try {
  284. // message = parse();
  285. // } catch (IOException e) {
  286. // logger.warn("Corrupted message received");
  287. // return;
  288. // }
  289. // }
  290. // // deliver to provider, so it can check up on circuit, play with
  291. // // timers and deliver.
  292. // if(message!=null)
  293. // {
  294. // try{
  295. // provider.receive(message);
  296. // }catch(Exception e)
  297. // {
  298. // //TODO: add proper answer?
  299. // }
  300. // }
  301. // }
  302. // }
  303. }