// A simple test program for the TelnetStdioRedirector class. package biz.source_code.utils; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintStream; public class TestTelnetStdioRedirector { private static PrintStream localOut; private static TelnetStdioRedirector redirector; public static void main (String[] args) throws Exception { if (args.length > 1) { throw new Exception("Invalid number of commandline arguments."); } int port = 8080; if (args.length >= 1) { port = Integer.valueOf(args[0]); } System.out.println("Main started."); localOut = System.out; redirector = new TelnetStdioRedirector(port, false, "UTF-8") { @Override public void processSessionDialog() throws Exception { TestTelnetStdioRedirector.processSessionDialog(); }}; redirector.waitForTermination(); System.out.println("Main terminating"); } private static void processSessionDialog() throws Exception { localOut.println("processSessionDialog() startet."); System.out.println("q=quit session, x=exit server, t1=test blocking readChar, t2=test non-blocking"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); while (true) { System.out.print("==> "); String s = reader.readLine(); if (s.equals("q")) { break; } else if (s.equals("x")) { redirector.stop(); break; } else if (s.equals("t1")) { test1(); } else if (s.equals("t2")) { test2(); } else { System.out.println("Input len=" + s.length() + ": \"" + s + "\""); localOut.println("Remote input: \"" + s + "\""); }} localOut.println("processSessionDialog() completed."); } private static void test1() throws Exception { System.out.println("Test1: Blocking single key input. Press q to quit."); TelnetStdioRedirector.RawConsoleReader raw = (TelnetStdioRedirector.RawConsoleReader)System.in; while (true) { int i = raw.readChar(true); if (i == 'q') { break; } System.out.println("Key = " + i); }} private static void test2() throws Exception { System.out.println("Test2: Nonblocking single key input. Press q to quit."); TelnetStdioRedirector.RawConsoleReader raw = (TelnetStdioRedirector.RawConsoleReader)System.in; while (true) { int i = raw.readChar(false); if (i == -2) { System.out.print("."); Thread.sleep(100); continue; } if (i == 'q') { break; } System.out.println("Key = " + i); } System.out.println(); } }