/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/as/ClientM3UAProcess.java

http://mobicents.googlecode.com/ · Java · 213 lines · 137 code · 40 blank · 36 comment · 17 complexity · 42b83866e76acf9954bf23016a292072 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.as;
  23. import java.io.IOException;
  24. import java.nio.ByteBuffer;
  25. import java.nio.channels.ClosedSelectorException;
  26. import javolution.util.FastList;
  27. import org.apache.log4j.Logger;
  28. import org.mobicents.protocols.ss7.m3ua.M3UAChannel;
  29. import org.mobicents.protocols.ss7.m3ua.M3UAProcess;
  30. import org.mobicents.protocols.ss7.m3ua.M3UAProvider;
  31. import org.mobicents.protocols.ss7.m3ua.M3UASelectionKey;
  32. import org.mobicents.protocols.ss7.m3ua.impl.As;
  33. import org.mobicents.protocols.ss7.m3ua.impl.AspFactory;
  34. import org.mobicents.protocols.ss7.m3ua.impl.CommunicationListener.CommunicationState;
  35. import org.mobicents.protocols.ss7.m3ua.message.M3UAMessage;
  36. import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
  37. import org.mobicents.protocols.ss7.m3ua.message.MessageType;
  38. import org.mobicents.protocols.ss7.m3ua.message.transfer.PayloadData;
  39. import org.mobicents.protocols.ss7.m3ua.parameter.ProtocolData;
  40. /**
  41. *
  42. * @author amit bhayani
  43. *
  44. */
  45. public class ClientM3UAProcess implements M3UAProcess {
  46. private static Logger logger = Logger.getLogger(ClientM3UAProcess.class);
  47. private volatile boolean started = false;
  48. private ClientM3UAManagement clientM3UAManagement;
  49. public ClientM3UAProcess() {
  50. }
  51. public ClientM3UAManagement getClientM3UAManagement() {
  52. return clientM3UAManagement;
  53. }
  54. public void setClientM3UAManagement(ClientM3UAManagement clientM3UAManagement) {
  55. this.clientM3UAManagement = clientM3UAManagement;
  56. }
  57. public int read(ByteBuffer b) throws IOException {
  58. if (!started) {
  59. throw new IOException("Client M3UAProcess is stopped");
  60. }
  61. int initialPosition = b.position();
  62. FastList<As> appServers = clientM3UAManagement.getAppServers();
  63. for (FastList.Node<As> n = appServers.head(), end = appServers.tail(); (n = n.getNext()) != end;) {
  64. As as = n.getValue();
  65. byte[] msu = as.poll();
  66. if (msu != null) {
  67. b.put(msu);
  68. // Remember read is only one message at a time
  69. break;
  70. }
  71. }// for
  72. return (b.position() - initialPosition);
  73. }
  74. public int write(ByteBuffer b) throws IOException {
  75. if (!started) {
  76. throw new IOException("Client M3UAProcess is stopped");
  77. }
  78. int write = 0;
  79. if (b.hasRemaining()) {
  80. byte[] msu = new byte[b.limit() - b.position()];
  81. write = msu.length;
  82. b.get(msu);
  83. M3UAProvider m3uaProvider = this.clientM3UAManagement.getM3uaProvider();
  84. ProtocolData data = m3uaProvider.getParameterFactory().createProtocolData(0, msu);
  85. PayloadData payload = (PayloadData) m3uaProvider.getMessageFactory().createMessage(
  86. MessageClass.TRANSFER_MESSAGES, MessageType.PAYLOAD);
  87. payload.setData(data);
  88. //System.out.println(String.format("Sending payload data = %s", payload));
  89. As as = this.clientM3UAManagement.getAsForDpc(data.getDpc(), data.getSLS());
  90. if (as == null) {
  91. logger.error(String.format("Tx : No AS found for routing message %s", payload));
  92. return write;
  93. }
  94. payload.setRoutingContext(as.getRoutingContext());
  95. as.write(payload);
  96. }
  97. return write;
  98. }
  99. public void start() throws IOException {
  100. this.started = true;
  101. }
  102. public void stop() {
  103. this.started = false;
  104. }
  105. public void execute() throws IOException {
  106. if (!started) {
  107. throw new IOException("Client M3UAProcess is stopped");
  108. }
  109. clientM3UAManagement.getM3uaScheduler().tick();
  110. try{
  111. FastList<M3UASelectionKey> selections = clientM3UAManagement.selector.selectNow();
  112. for (FastList.Node<M3UASelectionKey> n = selections.head(), end = selections.tail(); (n = n.getNext()) != end;) {
  113. M3UASelectionKey key = n.getValue();
  114. // if(!key.isValid()){
  115. // //If Key is not valid, lets go to next one
  116. // continue;
  117. // }
  118. if (key.isReadable()) {
  119. if (logger.isTraceEnabled()) {
  120. logger.trace("Transmitting data from M3UA channel to hardware");
  121. }
  122. read(key);
  123. }
  124. if (key.isWritable()) {
  125. write(key);
  126. }
  127. }
  128. }catch(ClosedSelectorException cse)
  129. {
  130. //do nothing, its a valid case for competing threads here.
  131. }
  132. }
  133. private void read(M3UASelectionKey key) {
  134. M3UAChannel channel = null;
  135. try {
  136. channel = (M3UAChannel) key.channel();
  137. M3UAMessage message = channel.receive();
  138. ((AspFactory) key.attachment()).read(message);
  139. } catch (IOException e) {
  140. logger.error(
  141. String.format("IOException while reading for Aspfactory name=%s ",
  142. ((AspFactory) key.attachment()).getName()), e);
  143. try {
  144. channel.close();
  145. } catch (Exception ex) {
  146. // Ignore
  147. }
  148. ((AspFactory) key.attachment()).onCommStateChange(CommunicationState.LOST);
  149. }
  150. }
  151. private void write(M3UASelectionKey key) {
  152. M3UAChannel channel = null;
  153. try {
  154. channel = (M3UAChannel) key.channel();
  155. AspFactory factory = ((AspFactory) key.attachment());
  156. M3UAMessage message = null;
  157. while ((message = factory.txPoll()) != null) {
  158. channel.send(message);
  159. }
  160. } catch (IOException e) {
  161. logger.error(
  162. String.format("IOException while writting for Aspfactory name=%s ",
  163. ((AspFactory) key.attachment()).getName()), e);
  164. try {
  165. channel.close();
  166. } catch (Exception ex) {
  167. // Ignore
  168. }
  169. // TODO Transition to COMM_DOWN
  170. ((AspFactory) key.attachment()).onCommStateChange(CommunicationState.LOST);
  171. }
  172. }
  173. }