diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/Connection.kt b/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/Connection.kt index d4a7eb8..3ad9e78 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/Connection.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/Connection.kt @@ -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) : Array { + fun query(query : Array) : Array { 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>(pkg.content, Array::class.java) + return Gson().fromJson>(pkg.content, Array::class.java).map { + tex -> tex.toTexture() + }.toTypedArray() } catch (e : JsonSyntaxException ){ - throw ConnectionInvalidJsonException(e) + throw ConnectionInvalidJsonException() } } is BinaryPacket -> throw ConnectionUnexpectedPacket() diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/InternalDataModel.kt b/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/InternalDataModel.kt index 787788c..8f7dacf 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/InternalDataModel.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/InternalDataModel.kt @@ -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, - val format : String, + val id: String, + val name: String, + val tags: Array, + val format: String, val resolution: Array, - val added_on : String, + val added_on: Array, val texture_hash: String -) \ No newline at end of file +) { + 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() + } + } +} \ No newline at end of file diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/error.kt b/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/error.kt index 7804f55..93679f0 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/error.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/error.kt @@ -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.") diff --git a/client/src/main/kotlin/org/hso/texturesyncclient/model/DataModel.kt b/client/src/main/kotlin/org/hso/texturesyncclient/model/DataModel.kt index 089a425..7957335 100644 --- a/client/src/main/kotlin/org/hso/texturesyncclient/model/DataModel.kt +++ b/client/src/main/kotlin/org/hso/texturesyncclient/model/DataModel.kt @@ -16,6 +16,6 @@ data class Texture( val tags : Array, val format : TextureFormat, val resolution : Pair, - val addedOn : Date, + val addedOn : Calendar, val textureHash : ByteArray )