Giter Club home page Giter Club logo

tcp-server's Introduction

TCP Server

About

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.

Specification

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.

Design

  • 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 direction y-axis or row direction to move the cursor. Whereas the value in [x] represents which direction in the x-axis or column direction to move the cursor. As a result the 8 values stored in the directions 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]
  • There is a current_direction variable that stores the current direction the cursor should move in. current_direction is mapped to an index in the directions array. So for example, if current_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 to top.
  • The current_direction variable wraps around the directions array. So if for example current_direction = 0 and a user types in right 9, the current_direction will be 9 % 8 = [1] in the directions array.

Running

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

  • To close server just enter Ctrl + C or cmd + C in the terminal

tcp-server's People

Contributors

kkuivi avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.