Limit number of internet connections, connect to random node.

This commit is contained in:
Felix Ableitner 2016-02-16 19:08:51 +01:00
parent 375d245765
commit 4d6afbc9cd
4 changed files with 23 additions and 10 deletions

View file

@ -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()

View file

@ -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())
}
}

View file

@ -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)
}
}

View file

@ -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()