[Commit] rrserver dispatch.5c,1.18,1.19 games.5c,1.17,1.18 lex.5c,1.3,1.4 readreq.5c,1.9,1.10 rr.5c,1.10,1.11 server.5c,1.10,1.11

Keith Packard commit at keithp.com
Tue Jun 3 02:02:56 PDT 2003


Committed by: keithp

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

Modified Files:
	dispatch.5c games.5c lex.5c readreq.5c rr.5c server.5c 
Log Message:
rewrite dispatching to eliminate wasted datatypes, functions and switches

Index: dispatch.5c
===================================================================
RCS file: /local/src/CVS/rrserver/dispatch.5c,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -d -r1.18 -r1.19
--- dispatch.5c	31 May 2003 16:13:29 -0000	1.18
+++ dispatch.5c	3 Jun 2003 08:02:54 -0000	1.19
@@ -33,258 +33,31 @@
 extend namespace Server {
     public namespace Dispatch {
 
+	string function upper (string s)
+	{
+	    string  r = "";
+	    for (int i = 0; i < String::length (s); i++)
+		r = r + String::new (Ctype::toupper (s[i]));
+	    return r;
+	}
 
[...1077 lines suppressed...]
+		    string  command = upper (words[0]);
+		    string[dim(words)-1] args = { [i] = words[i+1] };
+		    
+		    if (words[0] == "") return;
+		    int i;
+		    for (i = 0; i < dim(commands); i++)
+			if (command == commands[i].command)
+			{
+			    commands[i].f (args);
+			    break;
+			}
+		    if (i == dim(commands))
+			raise rr_error (Error.COMMAND);
+		} catch rr_error (Error e) {
 		    respond ("ERROR %E\n", e);
-		} catch Readreq::request_closed () {
+		} catch closed () {
 		    return;
 		}
 	    }

Index: games.5c
===================================================================
RCS file: /local/src/CVS/rrserver/games.5c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- games.5c	1 Jun 2003 01:29:32 -0000	1.17
+++ games.5c	3 Jun 2003 08:02:54 -0000	1.18
@@ -51,7 +51,7 @@
     
 	void assert_active (&Game g, &Client c) {
 	    if (g.active != (ClientRef.client) (&c))
-		raise error (Error.NOTACTIVE);
+		raise rr_error (Error.NOTACTIVE);
 	}
 
 	void assert_active_or_done (&Game g, &Client c) {
@@ -60,18 +60,18 @@
 	    if (g.state == GameState.SHOW &&
 	        g.active == (ClientRef.client) (&c))
 		return;
-	    raise error (Error.NOTACTIVE);
+	    raise rr_error (Error.NOTACTIVE);
 	}
 
 	void assert_playing (&Game c, &Client c) {
 	    if (!c.playing)
-		raise error (Error.NOTPLAYING);
+		raise rr_error (Error.NOTPLAYING);
 	}
 
 	void assert_done (&Game g) {
 	    if (g.state == GameState.DONE)
 		return;
-	    raise error (Error.NOTDONE);
+	    raise rr_error (Error.NOTDONE);
 	}
 	
 	/* find a game by name */
@@ -340,7 +340,7 @@
 		break;
 	    case GameState.SHOW:
 	    case GameState.DONE:
-		raise error (Error.NOTBIDDING);
+		raise rr_error (Error.NOTBIDDING);
 		break;
 	    }
 	    
@@ -372,7 +372,7 @@
 
 	public void dispose (&Game g) {
 	    void bail (&Client c) {
-		raise error (Error.NOTEMPTY);
+		raise rr_error (Error.NOTEMPTY);
 	    }
 	    iterate_client (&g, bail, true, false);
 	    remove (&g);
@@ -430,7 +430,7 @@
 		set_state (&g, GameState.BID);
 	    /*
 	    if (c.bid != Bid.none && c.bid.number <= number)
-		raise error (Error.NOTLOWER);
+		raise rr_error (Error.NOTLOWER);
 	     */
 	    c.bid = (Bid.number) number;
 	    game_send (&g, "NOTICE BID %s %d\n", c.user.username, number);
@@ -440,7 +440,7 @@
 	    assert_playing (&g, &c);
 	    assert_bidding (&g);
 	    if (c.bid == Bid.none)
-		raise error (Error.NOBID);
+		raise rr_error (Error.NOBID);
 	    c.bid = Bid.none;
 	    game_send (&g, "NOTICE REVOKE %s\n", c.user.username);
 	    if (!any_bids(&g))
@@ -506,11 +506,11 @@
 	    assert_active_or_done (&g, &c);
 	    if (g.state == GameState.SHOW &&
 		count (&g) >= c.bid.number)
-		raise error (Error.TOOMANYMOVES);
+		raise rr_error (Error.TOOMANYMOVES);
 	    ObjectLoc	src = Boards::find_robot (&g.board, color);
 	    ObjectLoc	dst = Boards::move_robot (&g.board, color, dir);
 	    if (src == dst)
-		raise error (Error.BLOCKED);
+		raise rr_error (Error.BLOCKED);
 	    Array::push (&g.history, src);
 	    Boards::position_robot (&g.board, color, dst.x, dst.y);
 	    game_send (&g, "NOTICE MOVE %d %C %D\n", count (&g), color, dir);

Index: lex.5c
===================================================================
RCS file: /local/src/CVS/rrserver/lex.5c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- lex.5c	29 May 2003 17:46:29 -0000	1.3
+++ lex.5c	3 Jun 2003 08:02:54 -0000	1.4
@@ -118,48 +118,5 @@
     	    skipwhite (f, true);
     	    return next_word (f);
 	}
