public static void main(String[] args) throws IOException, DeploymentException, InterruptedException {
final ClientManager clientManager = new ClientManager();
try {
final ConsoleReader console = new ConsoleReader(
NAME,
new FileInputStream(FileDescriptor.in), System.out, null);
console.addCompleter(new StringsCompleter("open", "close", "send", "ping", "exit", "quit", "help"));
console.setPrompt(getPrompt());
if (args.length > 0) {
int i = 0;
String arg;
while (i < args.length && args[i].startsWith("--")) {
arg = args[i++];
if (arg.equals("--proxy")) {
if (i < args.length) {
final String proxyUrl = args[i++];
clientManager.getProperties().put(ClientManager.PROXY_URI, proxyUrl);
} else {
ClientCli.print(console, null, String.format("--proxy requires an argument (url)"), false);
}
}
if (arg.equals("--help")) {
String help = "\n"
+ "\nUsage: cmd [--proxy proxyUrl] [ws uri]"
+ "\n"
+ "\nruntime commands:"
+ "\n\topen uri : open a connection to the web socket uri"
+ "\n\tclose : close a currently open web socket session"
+ "\n\tsend message : send a text message"
+ "\n\tsend : send a multiline text message teminated with a ."
+ "\n\tping : send a ping message"
+ "\n\tquit | exit : exit this tool"
+ "\n\thelp : display this message";
ClientCli.print(console, null, help, false);
return;
}
}
if (i == (args.length - 1)) {
connectToURI(console, args[i], clientManager);
console.getHistory().add("open " + args[i]);
console.setPrompt(getPrompt());
i++;
}
if (i != args.length) {
ClientCli.print(console, null, String.format("Invalid argument count, usage cmd [--proxy proxyUrl] [ws uri]"), false);
return;
}
}
String line;
mainLoop:
while ((line = console.readLine()) != null) {
try {
// Get rid of extranious white space
line = line.trim();
if (line.length() == 0) {
// Do nothing
} else if (line.startsWith("open ")) {
final String uri = line.substring(5).trim();
connectToURI(console, uri, clientManager);
} else if (line.startsWith("close")) {
if (session != null) {
session.close();
}
session = null;
ClientCli.print(console, null, String.format("Session closed"), false);
} else if (line.startsWith("send ")) {
final String message = line.substring(5);
if (session != null) {
session.getBasicRemote().sendText(message);
}
// Multiline send, complets on the full stop
} else if (line.startsWith("send")) {
ClientCli.print(console, null, String.format("End multiline message with . on own line"), false);
String temporaryPrompt = "send...> ";
console.setPrompt(temporaryPrompt);
StringBuilder sb = new StringBuilder();
String subLine;
while (!".".equals((subLine = console.readLine()))) {
sb.append(subLine);
sb.append('\n');
}
// Send message
if (session != null) {
session.getBasicRemote().sendText(sb.toString());
}
} else if (line.startsWith("ping")) {
if (session != null) {
session.getBasicRemote().sendPing(ByteBuffer.wrap("tyrus-client-ping".getBytes("UTF-8")));
}
} else if (line.startsWith("exit") || line.startsWith("quit")) {
break mainLoop;
} else if (line.startsWith("help")) {
String help = ""
+ "\n\topen uri : open a connection to the web socket uri"
+ "\n\tclose : close a currently open web socket session"
+ "\n\tsend message : send a text message"
+ "\n\tsend : send a multiline text message teminated with a ."
+ "\n\tping : send a ping message"
+ "\n\tquit | exit : exit this tool"
+ "\n\thelp : display this message"
+ "\n\t";
ClientCli.print(console, null, help, false);
} else {
ClientCli.print(console, null, "Unable to parse given command.", false);
}
} catch (IOException e) {
ClientCli.print(console, null, String.format("IOException: %s", e.getMessage()), false);
}
// Restore prompt
console.setPrompt(getPrompt());
}
} finally {
try {
TerminalFactory.get().restore();