Skip to content
Snippets Groups Projects
Verified Commit 7f3268d2 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski Committed by Janne Mareike Koschinski
Browse files

Split libquassel out from quasseldroid

parents
Branches
Tags
No related merge requests found
Showing
with 519 additions and 0 deletions
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers
import de.justjanne.libquassel.protocol.variant.QtType
import de.justjanne.libquassel.protocol.variant.QuasselType
sealed class NoSerializerForTypeException : Exception() {
data class Qt(
private val type: Int,
private val javaType: Class<*>? = null
) : NoSerializerForTypeException() {
constructor(
type: QtType,
javaType: Class<*>? = null
) : this(type.id, javaType)
override fun toString(): String {
return "NoSerializerForTypeException.Qt(type=$type, javaType=$javaType)"
}
}
data class Quassel(
private val type: Int,
private val typename: String?,
private val javaType: Class<*>? = null
) : NoSerializerForTypeException() {
constructor(
type: QtType,
typename: String?,
javaType: Class<*>? = null
) : this(type.id, typename, javaType)
constructor(
type: QuasselType,
javaType: Class<*>? = null
) : this(type.qtType, type.typeName, javaType)
override fun toString(): String {
return "NoSerializerForTypeException.Quassel(type=$type, typename=$typename, javaType=$javaType)"
}
}
data class Handshake(
private val type: String,
private val javaType: Class<*>? = null
) : NoSerializerForTypeException() {
override fun toString(): String {
return "NoSerializerForTypeException.Handshake(type='$type', javaType=$javaType)"
}
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers
import de.justjanne.libquassel.protocol.variant.QtType
interface QtSerializer<T> : Serializer<T> {
val qtType: QtType
val javaType: Class<out T>
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers
import de.justjanne.libquassel.protocol.serializers.qt.BoolSerializer
import de.justjanne.libquassel.protocol.serializers.qt.ByteBufferSerializer
import de.justjanne.libquassel.protocol.serializers.qt.ByteSerializer
import de.justjanne.libquassel.protocol.serializers.qt.DateSerializer
import de.justjanne.libquassel.protocol.serializers.qt.DateTimeSerializer
import de.justjanne.libquassel.protocol.serializers.qt.DoubleSerializer
import de.justjanne.libquassel.protocol.serializers.qt.FloatSerializer
import de.justjanne.libquassel.protocol.serializers.qt.IntSerializer
import de.justjanne.libquassel.protocol.serializers.qt.LongSerializer
import de.justjanne.libquassel.protocol.serializers.qt.QCharSerializer
import de.justjanne.libquassel.protocol.serializers.qt.QStringListSerializer
import de.justjanne.libquassel.protocol.serializers.qt.QVariantListSerializer
import de.justjanne.libquassel.protocol.serializers.qt.QVariantMapSerializer
import de.justjanne.libquassel.protocol.serializers.qt.QVariantSerializer
import de.justjanne.libquassel.protocol.serializers.qt.ShortSerializer
import de.justjanne.libquassel.protocol.serializers.qt.StringSerializerUtf16
import de.justjanne.libquassel.protocol.serializers.qt.TimeSerializer
import de.justjanne.libquassel.protocol.serializers.qt.UByteSerializer
import de.justjanne.libquassel.protocol.serializers.qt.UIntSerializer
import de.justjanne.libquassel.protocol.serializers.qt.ULongSerializer
import de.justjanne.libquassel.protocol.serializers.qt.UShortSerializer
import de.justjanne.libquassel.protocol.serializers.qt.VoidSerializer
import de.justjanne.libquassel.protocol.variant.QtType
object QtSerializers {
private val serializers = setOf<QtSerializer<*>>(
VoidSerializer,
BoolSerializer,
ByteSerializer,
UByteSerializer,
ShortSerializer,
UShortSerializer,
IntSerializer,
UIntSerializer,
LongSerializer,
ULongSerializer,
FloatSerializer,
DoubleSerializer,
QCharSerializer,
StringSerializerUtf16,
QStringListSerializer,
ByteBufferSerializer,
DateSerializer,
TimeSerializer,
DateTimeSerializer,
QVariantSerializer,
QVariantListSerializer,
QVariantMapSerializer,
).associateBy(QtSerializer<*>::qtType)
operator fun get(type: QtType) = serializers[type]
@Suppress("UNCHECKED_CAST")
inline fun <reified T> find(type: QtType): QtSerializer<T> {
val serializer = get(type)
?: throw NoSerializerForTypeException.Qt(type, T::class.java)
if (serializer.javaType == T::class.java) {
return serializer as QtSerializer<T>
} else {
throw NoSerializerForTypeException.Qt(type, T::class.java)
}
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers
import de.justjanne.libquassel.protocol.variant.QtType
import de.justjanne.libquassel.protocol.variant.QuasselType
interface QuasselSerializer<T> : QtSerializer<T> {
override val qtType: QtType get() = quasselType.qtType
val quasselType: QuasselType
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers
import de.justjanne.libquassel.protocol.serializers.quassel.BufferIdSerializer
import de.justjanne.libquassel.protocol.serializers.quassel.BufferInfoSerializer
import de.justjanne.libquassel.protocol.serializers.quassel.DccIpDetectionModeSerializer
import de.justjanne.libquassel.protocol.serializers.quassel.DccPortSelectionModeSerializer
import de.justjanne.libquassel.protocol.serializers.quassel.IdentityIdSerializer
import de.justjanne.libquassel.protocol.serializers.quassel.MessageSerializer
import de.justjanne.libquassel.protocol.serializers.quassel.MsgIdSerializer
import de.justjanne.libquassel.protocol.serializers.quassel.NetworkIdSerializer
import de.justjanne.libquassel.protocol.serializers.quassel.PeerPtrSerializer
import de.justjanne.libquassel.protocol.serializers.quassel.QHostAddressSerializer
import de.justjanne.libquassel.protocol.variant.QuasselType
object QuasselSerializers {
private val serializers = listOf<QuasselSerializer<*>>(
BufferIdSerializer,
BufferInfoSerializer,
DccIpDetectionModeSerializer,
DccPortSelectionModeSerializer,
// IrcUserSerializer,
// IrcChannelSerializer,
// IdentitySerializer,
IdentityIdSerializer,
MessageSerializer,
MsgIdSerializer,
NetworkIdSerializer,
// NetworkInfoSerializer,
// NetworkServerSerializer,
QHostAddressSerializer,
PeerPtrSerializer,
).associateBy(QuasselSerializer<*>::quasselType)
operator fun get(type: QuasselType) = serializers[type]
@Suppress("UNCHECKED_CAST")
inline fun <reified T> find(type: QuasselType): QuasselSerializer<T> {
val serializer = get(type)
?: throw NoSerializerForTypeException.Quassel(type, T::class.java)
if (serializer.javaType == T::class.java) {
return serializer as QuasselSerializer<T>
} else {
throw NoSerializerForTypeException.Quassel(type, T::class.java)
}
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers
import de.justjanne.libquassel.protocol.features.FeatureSet
import de.justjanne.libquassel.protocol.io.ChainedByteBuffer
import java.nio.ByteBuffer
interface Serializer<T> {
fun serialize(buffer: ChainedByteBuffer, data: T, featureSet: FeatureSet)
fun deserialize(buffer: ByteBuffer, featureSet: FeatureSet): T
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers.qt
import de.justjanne.libquassel.protocol.features.FeatureSet
import de.justjanne.libquassel.protocol.io.ChainedByteBuffer
import de.justjanne.libquassel.protocol.serializers.QtSerializer
import de.justjanne.libquassel.protocol.variant.QtType
import java.nio.ByteBuffer
object BoolSerializer : QtSerializer<Boolean> {
override val qtType: QtType = QtType.Bool
override val javaType: Class<Boolean> = Boolean::class.javaObjectType
override fun serialize(buffer: ChainedByteBuffer, data: Boolean, featureSet: FeatureSet) {
buffer.put(
if (data) 0x01.toByte()
else 0x00.toByte()
)
}
override fun deserialize(buffer: ByteBuffer, featureSet: FeatureSet): Boolean {
return buffer.get() != 0x00.toByte()
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers.qt
import de.justjanne.libquassel.protocol.features.FeatureSet
import de.justjanne.libquassel.protocol.io.ChainedByteBuffer
import de.justjanne.libquassel.protocol.io.copyData
import de.justjanne.libquassel.protocol.serializers.QtSerializer
import de.justjanne.libquassel.protocol.variant.QtType
import java.nio.ByteBuffer
object ByteBufferSerializer : QtSerializer<ByteBuffer?> {
override val qtType: QtType = QtType.QByteArray
override val javaType: Class<out ByteBuffer?> = ByteBuffer::class.java
override fun serialize(buffer: ChainedByteBuffer, data: ByteBuffer?, featureSet: FeatureSet) {
IntSerializer.serialize(buffer, data?.remaining() ?: -1, featureSet)
if (data != null) {
buffer.put(data)
}
}
override fun deserialize(buffer: ByteBuffer, featureSet: FeatureSet): ByteBuffer? {
val length = IntSerializer.deserialize(buffer, featureSet)
if (length < 0) {
return null
}
val result = copyData(buffer, length)
result.limit(length)
return result
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers.qt
import de.justjanne.libquassel.protocol.features.FeatureSet
import de.justjanne.libquassel.protocol.io.ChainedByteBuffer
import de.justjanne.libquassel.protocol.serializers.QtSerializer
import de.justjanne.libquassel.protocol.variant.QtType
import java.nio.ByteBuffer
object ByteSerializer : QtSerializer<Byte> {
override val qtType: QtType = QtType.Char
override val javaType: Class<Byte> = Byte::class.javaObjectType
override fun serialize(buffer: ChainedByteBuffer, data: Byte, featureSet: FeatureSet) {
buffer.put(data)
}
override fun deserialize(buffer: ByteBuffer, featureSet: FeatureSet): Byte {
return buffer.get()
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers.qt
import de.justjanne.libquassel.protocol.features.FeatureSet
import de.justjanne.libquassel.protocol.io.ChainedByteBuffer
import de.justjanne.libquassel.protocol.serializers.QtSerializer
import de.justjanne.libquassel.protocol.variant.QtType
import org.threeten.bp.LocalDate
import org.threeten.bp.temporal.JulianFields
import java.nio.ByteBuffer
object DateSerializer : QtSerializer<LocalDate> {
override val qtType: QtType = QtType.QDate
override val javaType: Class<out LocalDate> = LocalDate::class.java
override fun serialize(buffer: ChainedByteBuffer, data: LocalDate, featureSet: FeatureSet) {
val julianDay = data.getLong(JulianFields.JULIAN_DAY).toInt()
IntSerializer.serialize(buffer, julianDay, featureSet)
}
override fun deserialize(buffer: ByteBuffer, featureSet: FeatureSet): LocalDate {
val julianDay = IntSerializer.deserialize(buffer, featureSet).toLong()
return LocalDate.ofEpochDay(0).with(JulianFields.JULIAN_DAY, julianDay)
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.justjanne.libquassel.protocol.serializers.qt
import de.justjanne.libquassel.protocol.features.FeatureSet
import de.justjanne.libquassel.protocol.io.ChainedByteBuffer
import de.justjanne.libquassel.protocol.serializers.QtSerializer
import de.justjanne.libquassel.protocol.variant.QtType
import java.nio.ByteBuffer
object DoubleSerializer : QtSerializer<Double> {
override val qtType: QtType = QtType.Double
override val javaType: Class<Double> = Double::class.javaObjectType
override fun serialize(buffer: ChainedByteBuffer, data: Double, featureSet: FeatureSet) {
buffer.putDouble(data)
}
override fun deserialize(buffer: ByteBuffer, featureSet: FeatureSet): Double {
return buffer.getDouble()
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment