A TCP server implementing a simple protocol very loosely inspired by LOGO. When a client connects, it will be able to send simple commands to the server to draw on a canvas. It will also call ask the server to render the current canvas.
When a client connects, the server will allocate a canvas. The state of the canvas is defined by:
- The canvas itself: a
30x30 buffer
. - The cursor, which is initially located at
(15,15)
. - The current direction, which is initially set to
top
.
The server opens a local TCP socket on port 8124
. The server accepts commands separated by newlines (\r\n)
. All server responses are also terminated by \r\n
.
steps <n>
: move the cursor n steps in the current direction.left <n>, right <n>
: change the direction (see “Directions” below).hover, draw, eraser
: set the brush mode (see “Drawing” below).coord
: print the current coordinates of the cursor with the format (x,y).render
: print the current canvas.clear
: erase the current canvas, while keeping the current cursor and direction.quit
: closes the current connection.
- The server contains a 2d
(30 x 30)
array that represents the canvas. - The cursor starts at index
[15,15]
. - There is a
directions
array that represents all the 8 directions the cursor can move in. The values in the directions array are 2d array of the format[y, x]
. The value in[y]
represents which directiony-axis
orrow direction
to move the cursor. Whereas the value in[x]
represents which direction in thex-axis
orcolumn direction
to move the cursor. As a result the 8 values stored in thedirections
array are:- up =
[-1, 0]
- up_right =
[-1, 1]
- right =
[0, 1]
- down_right =
[1, 1]
- down =
[1, 0]
- down_left =
[1, -1]
- left =
[0, -1]
- up_left =
[-1, -1]
- up =
- There is a
current_direction
variable that stores the current direction the cursor should move in.current_direction
is mapped to an index in thedirections
array. So for example, ifcurrent_direction = 0
and the directions arrray is[[-1, 0], [-1, 1], [0, 1], [1, 1], [1, 0], [1, -1], [0, -1], [-1, -1]]
then it means the current direction of cursor is[-1, 0]
which is equal totop
. - The
current_direction
variable wraps around thedirections
array. So if for examplecurrent_direction = 0
and a user types inright 9
, thecurrent_direction
will be9 % 8 = [1]
in thedirections
array.
- To run the server, open a terminal and
cd
into this project's directory. Then simply run:python3 socket_server.py -i
.- Caveat: For unit tests, just run
python3 socket_server.py
--> Removes the verbose prompting sent to the user indicating that an action was completed.
- Caveat: For unit tests, just run
- To close server just enter
Ctrl + C
orcmd + C
in the terminal