TextureSync/client/src/main/kotlin/org/hso/texturesyncclient/controller/net/Connection.kt

231 lines
7.1 KiB
Kotlin

package org.hso.texturesyncclient.controller.net
import com.google.gson.*
import javafx.scene.image.Image
import org.hso.texturesyncclient.model.Sha256
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 {
var socket: Socket? = null
var output: DataOutputStream? = null
var input: DataInputStream? = null
@Throws(IOException::class)
@Synchronized
private fun getStreams(): Pair<DataInputStream, DataOutputStream> {
val i: DataInputStream
val o: DataOutputStream
if (socket == null || !socket!!.isConnected) {
val sock = Socket(address, port)
i = DataInputStream(BufferedInputStream(sock.getInputStream()))
o = DataOutputStream(BufferedOutputStream(sock.getOutputStream()))
input = i
output = o
socket = sock
} else {
i = input!!
o = output!!
}
return Pair(i, o)
}
@Throws(IOException::class, ConnectionException::class)
@Synchronized
fun ping() {
val io = getStreams()
val obj = JsonObject()
obj.add("ping", JsonObject())
JsonPackage(obj).write(io.second)
when (val pkg = Package.read(io.first)) {
is JsonPackage -> return
is BinaryPackage -> throw ConnectionUnexpecedPacketException()
is ErrorPackage -> throw ConnectionErrorException(pkg)
else -> throw RuntimeException("Unreachable")
}
}
@Throws(IOException::class, ConnectionException::class)
@Synchronized
fun query(query : Array<String>) : Array<Texture> {
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
}())
JsonPackage(obj).write(io.second)
when (val pkg = Package.read(io.first)) {
is JsonPackage -> {
try {
return Gson().fromJson<Array<InternalTexture>>(pkg.content, Array<InternalTexture>::class.java).map {
tex -> tex.toTexture()
}.toTypedArray()
} 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 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, ConnectionException::class)
@Synchronized
fun getTextureFile(hash : Sha256) : ByteArray {
val io = getStreams()
val obj = JsonObject()
obj.add("get_texture_file", {
val inner = JsonObject()
inner.addProperty("texture_hash", hash.toString())
inner
}())
JsonPackage(obj).write(io.second)
when (val pkg = Package.read(io.first)) {
is JsonPackage -> throw ConnectionUnexpecedPacketException()
is BinaryPackage -> return pkg.content
is ErrorPackage -> throw ConnectionErrorException(pkg)
else -> throw RuntimeException("Unreachable")
}
}
@Throws(IOException::class, ConnectionException::class)
@Synchronized
fun getTexturePreview(hash : Sha256) : Image {
val io = getStreams()
val obj = JsonObject()
obj.add("get_texture_file", {
val inner = JsonObject()
inner.addProperty("texture_hash", hash.toString())
inner.addProperty("desired_format", "jpeg")
inner
}())
JsonPackage(obj).write(io.second)
when (val pkg = Package.read(io.first)) {
is JsonPackage -> throw ConnectionUnexpecedPacketException()
is BinaryPackage -> {
return Image(ByteArrayInputStream(pkg.content))
}
is ErrorPackage -> throw ConnectionErrorException(pkg)
else -> throw RuntimeException("Unreachable")
}
}
@Throws(IOException::class)
@Synchronized
override fun close() {
if (output != null) {
output!!.close()
}
if (input != null) {
input!!.close()
}
if (socket != null) {
socket!!.close()
}
}
}