Commit ac09f06a authored by Waweic's avatar Waweic 💬
Browse files

Move Stream reading to renderer

parent 2cd9afc9
Pipeline #290266 passed with stages
in 4 minutes and 19 seconds
......@@ -3,6 +3,7 @@
<words>
<w>decentral</w>
<w>geminiclient</w>
<w>redirections</w>
</words>
</dictionary>
</component>
\ No newline at end of file
......@@ -5,6 +5,7 @@ import android.widget.EditText
import android.widget.TextView
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import java.net.URI
class GeminiClient(applicationContext: Context, mainContentView: TextView, val uriField: EditText) {
......@@ -20,7 +21,7 @@ class GeminiClient(applicationContext: Context, mainContentView: TextView, val u
}
uriField.setText(uri.toString())
val deferredResult = GlobalScope.async { connection.request(uri) }
responseHandler.handle(deferredResult)
GlobalScope.launch { responseHandler.handle(deferredResult) }
}
fun goToPage(uri: String) {
//TODO: Do checking and stuff
......
package rocks.ism.decentral.geminiclient
import android.util.Log
import java.io.*
import java.net.URI
import javax.net.ssl.SSLSocket
......@@ -9,7 +10,7 @@ import javax.net.ssl.SSLSocketFactory
class GeminiConnection {
var currentURI : URI? = null
private set
suspend fun request(uri: URI): GeminiResult {
fun request(uri: URI): GeminiResult {
currentURI = uri
if(uri.scheme != "gemini") {
return GeminiResult(this, GeminiConnectionState.FAILED_WRONG_URI_SCHEME)
......@@ -23,6 +24,7 @@ class GeminiConnection {
val factory: SSLSocketFactory = SSLSocketFactory.getDefault() as SSLSocketFactory
val socket: SSLSocket = factory.createSocket(uri.host, port) as SSLSocket
socket.enabledProtocols = arrayOf("TLSv1.2")
socket.startHandshake()
val outWriter = PrintWriter(
......@@ -47,13 +49,15 @@ class GeminiConnection {
)
val responseHeader: String = inReader.readLine()
val responseBody: String = inReader.readText()
//val responseBody: String = inReader.readText()
inReader.close()
outWriter.close()
/*
socket.close()
*/
return GeminiResult(this, GeminiConnectionState.SUCCESS, responseHeader, responseBody)
return GeminiResult(this, GeminiConnectionState.SUCCESS, responseHeader, socket.inputStream)
}
}
......@@ -74,7 +78,7 @@ class GeminiResult(val connection: GeminiConnection, val connectionState: Gemini
var meta: String = ""
private set
var body: String = ""
lateinit var body: InputStream
private set
constructor (connection: GeminiConnection, connectionState: GeminiConnectionState, header: String) : this(connection, connectionState) {
......@@ -84,7 +88,7 @@ class GeminiResult(val connection: GeminiConnection, val connectionState: Gemini
this.statusCode = headerParts[0].toInt() //TODO: Check if not an int
this.meta = headerParts[1]
}
constructor (connection: GeminiConnection, connectionState: GeminiConnectionState, header: String, body: String) : this(connection, connectionState, header) {
constructor (connection: GeminiConnection, connectionState: GeminiConnectionState, header: String, body: InputStream) : this(connection, connectionState, header) {
this.body = body
}
......
package rocks.ism.decentral.geminiclient
import android.graphics.Typeface
import android.provider.Settings
import android.text.SpannableString
import android.text.Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
import android.text.method.LinkMovementMethod
......@@ -8,13 +9,21 @@ import android.text.style.BulletSpan
import android.text.style.RelativeSizeSpan
import android.text.style.StyleSpan
import android.text.style.URLSpan
import android.util.Log
import android.widget.TextView
import kotlinx.coroutines.*
import java.io.BufferedReader
import java.io.InputStream
import java.io.InputStreamReader
import java.io.StringReader
import java.util.*
class GeminiRenderer (val textView: TextView, val geminiClient: GeminiClient) {
var preRenderMode = false
fun render(resultBody: String, mimetype: String) {
textView.text = ""
textView.movementMethod = LinkMovementMethod.getInstance()
if(mimetype == "text/gemini") {
renderGemini(resultBody)
}
......@@ -22,11 +31,51 @@ class GeminiRenderer (val textView: TextView, val geminiClient: GeminiClient) {
renderPlaintext(resultBody)
}
}
fun renderStream(inputStream: InputStream, mimetype: String) {
val scanner = Scanner(inputStream, "utf-8")
CoroutineScope(Dispatchers.Main).launch {
textView.text = ""
textView.movementMethod = LinkMovementMethod.getInstance()
/*
var line: String?
do {
line = GlobalScope.async {
if(scanner.hasNextLine()) {
scanner.nextLine()
} else {
null
}
}.await()
line?.let {renderLine(it, mimetype)}
} while(line != null)
*/
var allLines = GlobalScope.async {
BufferedReader (
inputStream.reader()
).readLines()
}
allLines.await().forEach() {
renderLine(it, mimetype)
}
}
}
fun renderLine(line: String, mimetype: String) {
if(mimetype == "text/gemini") {
renderGeminiLine(line)
}
else {
textView.append(line + "\n")
}
}
fun renderGemini(resultBody: String) {
val bodyReader = BufferedReader(StringReader(resultBody))
preRenderMode = false
textView.text = ""
textView.movementMethod = LinkMovementMethod.getInstance()
var line: String? = null
while ({ line = bodyReader.readLine(); line }() != null) {
line?.let {
......
......@@ -3,16 +3,12 @@ package rocks.ism.decentral.geminiclient
import android.content.Context
import android.content.Intent
import android.net.Uri
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
class GeminiResponseHandler (val renderer: GeminiRenderer, private val context: Context, val geminiClient: GeminiClient) {
var numberOfConsecutiveRedirects = 0
fun handle (deferredResult: Deferred<GeminiResult>) {
CoroutineScope(Dispatchers.Main).launch {
suspend fun handle (deferredResult: Deferred<GeminiResult>) {
val result = deferredResult.await()
if(result.header.startsWith("3")) {
......@@ -36,15 +32,13 @@ class GeminiResponseHandler (val renderer: GeminiRenderer, private val context:
else if(result.header.startsWith("2")){
numberOfConsecutiveRedirects = 0
if(result.meta == "text/gemini") {
renderer.render(result.body, "text/gemini")
renderer.renderStream(result.body, "text/gemini")
} else if (result.meta.startsWith("text/")) {
renderer.render(result.body,"text/plain")
renderer.renderStream(result.body,"text/plain")
}
}
else {
renderer.render(result.header,"text/plain")
}
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment