restructure & rename all things in Net
This commit is contained in:
		@ -6,21 +6,21 @@ import java.io.*
 | 
			
		||||
import java.lang.RuntimeException
 | 
			
		||||
import java.net.*
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@Suppress("MemberVisibilityCanBePrivate")
 | 
			
		||||
class Connection(val addr: InetAddress, val port: Int = 10796) : Closeable {
 | 
			
		||||
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(addr, port)
 | 
			
		||||
            val sock = Socket(address, port)
 | 
			
		||||
            i = DataInputStream(BufferedInputStream(sock.getInputStream()))
 | 
			
		||||
            o = DataOutputStream(BufferedOutputStream(sock.getOutputStream()))
 | 
			
		||||
 | 
			
		||||
@ -35,31 +35,32 @@ class Connection(val addr: InetAddress, val port: Int = 10796) : Closeable {
 | 
			
		||||
        return Pair(i, o)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Throws(IOException::class)
 | 
			
		||||
    @Throws(IOException::class, ConnectionException::class)
 | 
			
		||||
    @Synchronized
 | 
			
		||||
    fun ping() {
 | 
			
		||||
        val io = getStreams()
 | 
			
		||||
 | 
			
		||||
        val obj = JsonObject()
 | 
			
		||||
        obj.add("ping", JsonObject())
 | 
			
		||||
 | 
			
		||||
        JsonPacket(obj).write(io.second)
 | 
			
		||||
        JsonPackage(obj).write(io.second)
 | 
			
		||||
 | 
			
		||||
        val pkg = Packet.read(io.first)
 | 
			
		||||
 | 
			
		||||
        when (pkg) {
 | 
			
		||||
            is JsonPacket -> return
 | 
			
		||||
            is BinaryPacket -> throw ConnectionUnexpectedPacket()
 | 
			
		||||
            is ErrorPacket -> throw ConnectionErrorException(pkg)
 | 
			
		||||
        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();
 | 
			
		||||
            val inner = JsonObject()
 | 
			
		||||
            inner.add("query", {
 | 
			
		||||
                val array = JsonArray()
 | 
			
		||||
                for(queryString in query) {
 | 
			
		||||
@ -70,12 +71,10 @@ class Connection(val addr: InetAddress, val port: Int = 10796) : Closeable {
 | 
			
		||||
            inner
 | 
			
		||||
        }())
 | 
			
		||||
 | 
			
		||||
        JsonPacket(obj).write(io.second)
 | 
			
		||||
        JsonPackage(obj).write(io.second)
 | 
			
		||||
 | 
			
		||||
        val pkg = Packet.read(io.first)
 | 
			
		||||
 | 
			
		||||
        when (pkg) {
 | 
			
		||||
            is JsonPacket -> {
 | 
			
		||||
        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()
 | 
			
		||||
@ -84,13 +83,14 @@ class Connection(val addr: InetAddress, val port: Int = 10796) : Closeable {
 | 
			
		||||
                    throw ConnectionInvalidJsonException()
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
            is BinaryPacket -> throw ConnectionUnexpectedPacket()
 | 
			
		||||
            is ErrorPacket -> throw ConnectionErrorException(pkg)
 | 
			
		||||
            is BinaryPackage -> throw ConnectionUnexpecedPacketException()
 | 
			
		||||
            is ErrorPackage -> throw ConnectionErrorException(pkg)
 | 
			
		||||
            else -> throw RuntimeException("Unreachable")
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Throws(IOException::class)
 | 
			
		||||
    @Synchronized
 | 
			
		||||
    override fun close() {
 | 
			
		||||
        if (output != null) {
 | 
			
		||||
            output!!.close()
 | 
			
		||||
 | 
			
		||||
@ -1,28 +1,14 @@
 | 
			
		||||
package org.hso.texturesyncclient.controller.net
 | 
			
		||||
 | 
			
		||||
// These types will be converted to the model.DataModel
 | 
			
		||||
 | 
			
		||||
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(
 | 
			
		||||
internal data class InternalTexture(
 | 
			
		||||
    val id: String,
 | 
			
		||||
    val name: String,
 | 
			
		||||
    val tags: Array<String>,
 | 
			
		||||
@ -31,6 +17,7 @@ data class InternalTexture(
 | 
			
		||||
    val added_on: Array<Int>,
 | 
			
		||||
    val texture_hash: String
 | 
			
		||||
) {
 | 
			
		||||
 | 
			
		||||
    constructor(tex: Texture) : this(
 | 
			
		||||
        id = tex.id.toString(),
 | 
			
		||||
        name = tex.name,
 | 
			
		||||
@ -69,4 +56,18 @@ data class InternalTexture(
 | 
			
		||||
            throw ConnectionInvalidJsonException()
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
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()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -7,7 +7,7 @@ import java.io.DataInputStream
 | 
			
		||||
import java.io.DataOutputStream
 | 
			
		||||
import java.io.IOException
 | 
			
		||||
 | 
			
		||||
abstract class Packet {
 | 
			
		||||
internal abstract class Package {
 | 
			
		||||
 | 
			
		||||
    @Throws(IOException::class)
 | 
			
		||||
    abstract fun write(out: DataOutputStream)
 | 
			
		||||
@ -24,7 +24,7 @@ abstract class Packet {
 | 
			
		||||
        protected const val TYPE_BINARY_MAX_PAYLOAD: Int = 512 * 1024 * 1024
 | 
			
		||||
 | 
			
		||||
        @Throws(PacketException::class, IOException::class)
 | 
			
		||||
        fun read(input: DataInputStream): Packet {
 | 
			
		||||
        fun read(input: DataInputStream): Package {
 | 
			
		||||
            // Type Byte
 | 
			
		||||
            val type = input.readByte()
 | 
			
		||||
 | 
			
		||||
@ -64,12 +64,12 @@ abstract class Packet {
 | 
			
		||||
                        val code = results[0].toIntOrNull()
 | 
			
		||||
 | 
			
		||||
                        if (code == null) {
 | 
			
		||||
                            ErrorPacket(0, msg)
 | 
			
		||||
                            ErrorPackage(0, msg)
 | 
			
		||||
                        } else {
 | 
			
		||||
                            ErrorPacket(code, results[1])
 | 
			
		||||
                            ErrorPackage(code, results[1])
 | 
			
		||||
                        }
 | 
			
		||||
                    } else {
 | 
			
		||||
                        ErrorPacket(0, msg)
 | 
			
		||||
                        ErrorPackage(0, msg)
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
@ -77,14 +77,14 @@ abstract class Packet {
 | 
			
		||||
                    try {
 | 
			
		||||
                        val obj = JsonParser().parse(String(payload))
 | 
			
		||||
 | 
			
		||||
                        return JsonPacket(obj)
 | 
			
		||||
                        return JsonPackage(obj)
 | 
			
		||||
                    } catch (e: JsonParseException) {
 | 
			
		||||
                        throw PacketInvalidData()
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                TYPE_BINARY -> {
 | 
			
		||||
                    return BinaryPacket(payload)
 | 
			
		||||
                    return BinaryPackage(payload)
 | 
			
		||||
                }
 | 
			
		||||
 | 
			
		||||
                else -> {
 | 
			
		||||
@ -97,7 +97,7 @@ abstract class Packet {
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
data class JsonPacket(val content: JsonElement) : Packet() {
 | 
			
		||||
internal data class JsonPackage(val content: JsonElement) : Package() {
 | 
			
		||||
    override fun write(out: DataOutputStream) {
 | 
			
		||||
        val payload = content.toString().toByteArray()
 | 
			
		||||
        // Tag Byte
 | 
			
		||||
@ -119,7 +119,7 @@ data class JsonPacket(val content: JsonElement) : Packet() {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@Suppress("ArrayInDataClass")
 | 
			
		||||
data class BinaryPacket(val content: ByteArray) : Packet() {
 | 
			
		||||
internal data class BinaryPackage(val content: ByteArray) : Package() {
 | 
			
		||||
    override fun write(out: DataOutputStream) {
 | 
			
		||||
        // Tag Byte
 | 
			
		||||
        out.writeByte(TYPE_BINARY.toInt())
 | 
			
		||||
@ -139,7 +139,7 @@ data class BinaryPacket(val content: ByteArray) : Packet() {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
data class ErrorPacket(val code: Int, val message: String) : Packet() {
 | 
			
		||||
internal data class ErrorPackage(val code: Int, val message: String) : Package() {
 | 
			
		||||
    override fun write(out: DataOutputStream) {
 | 
			
		||||
        val payload = "$code $message".toByteArray()
 | 
			
		||||
        // Tag Byte
 | 
			
		||||
@ -1,18 +1,18 @@
 | 
			
		||||
@file:Suppress("MemberVisibilityCanBePrivate")
 | 
			
		||||
 | 
			
		||||
package org.hso.texturesyncclient.controller.net
 | 
			
		||||
 | 
			
		||||
import com.google.gson.JsonSyntaxException
 | 
			
		||||
import java.lang.Exception
 | 
			
		||||
 | 
			
		||||
sealed class ConnectionException(override val message : String) : Exception(message)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
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 ConnectionErrorException(val errorCode : Int, val errorMessage : String) : ConnectionException("${errorCode} ${errorMessage}") {
 | 
			
		||||
    internal  constructor(err : ErrorPackage) : this(err.code, err.message)
 | 
			
		||||
}
 | 
			
		||||
class ConnectionUnexpecedPacketException  : 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.")
 | 
			
		||||
class PacketInvalidType : PacketException("The Package has an Invalid Type.")
 | 
			
		||||
 | 
			
		||||
@ -2,8 +2,7 @@ package org.hso.texturesyncclient.model
 | 
			
		||||
 | 
			
		||||
import java.util.*
 | 
			
		||||
 | 
			
		||||
class DataModel {
 | 
			
		||||
}
 | 
			
		||||
class DataModel
 | 
			
		||||
 | 
			
		||||
enum class TextureFormat {
 | 
			
		||||
    PNG, JPEG,
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user