Giter VIP home page Giter VIP logo

elevator-vertx's Introduction

This is a attempt to make a more modern and efficient design of elevator system compare to my old https://github.com/quangIO/Elevator. The older one uses multi-threaded, inefficient, "eager" approach. An event loop keep tracking the request queue to see if there exists some requests need to be sent to elevators. (Lock based, CPU hungry, and waste of electricity)

How does this work?

The idea is based on Actor model. There are ElevatorActors and a ElevatorControllerActor. The messages are passed from Elevators to the centralized ElevatorController (MainVerticle) by using VertX's event bus (just for convenience, you can just use Rx's PublishBehavior or built-in Kotlin's Coroutine features like channel to implement it).

Elevator
  • id self-explanatory
  • timeToReach time to move from floor i to i+1 or other way around
  • pickUpTime time to pickup
  • vertx hold the vertx instance for event bus and some other utilities

Each elevator accepts the following commands from MainVerticle: moveUp, moveDown, and pickUp. There is a timer that simulates the process. The isMoving variable is atomic, helping to check the current state of an elevator. It will be true if the elevator is picking up something or moving between 2 floors.

When an elevator reached a floor, it notifies its current state to the controller.

Tl;dr: Elevators basically do not anything. It just follows the order dispatched from MainVerticle and updates its state.

MainVerticle

This holds all the logic of the system, including outsideRequests (all requests from outside of the elevator), insideRequests (similarly), elevatorStatuses (current states of elevators), elevators (references to each elevator), and requestQueue (for fairness purpose - TODO: not optimized yet).

All those data should be thread-safe. For the simplicity, I use built-in concurrent data structures with naive filters. However, in real production, an in-memory should be used (it is simpler to code actually).

It is event-based
  • When it received an outside request: if it is moving up, it will try to reach the highest floor, and the same goes for moving down. The cost to select the elevator is calculated as abs(toFloor - request.atFloor) where toFloor is where the elevator is heading and request.atFloor is where the request comes from. In case we cannot find an elevator to process a request, we add it to requestQueue
  • When it received an inside request: similar to outside request but with specific elevator ID, so it is a lot simpler.
  • When elevators' states changed
    • If should pick up: pickup
    • If already doing something, return
    • If toFloor > atFloor: move up to reach toFloor
    • else if toFloor < atFloor: move down to reach toFloor
    • else (it is idling): find something to do (try to empty requestQueue, in|outsideRequests)

Run

./gradlew shadowJar
java -jar build/libs/*.jar

or

Open import with IntelliJ Idea and run the main function

The server should start and we can use this dashboard to try the simulation. To change the number of elevators, change val n = [number of elevators here] in the MainVerticle.kt (101 floors)

Bugs?

Likely. I would be very appreciated if you point out my errors.

elevator-vertx's People

Contributors

quangio avatar

Stargazers

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