From 51be62d27ff6d368d6e4600e1e1a3bedfafa4f3b Mon Sep 17 00:00:00 2001 From: jessehartloff Date: Thu, 23 Apr 2020 16:40:18 -0400 Subject: [PATCH] add developer mode -Developer mode does not require a MySQL database connection --- nginx/public/officeHours.js | 2 +- src/main/scala/model/Configuration.scala | 7 ++++++ src/main/scala/model/OfficeHoursServer.scala | 15 ++++++++---- .../scala/model/{ => database}/Database.scala | 17 +++++++------ .../scala/model/database/DatabaseAPI.scala | 11 +++++++++ .../model/database/TestingDatabase.scala | 24 +++++++++++++++++++ 6 files changed, 64 insertions(+), 12 deletions(-) create mode 100644 src/main/scala/model/Configuration.scala rename src/main/scala/model/{ => database}/Database.scala (76%) create mode 100644 src/main/scala/model/database/DatabaseAPI.scala create mode 100644 src/main/scala/model/database/TestingDatabase.scala diff --git a/nginx/public/officeHours.js b/nginx/public/officeHours.js index f0c0c95..3514c14 100644 --- a/nginx/public/officeHours.js +++ b/nginx/public/officeHours.js @@ -1,4 +1,4 @@ -const socket = io.connect({transports: ['websocket']}); +const socket = io.connect("http://localhost:8080", {transports: ['websocket']}); socket.on('queue', displayQueue); socket.on('message', displayMessage); diff --git a/src/main/scala/model/Configuration.scala b/src/main/scala/model/Configuration.scala new file mode 100644 index 0000000..7b8ece5 --- /dev/null +++ b/src/main/scala/model/Configuration.scala @@ -0,0 +1,7 @@ +package model + +object Configuration { + + val DEV_MODE = true + +} diff --git a/src/main/scala/model/OfficeHoursServer.scala b/src/main/scala/model/OfficeHoursServer.scala index 8ea6167..09ef61b 100644 --- a/src/main/scala/model/OfficeHoursServer.scala +++ b/src/main/scala/model/OfficeHoursServer.scala @@ -2,11 +2,18 @@ package model import com.corundumstudio.socketio.listener.{DataListener, DisconnectListener} import com.corundumstudio.socketio.{AckRequest, Configuration, SocketIOClient, SocketIOServer} +import model.database.{Database, DatabaseAPI, TestingDatabase} import play.api.libs.json.{JsValue, Json} class OfficeHoursServer() { + val database: DatabaseAPI = if(Configuration.DEV_MODE){ + new TestingDatabase + }else{ + new Database + } + var usernameToSocket: Map[String, SocketIOClient] = Map() var socketToUsername: Map[SocketIOClient, String] = Map() @@ -24,7 +31,7 @@ class OfficeHoursServer() { server.start() def queueJSON(): String = { - val queue: List[StudentInQueue] = Database.getQueue() + val queue: List[StudentInQueue] = database.getQueue val queueJSON: List[JsValue] = queue.map((entry: StudentInQueue) => entry.asJsValue()) Json.stringify(Json.toJson(queueJSON)) } @@ -53,7 +60,7 @@ class DisconnectionListener(server: OfficeHoursServer) extends DisconnectListene class EnterQueueListener(server: OfficeHoursServer) extends DataListener[String] { override def onData(socket: SocketIOClient, username: String, ackRequest: AckRequest): Unit = { - Database.addStudentToQueue(StudentInQueue(username, System.nanoTime())) + server.database.addStudentToQueue(StudentInQueue(username, System.nanoTime())) server.socketToUsername += (socket -> username) server.usernameToSocket += (username -> socket) server.server.getBroadcastOperations.sendEvent("queue", server.queueJSON()) @@ -63,10 +70,10 @@ class EnterQueueListener(server: OfficeHoursServer) extends DataListener[String] class ReadyForStudentListener(server: OfficeHoursServer) extends DataListener[Nothing] { override def onData(socket: SocketIOClient, dirtyMessage: Nothing, ackRequest: AckRequest): Unit = { - val queue = Database.getQueue().sortBy(_.timestamp) + val queue = server.database.getQueue.sortBy(_.timestamp) if(queue.nonEmpty){ val studentToHelp = queue.head - Database.removeStudentFromQueue(studentToHelp.username) + server.database.removeStudentFromQueue(studentToHelp.username) socket.sendEvent("message", "You are now helping " + studentToHelp.username) if(server.usernameToSocket.contains(studentToHelp.username)){ server.usernameToSocket(studentToHelp.username).sendEvent("message", "A TA is ready to help you") diff --git a/src/main/scala/model/Database.scala b/src/main/scala/model/database/Database.scala similarity index 76% rename from src/main/scala/model/Database.scala rename to src/main/scala/model/database/Database.scala index c3c5c78..bbc9f7a 100644 --- a/src/main/scala/model/Database.scala +++ b/src/main/scala/model/database/Database.scala @@ -1,10 +1,13 @@ -package model +package model.database import java.sql.{Connection, DriverManager, ResultSet} -object Database { +import model.StudentInQueue - val url = "jdbc:mysql://mysql/officehours" + +class Database extends DatabaseAPI{ + + val url = "jdbc:mysql://mysql/officehours?autoReconnect=true" val username: String = sys.env("DB_USERNAME") val password: String = sys.env("DB_PASSWORD") @@ -18,7 +21,7 @@ object Database { } - def addStudentToQueue(student: StudentInQueue): Unit = { + override def addStudentToQueue(student: StudentInQueue): Unit = { val statement = connection.prepareStatement("INSERT INTO queue VALUE (?, ?)") statement.setString(1, student.username) @@ -28,7 +31,7 @@ object Database { } - def removeStudentFromQueue(username: String): Unit = { + override def removeStudentFromQueue(username: String): Unit = { val statement = connection.prepareStatement("DELETE FROM queue WHERE username=?") statement.setString(1, username) @@ -37,7 +40,7 @@ object Database { } - def getQueue(): List[StudentInQueue] = { + override def getQueue: List[StudentInQueue] = { val statement = connection.prepareStatement("SELECT * FROM queue") val result: ResultSet = statement.executeQuery() @@ -49,7 +52,7 @@ object Database { queue = new StudentInQueue(username, timestamp) :: queue } - queue + queue.reverse } } diff --git a/src/main/scala/model/database/DatabaseAPI.scala b/src/main/scala/model/database/DatabaseAPI.scala new file mode 100644 index 0000000..bad3a3b --- /dev/null +++ b/src/main/scala/model/database/DatabaseAPI.scala @@ -0,0 +1,11 @@ +package model.database + +import model.StudentInQueue + +trait DatabaseAPI { + + def addStudentToQueue(student: StudentInQueue): Unit + def removeStudentFromQueue(username: String): Unit + def getQueue: List[StudentInQueue] + +} diff --git a/src/main/scala/model/database/TestingDatabase.scala b/src/main/scala/model/database/TestingDatabase.scala new file mode 100644 index 0000000..a6199fb --- /dev/null +++ b/src/main/scala/model/database/TestingDatabase.scala @@ -0,0 +1,24 @@ +package model.database + +import model.StudentInQueue + +class TestingDatabase extends DatabaseAPI { + + var data: List[StudentInQueue] = List() + + + override def addStudentToQueue(student: StudentInQueue): Unit = { + data ::= student + } + + + override def removeStudentFromQueue(username: String): Unit = { + data = data.filter(_.username != username) + } + + + override def getQueue: List[StudentInQueue] = { + data.reverse + } + +}