Software Construction Assignment 7: Networking

Due: May 9, 2017 @ 5pm. Please include a README.txt that has both partner's names and email addresses.

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

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.

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

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.

The do-move message comes once for each roll of the dice. If it has four numbers, then the player rolled doubles and all of their pieces are out, and the four numbers correspond to the tops and bottoms of the dice. Otherwise, there are two numbers, and they correspond to the tops of the dice.

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

= <start-game> color  </start-game>
= <name> string  </name>
= <do-move> board  dice  </do-move>
= <moves> move ... </moves>
= <doubles-penalty> </doubles-penalty>
= <void> </void>
= <board> start  main  home-rows  home  </board>
= <start> pawn ... </start>
= <main> piece-loc ... </main>
= <home-rows> piece-loc ... </home-rows>
= <home> pawn ... </home>
= <piece-loc> pawn  <loc> number  </loc> </piece-loc>
= <enter-piece> pawn  </enter-piece>
| <move-piece-main> pawn  start  distance  </move-piece-main>
| <move-piece-home> pawn  start  distance  </move-piece-home>
= <start> number  </start>
= <distance> number  </distance>
= <pawn> <color> color  </color> id  </pawn>
= <id> 0 </id>
| <id> 1 </id>
| <id> 2 </id>
| <id> 3 </id>
= red
| green
| blue
| yellow
= <dice> die  die  </dice>
| <dice> die  die  die  die  </dice>
= <die> 1 </die>
| <die> 2 </die>
| <die> 3 </die>
| <die> 4 </die>
| <die> 5 </die>
| <die> 6 </die>
= 0
| 1
| 2
= 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 boldface 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 only legal response:

<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.

Speaking of tests, if you install Racket v6.9 and download this zip file or this tar file you can compare your rules checker to mine. The bundle includes some test cases and instructions to generate more. Let me know if you find any bugs!

Software Construction