-
-	public int number (file f) {
-	    string w = word (f);
-	    return string_to_integer (w);
-	}
-
-	public exception invalid_color (string s);
-
-	public Color color (file f) {
-	    switch ((string w = word(f))[0]) {
-	    case 'r': case 'R': return Color.Red;
-	    case 'y': case 'Y': return Color.Yellow;
-	    case 'g': case 'G': return Color.Green;
-	    case 'b': case 'B': return Color.Blue;
-	    case 'w': case 'W': return Color.Whirl;
-		default: raise invalid_color (w);
-	    }
-	}
-
-	public exception invalid_shape (string w);
-
-	public Shape shape (file f) {
-	    switch ((string w = word(f))[0]) {
-	    case 't': case 'T': return Shape.Triangle;
-	    case 's': case 'S': return Shape.Square;
-	    case 'o': case 'O': return Shape.Octagon;
-	    case 'c': case 'C': return Shape.Circle;
-	    case 'w': case 'W': return Shape.Whirl;
-		default: raise invalid_shape (w);
-	    }
-	}
-
-	public exception invalid_direction (string w);
-
-	public Direction direction (file f) {
-	    switch ((string w = word(f))[0]) {
-	    case 'n': case 'N': return Direction.North;
-	    case 'e': case 'E': return Direction.East;
-	    case 's': case 'S': return Direction.South;
-	    case 'w': case 'W': return Direction.West;
-		default: raise invalid_direction (w);
-	    }
-	}
     }
 }

