[prev in list] [next in list] [prev in thread] [next in thread]
List: jakarta-commons-user
Subject: [net] TelnetClient usage, and IOUtil et. al.
From: thufir <hawat.thufir () gmail ! com>
Date: 2013-09-03 15:52:23
Message-ID: l050jn$7bh$1 () ger ! gmane ! org
[Download RAW message or body]
I was contacted offlist to suggest that, if want Command Parsing reacting
to Events not to use TelnetClient, but and Event Based System?
here's the MUD client I'm working on:
https://github.com/THUFIR/TelnetConsole
my controller:
package telnet;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Iterator;
import java.util.Observable;
import java.util.Observer;
import java.util.Properties;
import java.util.Queue;
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 Regex regex = new Regex();
private final ConcurrentLinkedQueue<Character> telnetData = new
ConcurrentLinkedQueue();
private OutputStream outputStream;
private Fight fight = new Fight();
private Stats s = Stats.INSTANCE;
public void readPrintParse(final InputStream inputStream) throws
SocketException, IOException {
serverReader.print(inputStream, telnetData);
consoleReader.read();
consoleReader.addObserver(this);
dataProcessor.read(telnetData);
dataProcessor.addObserver(this);
}
private void sendCommand(String command) {
if (command != null) {
try {
byte b = 10;
byte[] bytes = command.getBytes();
outputStream.write(bytes);
outputStream.write(10);
outputStream.flush();
} 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();
regex.parse(data);
Queue<String> commands = fight.getCommands();
Iterator<String> it = commands.iterator();
while (it.hasNext()) {
command = commands.remove();
//System.out.print("\tsending\t" + command);
sendCommand(command);
}
s.peace();
}
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();
}
}
which is getting a bit out of hand. If there's a better API, please do
let me know. Or, if there's a better way of using TelnetClient, please
let me know.
I had to have break up printing output to screen from reacting to output
from TelnetClient into threads. Could either of these get made to work
for this:
http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/
output/TeeOutputStream.html
I can't see how to get the split OutputStream, or
http://commons.apache.org/proper/commons-net/apidocs/org/apache/commons/
net/io/Util.html#copyStream%28java.io.InputStream,%20java.io.OutputStream%
29
seems to also not really let you "get" the copied OutputStream.
Maybe I'm not using the Apache API correctly
---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic