Added user option for server.

This commit is contained in:
Felix Ableitner 2016-01-25 22:17:56 +01:00
parent 1df3d9a46f
commit ddb3d64708
9 changed files with 50 additions and 21 deletions

View file

@ -91,6 +91,9 @@
<!-- Preference title (debug only)-->
<string name="max_connections" translatable="false">Maximum Number of Connections</string>
<!-- Preference title -->
<string name="servers">Servers</string>
<!-- Preference title -->
<string name="report_issue">Report Issue</string>

View file

@ -32,6 +32,10 @@
android:inputType="number"
android:numeric="integer" />
<EditTextPreference
android:title="@string/servers"
android:key="servers" />
<Preference
android:title="@string/report_issue"
android:summary="@string/report_issue_summary" >

View file

@ -69,11 +69,12 @@ class FirstStartActivity extends AppCompatActivity with OnEditorActionListener w
preferences
.edit()
.putBoolean(KeyIsFirstStart, false)
.putString(SettingsInterface.KeyUserName, username.getText.toString.trim)
.putString(SettingsInterface.KeyUserStatus, SettingsInterface.DefaultUserStatus)
.putBoolean(SettingsInterface.KeyNotificationSoundsOn, DefaultNotificationSoundsOn)
.putString(SettingsInterface.KeyScanInterval, DefaultScanInterval.toString)
.putString(SettingsInterface.KeyMaxConnections, DefaultMaxConnections.toString)
.putString(KeyUserName, username.getText.toString.trim)
.putString(KeyUserStatus, SettingsInterface.DefaultUserStatus)
.putBoolean(KeyNotificationSoundsOn, DefaultNotificationSoundsOn)
.putString(KeyScanInterval, DefaultScanInterval.toString)
.putString(KeyMaxConnections, DefaultMaxConnections.toString)
.putString(KeyServers, DefaultServers.mkString(", "))
.apply()
startMainActivity()

View file

@ -1,6 +1,6 @@
package com.nutomic.ensichat.fragments
import android.content.SharedPreferences
import android.content.{Intent, SharedPreferences}
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
import android.os.Bundle
import android.preference.{PreferenceFragment, PreferenceManager}
@ -8,6 +8,7 @@ import com.nutomic.ensichat.activities.EnsichatActivity
import com.nutomic.ensichat.core.body.UserInfo
import com.nutomic.ensichat.core.interfaces.SettingsInterface._
import com.nutomic.ensichat.fragments.SettingsFragment._
import com.nutomic.ensichat.service.ChatService
import com.nutomic.ensichat.util.Database
import com.nutomic.ensichat.{BuildConfig, R}
@ -54,6 +55,10 @@ class SettingsFragment extends PreferenceFragment with OnSharedPreferenceChangeL
val service = getActivity.asInstanceOf[EnsichatActivity].service
val ui = new UserInfo(prefs.getString(KeyUserName, ""), prefs.getString(KeyUserStatus, ""))
database.getContacts.foreach(c => service.get.sendTo(c.address, ui))
case KeyServers =>
val intent = new Intent(getActivity, classOf[ChatService])
intent.setAction(ChatService.ActionNetworkChanged)
getActivity.startService(intent)
case _ =>
}
}

View file

@ -41,7 +41,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)
transmissionInterfaces += new InternetInterface(this, crypto, settings)
transmissionInterfaces.foreach(_.create())
}
}

View file

@ -7,19 +7,25 @@ object SettingsInterface {
val KeyNotificationSoundsOn = "notification_sounds"
/**
* NOTE: Stored as string in settings.
* NOTE: Stored as string.
*/
val KeyScanInterval = "scan_interval_seconds"
/**
* NOTE: Stored as string in settings.
* NOTE: Stored as string.
*/
val KeyMaxConnections = "max_connections"
/**
* NOTE: Stored as comma separated string.
*/
val KeyServers = "servers"
val DefaultUserStatus = "Let's chat!"
val DefaultScanInterval = 15
val DefaultNotificationSoundsOn = true
val DefaultMaxConnections = 1000000
val DefaultServers = Set("46.101.249.188:26344")
}

