PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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