Skip to content
Snippets Groups Projects
Commit e80cb764 authored by Clément Pit-Claudel's avatar Clément Pit-Claudel
Browse files

server: Refactor to use per-instance locking

Instead of having a `WebServer` object handling state machine state and a
`WebSocketsCollection` handling websocket connections, we now have all
per-instance state in `ServerApp` objects: websocket connections are tracked in
`ServerApp.channels`, and state-machine state is in
`StateMachineServerApp.state`.  Updates to different instances can happen in
parallel: synchronization happens at the level of independent apps.

Also take this opportunity to clean up clock-driven apps: events are now sent
unserialized, rather than being serialized and immediately deserialized
afterwards.

* jvm/src/main/scala/cs214/webapp/server/web/WebServer.scala:
  (Wordlist):
    New class.  Move wordlist management here.
  (instanceIdWords):
    Load wordlist just once, eagerly.
  (RunningClock, RunningServerAppInstance):
    Remove (moved to `ServerApp.scala`)
  (apps):
    Rename to `instances`.
  (register):
    Overload to handle clock-driven and regular state machines separately.
  (withFreshInstanceId):
    New function.
  (createInstance):
    Simplify by moving functionality to `withFreshInstanceId` and `Wordlist`.
  (startClockFor, transition, handleMessage):
    Move functionality to `ServerApp.scala`.

* jvm/src/main/scala/cs214/webapp/server/web/WebSocketsCollection.scala:
  Delete and merge all functionality into ServerApp.scala.

* jvm/src/main/scala/cs214/webapp/server/web/ServerApp.scala:
  (ServerApp):
    Rename to `ServerAppFactory`.
    Add `instanceId` to `init` signature.
  (ServerAppInstance):
    Rename to `ServerApp`.
    Extend to handle websocket state.
    Implement a `shutdown` API to close all websocket connections.
    Synchronize every method on a per-instance lock.
  (StateMachineServerAppInstance):
    Rename to `StateMachineServerApp`.
  (Clock, ClockDrivenStateMachineServerApp):
    New classes. Move clock threads here and use the new `project` API to send
    unserialized Tick events.

* jvm/src/main/scala/cs214/webapp/server/ApplicationJVM.scala:
  Don't allow repeated registrations.

* jvm/src/main/scala/cs214/webapp/server/web/WebServerRoutes.scala:
  (getAppInfo):
    Avoid race condition due to `.contains` … `.apply` pattern.
parent 44b6426f
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment