/protocols/ss7/sgw/gateway/src/main/java/org/mobicents/ss7/sgw/ShellExecutor.java

http://mobicents.googlecode.com/ · Java · 225 lines · 149 code · 48 blank · 28 comment · 24 complexity · 4804100515075a54f35a3ec70e44d5d8 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.ss7.sgw;
  23. import java.io.IOException;
  24. import java.net.InetSocketAddress;
  25. import java.nio.channels.SelectionKey;
  26. import javolution.util.FastSet;
  27. import org.apache.log4j.Logger;
  28. import org.mobicents.protocols.ss7.m3ua.impl.oam.M3UAShellExecutor;
  29. import org.mobicents.ss7.linkset.oam.LinksetExecutor;
  30. import org.mobicents.ss7.management.console.Subject;
  31. import org.mobicents.ss7.management.transceiver.ChannelProvider;
  32. import org.mobicents.ss7.management.transceiver.ChannelSelectionKey;
  33. import org.mobicents.ss7.management.transceiver.ChannelSelector;
  34. import org.mobicents.ss7.management.transceiver.Message;
  35. import org.mobicents.ss7.management.transceiver.MessageFactory;
  36. import org.mobicents.ss7.management.transceiver.ShellChannel;
  37. import org.mobicents.ss7.management.transceiver.ShellServerChannel;
  38. /**
  39. *
  40. * @author amit bhayani
  41. *
  42. */
  43. public class ShellExecutor {
  44. Logger logger = Logger.getLogger(ShellExecutor.class);
  45. private ChannelProvider provider;
  46. private ShellServerChannel serverChannel;
  47. private ShellChannel channel;
  48. private ChannelSelector selector;
  49. private ChannelSelectionKey skey;
  50. private MessageFactory messageFactory = null;
  51. private String rxMessage = "";
  52. private String txMessage = "";
  53. private LinksetExecutor linksetExecutor = null;
  54. private M3UAShellExecutor m3UAShellExecutor = null;
  55. private volatile boolean started = false;
  56. private String address;
  57. private int port;
  58. public ShellExecutor() throws IOException {
  59. }
  60. public String getAddress() {
  61. return address;
  62. }
  63. public void setAddress(String address) {
  64. this.address = address;
  65. }
  66. public int getPort() {
  67. return port;
  68. }
  69. public void setPort(int port) {
  70. this.port = port;
  71. }
  72. public LinksetExecutor getLinksetExecutor() {
  73. return linksetExecutor;
  74. }
  75. public void setLinksetExecutor(LinksetExecutor linksetExecutor) {
  76. this.linksetExecutor = linksetExecutor;
  77. }
  78. public M3UAShellExecutor getM3UAShellExecutor() {
  79. return m3UAShellExecutor;
  80. }
  81. public void setM3UAShellExecutor(M3UAShellExecutor shellExecutor) {
  82. m3UAShellExecutor = shellExecutor;
  83. }
  84. public void start() throws IOException {
  85. provider = ChannelProvider.provider();
  86. serverChannel = provider.openServerChannel();
  87. InetSocketAddress inetSocketAddress = new InetSocketAddress(address, port);
  88. serverChannel.bind(inetSocketAddress);
  89. selector = provider.openSelector();
  90. skey = serverChannel.register(selector, SelectionKey.OP_ACCEPT);
  91. messageFactory = ChannelProvider.provider().getMessageFactory();
  92. this.logger.info(String.format("ShellExecutor listening at %s", inetSocketAddress));
  93. this.started = true;
  94. }
  95. public void stop() {
  96. this.started = false;
  97. try {
  98. skey.cancel();
  99. if (channel != null) {
  100. channel.close();
  101. }
  102. serverChannel.close();
  103. selector.close();
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. public void perform() {
  109. if (started) {
  110. try {
  111. FastSet<ChannelSelectionKey> keys = selector.selectNow();
  112. for (FastSet.Record record = keys.head(), end = keys.tail(); (record = record.getNext()) != end;) {
  113. ChannelSelectionKey key = (ChannelSelectionKey) keys.valueOf(record);
  114. if (key.isAcceptable()) {
  115. accept();
  116. } else if (key.isReadable()) {
  117. ShellChannel chan = (ShellChannel) key.channel();
  118. Message msg = (Message) chan.receive();
  119. if (msg != null) {
  120. System.out.println("Receive " + msg);
  121. rxMessage = msg.toString();
  122. if (rxMessage.compareTo("disconnect") == 0) {
  123. this.txMessage = "Bye";
  124. chan.send(messageFactory.createMessage(txMessage));
  125. } else {
  126. String[] options = rxMessage.split(" ");
  127. Subject subject = Subject.getSubject(options[0]);
  128. if (subject == null) {
  129. chan.send(messageFactory.createMessage("Invalid Subject"));
  130. } else {
  131. // Nullify examined options
  132. options[0] = null;
  133. switch (subject) {
  134. case LINKSET:
  135. this.txMessage = this.linksetExecutor.execute(options);
  136. chan.send(messageFactory.createMessage(this.txMessage));
  137. break;
  138. case M3UA:
  139. this.txMessage = this.m3UAShellExecutor.execute(options);
  140. chan.send(messageFactory.createMessage(this.txMessage));
  141. break;
  142. }
  143. }
  144. } // if (rxMessage.compareTo("disconnect")
  145. } // if (msg != null)
  146. // TODO Handle message
  147. rxMessage = "";
  148. } else if (key.isWritable() && txMessage.length() > 0) {
  149. if (this.txMessage.compareTo("Bye") == 0) {
  150. this.closeChannel();
  151. }
  152. this.txMessage = "";
  153. }
  154. }
  155. } catch (IOException e) {
  156. logger.error("Error while operating on ChannelSelectionKey", e);
  157. }
  158. }
  159. }
  160. private void accept() throws IOException {
  161. channel = serverChannel.accept();
  162. skey.cancel();
  163. skey = channel.register(selector, SelectionKey.OP_READ | SelectionKey.OP_WRITE);
  164. System.out.println("Client connected");
  165. }
  166. private void closeChannel() throws IOException {
  167. if (channel != null) {
  168. try {
  169. this.channel.close();
  170. } catch (IOException e) {
  171. logger.error("Error closing channel", e);
  172. }
  173. }
  174. skey.cancel();
  175. skey = serverChannel.register(selector, SelectionKey.OP_ACCEPT);
  176. }
  177. }