Match DataModel On Query

This commit is contained in:
CodeSteak 2019-06-02 22:32:19 +02:00
parent 40cda05ad4
commit d7dd858d42
4 changed files with 76 additions and 12 deletions

View File

@ -1,6 +1,7 @@
package org.hso.texturesyncclient.controller.net
import com.google.gson.*
import org.hso.texturesyncclient.model.Texture
import java.io.*
import java.lang.RuntimeException
import java.net.*
@ -53,7 +54,7 @@ class Connection(val addr: InetAddress, val port: Int = 10796) : Closeable {
}
}
fun query(query : Array<String>) : Array<InternalTexture> {
fun query(query : Array<String>) : Array<Texture> {
val io = getStreams()
val obj = JsonObject()
@ -76,9 +77,11 @@ class Connection(val addr: InetAddress, val port: Int = 10796) : Closeable {
when (pkg) {
is JsonPacket -> {
try {
return Gson().fromJson<Array<InternalTexture>>(pkg.content, Array<InternalTexture>::class.java)
return Gson().fromJson<Array<InternalTexture>>(pkg.content, Array<InternalTexture>::class.java).map {
tex -> tex.toTexture()
}.toTypedArray()
} catch (e : JsonSyntaxException ){
throw ConnectionInvalidJsonException(e)
throw ConnectionInvalidJsonException()
}
}
is BinaryPacket -> throw ConnectionUnexpectedPacket()

View File

@ -1,14 +1,72 @@
package org.hso.texturesyncclient.controller.net
import org.hso.texturesyncclient.model.Texture
import org.hso.texturesyncclient.model.TextureFormat
import java.lang.Exception
import java.util.*
// These types will be converted to the model.DataModel
private fun bytes2hexString(bytes: ByteArray): String {
val s = StringBuilder(64)
for (byte in bytes) {
s.append(String.format("%02X"), byte)
}
return s.toString()
}
private fun hexString2Bytes(bytes: String): ByteArray {
return ByteArray(bytes.length / 2) { i ->
bytes.substring(i * 2, i * 2 + 2).toInt(16).toByte()
}
}
@Suppress("ArrayInDataClass")
data class InternalTexture(
val id : String,
val name : String,
val tags : Array<String>,
val format : String,
val id: String,
val name: String,
val tags: Array<String>,
val format: String,
val resolution: Array<Int>,
val added_on : String,
val added_on: Array<Int>,
val texture_hash: String
)
) {
constructor(tex: Texture) : this(
id = tex.id.toString(),
name = tex.name,
tags = tex.tags.clone(),
format = when (tex.format) {
TextureFormat.PNG -> "png"
TextureFormat.JPEG -> "jpeg"
},
resolution = arrayOf(tex.resolution.first, tex.resolution.second),
added_on = arrayOf(
tex.addedOn.get(Calendar.YEAR), //
tex.addedOn.get(Calendar.MONTH), //
tex.addedOn.get(Calendar.DAY_OF_MONTH)
),
texture_hash = bytes2hexString(tex.textureHash)
)
@Throws(ConnectionException::class)
fun toTexture(): Texture {
try {
return Texture(
id = UUID.fromString(id),
name = name,
tags = tags.clone(),
format = when (format.toLowerCase()) {
"jpeg" -> TextureFormat.JPEG
"jpg" -> TextureFormat.JPEG
"png" -> TextureFormat.PNG
else -> throw ConnectionInvalidJsonException()
},
resolution = Pair(resolution[0], resolution[1]),
addedOn = GregorianCalendar(added_on[0], added_on[1], added_on[2]),
textureHash = hexString2Bytes(texture_hash)
)
} catch (e: Exception) { // i Know, but no time :[]
throw ConnectionInvalidJsonException()
}
}
}

View File

@ -6,9 +6,12 @@ import java.lang.Exception
sealed class ConnectionException(val msg : String) : Exception(msg)
class ConnectionErrorException(val error : ErrorPacket) : ConnectionException("${error.code} ${error.message}")
class ConnectionUnexpectedPacket() : ConnectionException("Got Unexpected Type of Packet")
class ConnectionInvalidJsonException(val jsonError : JsonSyntaxException) : ConnectionException("The Format of the Json Received is Unexpected.")
class ConnectionUnexpectedPacket : ConnectionException("Got Unexpected Type of Packet")
class ConnectionInvalidJsonException : ConnectionException("The Format of the Json Received is Unexpected.")
sealed class PacketException(msg: String) : ConnectionException(msg)
class PacketTooLongException : PacketException("The Package is too long.")

View File

@ -16,6 +16,6 @@ data class Texture(
val tags : Array<String>,
val format : TextureFormat,
val resolution : Pair<Int, Int>,
val addedOn : Date,
val addedOn : Calendar,
val textureHash : ByteArray
)