CSPP 51090 & CMSC 22001: Software Construction Assignment 8: Networking

Due: November 30, 2004 @ 5pm

Extend your player interface slightly so that the result of startGame is a string that names your player. [1pt]

Design a new server infrastructure that simulates the administrator for a remote player. This should be a program that listens to the network, interprets the data, consults one of your existing players for the actual strategy and then sends the results back over the network. [5pts]

Design a new player that accepts its strategy from a remote site, vita tcp/ip. [4pts]

For now, test your new system on a local machine. Use port 8000 to communicate via TCP.


Each interaction across the network consists of a pair of messages, one in each direction. The content of the messages is goverend by the method type specifications in the Player interface. The sequence of the messages is governed by the sequence contract given in assignment 5.
  • When the server sends a start-game message, the response must be name.
  • When the server sends a do-move message, the response must be moves.
  • When the server sends a doubles-penalty message, the response must be void.

This is the specification for the data that passes over the network connection:

start-game
 = 
 <start-game> color  </start-game>
name
 = 
 <name> string  </name>
do-move
 = 
 <do-move> board  dice  </do-move>
moves
 = 
 <moves> move ... </moves>
doubles-penalty
 = 
 <doubles-penalty> </doubles-penalty>
void
 = 
 <void> </void>
 
board
 = 
 <board> start  main  home-rows  home  </board>
start
 = 
 <start> pawn ... </start>
main
 = 
 <main> piece-loc ... </main>
home-rows
 = 
 <home-rows> piece-loc ... </home-rows>
home
 = 
 <home> pawn ... </home>
piece-loc
 = 
 <piece-loc> pawn  <loc> number  </loc> </piece-loc>
 
move
 = 
 <enter-piece> pawn  </enter-piece>
 <move-piece-main> pawn  start  distance  </move-piece-main>
 <move-piece-home> pawn  start  distance  </move-piece-home>
start
 = 
 <start> number  </start>
distance
 = 
 <distance> number  </distance>
 
pawn
 = 
 <pawn> <color> color  </color> id  </pawn>
id
 = 
 <id> 0 </id>
 <id> 1 </id>
 <id> 2 </id>
 <id> 3 </id>
color
 = 
 red
 green
 blue
 yellow
dice
 = 
 <dice> die ... </dice>
die
 = 
 <die> 1 </die>
 <die> 2 </die>
 <die> 3 </die>
 <die> 4 </die>
 <die> 5 </die>
 <die> 6 </die>
number
 = 
 0
 1
 2
...
string
 = 
 a sequence of characters 

Things in the purple fixed width font are literal characters that pass across the network, things in green italics refer to other parts of the specification, and things in orange are meta notation about the spec. The ellipses that appear inside tags indicate repetitions: the element just before the ellipses can appear any number of times (including zero times). The ellipses in the number non-terminal indicates that all natural numbers are okay.

Each message must be terminated by a newline character and there must not be any other newlines in messages (altho whitespace is otherwise insignificant, except that it separates tokens).

The indicies for the main ring of the board start in the central square on the bottom of the board, with zero and continue around counter-clockwise (the directions the pawns travel). Accordingly, green's start square (on the main ring) is at position 5, and green enters the home row from position 0. Similarly, red enters at 22 and exits at 17.

