Changeset 920


Ignore:
Timestamp:
Nov 6, 2010, 1:29:40 PM (7 years ago)
Author:
stefan
Message:

simplified TCP connections

Location:
trunk/src/jlatexeditor/remote
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/jlatexeditor/remote/MasterNode.java

    r919 r920  
    4141      Socket socket = serverSocket.accept();
    4242
    43       BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    44       String line = reader.readLine();
     43      SocketConnection conn = new SocketConnection(socket);
     44      String line = conn.receive();
    4545      logger.info("reading: " + line);
    4646
    4747      if (line == null) {
    48         reader.close();
    49         socket.close();
     48        conn.close();
    5049        continue;
    5150      }
     
    5352      if (line.equals("register")) {
    5453        // client as slave node
    55         slaves.add(new RemoteSlave(socket, reader));
     54        slaves.add(new RemoteSlave(conn));
    5655      } else {
    5756        if (line.startsWith("open: ")) {
     
    103102        }
    104103
    105         reader.close();
    106         socket.close();
     104        conn.close();
    107105      }
    108106    }
  • trunk/src/jlatexeditor/remote/RemoteSlave.java

    r918 r920  
    1010 */
    1111public class RemoteSlave {
    12   private Socket socket;
    13   private BufferedWriter writer;
    14   private BufferedReader reader;
     12  private SocketConnection conn;
    1513
    16   public RemoteSlave (Socket socket, BufferedReader reader) throws IOException {
    17     this.socket = socket;
    18     this.reader = reader;
    19     this.writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
     14  public RemoteSlave (SocketConnection conn) throws IOException {
     15    this.conn = conn;
    2016
    21     writer.write("registered\n");
    22     writer.flush();
     17    conn.send("registered");
    2318  }
    2419
    2520  public boolean isOpen(String fileString) throws IOException {
    26     writer.write("is open?: " + fileString + "\n");
    27     writer.flush();
    28     return reader.readLine().equals("true");
     21    conn.send("is open?: " + fileString);
     22    return conn.receive().equals("true");
    2923  }
    3024
    3125  public boolean isResponsibleForFile(String fileString) throws IOException {
    32     writer.write("is responsible for?: " + fileString + "\n");
    33     writer.flush();
    34     return reader.readLine().equals("true");
     26    conn.send("is responsible for?: " + fileString);
     27    return conn.receive().equals("true");
    3528  }
    3629
    3730  public void open(String fileString) throws IOException {
    38     writer.write("open: " + fileString + "\n");
    39     writer.flush();
     31    conn.send("open: " + fileString);
    4032  }
    4133}
  • trunk/src/jlatexeditor/remote/SlaveNode.java

    r919 r920  
    2020
    2121  private JLatexEditorJFrame jle;
    22   private Socket socket;
    23   private BufferedWriter writer;
    24   private BufferedReader reader;
     22  private SocketConnection conn;
    2523
    2624  public SlaveNode (JLatexEditorJFrame jle, int port) throws IOException {
    2725    this.jle = jle;
    28     this.socket = new Socket(InetAddress.getLocalHost(), port);
    29     reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
    30     writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
    31 
    32     writer.write("register\n");
    33     writer.flush();
     26    Socket socket = new Socket(InetAddress.getLocalHost(), port);
     27    conn = new SocketConnection(socket);
     28    conn.send("register");
    3429
    3530    run();
     
    3833  public void run() throws IOException {
    3934    while (true) {
    40       String line = reader.readLine();
     35      String line = conn.receive();
    4136      logger.info("reading: " + line);
    4237      if (line == null) throw new IOException("InputStream closed");
     
    4641        File file = fileLineNr.file;
    4742
    48         if (jle.isOpen(file)) {
    49           writer.write("true\n");
    50         } else {
    51           writer.write("false\n");
    52         }
    53         writer.flush();
     43        conn.send(jle.isOpen(file) ? "true" : "false");
    5444      } else
    5545      if (line.startsWith("is responsible for?: ")) {
     
    5747        File file = fileLineNr.file;
    5848
    59         if (jle.isResponsibleFor(file)) {
    60           writer.write("true\n");
    61         } else {
    62           writer.write("false\n");
    63         }
    64         writer.flush();
     49        conn.send(jle.isResponsibleFor(file) ? "true" : "false");
    6550      } else
    6651      if (line.startsWith("open: ")) {
Note: See TracChangeset for help on using the changeset viewer.