From 7f8277415ef4842d35ecef3d6a199400f3c89806 Mon Sep 17 00:00:00 2001 From: CodeSteak Date: Mon, 3 Jun 2019 15:18:42 +0200 Subject: [PATCH] implement 'get Texture' Netop --- .../controller/net/Connection.kt | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) 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 3511810..02571c7 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 @@ -5,6 +5,7 @@ import org.hso.texturesyncclient.model.Texture import java.io.* import java.lang.RuntimeException import java.net.* +import java.util.* @Suppress("MemberVisibilityCanBePrivate") class Connection(val address: InetAddress, val port: Int = 10796) : Closeable { @@ -89,6 +90,80 @@ class Connection(val address: InetAddress, val port: Int = 10796) : Closeable { } } + @Throws(IOException::class, ConnectionException::class) + @Synchronized + fun getTextureById(id : UUID) : Optional { + val io = getStreams() + + val obj = JsonObject() + obj.add("get_texture", { + val inner = JsonObject() + inner.addProperty("id", id.toString()) + inner + }()) + + JsonPackage(obj).write(io.second) + + when (val pkg = Package.read(io.first)) { + is JsonPackage -> { + return if (pkg.content.isJsonNull) { + Optional.empty() + } else { + try { + Optional.of( + Gson() + .fromJson(pkg.content, InternalTexture::class.java) + .toTexture() + ) + } catch (e : JsonSyntaxException ){ + throw ConnectionInvalidJsonException() + } + } + } + is BinaryPackage -> throw ConnectionUnexpecedPacketException() + is ErrorPackage -> throw ConnectionErrorException(pkg) + else -> throw RuntimeException("Unreachable") + } + + } + + @Throws(IOException::class, ConnectionException::class) + @Synchronized + fun getTextureByName(name : String) : Optional { + val io = getStreams() + + val obj = JsonObject() + obj.add("get_texture", { + val inner = JsonObject() + inner.addProperty("name", name) + inner + }()) + + JsonPackage(obj).write(io.second) + + when (val pkg = Package.read(io.first)) { + is JsonPackage -> { + return if (pkg.content.isJsonNull) { + Optional.empty() + } else { + try { + Optional.of( + Gson() + .fromJson(pkg.content, InternalTexture::class.java) + .toTexture() + ) + } catch (e : JsonSyntaxException ){ + throw ConnectionInvalidJsonException() + } + } + } + is BinaryPackage -> throw ConnectionUnexpecedPacketException() + is ErrorPackage -> throw ConnectionErrorException(pkg) + else -> throw RuntimeException("Unreachable") + } + + } + @Throws(IOException::class) @Synchronized override fun close() {