The indicies for the home rows start at 0 and continue to 6. This does not includes the purple squares at the beginning of the home row, that square is considered to be on the main ring. On the board, the home row squares are colored according to the color of the pieces (blue's home row is the blue squares, etc).


Here are two example boards with their XML representations next to them:

<board> <start> <pawn> <color> yellow </color> <id> 3 </id> </pawn> <pawn> <color> yellow </color> <id> 2 </id> </pawn> <pawn> <color> yellow </color> <id> 1 </id> </pawn> <pawn> <color> yellow </color> <id> 0 </id> </pawn> <pawn> <color> red </color> <id> 3 </id> </pawn> <pawn> <color> red </color> <id> 2 </id> </pawn> <pawn> <color> red </color> <id> 1 </id> </pawn> <pawn> <color> red </color> <id> 0 </id> </pawn> <pawn> <color> green </color> <id> 3 </id> </pawn> <pawn> <color> green </color> <id> 2 </id> </pawn> <pawn> <color> green </color> <id> 1 </id> </pawn> <pawn> <color> green </color> <id> 0 </id> </pawn> <pawn> <color> blue </color> <id> 3 </id> </pawn> <pawn> <color> blue </color> <id> 2 </id> </pawn> <pawn> <color> blue </color> <id> 1 </id> </pawn> <pawn> <color> blue </color> <id> 0 </id> </pawn> </start> <main> </main> <home-rows> </home-rows> <home> </home> </board>


<board> <start> <pawn> <color> yellow </color> <id> 3 </id> </pawn> <pawn> <color> red </color> <id> 2 </id> </pawn> <pawn> <color> green </color> <id> 1 </id> </pawn> <pawn> <color> blue </color> <id> 0 </id> </pawn> </start> <main> <piece-loc> <pawn> <color> yellow </color> <id> 2 </id> </pawn> <loc> 56 </loc> </piece-loc> <piece-loc> <pawn> <color> blue </color> <id> 3 </id> </pawn> <loc> 39 </loc> </piece-loc> <piece-loc> <pawn> <color> red </color> <id> 1 </id> </pawn> <loc> 22 </loc> </piece-loc> <piece-loc> <pawn> <color> green </color> <id> 0 </id> </pawn> <loc> 5 </loc> </piece-loc> </main> <home-rows> <piece-loc> <pawn> <color> green </color> <id> 2 </id> </pawn> <loc> 0 </loc> </piece-loc> <piece-loc> <pawn> <color> red </color> <id> 3 </id> </pawn> <loc> 1 </loc> </piece-loc> <piece-loc> <pawn> <color> blue </color> <id> 1 </id> </pawn> <loc> 2 </loc> </piece-loc> <piece-loc> <pawn> <color> yellow </color> <id> 0 </id> </pawn> <loc> 3 </loc> </piece-loc> </home-rows> <home> <pawn> <color> yellow </color> <id> 1 </id> </pawn> <pawn> <color> red </color> <id> 0 </id> </pawn> <pawn> <color> green </color> <id> 3 </id> </pawn> <pawn> <color> blue </color> <id> 2 </id> </pawn> </home> </board>

If the initial roll is a 5 and a 3, here is the message that would be sent to the player:

<do-move> <board> <start> <pawn> <color> yellow </color> <id> 3 </id> </pawn> <pawn> <color> yellow </color> <id> 2 </id> </pawn> <pawn> <color> yellow </color> <id> 1 </id> </pawn> <pawn> <color> yellow </color> <id> 0 </id> </pawn> <pawn> <color> red </color> <id> 3 </id> </pawn> <pawn> <color> red </color> <id> 2 </id> </pawn> <pawn> <color> red </color> <id> 1 </id> </pawn> <pawn> <color> red </color> <id> 0 </id> </pawn> <pawn> <color> green </color> <id> 3 </id> </pawn> <pawn> <color> green </color> <id> 2 </id> </pawn> <pawn> <color> green </color> <id> 1 </id> </pawn> <pawn> <color> green </color> <id> 0 </id> </pawn> <pawn> <color> blue </color> <id> 3 </id> </pawn> <pawn> <color> blue </color> <id> 2 </id> </pawn> <pawn> <color> blue </color> <id> 1 </id> </pawn> <pawn> <color> blue </color> <id> 0 </id> </pawn> </start> <main> </main> <home-rows> </home-rows> <home> </home> </board> <dice> <die> 5 </die> <die> 3 </die> </dice> </do-move>

and this is the one of the legal responses:

<moves> <enter-piece> <pawn> <color> green </color> <id> 0 </id> </pawn> </enter-piece> <move-piece-main> <pawn> <color> green </color> <id> 0 </id> </pawn> <start> 5 </start> <distance> 3 </distance> </move-piece-main> </moves>

Overall Hints First, develop the programs that can translate method calls into text messages and text messages into method calls (test the parsing!). Second, wire in your existing code and test each half independently by simulating messages from the other half for simple interactions. Finally, wire everything together and play some games.

Parsing Hints There are two options for parsing. First, find a XML parser for your language of choice and use it (this approach can be tricky, since most of the XML libraries I've seen are very heavyweight). Second, you can write your own parser. The protocol only calls for a simple subset of the XML language. There are no dtds required. There are no attributes in the tags. Whitespace is only significant as a delimiter (and inside a name). All of this should make rolling your own special-purpose parser much easier.

Be sure to organize your parser as a separate library, so when you find bugs it will be easy to add test cases.



CSPP 51090 & CMSC 22001: Software Construction