02 Sep, 2013, THUFIR wrote in the 1st comment:
Votes: 0
Uploaded code for my the MUD client, based on Apache Telnet, to github:

https://github.com/THUFIR/TelnetConsole

I'm solved the input/output problems, but introduced an organization problem.

src/
??? connection.properties
??? telnet
??? ConsoleReader.java
??? Controller.java
??? InputStreamReader.java
??? PropertiesReader.java
??? QueueWatcher.java
??? Triggers.java



Controller:

package telnet;

import static java.lang.System.out;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Observable;
import java.util.Observer;
import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.commons.net.telnet.TelnetClient;

public final class Controller implements Observer {

private TelnetClient telnetClient = new TelnetClient();
private InputStreamReader serverReader = new InputStreamReader();
private ConsoleReader consoleReader = new ConsoleReader();
private QueueWatcher dataProcessor = new QueueWatcher();
private Triggers triggers = new Triggers();
private final ConcurrentLinkedQueue<Character> telnetData = new ConcurrentLinkedQueue();

public void readPrintParse(final InputStream inputStream) throws SocketException, IOException {
serverReader.print(inputStream, telnetData);
serverReader.addObserver(this);
consoleReader.read();
consoleReader.addObserver(this);
dataProcessor.read(telnetData);
dataProcessor.addObserver(this);
triggers.addObserver(this);
}

private void sendCommand(String command) {
try {
byte[] bytes = command.getBytes();
OutputStream outPutStream = telnetClient.getOutputStream();
outPutStream.write(bytes);
outPutStream.flush();
} catch (IOException ex) {
}
}

@Override
public void update(Observable o, Object arg) {
String command = "help";

if (o instanceof QueueWatcher) {
String data = dataProcessor.getFinalData();
out.println(data);
command = triggers.parse(data);
sendCommand(command);
}

if (o instanceof ConsoleReader) {
command = consoleReader.getCommand();
//out.println("your command\t\t" + command);
sendCommand(command);
}
}

public Controller() throws SocketException, IOException {
Properties props = PropertiesReader.getProps();
InetAddress host = InetAddress.getByName(props.getProperty("host"));
int port = Integer.parseInt(props.getProperty("port"));
telnetClient.connect(host, port);
readPrintParse(telnetClient.getInputStream());
}

public static void main(String[] args) throws SocketException, IOException {
new Controller();
}
}



Any input appreciated. I tried to break it down into components, but seem to have broken it organizationally, hence the commit to github.
02 Sep, 2013, THUFIR wrote in the 2nd comment:
Votes: 0
It's a kludge, not what I want, but seems more functional:

package telnet;

import static java.lang.System.out;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Observable;
import java.util.Observer;
import java.util.Properties;
import java.util.concurrent.ConcurrentLinkedQueue;
import org.apache.commons.net.telnet.TelnetClient;

public final class Controller implements Observer {

private TelnetClient telnetClient = new TelnetClient();
private InputStreamReader serverReader = new InputStreamReader();
private ConsoleReader consoleReader = new ConsoleReader();
private DataProcessor dataProcessor = new DataProcessor();
private Triggers triggers = new Triggers();
private final ConcurrentLinkedQueue<Character> telnetData = new ConcurrentLinkedQueue();
private OutputStream outputStream;

public void readPrintParse(final InputStream inputStream) throws SocketException, IOException {
serverReader.print(inputStream, telnetData);
consoleReader.read();
consoleReader.addObserver(this);
dataProcessor.read(telnetData);
dataProcessor.addObserver(this);
triggers.addObserver(this);
}

private void sendCommand(String command) {
out.println("Controller.command…\t" + command);
try {
byte b = 10;
if ("".equals(command)) {
outputStream.write(10);
outputStream.flush();
}
byte[] bytes = command.getBytes();
outputStream.write(bytes);
outputStream.flush();
out.println("command sent\t" + command);
} catch (IOException | NullPointerException ex) {
out.println("Controller.sendCommand.no valid command\t" + command + "\t" + ex);
}
}

@Override
public void update(Observable o, Object arg) {
String command = "help";

if (o instanceof DataProcessor) {
String data = dataProcessor.getFinalData();
if (data.length() > 2) {
command = triggers.parse(data);
sendCommand(command);
}
}

if (o instanceof ConsoleReader) {
command = consoleReader.getCommand();
sendCommand(command);
}
}

public Controller() throws SocketException, IOException {
Properties props = PropertiesReader.getProps();
InetAddress host = InetAddress.getByName(props.getProperty("host"));
int port = Integer.parseInt(props.getProperty("port"));
telnetClient.connect(host, port);
outputStream = telnetClient.getOutputStream();
readPrintParse(telnetClient.getInputStream());
}

public static void main(String[] args) throws SocketException, IOException {
new Controller();
}
}



