2013-08-28 81 views
3

目前我有一个basic MUD client四类:WeatherDriver主类,并LineReader把处理的InputStreamLineParser解析StringQueue的,而Connection持有Apache telnet connection。这是基于Apache Weather Telnet example何时停止读取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(); 
    } 
} 

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

回答

1

你行的读者已经了解足够的 “协议” 的当程序控制检测终端需要输入。即必须有某种类型的提示表明“线路转换”。当它检测到它时会停止阅读,并让您的前端执行下一个操作。

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

您可以从绘制状态图中受益,该状态图显示远程程序可以处于的各种状态以及程序输出到远程登录会话的状态转换过程。

+0

请看看我的解决方法。 – Thufir

0

Took a page from 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); 
     } 
    } 
} 
+0

这不是一个基于行的解决方案。 –