diff --git a/android/src/main/scala/com/nutomic/ensichat/service/ChatService.scala b/android/src/main/scala/com/nutomic/ensichat/service/ChatService.scala index b266240..bcaf3cf 100644 --- a/android/src/main/scala/com/nutomic/ensichat/service/ChatService.scala +++ b/android/src/main/scala/com/nutomic/ensichat/service/ChatService.scala @@ -32,7 +32,7 @@ class ChatService extends Service { private lazy val connectionHandler = new ConnectionHandler(new SettingsWrapper(this), new Database(this), callbackHandler, - ChatService.newCrypto(this)) + ChatService.newCrypto(this), 1) private val networkReceiver = new NetworkChangedReceiver() diff --git a/core/src/main/scala/com/nutomic/ensichat/core/ConnectionHandler.scala b/core/src/main/scala/com/nutomic/ensichat/core/ConnectionHandler.scala index 75df211..523020e 100644 --- a/core/src/main/scala/com/nutomic/ensichat/core/ConnectionHandler.scala +++ b/core/src/main/scala/com/nutomic/ensichat/core/ConnectionHandler.scala @@ -12,9 +12,13 @@ import scala.concurrent.ExecutionContext.Implicits.global /** * High-level handling of all message transfers and callbacks. + * + * @param maxInternetConnections Maximum number of concurrent connections that should be opened by + * [[InternetInterface]]. */ final class ConnectionHandler(settings: SettingsInterface, database: DatabaseInterface, - callbacks: CallbackInterface, crypto: Crypto) { + callbacks: CallbackInterface, crypto: Crypto, + maxInternetConnections: Int) { private val Tag = "ConnectionHandler" @@ -40,7 +44,7 @@ final class ConnectionHandler(settings: SettingsInterface, database: DatabaseInt Log.i(Tag, "Service started, address is " + crypto.localAddress) Log.i(Tag, "Local user is " + settings.get(SettingsInterface.KeyUserName, "none") + " with status '" + settings.get(SettingsInterface.KeyUserStatus, "") + "'") - transmissionInterfaces += new InternetInterface(this, crypto, settings) + transmissionInterfaces += new InternetInterface(this, crypto, settings, maxInternetConnections) transmissionInterfaces.foreach(_.create()) } } diff --git a/core/src/main/scala/com/nutomic/ensichat/core/internet/InternetInterface.scala b/core/src/main/scala/com/nutomic/ensichat/core/internet/InternetInterface.scala index 55f16fc..be8ece7 100644 --- a/core/src/main/scala/com/nutomic/ensichat/core/internet/InternetInterface.scala +++ b/core/src/main/scala/com/nutomic/ensichat/core/internet/InternetInterface.scala @@ -9,6 +9,7 @@ import com.nutomic.ensichat.core.util.FutureHelper import com.nutomic.ensichat.core.{Address, ConnectionHandler, Crypto, Message} import scala.concurrent.ExecutionContext.Implicits.global +import scala.util.Random object InternetInterface { @@ -18,9 +19,12 @@ object InternetInterface { /** * Handles all Internet connectivity. + * + * @param maxConnections Maximum number of concurrent connections that should be opened. */ class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto, - settings: SettingsInterface) extends TransmissionInterface { + settings: SettingsInterface, maxConnections: Int) + extends TransmissionInterface { private val Tag = "InternetInterface" @@ -37,7 +41,7 @@ class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto, override def create(): Unit = { FutureHelper { serverThread.start() - openAllConnections() + openAllConnections(maxConnections) } } @@ -49,11 +53,15 @@ class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto, connections.foreach(_.close()) } - private def openAllConnections(): Unit = - settings.get(SettingsInterface.KeyServers, "") + private def openAllConnections(maxConnections: Int): Unit = { + val addresses = settings.get(SettingsInterface.KeyServers, "") .split(",") .map(_.trim()) + + Random.shuffle(addresses.toList) + .take(maxConnections) .foreach(openConnection) + } private def openConnection(addressPort: String): Unit = { val split = addressPort.split(":") @@ -124,9 +132,9 @@ class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto, def connectionChanged(): Unit = { FutureHelper { - Log.i(Tag, "Network has changed. Close all connections and connect to bootstrap nodes again") + Log.i(Tag, "Network has changed. Closing all connections and connecting to bootstrap nodes again") connections.foreach(_.close()) - openAllConnections() + openAllConnections(maxConnections) } } diff --git a/server/src/main/scala/com/nutomic/ensichat/server/Main.scala b/server/src/main/scala/com/nutomic/ensichat/server/Main.scala index faec70a..10da5f8 100644 --- a/server/src/main/scala/com/nutomic/ensichat/server/Main.scala +++ b/server/src/main/scala/com/nutomic/ensichat/server/Main.scala @@ -22,7 +22,8 @@ object Main extends App with CallbackInterface { private lazy val logInstance = new Logging() private lazy val settings = new Settings(ConfigFile) private lazy val crypto = new Crypto(settings, KeyFolder) - private lazy val connectionHandler = new ConnectionHandler(settings, new Database(), this, crypto) + private lazy val connectionHandler = + new ConnectionHandler(settings, new Database(), this, crypto, 7) init()