MetaController should Observe three objects
I'm caught in a mental loop, or at least a lack of understanding of how to
implement the observer pattern. My intended Controller implements Observer
because it's meant to, literally, observe the instances it, literally,
controls.
The observed objects extend Observable because they are meant to be
observed, literally, by the Controller. What do I invoke
.addObserver(responseHandler); on? Another class adds an observer to the
instance which, literally, implements the `Observer' interface? That can't
be right.
As an aside, is there a naming problem in this pattern as implemented in
Java, or just a lack of understanding on my part?
Here's the Controller, which has now morphed into a Controller of a
Controller:
public class MetaController implements Observer {
private final static Logger LOG =
Logger.getLogger(MetaController.class.getName());
private Printer telnetPrinter = new Printer();
private telnetDataProcessor telnetDataProcessor = new
telnetDataProcessor();
private StringReader stringReader = new StringReader();
private final ConcurrentLinkedQueue<Character> telnetData = new
ConcurrentLinkedQueue();
public MetaController() {
}
//the printer and processor each spawn their own thread so that they
don't
//block each other waiting for each other
public void readPrintParse(final InputStream inputStream) throws
SocketException, IOException {
telnetPrinter.print(inputStream, telnetData); //populate
telnetData in its own thread
telnetDataProcessor.read(telnetData); //process telnetData in its
own thread
}
//the StringReader just looks for particular keywords like "press
enter to continue"
//so that some phrases will trigger a response
//commands may also be typed manually by the user (not implemented yet)
@Override
public void update(Observable o, Object arg) {
//when telnetDataProcessor sends a String, send that String on to
stringReader
String cmd = stringReader.parse(null); //parse what and how?
//send the command string back to the telnetClient
}
}
Reading the API directly, unfortunately, is not illuminating for me.
This is an extension of an Apache WeatherTelnet example, a sort of
poor-mans Netty to allow concurrent scripted telnet and live telnet
responses for a simple MUD client, which requires live processing of a
non-terminated telnet data stream, as well as live output and user input.
No comments:
Post a Comment