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