Limit number of internet connections, connect to random node.
This commit is contained in:
parent
375d245765
commit
4d6afbc9cd
4 changed files with 23 additions and 10 deletions
|
@ -32,7 +32,7 @@ class ChatService extends Service {
|
||||||
|
|
||||||
private lazy val connectionHandler =
|
private lazy val connectionHandler =
|
||||||
new ConnectionHandler(new SettingsWrapper(this), new Database(this), callbackHandler,
|
new ConnectionHandler(new SettingsWrapper(this), new Database(this), callbackHandler,
|
||||||
ChatService.newCrypto(this))
|
ChatService.newCrypto(this), 1)
|
||||||
|
|
||||||
private val networkReceiver = new NetworkChangedReceiver()
|
private val networkReceiver = new NetworkChangedReceiver()
|
||||||
|
|
||||||
|
|
|
@ -12,9 +12,13 @@ import scala.concurrent.ExecutionContext.Implicits.global
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* High-level handling of all message transfers and callbacks.
|
* 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,
|
final class ConnectionHandler(settings: SettingsInterface, database: DatabaseInterface,
|
||||||
callbacks: CallbackInterface, crypto: Crypto) {
|
callbacks: CallbackInterface, crypto: Crypto,
|
||||||
|
maxInternetConnections: Int) {
|
||||||
|
|
||||||
private val Tag = "ConnectionHandler"
|
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, "Service started, address is " + crypto.localAddress)
|
||||||
Log.i(Tag, "Local user is " + settings.get(SettingsInterface.KeyUserName, "none") +
|
Log.i(Tag, "Local user is " + settings.get(SettingsInterface.KeyUserName, "none") +
|
||||||
" with status '" + settings.get(SettingsInterface.KeyUserStatus, "") + "'")
|
" with status '" + settings.get(SettingsInterface.KeyUserStatus, "") + "'")
|
||||||
transmissionInterfaces += new InternetInterface(this, crypto, settings)
|
transmissionInterfaces += new InternetInterface(this, crypto, settings, maxInternetConnections)
|
||||||
transmissionInterfaces.foreach(_.create())
|
transmissionInterfaces.foreach(_.create())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@ import com.nutomic.ensichat.core.util.FutureHelper
|
||||||
import com.nutomic.ensichat.core.{Address, ConnectionHandler, Crypto, Message}
|
import com.nutomic.ensichat.core.{Address, ConnectionHandler, Crypto, Message}
|
||||||
|
|
||||||
import scala.concurrent.ExecutionContext.Implicits.global
|
import scala.concurrent.ExecutionContext.Implicits.global
|
||||||
|
import scala.util.Random
|
||||||
|
|
||||||
object InternetInterface {
|
object InternetInterface {
|
||||||
|
|
||||||
|
@ -18,9 +19,12 @@ object InternetInterface {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Handles all Internet connectivity.
|
* Handles all Internet connectivity.
|
||||||
|
*
|
||||||
|
* @param maxConnections Maximum number of concurrent connections that should be opened.
|
||||||
*/
|
*/
|
||||||
class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto,
|
class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto,
|
||||||
settings: SettingsInterface) extends TransmissionInterface {
|
settings: SettingsInterface, maxConnections: Int)
|
||||||
|
extends TransmissionInterface {
|
||||||
|
|
||||||
private val Tag = "InternetInterface"
|
private val Tag = "InternetInterface"
|
||||||
|
|
||||||
|
@ -37,7 +41,7 @@ class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto,
|
||||||
override def create(): Unit = {
|
override def create(): Unit = {
|
||||||
FutureHelper {
|
FutureHelper {
|
||||||
serverThread.start()
|
serverThread.start()
|
||||||
openAllConnections()
|
openAllConnections(maxConnections)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -49,11 +53,15 @@ class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto,
|
||||||
connections.foreach(_.close())
|
connections.foreach(_.close())
|
||||||
}
|
}
|
||||||
|
|
||||||
private def openAllConnections(): Unit =
|
private def openAllConnections(maxConnections: Int): Unit = {
|
||||||
settings.get(SettingsInterface.KeyServers, "")
|
val addresses = settings.get(SettingsInterface.KeyServers, "")
|
||||||
.split(",")
|
.split(",")
|
||||||
.map(_.trim())
|
.map(_.trim())
|
||||||
|
|
||||||
|
Random.shuffle(addresses.toList)
|
||||||
|
.take(maxConnections)
|
||||||
.foreach(openConnection)
|
.foreach(openConnection)
|
||||||
|
}
|
||||||
|
|
||||||
private def openConnection(addressPort: String): Unit = {
|
private def openConnection(addressPort: String): Unit = {
|
||||||
val split = addressPort.split(":")
|
val split = addressPort.split(":")
|
||||||
|
@ -124,9 +132,9 @@ class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto,
|
||||||
|
|
||||||
def connectionChanged(): Unit = {
|
def connectionChanged(): Unit = {
|
||||||
FutureHelper {
|
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())
|
connections.foreach(_.close())
|
||||||
openAllConnections()
|
openAllConnections(maxConnections)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -22,7 +22,8 @@ object Main extends App with CallbackInterface {
|
||||||
private lazy val logInstance = new Logging()
|
private lazy val logInstance = new Logging()
|
||||||
private lazy val settings = new Settings(ConfigFile)
|
private lazy val settings = new Settings(ConfigFile)
|
||||||
private lazy val crypto = new Crypto(settings, KeyFolder)
|
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()
|
init()
|
||||||
|
|
||||||
|
|
Reference in a new issue