3

目前我有四个用于基本 MUD 客户端的类:WeatherDriver用于主类,LineReader用于处理InputStreamLineParser解析Queueof String,同时Connection保持Apache telnet 连接。这是基于Apache Weather Telnet 示例

如何LineReader知道何时停止阅读InputStream发送消息WeatherDriver开始解析?

行阅读器:

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");
    }
}
4

2 回答 2

1

您的线路阅读器必须对“协议”有足够的了解,才能检测控制终端的程序何时需要输入。即必须有某种提示,指示“线路周转”。当它检测到这一点时,它会停止读取并让您的前端执行下一个操作。

如果远程系统有不同的方式来指示它正在等待输入(不同类型的提示),并且如果您需要检测超时条件并采取一些特殊操作,这将变得复杂。

您可能会受益于绘制一个状态图,该图显示远程程序可能处于的各种状态,以及程序的输出如何将从状态到状态的转换传达给 telnet 会话。

于 2013-08-28T20:41:44.153 回答
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);
        }
    }
}
于 2013-08-29T06:51:57.983 回答