implement query ; fix error handling
This commit is contained in:
parent
d907ff138a
commit
2e344da2f4
|
@ -2,9 +2,10 @@ package org.hso.texturesyncclient.controller.net
|
|||
|
||||
import com.google.gson.*
|
||||
import java.io.*
|
||||
import java.lang.Exception
|
||||
import java.lang.RuntimeException
|
||||
import java.net.*
|
||||
|
||||
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
class Connection(val addr: InetAddress, val port: Int = 10796) : Closeable {
|
||||
|
||||
|
@ -42,10 +43,47 @@ class Connection(val addr: InetAddress, val port: Int = 10796) : Closeable {
|
|||
|
||||
JsonPacket(obj).write(io.second)
|
||||
|
||||
when (Packet.read(io.first)) {
|
||||
val pkg = Packet.read(io.first)
|
||||
|
||||
when (pkg) {
|
||||
is JsonPacket -> return
|
||||
is ErrorPacket -> throw Exception("TODO")
|
||||
is BinaryPacket -> throw Exception("TODO")
|
||||
is BinaryPacket -> throw ConnectionUnexpectedPacket()
|
||||
is ErrorPacket -> throw ConnectionErrorException(pkg)
|
||||
else -> throw RuntimeException("Unreachable")
|
||||
}
|
||||
}
|
||||
|
||||
fun query(query : Array<String>) : Array<InternalTexture> {
|
||||
val io = getStreams()
|
||||
|
||||
val obj = JsonObject()
|
||||
obj.add("query", {
|
||||
val inner = JsonObject();
|
||||
inner.add("query", {
|
||||
val array = JsonArray()
|
||||
for(queryString in query) {
|
||||
array.add(queryString)
|
||||
}
|
||||
array
|
||||
}())
|
||||
inner
|
||||
}())
|
||||
|
||||
JsonPacket(obj).write(io.second)
|
||||
|
||||
val pkg = Packet.read(io.first)
|
||||
|
||||
when (pkg) {
|
||||
is JsonPacket -> {
|
||||
try {
|
||||
return Gson().fromJson<Array<InternalTexture>>(pkg.content, Array<InternalTexture>::class.java)
|
||||
} catch (e : JsonSyntaxException ){
|
||||
throw ConnectionInvalidJsonException(e)
|
||||
}
|
||||
}
|
||||
is BinaryPacket -> throw ConnectionUnexpectedPacket()
|
||||
is ErrorPacket -> throw ConnectionErrorException(pkg)
|
||||
else -> throw RuntimeException("Unreachable")
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
package org.hso.texturesyncclient.controller.net
|
||||
|
||||
// These types will be converted to the model.DataModel
|
||||
|
||||
@Suppress("ArrayInDataClass")
|
||||
data class InternalTexture(
|
||||
val id : String,
|
||||
val name : String,
|
||||
val tags : Array<String>,
|
||||
val format : String,
|
||||
val resolution: Array<Int>,
|
||||
val added_on : String,
|
||||
val texture_hash: String
|
||||
)
|
|
@ -1,17 +1,11 @@
|
|||
package org.hso.texturesyncclient.controller.net
|
||||
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonElement
|
||||
import com.google.gson.JsonParseException
|
||||
import com.google.gson.JsonParser
|
||||
import java.io.DataInputStream
|
||||
import java.io.DataOutputStream
|
||||
import java.io.IOException
|
||||
import java.lang.Exception
|
||||
|
||||
open class PacketException(msg: String) : Exception(msg)
|
||||
class PacketTooLongException : PacketException("The Package is too long.")
|
||||
class PacketInvalidType : PacketException("The Package has an Invalid Type.")
|
||||
class PacketInvalidData : PacketException("The Package has an Invalid Data. (e.g. Invalid Json.)")
|
||||
|
||||
abstract class Packet {
|
||||
|
||||
|
@ -81,7 +75,7 @@ abstract class Packet {
|
|||
|
||||
TYPE_JSON -> {
|
||||
try {
|
||||
val obj = JsonParser().parse(String(payload)).asJsonObject
|
||||
val obj = JsonParser().parse(String(payload))
|
||||
|
||||
return JsonPacket(obj)
|
||||
} catch (e: JsonParseException) {
|
||||
|
@ -103,7 +97,7 @@ abstract class Packet {
|
|||
|
||||
}
|
||||
|
||||
data class JsonPacket(val content: JsonObject) : Packet() {
|
||||
data class JsonPacket(val content: JsonElement) : Packet() {
|
||||
override fun write(out: DataOutputStream) {
|
||||
val payload = content.toString().toByteArray()
|
||||
// Tag Byte
|
||||
|
@ -124,8 +118,8 @@ data class JsonPacket(val content: JsonObject) : Packet() {
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
class BinaryPacket(val content: ByteArray) : Packet() {
|
||||
@Suppress("ArrayInDataClass")
|
||||
data class BinaryPacket(val content: ByteArray) : Packet() {
|
||||
override fun write(out: DataOutputStream) {
|
||||
// Tag Byte
|
||||
out.writeByte(TYPE_BINARY.toInt())
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package org.hso.texturesyncclient.controller.net
|
||||
|
||||
import com.google.gson.JsonSyntaxException
|
||||
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.")
|
||||
|
||||
sealed class PacketException(msg: String) : ConnectionException(msg)
|
||||
class PacketTooLongException : PacketException("The Package is too long.")
|
||||
class PacketInvalidType : PacketException("The Package has an Invalid Type.")
|
||||
class PacketInvalidData : PacketException("The Package has an Invalid Data. (e.g. Invalid Json.)")
|
|
@ -9,5 +9,14 @@ fun main() {
|
|||
|
||||
con.ping()
|
||||
|
||||
|
||||
println("Query:")
|
||||
for (tex in con.query(
|
||||
arrayOf("Red", "Food")
|
||||
)) {
|
||||
println(tex.toString())
|
||||
}
|
||||
println()
|
||||
|
||||
con.close()
|
||||
}
|
Loading…
Reference in New Issue