From 4de67b497e0e229fe4a42f66f833640b6e50fd5a Mon Sep 17 00:00:00 2001 From: Joel Kronqvist Date: Sun, 17 Nov 2024 13:45:44 +0200 Subject: Moved the project to an IDEA project & wrote part of README.txt --- .../Server/ConnectionGetter.scala | 25 ++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/scalevalapokalypsi/Server/ConnectionGetter.scala (limited to 'src/scalevalapokalypsi/Server/ConnectionGetter.scala') diff --git a/src/scalevalapokalypsi/Server/ConnectionGetter.scala b/src/scalevalapokalypsi/Server/ConnectionGetter.scala new file mode 100644 index 0000000..40830c7 --- /dev/null +++ b/src/scalevalapokalypsi/Server/ConnectionGetter.scala @@ -0,0 +1,25 @@ +package scalevalapokalypsi.Server + +import java.io.IOException +import java.net.{ServerSocket, Socket} +import scala.concurrent.Future +import scala.util.{Failure, Success} +import scala.concurrent.ExecutionContext.Implicits.global + +/** Small helper class for getting new connections using futures */ +class ConnectionGetter(val socket: ServerSocket): + + private var nextClient: Future[Socket] = Future.failed(IOException()) + + /** Returns a new socket to a client if there is any new connections. */ + def newClient(): Option[Socket] = + this.nextClient.value match + case Some(Success(s)) => + nextClient = Future(socket.accept()) + Some(s) + case Some(Failure(e)) => + nextClient = Future(socket.accept()) + None + case None => None + +end ConnectionGetter -- cgit v1.2.3