何时停止读取Telnet输入?

3
目前我有四个与基础MUD客户端相关的类:WeatherDriver是主类,LineReader用于处理InputStreamLineParser用于解析QueueString,而Connection则保存了Apache telnet连接。这是基于Apache天气Telnet示例构建的。 LineReader如何知道何时停止读取InputStream并向WeatherDriver发送消息以开始解析呢? 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 to determine EOL somehow...
    }
}

连接:

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();
    }
}

天气驱动程序:

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");
    }
}
2个回答

1
你的行读取器必须理解足够的“协议”,以便检测控制终端的程序何时需要输入。也就是说,必须有某种提示表示“行回转”。当它检测到这一点时,它停止读取并让前端执行下一个操作。
如果远程系统有不同的方式来指示它正在等待输入(不同类型的提示),并且您需要检测超时条件并采取某些特殊操作,则会变得复杂。
您可能会受益于绘制状态图,显示远程程序可以处于的各种状态以及程序输出到Telnet会话中的状态之间的转换方式。

0

借鉴了Pearson的方法,接下来的代码会输出每个字符(我想是这样)。这是printToConsole方法。

package teln;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.net.InetAddress;
import java.net.SocketException;
import org.apache.commons.net.telnet.TelnetClient;

public class Connection {

    private TelnetClient tc = new TelnetClient();
    private boolean isl = false;
    private String u;
    private String pw;
    //private StreamReader sr;
    private InputStream in;
    private PrintStream out;

    private Connection() {
    }

    public Connection(InetAddress h, int p, String prompt, String u, String pw) throws SocketException, IOException, InterruptedException {
        tc.connect(h, p);
        this.u = u;
        this.pw = pw;
        in = tc.getInputStream();
        out = new PrintStream(tc.getOutputStream());
        printToConsole();
    }

    public void printToConsole() throws IOException  {
        char ch = (char) in.read();
        while (true) {
            System.out.print(ch);
            ch = (char) in.read();
        }
    }

    public InputStream getInputStream() {
        return tc.getInputStream();
    }

    void cmd(String s) throws IllegalArgumentException, IOException {
        byte[] by = s.getBytes();
        for (Byte b : by) {
            tc.sendCommand(b);
        }
    }
}

这不是一种基于行的解决方案。 - Barışcan Kayaoğlu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接