java - when to stop reading telnet input? -
presently have 4 classes basic mud client: weatherdriver
main class, , linereader
put handle inputstream
, lineparser
parse queue
of string
's, while connection
holds apache telnet connection. based off apache weather telnet example.
how linereader
know when stop reading inputstream
send message weatherdriver
start parsing?
linereader:
package teln; import static java.lang.system.out; import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstream; import java.io.inputstreamreader; import java.util.linkedlist; import java.util.queue; public class linereader { private string prompt = "/[#]/"; public linereader() { } public queue<string> readinputstream(inputstream inputstream) throws ioexception { inputstreamreader inputstreamreader = new inputstreamreader(inputstream); stringbuilder sb = new stringbuilder(); bufferedreader br = new bufferedreader(inputstreamreader); string line = br.readline(); queue<string> lines = new linkedlist<>(); while (line != null) { //never terminates... sb.append(line); line = br.readline(); lines.add(line); } out.println(lines); return lines; } public void setprompt(string prompt) { this.prompt = prompt; //need determine eol somehow... } }
connection:
package teln; import java.io.ioexception; import java.io.inputstream; import java.net.inetaddress; import java.net.socketexception; import org.apache.commons.net.telnet.telnetclient; public class connection { private telnetclient tc = new telnetclient(); public connection() { } public connection(inetaddress h, int p, string prompt) throws socketexception, ioexception { tc.connect(h, p); } public inputstream getinputstream() { return tc.getinputstream(); } }
weatherdriver:
package teln; import static java.lang.system.out; import java.io.ioexception; import java.io.inputstream; import java.net.inetaddress; import java.net.socketexception; import java.net.unknownhostexception; import java.util.properties; import java.util.queue; public final class weatherdriver { private static connection c; private static linereader linereader = new linereader(); private static lineparser lineparser = new lineparser(); public static void main(string[] args) throws unknownhostexception, socketexception, ioexception { properties props = propertiesreader.getprops(); inetaddress host = inetaddress.getbyname(props.getproperty("host")); int port = integer.parseint(props.getproperty("port")); string prompt = props.getproperty("prompt"); out.println("connecting..."); c = new connection(host, port, prompt); inputstream inputstream = c.getinputstream(); out.println("got stream"); queue<string> lines = linereader.readinputstream(inputstream); out.println("got lines"); lineparser.parselines(lines); out.println("parsed lines"); } }
your line reader has understand enough of "protocol" detect when program controlling terminal wants input. i.e. there must prompt of kind indicating "line turnaround". when detects stops reading , lets front-end perform next action.
this gets complex if remote system has different ways of indicating it's waiting input (different kinds of prompts) , if need detect timeout conditions , take special action.
you may benefit drawing state diagram showing various states remote program can in , how transitions state state communicated program's output telnet session.
Comments
Post a Comment