View file

@ -4,29 +4,27 @@ import java.io.IOException
import java.net.{InetAddress, Socket}
import com.nutomic.ensichat.core.body.ConnectionInfo
import com.nutomic.ensichat.core.interfaces.{Log, TransmissionInterface}
import com.nutomic.ensichat.core.interfaces.{SettingsInterface, Log, TransmissionInterface}
import com.nutomic.ensichat.core.util.FutureHelper
import com.nutomic.ensichat.core.{Address, ConnectionHandler, Crypto, Message}
import scala.concurrent.ExecutionContext.Implicits.global
object InternetInterface {
val Port = 26344
val BootstrapNodes = Set("192.168.1.104:26344", // T420
"46.101.249.188:26344") // digital ocean
val ServerPort = 26344
}
/**
* Handles all Internet connectivity.
*/
class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto)
extends TransmissionInterface {
class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto,
settings: SettingsInterface) extends TransmissionInterface {
private val Tag = "InternetInterface"
private lazy val serverThread = new InternetServerThread(crypto, onConnected, onDisconnected, onReceiveMessage)
private lazy val serverThread =
new InternetServerThread(crypto, onConnected, onDisconnected, onReceiveMessage)
private var connections = Set[InternetConnectionThread]()
@ -38,7 +36,7 @@ class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto)
override def create(): Unit = {
FutureHelper {
serverThread.start()
InternetInterface.BootstrapNodes.foreach(openConnection)
openAllConnections()
}
}
@ -50,9 +48,18 @@ class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto)
connections.foreach(_.close())
}
private def openAllConnections(): Unit =
settings.get(SettingsInterface.KeyServers, "")
.split(",")
.map(_.trim())
.foreach(openConnection)
private def openConnection(addressPort: String): Unit = {
val split = addressPort.split(":")
openConnection(split(0), split(1).toInt)
if (split.length >= 2) {
Log.d(Tag, "Attempting connection to " + addressPort)
openConnection(split(0), split(1).toInt)
}
}
/**
@ -118,7 +125,7 @@ class InternetInterface(connectionHandler: ConnectionHandler, crypto: Crypto)
FutureHelper {
Log.i(Tag, "Network has changed. Close all connections and connect to bootstrap nodes again")
connections.foreach(_.close())
InternetInterface.BootstrapNodes.foreach(openConnection)
openAllConnections()
}
}

View file

@ -14,7 +14,7 @@ class InternetServerThread(crypto: Crypto, onConnected: (InternetConnectionThrea
private val Tag = "InternetServerThread"
private lazy val socket: Option[ServerSocket] = try {
Option(new ServerSocket(InternetInterface.Port))
Option(new ServerSocket(InternetInterface.ServerPort))
} catch {
case e: IOException =>
Log.w(Tag, "Failed to create server socket", e)

View file

@ -4,6 +4,7 @@ import java.io.File
import java.util.concurrent.TimeUnit
import com.nutomic.ensichat.core.body.Text
import com.nutomic.ensichat.core.interfaces.SettingsInterface._
import com.nutomic.ensichat.core.interfaces.{CallbackInterface, Log, SettingsInterface}
import com.nutomic.ensichat.core.{Message, ConnectionHandler, Crypto}
import scopt.OptionParser
@ -35,6 +36,8 @@ object Main extends App with CallbackInterface {
KeyFolder.mkdirs()
Log.setLogInstance(logInstance)
sys.addShutdownHook(connectionHandler.stop())
settings.put(KeyServers, DefaultServers.mkString(", "))
val parser = new OptionParser[Config]("ensichat") {
head("ensichat")
opt[String]('n', "name") action { (x, c) =>