[Commit] RRClient RRBoard.java,1.6,1.7 RRClient.java,1.4,1.5 RRNetIn.java,1.1,1.2 RRNetOut.java,1.1,1.2

Bart Massey commit at keithp.com
Sun Jun 8 04:31:27 PDT 2003


Committed by: bart

Update of /local/src/CVS/RRClient
In directory home.keithp.com:/tmp/cvs-serv27804

Modified Files:
	RRBoard.java RRClient.java RRNetIn.java RRNetOut.java 
Log Message:
It lives!  Talks to the server, displays the board.



Index: RRBoard.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRBoard.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- RRBoard.java	8 Jun 2003 09:02:57 -0000	1.6
+++ RRBoard.java	8 Jun 2003 10:31:23 -0000	1.7
@@ -80,10 +80,6 @@
 	}
 	int c = s.charAt(ci++);
 	expect(c, ' ');
-	if (i == dim) {
-	    c = s.charAt(ci++);
-	    expect(c, '"');
-	}
     }
 
     void target_line(String s, int i)
@@ -219,13 +215,14 @@
       throws IOException {
 	goal = null;
 	StringTokenizer st = new StringTokenizer(board, "\n");
+	String s;
 	for (int i = 0; i < dim; i++) {
-	    String s = st.nextToken();
+	    s = st.nextToken();
 	    wall_line(s, i);
 	    s = st.nextToken();
 	    target_line(s, i);
 	}
-	String s = st.nextToken();
+	s = st.nextToken();
 	wall_line(s, dim);
     }
 

Index: RRClient.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRClient.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- RRClient.java	8 Jun 2003 09:02:57 -0000	1.4
+++ RRClient.java	8 Jun 2003 10:31:23 -0000	1.5
@@ -28,28 +28,31 @@
 	    System.err.println("connection error");
 	    return;
 	}
-	RRNetOut netout;
+        RRBoard board;
+	RRNetIn netin;
+	JPanel bp;
 	try {
-	    netout = new RRNetOut(s);
+	    RRNetOut netout = new RRNetOut(s);
+	    board = new RRBoard(netout);
+	    RRImages images = new RRImages(this);
+	    bp = new RRBoardPanel(board, images);
+	    netin = new RRNetIn(s, board, bp);
 	} catch (IOException e) {
 	    System.err.println("could not connect to host");
 	    return;
 	}
-        RRBoard board = new RRBoard(netout);
-        RRImages images = new RRImages(this);
-        JPanel bp = new RRBoardPanel(board, images);
         getContentPane().add(bp, BorderLayout.CENTER);
+	netin.start();
     }
 
     public static void main(String[] args)
       throws IOException {
-    	Socket s = new Socket("localhost", 5252);
+    	Socket s = new Socket(args[0], 5252);
 	RRNetOut netout = new RRNetOut(s);
-	FileReader fin = new FileReader(args[0]);
-	BufferedReader in = new BufferedReader(fin);
-        RRBoard board = new RRBoard(in, netout);
+        RRBoard board = new RRBoard(netout);
         RRImages images = new RRImages(Toolkit.getDefaultToolkit());
         JPanel bp = new RRBoardPanel(board, images);
+	RRNetIn netin = new RRNetIn(s, board, bp);
         JFrame f = new JFrame("Ricochet Robots");
         f.addWindowListener(new WindowAdapter() {
                 public void windowClosing(WindowEvent e) {
@@ -58,8 +61,13 @@
             });
 
         f.getContentPane().add(bp, BorderLayout.CENTER);
-        f.setSize(new Dimension(494,494));
+        f.setSize(new Dimension(496,496));
         f.setVisible(true);
+
+	netin.start();
+	netout.hello(args[1]);
+	netout.watch(args[2]);
+	netout.show();
     }
 
 }

Index: RRNetIn.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRNetIn.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- RRNetIn.java	8 Jun 2003 09:02:57 -0000	1.1
+++ RRNetIn.java	8 Jun 2003 10:31:23 -0000	1.2
@@ -10,24 +10,73 @@
 import java.io.*;
 import java.net.*;
 import java.util.*;