Index: readreq.5c
===================================================================
RCS file: /local/src/CVS/rrserver/readreq.5c,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -d -r1.9 -r1.10
--- readreq.5c	31 May 2003 16:13:29 -0000	1.9
+++ readreq.5c	3 Jun 2003 08:02:54 -0000	1.10
@@ -29,187 +29,30 @@
 
 extend namespace Server {
     public namespace Readreq {
-	public exception invalid_request (string w);
-	public exception request_closed ();
-
 	import RR;
 	import Lex;
 
-	string function upper (string s)
-	{
-	    string  r = "";
-	    for (int i = 0; i < String::length (s); i++)
-		r = r + String::new (Ctype::toupper (s[i]));
-	    return r;
-	}
+	string[] read_unlocked (file f) {
 
-	Request read_unlocked (file f) {
-	    struct {} packet;
-	    void username_ (&struct { string username; } packet) {
-		packet.username = word (f); 
-	    }
-	    void game_ (&struct { string game; } packet) {
-		packet.game = word (f); 
-	    }
-	    void number_ (&struct { int number; } packet) {
-		packet.number = number (f);
-	    }
-	    void color_ (&struct { Color color; } packet) {
-		packet.color = color (f);
-	    }
-	    void shape_ (&struct { Shape shape; } packet) {
-		packet.shape = shape (f);
-	    }
-	    void direction_ (&struct { Direction direction; } packet) {
-		packet.direction = direction (f);
-	    }
-	    void directions_ (&struct { Direction[*] directions; } packet) {
-		while (!eol (f))
-		    Array::push (&packet.directions, direction(f));
-	    }
-	    void text_ (&struct { string text; } packet) {
-		packet.text = word (f);
-	    }
-	    void texts_ (&struct { string text; } packet) {
-		packet.text = word (f);
-		while (!eol (f))
-		    packet.text += " " + word (f);
-	    }
-	    void upper_ (&struct { string text; } packet) {
-		packet.text = upper (word (f));
-	    }
-	    Request req;
-	    switch (string w = upper (firstword (f))) {
-	    case "HELO":
-		packet = (struct { string username; }) {};
-		username_ (&packet);
-		req = (Request.HELO) packet;
-		break;
-	    case "WHO":
-		req = Request.WHO;
-		break;
-	    case "GAMES":
-		req = Request.GAMES;
-		break;
-	    case "PLAYERS":
-		packet = (struct { string game; }) {};
-		game_ (&packet);
-		req = (Request.PLAYERS) packet;
-		break;
-	    case "WATCHERS":
-		packet = (struct { string game; }) {};
-		game_ (&packet);
-		req = (Request.WATCHERS) packet;
-		break;
-	    case "GAMEINFO":
-		packet = (struct { string game; }) {};
-		game_ (&packet);
-		req = (Request.GAMEINFO) packet;
-		break;
-	    case "USERINFO":
-		packet = (struct { string username; }) {};
-		username_ (&packet);
-		req = (Request.USERINFO) packet;
-		break;
-	    case "NEW":
-		packet = (struct { string game; }) {};
-		game_ (&packet);
-		req = (Request.NEW) packet;
-		break;
-	    case "JOIN":
-		packet = (struct { string game; }) {};
-		game_ (&packet);
-		req = (Request.JOIN) packet;
-		break;
-	    case "WATCH":
-		packet = (struct { string game; }) {};
-		game_ (&packet);
-		req = (Request.WATCH) packet;
-		break;
-	    case "DISPOSE":
-		packet = (struct { string game; }) {};
-		game_ (&packet);
-		req = (Request.DISPOSE) packet;
-		break;
-	    case "SHOW":
-		req = Request.SHOW;
-		break;
-	    case "BID":
-		packet = (struct { int number; }) {};
-		number_ (&packet);
-		req = (Request.BID) packet;
-		break;
-	    case "NOBID":
-		req = Request.NOBID;
-		break;
-	    case "REVOKE":
-		req = Request.REVOKE;
-		break;
-	    case "ABANDON":
-		req = Request.ABANDON;
-		break;
-	    case "MOVE":
-		packet = (struct { 
-		    Color color; 
-		    Direction[*] directions; 
-		}) { directions = {} };
-		color_ (&packet);
-		directions_ (&packet);
-		req = (Request.MOVE) packet;
-		break;
-	    case "UNDO":
-		req = Request.UNDO;
-		break;
-	    case "RESET":
-		req = Request.RESET;
-		break;
-	    case "TURN":
-		req = Request.TURN;
-		break;
-	    case "PASS":
-		req = Request.PASS;
-		break;
-	    case "MESSAGE":
-		packet = (struct { string text; }) {};
-		texts_ (&packet);
-		req = (Request.MESSAGE) packet;
-		break;
-	    case "PART":
-		req = Request.PART;
-		break;
-	    case "QUIT":
-		req = Request.QUIT;
-		break;
-	    case "HELP":
-		packet = (struct { string text; }) { text = "" };
-		if (!eol (f))
-		    upper_ (&packet);
-		req = (Request.HELP) packet;
-		break;
-	    case "":
-		raise request_closed ();
-	    default:
-		raise error (Error.COMMAND);
+	    string[*]	words = {};
+
+	    void add (string w) {
+		words = (string[dim(words)+1]) { 
+		    [i] = i == dim(words) ? w :	words[i]
+		};
 	    }
-	    if (!eol (f))
-		raise error (Error.SYNTAX);
-	    return req;
+
+	    string w = firstword (f);
+	    add (w);
+	    while (!eol (f))
+	        add (word(f));
+	    return words;
 	}
 
-	public Request read (file f) {
-	    try {
-		twixt (unlock (); lock ())
-		    twixt (true; skipline (f))
-			return read_unlocked (f);
-	    } catch RR::Lex::syntax() {
-		raise error (Error.SYNTAX);
-	    } catch RR::Lex::invalid_color (string color) {
-		raise error (Error.NOTCOLOR);
-	    } catch RR::Lex::invalid_shape (string shape) {
-		raise error (Error.NOTSHAPE);
-	    } catch RR::Lex::invalid_direction (string direction) {
-		raise error (Error.NOTDIRECTION);
-	    } 
+	public string[] read (file f) {
+	    twixt (unlock (); lock ())
+		twixt (true; skipline (f))
+		    return read_unlocked (f);
 	    raise notreached ();
 	}
     }

Index: rr.5c
===================================================================
RCS file: /local/src/CVS/rrserver/rr.5c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- rr.5c	31 May 2003 16:13:29 -0000	1.10
+++ rr.5c	3 Jun 2003 08:02:54 -0000	1.11
@@ -71,140 +71,6 @@
     
     public typedef Object[Width,Height]	Board;
 
-    public typedef union {
-	struct {
-	    string	username;
-	}	HELO;
-	void    WHO;
-	void    GAMES;
-	struct {
-	    string	game;
-	}	PLAYERS;
-	struct {
-	    string	game;
-	}	WATCHERS;
-	struct {
-	    string	game;
-	}	GAMEINFO;
-	struct {
-	    string	username;
-	}	USERINFO;
-	struct {
-	    string	game;
-	}	NEW;
-	struct {
-	    string	game;
-	}	JOIN;
-	struct {
-	    string	game;
-	}	WATCH;
-	struct {
-	    string	game;
-	}	DISPOSE;
-	void    SHOW;
-	struct {
-	    int	number;
-	}	BID;
-	void	NOBID;
-	void	REVOKE;
-	void	ABANDON;
-	struct {
-	    Color	    color;
-	    Direction[*]    directions;
-	}	MOVE;
-	void    UNDO;
-	void    RESET;
-	void    TURN;
-	void    PASS;
-	struct {
-	    string	text;
-	}	MESSAGE;
-	void	PART;
-	void	QUIT;
-	struct {
-	    string	text;
-	}	HELP;
-    } Request;
-
-    public typedef union {
-	struct {
-	    string	servername;
-	}	    HELO;
-	struct {
-	    string[]    users;
-	}	    WHO;
-	struct {
-	    string[]    games;
-	}	    GAMES;
-	struct {
-	    string[]    users;
-	}	    USERS;
-	struct {
-	    string	    game;
-	}	    NEW;
-	struct {
-	    string[]    users;
-	}	    JOIN;
-	struct {
-	    string[]    users;
-	}	    WATCH;
-	struct {
-	    Board	    board;
-	}	    SHOW;
-	void    BID;
-	void    MOVE;
-	void    UNDO;
-	void    RESET;
-	void    TURN;
-	void    PASS;
-	void    MESSAGE;
-    } Reply;
-
-    public typedef struct {
-	struct {
-	    Color	color;
-	    Direction   direction;
-	}	MOVE;
-	struct {
-	    string	username;
-	}	USER;
-	struct {
-	    string	username;
-	}	PART;
-	struct {
-	    string	game;
-	}	GAME;
-	struct {
-	    string	game;
-	}	TERMINATE;
-	struct {
-	    int	number;
-	    string	username;
-	}	BID;
-	struct {
-	    string	username;
-	}	REVOKE;
-	struct {
-	    int	seconds;
-	}	TIME;
-	struct {
-	    string	username;
-	}	    ACTIVE;
-	void    UNDO;
-	void    RESET;
-	struct {
-	    Color	color;
-	    Shape	shape;
-	}	TURN;
-	struct {
-	    string	text;
-	    string	username;
-	}	MESSAGE;
-	struct {
-	    string	username;
-	}	ABANDON;
-    } Notice;
-
     public typedef enum {
 	NOGAME,
 	NOUSER,
@@ -228,9 +94,37 @@
 	INVALIDNAME
     } Error;
 
-    public typedef union {
-	Reply	reply;
-	Notice	notice;
-	Error	error;
-    } Response;
+    public exception rr_error (Error e);
+
+    public Color color (string w) {
+	switch (w[0]) {
+	case 'r': case 'R': return Color.Red;
+	case 'y': case 'Y': return Color.Yellow;
+	case 'g': case 'G': return Color.Green;
+	case 'b': case 'B': return Color.Blue;
+	case 'w': case 'W': return Color.Whirl;
+	    default: raise rr_error (Error.NOTCOLOR);
+	}
+    }
+    
+    public Shape shape (string w) {
+	switch (w[0]) {
+	case 't': case 'T': return Shape.Triangle;
+	case 's': case 'S': return Shape.Square;
+	case 'o': case 'O': return Shape.Octagon;
+	case 'c': case 'C': return Shape.Circle;
+	case 'w': case 'W': return Shape.Whirl;
+	    default: raise rr_error (Error.NOTSHAPE);
+	}
+    }
+
+    public Direction direction (string w) {
+	switch (w[0]) {
+	case 'n': case 'N': return Direction.North;
+	case 'e': case 'E': return Direction.East;
+	case 's': case 'S': return Direction.South;
+	case 'w': case 'W': return Direction.West;
+	    default: raise rr_error (Error.NOTDIRECTION);
+	}
+    }
 }

Index: server.5c
===================================================================
RCS file: /local/src/CVS/rrserver/server.5c,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- server.5c	31 May 2003 16:13:29 -0000	1.10
+++ server.5c	3 Jun 2003 08:02:54 -0000	1.11
@@ -37,8 +37,6 @@
 
     public void set_server_id (string id) { server_id = id; }
 
-    public exception error (Error e);
-
     public bool lock () {
 	Mutex::acquire (server_mutex);
 #	printf ("lock %v\n", Thread::current());




More information about the Commit mailing list