forked from hush/SilentDragonAndroid-old
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
212 lines
7.5 KiB
212 lines
7.5 KiB
5 years ago
|
package org.myhush.silentdragon
|
||
5 years ago
|
|
||
|
import android.content.Intent
|
||
|
import android.util.Log
|
||
|
import com.beust.klaxon.json
|
||
|
import okhttp3.*
|
||
|
import okio.ByteString
|
||
|
import java.net.ConnectException
|
||
5 years ago
|
import java.util.*
|
||
5 years ago
|
import java.util.concurrent.TimeUnit
|
||
|
|
||
|
|
||
|
object ConnectionManager {
|
||
|
|
||
|
val DATA_SIGNAL: String = "ConnectionManager_NewData_Signal"
|
||
|
|
||
|
/**
|
||
|
* Refresh all data, including attempting a connection if none exists
|
||
|
*/
|
||
|
fun refreshAllData() {
|
||
|
// First, try to make a connection.
|
||
|
|
||
|
makeConnection()
|
||
|
}
|
||
|
|
||
|
// Attempt a connection to the server. If there is no saved connection, we'll set the connection status
|
||
|
// to None
|
||
|
private fun makeConnection(directConn : Boolean = true) {
|
||
5 years ago
|
val connString =
|
||
|
DataModel.getConnString(SilentDragonApp.appContext!!)
|
||
5 years ago
|
if (connString.isNullOrBlank()) {
|
||
|
// The user might have just disconnected, so make sure we are disconnected
|
||
|
|
||
|
DataModel.ws?.close(1000, "disconnected")
|
||
|
sendUpdateDataSignal(true)
|
||
|
return
|
||
|
}
|
||
|
|
||
|
println("MakeConnection")
|
||
|
|
||
|
// If still connecting, this is a duplicate call, so do nothing but wait.
|
||
|
if (DataModel.connStatus == DataModel.ConnectionStatus.CONNECTING) {
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// If already connected, then refresh data
|
||
|
if (DataModel.connStatus == DataModel.ConnectionStatus.CONNECTED) {
|
||
|
DataModel.makeAPICalls()
|
||
|
return
|
||
|
}
|
||
|
|
||
|
println("Attempting new connection ${DataModel.connStatus}")
|
||
5 years ago
|
sendRefreshSignal(false)
|
||
5 years ago
|
|
||
|
// If direct connection, then connect to the URL in connection string
|
||
|
if (directConn) {
|
||
|
// Update status to connecting, so we can update the UI
|
||
5 years ago
|
DataModel.connStatus =
|
||
|
DataModel.ConnectionStatus.CONNECTING
|
||
5 years ago
|
println("Connstatus = connecting")
|
||
|
|
||
|
val client = OkHttpClient.Builder().connectTimeout(2, TimeUnit.SECONDS).build()
|
||
|
val request = Request.Builder().url(connString).build()
|
||
|
val listener = WebsocketClient(true)
|
||
|
|
||
|
DataModel.ws = client.newWebSocket(request, listener)
|
||
|
} else {
|
||
|
// Connect to the wormhole
|
||
5 years ago
|
DataModel.connStatus =
|
||
|
DataModel.ConnectionStatus.CONNECTING
|
||
5 years ago
|
|
||
|
println("Connstatus = connecting")
|
||
|
|
||
5 years ago
|
val client = OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS).build()
|
||
5 years ago
|
val request = Request.Builder().url("wss://wormhole.myhush.org:443").build()
|
||
5 years ago
|
//val request = Request.Builder().url("ws://192.168.5.187:7070").build()
|
||
5 years ago
|
val listener = WebsocketClient(false)
|
||
|
|
||
|
DataModel.ws = client.newWebSocket(request, listener)
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
fun closeConnection() {
|
||
|
DataModel.ws?.close(1000, "Close requested")
|
||
|
}
|
||
|
|
||
5 years ago
|
fun sendRefreshSignal(finished: Boolean) {
|
||
|
val i = Intent(DATA_SIGNAL)
|
||
|
i.putExtra("action", "refresh")
|
||
|
i.putExtra("finished", finished)
|
||
5 years ago
|
SilentDragonApp.appContext?.sendBroadcast(i)
|
||
5 years ago
|
}
|
||
|
|
||
5 years ago
|
fun sendUpdateDataSignal(updateTxns: Boolean = false) {
|
||
|
val i = Intent(DATA_SIGNAL)
|
||
|
i.putExtra("action", "newdata")
|
||
|
i.putExtra("updateTxns", updateTxns)
|
||
5 years ago
|
SilentDragonApp.appContext?.sendBroadcast(i)
|
||
5 years ago
|
}
|
||
|
|
||
|
fun sendErrorSignal(msg: String? = null, doDisconnect: Boolean = false) {
|
||
|
val i = Intent(DATA_SIGNAL)
|
||
|
i.putExtra("action", "error")
|
||
|
i.putExtra("msg", msg)
|
||
|
i.putExtra("doDisconnect", doDisconnect)
|
||
5 years ago
|
SilentDragonApp.appContext?.sendBroadcast(i)
|
||
5 years ago
|
}
|
||
|
|
||
|
|
||
|
private class WebsocketClient (directConn: Boolean) : WebSocketListener() {
|
||
|
val m_directConn = directConn
|
||
|
val TAG = "WebsocketClient"
|
||
|
|
||
|
override fun onOpen(webSocket: WebSocket, response: Response) {
|
||
|
Log.d(TAG, "Opened Websocket")
|
||
5 years ago
|
DataModel.connStatus =
|
||
|
DataModel.ConnectionStatus.CONNECTED
|
||
5 years ago
|
println("Connstatus = connected")
|
||
|
|
||
5 years ago
|
// If direct connection, start making API calls to get data.
|
||
5 years ago
|
if (m_directConn) {
|
||
|
DataModel.makeAPICalls()
|
||
|
} else {
|
||
5 years ago
|
// If this is a connection to wormhole, we have to register ourselves before we make any API calls
|
||
5 years ago
|
if (!DataModel.getWormholeCode().isNullOrBlank()) {
|
||
5 years ago
|
webSocket.send( json { obj( "register" to DataModel.getWormholeCode()) }.toJsonString())
|
||
5 years ago
|
|
||
|
// Delay sending the API calls a bit to let the register call finish
|
||
|
Timer().schedule(object : TimerTask() {
|
||
5 years ago
|
override fun run() {
|
||
|
DataModel.makeAPICalls()
|
||
|
}}, 100)
|
||
5 years ago
|
}
|
||
5 years ago
|
}
|
||
|
|
||
|
sendUpdateDataSignal()
|
||
|
}
|
||
|
|
||
|
override fun onMessage(webSocket: WebSocket?, text: String?) {
|
||
|
Log.i(TAG, "Receiving $text")
|
||
|
|
||
|
val r = DataModel.parseResponse(text!!)
|
||
|
|
||
|
if (r.displayMsg != null) {
|
||
5 years ago
|
sendErrorSignal(
|
||
|
r.displayMsg,
|
||
|
r.doDisconnect
|
||
|
)
|
||
5 years ago
|
|
||
|
if (r.doDisconnect) {
|
||
5 years ago
|
// We don't pass a reason here, because we already sent the error signal above
|
||
|
webSocket?.close(1000, null)
|
||
5 years ago
|
}
|
||
|
|
||
|
} else {
|
||
|
sendUpdateDataSignal(r.updateTxns)
|
||
5 years ago
|
sendRefreshSignal(r.updateTxns)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
|
||
|
override fun onMessage(webSocket: WebSocket?, bytes: ByteString) {
|
||
|
Log.i(TAG, "Receiving bytes : " + bytes.hex())
|
||
|
}
|
||
|
|
||
|
override fun onClosing(webSocket: WebSocket, code: Int, reason: String?) {
|
||
5 years ago
|
DataModel.connStatus =
|
||
|
DataModel.ConnectionStatus.DISCONNECTED
|
||
5 years ago
|
println("Connstatus = disconnected")
|
||
|
|
||
|
Log.i(TAG,"Closing : $code / $reason")
|
||
5 years ago
|
//if (!reason.isNullOrEmpty()) {
|
||
|
// sendErrorSignal(reason, true)
|
||
|
//}
|
||
5 years ago
|
sendRefreshSignal(true)
|
||
5 years ago
|
}
|
||
|
|
||
|
override fun onFailure(webSocket: WebSocket, t: Throwable, response: Response?) {
|
||
|
Log.e(TAG,"Failed $t")
|
||
5 years ago
|
DataModel.connStatus =
|
||
|
DataModel.ConnectionStatus.DISCONNECTED
|
||
5 years ago
|
|
||
5 years ago
|
val allowInternet = DataModel.getAllowInternet() && DataModel.getGlobalAllowInternet()
|
||
5 years ago
|
|
||
5 years ago
|
// If the connection is direct, and there is no need to further connect, so just error out
|
||
|
if (t is ConnectException && (m_directConn && !allowInternet)) {
|
||
5 years ago
|
var mesg = t.localizedMessage
|
||
|
if (!DataModel.getAllowInternet()) {
|
||
|
mesg += ": Connecting over the internet was not enabled by the desktop node."
|
||
|
} else if (!DataModel.getGlobalAllowInternet()) {
|
||
|
mesg += ": Connecting over the internet is disabled in settings."
|
||
|
}
|
||
|
|
||
|
sendErrorSignal(mesg, true)
|
||
5 years ago
|
sendRefreshSignal(true)
|
||
|
return
|
||
5 years ago
|
}
|
||
|
|
||
|
// If this was a direct connection and there was a failure to connect, retry connecting
|
||
|
// without the direct connection (i.e., through wormhole)
|
||
5 years ago
|
if (m_directConn && allowInternet) {
|
||
5 years ago
|
makeConnection(false)
|
||
5 years ago
|
} else {
|
||
5 years ago
|
// Not a direct connection (or we're not allowed to connect to internet) and there was a failure.
|
||
5 years ago
|
sendErrorSignal(
|
||
|
t.localizedMessage,
|
||
|
true
|
||
|
)
|
||
5 years ago
|
sendRefreshSignal(true)
|
||
5 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|