Revision: 16044
Author: [email protected]
Date: Fri Dec 24 21:11:00 2010
Log: more fixes
http://code.google.com/p/mobicents/source/detail?r=16044
Added:
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Client.java
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/ConsoleListenerImpl.java
Modified:
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Console.java
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/ConsoleListener.java
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Shell.java
=======================================
--- /dev/null
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Client.java
Fri Dec 24 21:11:00 2010
@@ -0,0 +1,115 @@
+package org.mobicents.protocols.ss7.management.console;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.nio.channels.SelectionKey;
+
+import javolution.util.FastList;
+
+import org.mobicents.ss7.management.transceiver.ChannelProvider;
+import org.mobicents.ss7.management.transceiver.ChannelSelectionKey;
+import org.mobicents.ss7.management.transceiver.ChannelSelector;
+import org.mobicents.ss7.management.transceiver.Message;
+import org.mobicents.ss7.management.transceiver.ShellChannel;
+
+public class Client {
+
+ private ChannelProvider provider;
+ private ShellChannel channel;
+ private ChannelSelector selector;
+ private ChannelSelectionKey skey;
+
+ private boolean isConnected = false;
+
+ public Client() {
+ provider = ChannelProvider.open();
+ }
+
+ public boolean isConnected() {
+ return isConnected;
+ }
+
+ public void connect(InetSocketAddress endpoint) throws IOException {
+
+ channel = provider.openChannel();
+ // channel.bind(new InetSocketAddress(address, port));
+
+ selector = provider.openSelector();
+ skey = channel.register(selector, SelectionKey.OP_READ
+ | SelectionKey.OP_WRITE);
+
+ channel.connect(endpoint);
+ if (channel.isConnectionPending()) {
+ while (!channel.isConnected()) {
+ channel.finishConnect();
+ try {
+ Thread.currentThread().sleep(1);
+ } catch (Exception e) {
+ }
+ }
+ }
+ this.isConnected = true;
+ }
+
+ public Message run(Message outgoing) throws IOException {
+
+ if (!this.isConnected) {
+ throw new IllegalStateException("Not yet connected");
+ }
+
+ int count = 3;
+
+ //Wait for 3 secs to get message
+ while (count > 0) {
+ FastList<ChannelSelectionKey> keys = selector.selectNow();
+
+ for (FastList.Node<ChannelSelectionKey> n = keys.head(), end =
keys
+ .tail(); (n = n.getNext()) != end;) {
+ ChannelSelectionKey key = n.getValue();
+ ShellChannel chan = (ShellChannel) key.channel();
+
+ if (key.isWritable()) {
+ chan.send(outgoing);
+ }
+
+ if (key.isReadable()) {
+ Message msg = (Message) chan.receive();
+ return msg;
+ }
+ }// End of For loop
+
+ try {
+ Thread.sleep(1000);
+ } catch (InterruptedException e) {
+ }
+ count--;
+ }//end of while
+ return null;
+
+ }
+
+ protected void stop() {
+ this.isConnected = false;
+
+ if (skey != null) {
+ skey.cancel();
+ skey = null;
+ }
+
+ if (selector != null) {
+ try {
+ selector.close();
+ } catch (IOException e) {
+ }
+ selector = null;
+ }
+
+ if (channel != null) {
+ try {
+ channel.close();
+ } catch (IOException e) {
+ }
+ channel = null;
+ }
+ }
+}
=======================================
--- /dev/null
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/ConsoleListenerImpl.java
Fri Dec 24 21:11:00 2010
@@ -0,0 +1,97 @@
+package org.mobicents.protocols.ss7.management.console;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import org.mobicents.ss7.management.transceiver.Message;
+import org.mobicents.ss7.management.transceiver.MessageFactory;
+
+public class ConsoleListenerImpl implements ConsoleListener {
+
+ // TODO : We still have to work on formatter and how text on CLI
should be
+ // displayed
+
+ private String address = "127.0.0.1";
+ private int port = 3435;
+
+ protected Console console;
+ private Client client = null;
+
+ private MessageFactory messageFactory = new MessageFactory();
+
+ public ConsoleListenerImpl() {
+ client = new Client();
+ }
+
+ public void setConsole(Console console) {
+ this.console = console;
+ }
+
+ public void commandEntered(String consoleInput) {
+ System.out.println(consoleInput);
+ if (consoleInput.compareTo("exit") == 0) {
+ if (this.client.isConnected()) {
+ this.console
+ .write("Still connected to server. Disconnect
first");
+ return;
+ }
+ console.stop();
+ } else if (consoleInput.startsWith("ss7 connect")) {
+
+ if (client.isConnected()) {
+ console.write("Already connected");
+ return;
+ }
+
+ String[] commands = consoleInput.split(" ");
+ if (commands.length == 2) {
+ // use default
+ try {
+ client.connect(new InetSocketAddress(address, port));
+ } catch (IOException e) {
+ console.write(e.getMessage());
+ }
+ } else if (commands.length == 4) {
+ address = commands[2];
+ port = Integer.parseInt(commands[3]);
+
+ try {
+ client.connect(new InetSocketAddress(address, port));
+ } catch (IOException e) {
+ console.write(e.getMessage());
+ }
+ } else {
+ console.write("Invalid command.");
+ }
+ } else if (consoleInput.startsWith("ss7 disconnect")) {
+ if (!client.isConnected()) {
+ console.write("Already disconnected");
+ return;
+ }
+ sendMessage("disconnect");
+
+ } else {
+ console.write("Invalid command.");
+ }
+ }
+
+ private void sendMessage(String text) {
+ Message outgoing = messageFactory.createMessage(text);
+ try {
+ Message incoming = this.client.run(outgoing);
+ if (incoming != null) {
+ this.console.write(incoming.toString());
+ } else {
+ this.console.write("No response from server");
+ }
+ } catch (IOException e) {
+ this.console.write(e.getMessage());
+ this.console.stop();
+ }
+
+ if(text.compareTo("disconnect")==0){
+ this.client.stop();
+ }
+ }
+
+}
=======================================
---
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Console.java
Fri Dec 24 01:42:01 2010
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Console.java
Fri Dec 24 21:11:00 2010
@@ -38,6 +38,7 @@
// Listener
this.consoleListener = consoleListener;
+ this.consoleListener.setConsole(this);
}
@@ -48,8 +49,8 @@
if (this.consoleListener != null) {
this.consoleListener.commandEntered(line);
}
-
- if(!this.stopped){
+
+ if (!this.stopped) {
addPrefix();
}
}
@@ -57,6 +58,7 @@
private void addPrefix() {
if (this.prefix != null) {
+ this.write("\n");
this.write(prefix);
}
}
=======================================
---
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/ConsoleListener.java
Thu Dec 23 23:04:08 2010
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/ConsoleListener.java
Fri Dec 24 21:11:00 2010
@@ -4,5 +4,7 @@
public interface ConsoleListener {
public void commandEntered(String consoleInput);
+
+ public void setConsole(Console console);
}
=======================================
---
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Shell.java
Fri Dec 24 01:37:23 2010
+++
/trunk/protocols/ss7/management/shell/src/main/java/org/mobicents/protocols/ss7/management/console/Shell.java
Fri Dec 24 21:11:00 2010
@@ -15,11 +15,11 @@
public static final String CONNECTED_MESSAGE = "Connected to %s
currently running on %s";
- public static final String cliPrefix = "Mobicents-SS7*CLI>";
+ public static final String cliPrefix = "mobicents>";
private final ConsoleListener listener;
private final Console console;
-
+
private static final InputStream in = System.in;
private static final PrintStream out = System.out;
@@ -31,17 +31,17 @@
System.out.println("-h This help screen");
}
- public Shell() {
+ public Shell() throws Exception {
listener = new ConsoleListenerImpl();
console = new Console(in, out, listener, cliPrefix);
}
- public static void main(String args[]) {
+ public static void main(String args[]) throws Exception {
Shell shell = new Shell();
shell.start(args);
}
- private void start(String args[]) {
+ private void start(String args[]) throws Exception {
// Take care of Cmd Line arguments
if (args != null && args.length > 0) {
@@ -65,15 +65,5 @@
console.start();
}
- private class ConsoleListenerImpl implements ConsoleListener {
-
- public void commandEntered(String consoleInput) {
- System.out.println(consoleInput);
- if (consoleInput.compareTo("exit") == 0) {
- console.stop();
- }
- }
-
- }
}