implement 'get Texture' Netop

This commit is contained in:
CodeSteak 2019-06-03 15:18:42 +02:00
parent badd50de38
commit 7f8277415e
1 changed files with 75 additions and 0 deletions

View File

@ -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<Texture> {
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<InternalTexture>(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<Texture> {
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<InternalTexture>(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() {