+import javax.swing.*;
+import java.awt.*;
+
+class Lexer {
+    BufferedReader in;
+    public boolean saw_eol = false;
+    public boolean saw_eof = false;
+    
+    public Lexer(BufferedReader in) {
+	this.in = in;
+    }
+
+    public String next()
+      throws IOException {
+	boolean in_token = false;
+	boolean string_token = false;
+	saw_eol = false;
+	StringBuffer b = new StringBuffer();
+	while (true) {
+	    int ch = in.read();
+	    if (string_token && ch == '"')
+		break;
+	    if (ch == -1) {
+		saw_eof = true;
+		return null;
+	    }
+	    if (string_token) {
+		b.append((char)ch);
+		continue;
+	    }
+	    if (ch == '\n') {
+		saw_eol = true;
+		if (in_token)
+		    break;
+		return null;
+	    }
+	    if (ch == ' ' || ch == '\t') {
+		if (in_token)
+		    break;
+		continue;
+	    }
+	    if (!in_token && ch == '"') {
+		in_token = true;
+		string_token = true;
+		continue;
+	    }
+	    in_token = true;
+	    b.append((char)ch);
+	}
+	return b.toString();
+    }
+}
 
 public class RRNetIn
   extends Thread {
-    StreamTokenizer in;
+    Lexer in;
     RRBoard board;
+    JPanel panel;
     
-    public RRNetIn(Socket s, RRBoard board)
+    public RRNetIn(Socket s, RRBoard board, JPanel panel)
       throws IOException {
 	this.board = board;
+	this.panel = panel;
 	InputStream si = s.getInputStream();
 	InputStreamReader sr = new InputStreamReader(si);
 	BufferedReader sb = new BufferedReader(sr);
-	in = new StreamTokenizer(sb);
-	in.slashSlashComments(false);
-	in.slashStarComments(false);
-	in.quoteChar('"');
-	in.lowerCaseMode(true);
-	in.eolIsSignificant(true);
+	in = new Lexer(sb);
     }
 
     interface NoticeHandler {
@@ -37,16 +86,23 @@
     class ShowHandler implements NoticeHandler {
 	public boolean match(String[] notice)
 	  throws IOException {
-	    if (notice[0] != "show")
+	    if (notice.length != 2 || !notice[0].equals("SHOW"))
 		return false;
 	    board.become(notice[1]);
+	    panel.repaint();
 	    return true;
 	}
     }
 
     class DefaultHandler implements NoticeHandler {
 	public boolean match(String[] notice) {
-	    System.out.println(notice);
+	    if (notice.length > 0)
+		System.out.print(notice[0]);
+	    for (int i = 1; i < notice.length; i++) {
+		System.out.print(' ');
+		System.out.print(notice[i]);
+	    }
+	    System.out.println();
 	    return true;
 	}
     }
@@ -63,36 +119,21 @@
 	try {
 	    while(true) {
 		Vector v = new Vector();
-		int n = 0;
 		while (true) {
-		    int t = in.nextToken();
-		    if (t == in.TT_EOF) {
-			if (n != 0)
-			    throw new IOException("notice line ended in middle");
-			return;
-		    }
-		    if (t == in.TT_EOL)
+		    String s = in.next();
+		    if (s != null)
+			v.add(s);
+		    if (in.saw_eol || in.saw_eof)
 			break;
-		    if (t == in.TT_NUMBER)
-			throw new Error("stream tokenizer returned number");
-		    if (t == in.TT_WORD) {
-			switch (in.ttype) {
-			case '"':
-			case StreamTokenizer.TT_WORD:
-			    v.add(in.sval);
-			    break;
-			default:
-			    throw new Error("unexpected token type");
-			}
-		    }
-		    n++;
 		}
 		String[] notice = new String[v.size()];
-		for (int i = 0; i < n; i++)
+		for (int i = 0; i < notice.length; i++)
 		    notice[i] = (String)v.elementAt(i);
 		for (int i = 0; i < handlers.length; i++)
 		    if (handlers[i].match(notice))
 			break;
+		if (in.saw_eof)
+		    return;
 	    }
 	} catch (IOException e) {
 	    System.err.println("I/O Exception in RRNetIn");

Index: RRNetOut.java
===================================================================
RCS file: /local/src/CVS/RRClient/RRNetOut.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- RRNetOut.java	8 Jun 2003 09:02:57 -0000	1.1
+++ RRNetOut.java	8 Jun 2003 10:31:23 -0000	1.2
@@ -69,6 +69,12 @@
 	out.println("show");
     }
 
+    public void watch(String game) {
+	out.print("watch ");
+	uniprint(game);
+	out.println();
+    }
+
     public void bid(int b) {
 	out.print("bid ");
 	out.print(b);




More information about the Commit mailing list