PageRenderTime 60ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/protocols/ss7/m3ua/impl/src/main/java/org/mobicents/protocols/ss7/m3ua/impl/sg/ServerM3UAProcess.java

http://mobicents.googlecode.com/
Java | 281 lines | 192 code | 52 blank | 37 comment | 33 complexity | 3964045972d4c90caf63936e086a56e6 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.m3ua.impl.sg;
  23. import java.io.IOException;
  24. import java.net.InetAddress;
  25. import java.net.InetSocketAddress;
  26. import java.net.SocketAddress;
  27. import java.nio.ByteBuffer;
  28. import java.nio.channels.SelectionKey;
  29. import java.util.Set;
  30. import javolution.util.FastList;
  31. import org.apache.log4j.Logger;
  32. import org.mobicents.protocols.ss7.m3ua.M3UAChannel;
  33. import org.mobicents.protocols.ss7.m3ua.M3UAProcess;
  34. import org.mobicents.protocols.ss7.m3ua.M3UAProvider;
  35. import org.mobicents.protocols.ss7.m3ua.M3UASelectionKey;
  36. import org.mobicents.protocols.ss7.m3ua.M3UASelector;
  37. import org.mobicents.protocols.ss7.m3ua.M3UAServerChannel;
  38. import org.mobicents.protocols.ss7.m3ua.impl.As;
  39. import org.mobicents.protocols.ss7.m3ua.impl.AsState;
  40. import org.mobicents.protocols.ss7.m3ua.impl.AspFactory;
  41. import org.mobicents.protocols.ss7.m3ua.impl.CommunicationListener.CommunicationState;
  42. import org.mobicents.protocols.ss7.m3ua.impl.sctp.SctpChannel;
  43. import org.mobicents.protocols.ss7.m3ua.message.M3UAMessage;
  44. import org.mobicents.protocols.ss7.m3ua.message.MessageClass;
  45. import org.mobicents.protocols.ss7.m3ua.message.MessageType;
  46. import org.mobicents.protocols.ss7.m3ua.message.transfer.PayloadData;
  47. import org.mobicents.protocols.ss7.m3ua.parameter.ProtocolData;
  48. /**
  49. *
  50. * @author amit bhayani
  51. *
  52. */
  53. public class ServerM3UAProcess implements M3UAProcess {
  54. private static Logger logger = Logger.getLogger(ServerM3UAProcess.class);
  55. private String address;
  56. private int port;
  57. private M3UAServerChannel serverChannel;
  58. private M3UASelector selector;
  59. private ServerM3UAManagement serverM3UAManagement;
  60. private boolean started = false;
  61. public ServerM3UAProcess(String address, int port) {
  62. this.address = address;
  63. this.port = port;
  64. }
  65. public ServerM3UAManagement getServerM3UAManagement() {
  66. return serverM3UAManagement;
  67. }
  68. public void setServerM3UAManagement(ServerM3UAManagement serverM3UAManagement) {
  69. this.serverM3UAManagement = serverM3UAManagement;
  70. }
  71. public int read(ByteBuffer b) throws IOException {
  72. if (!started) {
  73. throw new IOException("Client M3UAProcess is stopped");
  74. }
  75. int initialPosition = b.position();
  76. FastList<As> appServers = serverM3UAManagement.getAppServers();
  77. for (FastList.Node<As> n = appServers.head(), end = appServers.tail(); (n = n.getNext()) != end;) {
  78. As as = n.getValue();
  79. byte[] msu = as.poll();
  80. if (msu != null) {
  81. b.put(msu);
  82. break; // Remember read is only for one message
  83. }
  84. }
  85. return (b.position() - initialPosition);
  86. }
  87. public int write(ByteBuffer b) throws IOException {
  88. if (!started) {
  89. throw new IOException("Client M3UAProcess is stopped");
  90. }
  91. int write = 0;
  92. if (b.hasRemaining()) {
  93. byte[] msu = new byte[b.limit() - b.position()];
  94. write = msu.length;
  95. b.get(msu);
  96. M3UAProvider m3uaProvider = this.serverM3UAManagement.getM3uaProvider();
  97. ProtocolData data = m3uaProvider.getParameterFactory().createProtocolData(0, msu);
  98. PayloadData payload = (PayloadData) m3uaProvider.getMessageFactory().createMessage(
  99. MessageClass.TRANSFER_MESSAGES, MessageType.PAYLOAD);
  100. payload.setData(data);
  101. As as = this.serverM3UAManagement.m3uaRouter.getAs(data.getDpc(), data.getOpc(), (short) data.getSI());
  102. if (as != null && (as.getState() == AsState.ACTIVE || as.getState() == AsState.PENDING)) {
  103. payload.setRoutingContext(as.getRoutingContext());
  104. as.write(payload);
  105. } else {
  106. logger.error(String.format("No AS found for this message. Dropping message %s", payload));
  107. }
  108. }
  109. return write;
  110. }
  111. public void start() throws IOException {
  112. selector = this.serverM3UAManagement.getM3uaProvider().openSelector();
  113. serverChannel = this.serverM3UAManagement.getM3uaProvider().openServerChannel();
  114. serverChannel.bind(new InetSocketAddress(address, port));
  115. serverChannel.register(selector, SelectionKey.OP_ACCEPT);
  116. this.started = true;
  117. logger.info(String.format("Signalling Gateway Started. M3UA Bound to %s:%d", address, port));
  118. }
  119. public void stop() {
  120. // TODO : Process to bring down all ASP and AS
  121. started = false;
  122. try {
  123. selector.close();
  124. serverChannel.close();
  125. } catch (IOException e) {
  126. // TODO Auto-generated catch block
  127. e.printStackTrace();
  128. }
  129. }
  130. public void execute() throws IOException {
  131. if (!started) {
  132. return;
  133. }
  134. this.serverM3UAManagement.getM3uaScheduler().tick();
  135. FastList<M3UASelectionKey> selections = selector.selectNow();
  136. for (FastList.Node<M3UASelectionKey> n = selections.head(), end = selections.tail(); (n = n.getNext()) != end;) {
  137. M3UASelectionKey key = n.getValue();
  138. // if(!key.isValid()){
  139. // //If Key is not valid, lets go to next one
  140. // continue;
  141. // }
  142. if (key.isAcceptable()) {
  143. accept((M3UAServerChannel) key.channel());
  144. } else {
  145. if (key.isReadable()) {
  146. if (logger.isTraceEnabled()) {
  147. logger.trace("Transmitting data from SigGateway to M3UA channel");
  148. }
  149. read(key);
  150. }
  151. if (key.isWritable()) {
  152. write(key);
  153. }
  154. }
  155. }
  156. }
  157. private void accept(M3UAServerChannel serverChannel) throws IOException {
  158. boolean provisioned = false;
  159. int port = 0;
  160. InetAddress inetAddress = null;
  161. M3UAChannel channel = serverChannel.accept();
  162. Set<SocketAddress> socAddresses = ((SctpChannel) channel).getRemoteAddresses();
  163. for (SocketAddress sockAdd : socAddresses) {
  164. inetAddress = ((InetSocketAddress) sockAdd).getAddress();
  165. port = ((InetSocketAddress) sockAdd).getPort();
  166. FastList<AspFactory> aspfactories = this.serverM3UAManagement.getAspfactories();
  167. // accept connection only from provisioned IP and Port.
  168. for (FastList.Node<AspFactory> n = aspfactories.head(), end = aspfactories.tail(); (n = n.getNext()) != end
  169. && !provisioned;) {
  170. AspFactory aspFactory = n.getValue();
  171. // compare port and ip of remote with provisioned
  172. if ((port == aspFactory.getPort()) && (inetAddress.getHostAddress().compareTo(aspFactory.getIp()) == 0)) {
  173. M3UASelectionKey key = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
  174. key.attach(aspFactory);
  175. provisioned = true;
  176. ((SctpChannel) channel).setNotificationHandler(aspFactory.getAssociationHandler());
  177. logger.info("Connected with " + channel);
  178. aspFactory.onCommStateChange(CommunicationState.UP);
  179. break;
  180. }
  181. }
  182. }
  183. if (!provisioned) {
  184. logger.warn(String.format("Received connect request from non provisioned %s:%d address. Closing Channel",
  185. inetAddress.getHostAddress(), port));
  186. channel.close();
  187. }
  188. }
  189. private void read(M3UASelectionKey key) {
  190. M3UAChannel channel = null;
  191. try {
  192. channel = (M3UAChannel) key.channel();
  193. M3UAMessage message = channel.receive();
  194. ((AspFactory) key.attachment()).read(message);
  195. } catch (IOException e) {
  196. logger.error(
  197. String.format("IOException while reading for Aspfactory name=%s ",
  198. ((AspFactory) key.attachment()).getName()), e);
  199. try {
  200. channel.close();
  201. } catch (Exception ex) {
  202. // Ignore
  203. }
  204. ((AspFactory) key.attachment()).onCommStateChange(CommunicationState.LOST);
  205. }
  206. }
  207. private void write(M3UASelectionKey key) {
  208. M3UAChannel channel = null;
  209. try {
  210. channel = (M3UAChannel) key.channel();
  211. AspFactory factory = ((AspFactory) key.attachment());
  212. M3UAMessage message = null;
  213. while ((message = factory.txPoll()) != null) {
  214. channel.send(message);
  215. }
  216. } catch (IOException e) {
  217. logger.error(
  218. String.format("IOException while writting for Aspfactory name=%s ",
  219. ((AspFactory) key.attachment()).getName()), e);
  220. try {
  221. channel.close();
  222. } catch (Exception ex) {
  223. // Ignore
  224. }
  225. // TODO Transition to COMM_DOWN
  226. ((AspFactory) key.attachment()).onCommStateChange(CommunicationState.LOST);
  227. }
  228. }
  229. }