ServerConnectionModel

object ServerConnectionModel

Data model that stores all relevant information about the current server connection. Also handles server discovery and heartbeats.

Accessed through ServerConnectionViewModel.

Types

HandlerMessages
Link copied to clipboard
private object HandlerMessages

All potential messages that can be sent to mHandler

Functions

connectToServer
Link copied to clipboard
private fun connectToServer(url: String)

Connects to the server with the given url.

discover
Link copied to clipboard
private fun discover(context: Context)

Calls discoverServersOnNetwork to discover all available servers on the network, passes onServerURLsGet as a callback.

getRunnableInterval
Link copied to clipboard
private fun getRunnableInterval(): Long

Returns the interval in which heartbeatRunnable/discoverRunnable should be called.

heartbeat
Link copied to clipboard
private fun heartbeat()

Calls sendHeartbeatRequest if the application is currently connected to a server; Passes onHeartbeatFail as a callback.

isKnownServer
Link copied to clipboard
private fun isKnownServer(hostname: String): Boolean

Checks if a given hostname is already known (= has been connected to before)

onConnectionChanged
Link copied to clipboard
private fun onConnectionChanged(newConnectionState: Boolean)

Callback that gets executed when the current connection status changes. Updates isDiscovering&isHeartbeating and calls either startDiscoverThread or startHeartbeatThread depending on newConnectionState

onHeartbeatFail
Link copied to clipboard
private fun onHeartbeatFail()

Callback that gets executed when sending a heartbeat fails. Calls onConnectionChanged to update the connection state

onServerURLsGet
Link copied to clipboard
private fun onServerURLsGet(servers: List<Pair<String, String>>)

Callback that gets executed when new servers have been discovered via discoverServersOnNetwork.

setServerUrl
Link copied to clipboard
fun setServerUrl(hostname: String)

Changes the current serverUrl to the url that is associated with the provided hostname

setServerUrlList
Link copied to clipboard
fun setServerUrlList(serverUrlList: List<Pair<String, String>>): List<Pair<String, String>>

Replaces the current serverUrlList with the provided serverUrlList

start
Link copied to clipboard
fun start(application: Application, isForeground: Boolean)

Initiates the threads & handlers. Starts the first Runnable.

startDiscoverThread
Link copied to clipboard
private fun startDiscoverThread()
startHeartbeatThread
Link copied to clipboard
private fun startHeartbeatThread()
stopThreads
Link copied to clipboard
fun stopThreads()

Stops all runnables

tryConnectToFirstServer
Link copied to clipboard
private fun tryConnectToFirstServer(servers: List<Pair<String, String>>)

Automatically connects to the first known server in serverUrlList.

updateServerUrlList
Link copied to clipboard
private fun updateServerUrlList(newServers: List<Pair<String, String>>)

Helper function to update serverUrlList with newServers

Properties

application
Link copied to clipboard
private var application: Application? = null
discoverRunnable
Link copied to clipboard
private val discoverRunnable: ServerConnectionModel.<no name provided>

Runnable that discovers all available servers on the network.

handlerThread
Link copied to clipboard
private lateinit var handlerThread: HandlerThread
heartbeatRunnable
Link copied to clipboard
private val heartbeatRunnable: ServerConnectionModel.<no name provided>

Runnable that calls heartbeat in an interval of getRunnableInterval ms.

isConnected
Link copied to clipboard
var isConnected: MutableLiveData<Boolean>

Whether or not the application is connected to a server

isDiscovering
Link copied to clipboard
var isDiscovering: MutableLiveData<Boolean>

Whether or not the application is currently searching for servers

isForeground
Link copied to clipboard
private var isForeground: Boolean = true
isHeartbeating
Link copied to clipboard
var isHeartbeating: MutableLiveData<Boolean>

Whether or not the application is currently sending out heartbeats to a server

looper
Link copied to clipboard
private lateinit var looper: Looper
mHandler
Link copied to clipboard
private lateinit var mHandler: Handler
serverUrl
Link copied to clipboard
var serverUrl: MutableLiveData<String>

The currently connected server

serverUrlList
Link copied to clipboard
var serverUrlList: MutableLiveData<List<Pair<String, String>>>

All currently available servers