execution is not quite there, but close (no triggers below, just user input):

thufir@dur:~/NetBeansProjects/TelnetConsole$ 
thufir@dur:~/NetBeansProjects/TelnetConsole$ java -jar dist/TelnetConsole.jar
—————————————————————————–
* Welcome to THE WEATHER UNDERGROUND telnet service! *
——————————————————————————
* *
* National Weather Service information provided by Alden Electronics, Inc. *
* and updated each minute as reports come in over our data feed. *
* *
* **Note: If you cannot get past this opening screen, you must use a *
* different version of the "telnet" program–some of the ones for IBM *
* compatible PC's have a bug that prevents proper connection. *
* *
* comments: jmasters@wunderground.com *
——————————————————————————

Press Return to continue:
Controller.command…
command sent

Press Return for menu
or enter 3 letter forecast city code– dca
Controller.command… dca
command sent dca

Controller.command…
command sent
Weather Conditions at 01:23 AM EDT on 02 Sep 2013 for Washington, DC.
Temp(F) Humidity(%) Wind(mph) Pressure(in) Weather
========================================================================
73 78% SW at 8 29.81 Rain

Forecast for Washington, MD
130 am EDT Mon Sep 2 2013

.Overnight…Cloudy with a chance of showers and thunderstorms.
Patchy fog. Lows around 70. Southwest winds around 5 mph. Chance of
rain 50 percent.
.Monday…Partly sunny. Patchy fog in the morning. A chance of
showers and thunderstorms…mainly in the afternoon. Hot with highs
around 90. West winds 5 to 10 mph. Chance of rain 50 percent.
.Monday night…Mostly cloudy with a chance of showers and
thunderstorms. Lows around 70. West winds around 5 mph. Chance of
rain 50 percent.
.Tuesday…Mostly sunny. A chance of showers and thunderstorms in
the morning. Highs in the upper 80s. Northwest winds 5 to 10 mph.
Chance of rain 40 percent.
.Tuesday night…Mostly clear. Lows in the lower 60s. Northwest
winds 5 to 10 mph.
.Wednesday…Sunny. Highs in the lower 80s.
Press Return to continue, M to return to menu, X to exit:
Controller.command…
command sent
.Wednesday night…Partly cloudy. Lows in the upper 50s.
.Thursday…Mostly sunny. Highs in the lower 80s.
.Thursday night…Partly cloudy. Lows around 60.
.Friday…Sunny. Highs in the upper 70s.
.Friday night…Mostly clear. Lows in the lower 60s.
.Saturday…Mostly sunny. Highs in the lower 80s.
.Saturday night…Partly cloudy. Lows in the lower 60s.
.Sunday…Sunny. Highs in the mid 80s.



CITY FORECAST MENU
—————————————————
1) Print forecast for selected city
2) Print climatic data for selected city
3) Display 3-letter city codes for a selected state
4) Display all 2-letter state codes
M) Return to main menu
X) Exit program
?) Help
Selection:1
Controller.command… 1
command sent 1

Controller.command…
command sent

Enter 3-letter city code: x
Controller.command… x
command sent x

Controller.command…
command sent

CITY FORECAST MENU
—————————————————
1) Print forecast for selected city
2) Print climatic data for selected city
3) Display 3-letter city codes for a selected state
4) Display all 2-letter state codes
M) Return to main menu
X) Exit program
?) Help
Selection:x
Controller.command… x
command sent x
x
Controller.command… x
command sent x
^Cthufir@dur:~/NetBeansProjects/TelnetConsole$
thufir@dur:~/NetBeansProjects/TelnetConsole$
thufir@dur:~/NetBeansProjects/TelnetConsole$



I'm not sure that it's quite clear, but there's some finnicky logic in there so that pressing "return" doesn't seem to send what the server expects, and sometimes it's necessary to enter the same command a few times.

Capturing keyboard typing:

package telnet;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Observable;
import java.util.logging.Logger;

public class ConsoleReader extends Observable {

private final static Logger LOG = Logger.getLogger(ConsoleReader.class.getName());
private String command = "help";

public ConsoleReader() {
}

public String getCommand() {
return command;
}

public void read() {
Thread read = new Thread() {

@Override
public void run() {
BufferedReader bufferedInput = new BufferedReader(new InputStreamReader(System.in));
do {
try {
command = bufferedInput.readLine();
setChanged();
notifyObservers();
} catch (IOException ex) {
System.out.println(ex);
} finally {
}
} while (true);
}
};
read.start();
}
}
0.0/2