Skip to content
Snippets Groups Projects
Commit 921aeacb authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Moved UI code around, removed unnecessary object allocation/deallocation from connection

parent 699efd93
No related branches found
No related tags found
No related merge requests found
Showing
with 278 additions and 98 deletions
......@@ -10,9 +10,18 @@ import de.kuschku.libquassel.objects.types.CoreSetupAck;
import de.kuschku.libquassel.primitives.types.QVariant;
public class CoreSetupAckSerializer implements ObjectSerializer<CoreSetupAck> {
private static final CoreSetupAckSerializer serializer = new CoreSetupAckSerializer();
private CoreSetupAckSerializer() {
}
public static CoreSetupAckSerializer get() {
return serializer;
}
@Override
public QVariant<Map<String, QVariant>> toVariantMap(final CoreSetupAck data) {
return new QVariant<Map<String, QVariant>>(new HashMap<String, QVariant>());
return new QVariant<>(new HashMap<>());
}
@Override
......
......@@ -10,10 +10,19 @@ import de.kuschku.libquassel.objects.types.CoreSetupData;
import de.kuschku.libquassel.primitives.types.QVariant;
public class CoreSetupDataSerializer implements ObjectSerializer<CoreSetupData> {
private static final CoreSetupDataSerializer serializer = new CoreSetupDataSerializer();
private CoreSetupDataSerializer() {
}
public static CoreSetupDataSerializer get() {
return serializer;
}
@Override
public QVariant<Map<String, QVariant>> toVariantMap(final CoreSetupData data) {
final QVariant<Map<String, QVariant>> map = new QVariant<Map<String, QVariant>>(new HashMap<String, QVariant>());
map.data.put("SetupData", new SetupDataInitializer().toVariantMap(data.SetupData));
final QVariant<Map<String, QVariant>> map = new QVariant<>(new HashMap<>());
map.data.put("SetupData", SetupDataInitializer.get().toVariantMap(data.SetupData));
return map;
}
......@@ -25,7 +34,7 @@ public class CoreSetupDataSerializer implements ObjectSerializer<CoreSetupData>
@Override
public CoreSetupData fromLegacy(Map<String, QVariant> map) {
return new CoreSetupData(
new SetupDataInitializer().fromLegacy((Map<String, QVariant>) map.get("SetupData").data)
SetupDataInitializer.get().fromLegacy((Map<String, QVariant>) map.get("SetupData").data)
);
}
......
......@@ -10,9 +10,18 @@ import de.kuschku.libquassel.objects.types.CoreSetupReject;
import de.kuschku.libquassel.primitives.types.QVariant;
public class CoreSetupRejectSerializer implements ObjectSerializer<CoreSetupReject> {
private static final CoreSetupRejectSerializer serializer = new CoreSetupRejectSerializer();
private CoreSetupRejectSerializer() {
}
public static CoreSetupRejectSerializer get() {
return serializer;
}
@Override
public QVariant<Map<String, QVariant>> toVariantMap(final CoreSetupReject data) {
final QVariant<Map<String, QVariant>> map = new QVariant<Map<String, QVariant>>(new HashMap<String, QVariant>());
final QVariant<Map<String, QVariant>> map = new QVariant<>(new HashMap<>());
map.data.put("Error", new QVariant<>(data.Error));
return map;
}
......
......@@ -10,9 +10,18 @@ import de.kuschku.libquassel.objects.types.NetworkServer;
import de.kuschku.libquassel.primitives.types.QVariant;
public class NetworkServerSerializer implements ObjectSerializer<NetworkServer> {
private static final NetworkServerSerializer serializer = new NetworkServerSerializer();
private NetworkServerSerializer() {
}
public static NetworkServerSerializer get() {
return serializer;
}
@Override
public QVariant<Map<String, QVariant>> toVariantMap(NetworkServer data) {
final QVariant<Map<String, QVariant>> map = new QVariant<Map<String, QVariant>>(new HashMap<String, QVariant>());
final QVariant<Map<String, QVariant>> map = new QVariant<>(new HashMap<>());
map.data.put("UseSSL", new QVariant<>(data.UseSSL));
map.data.put("sslVersion", new QVariant<>(data.sslVersion));
map.data.put("Host", new QVariant<>(data.Host));
......
......@@ -10,9 +10,18 @@ import de.kuschku.libquassel.objects.types.SessionInit;
import de.kuschku.libquassel.primitives.types.QVariant;
public class SessionInitSerializer implements ObjectSerializer<SessionInit> {
private static final SessionInitSerializer serializer = new SessionInitSerializer();
private SessionInitSerializer() {
}
public static SessionInitSerializer get() {
return serializer;
}
@Override
public QVariant<Map<String, QVariant>> toVariantMap(final SessionInit data) {
final QVariant<Map<String, QVariant>> map = new QVariant<Map<String, QVariant>>(new HashMap<String, QVariant>());
final QVariant<Map<String, QVariant>> map = new QVariant<>(new HashMap<>());
map.data.put("SessionState", new SessionStateSerializer().toVariantMap(data.SessionState));
return map;
}
......
......@@ -10,9 +10,18 @@ import de.kuschku.libquassel.objects.types.SetupData;
import de.kuschku.libquassel.primitives.types.QVariant;
public class SetupDataInitializer implements ObjectSerializer<SetupData> {
private static final SetupDataInitializer serializer = new SetupDataInitializer();
private SetupDataInitializer() {
}
public static SetupDataInitializer get() {
return serializer;
}
@Override
public QVariant<Map<String, QVariant>> toVariantMap(final SetupData data) {
final QVariant<Map<String, QVariant>> map = new QVariant<Map<String, QVariant>>(new HashMap<String, QVariant>());
final QVariant<Map<String, QVariant>> map = new QVariant<>(new HashMap<>());
map.data.put("AdminPasswd", new QVariant<>(data.AdminPasswd));
map.data.put("AdminUser", new QVariant<>(data.AdminUser));
map.data.put("Backend", new QVariant<>(data.Backend));
......
......@@ -11,9 +11,18 @@ import de.kuschku.libquassel.objects.types.StorageBackend;
import de.kuschku.libquassel.primitives.types.QVariant;
public class StorageBackendSerializer implements ObjectSerializer<StorageBackend> {
private static final StorageBackendSerializer serializer = new StorageBackendSerializer();
private StorageBackendSerializer() {
}
public static StorageBackendSerializer get() {
return serializer;
}
@Override
public QVariant<Map<String, QVariant>> toVariantMap(final StorageBackend data) {
final QVariant<Map<String, QVariant>> map = new QVariant<Map<String, QVariant>>(new HashMap<String, QVariant>());
final QVariant<Map<String, QVariant>> map = new QVariant<>(new HashMap<>());
map.data.put("DisplayName", new QVariant<>(data.DisplayName));
map.data.put("SetupDefaults", new QVariant<>(data.SetupDefaults));
map.data.put("Description", new QVariant<>(data.Description));
......
......@@ -9,9 +9,18 @@ import de.kuschku.libquassel.functions.types.UnpackedFunction;
import de.kuschku.libquassel.primitives.types.QVariant;
public class StringObjectMapSerializer<T> implements ObjectSerializer<Map<String, T>> {
private static final StringObjectMapSerializer serializer = new StringObjectMapSerializer();
private StringObjectMapSerializer() {
}
public static <T> StringObjectMapSerializer<T> get() {
return serializer;
}
@Override
public QVariant<Map<String, QVariant>> toVariantMap(Map<String, T> data) {
final QVariant<Map<String, QVariant>> map = new QVariant<Map<String, QVariant>>(new HashMap<String, QVariant>());
final QVariant<Map<String, QVariant>> map = new QVariant<>(new HashMap<>());
for (Map.Entry<String, T> entry : data.entrySet()) {
map.data.put(entry.getKey(), new QVariant<>(entry.getValue()));
}
......
......@@ -43,35 +43,35 @@ public class QMetaTypeRegistry {
private static final Map<String, QMetaType> stringSerializerMap = new HashMap<>();
static {
addType(Void.class, QMetaType.Type.Void, new VoidSerializer());
addType(boolean.class, QMetaType.Type.Bool, new BoolSerializer());
addType(int.class, QMetaType.Type.Int, new IntSerializer());
addType(int.class, QMetaType.Type.UserType, "BufferId", new IntSerializer());
addType(int.class, QMetaType.Type.UserType, "NetworkId", new IntSerializer());
addType(int.class, QMetaType.Type.UserType, "IdentityId", new IntSerializer());
addType(int.class, QMetaType.Type.UserType, "MsgId", new IntSerializer());
addType(BufferInfo.class, QMetaType.Type.UserType, "BufferInfo", new BufferInfoSerializer());
addType(Message.class, QMetaType.Type.UserType, "Message", new MessageSerializer());
addType(Identity.class, QMetaType.Type.UserType, "Identity", new UserTypeSerializer<>(new IdentitySerializer()));
addType(NetworkServer.class, QMetaType.Type.UserType, "Network::Server", new UserTypeSerializer<>(new NetworkServerSerializer()));
addType(int.class, QMetaType.Type.UInt, new IntSerializer());
addType(short.class, QMetaType.Type.UShort, new ShortSerializer());
addType(Void.class, QMetaType.Type.Void, VoidSerializer.get());
addType(boolean.class, QMetaType.Type.Bool, BoolSerializer.get());
addType(int.class, QMetaType.Type.Int, IntSerializer.get());
addType(int.class, QMetaType.Type.UserType, "BufferId", IntSerializer.get());
addType(int.class, QMetaType.Type.UserType, "NetworkId", IntSerializer.get());
addType(int.class, QMetaType.Type.UserType, "IdentityId", IntSerializer.get());
addType(int.class, QMetaType.Type.UserType, "MsgId", IntSerializer.get());
addType(BufferInfo.class, QMetaType.Type.UserType, "BufferInfo", BufferInfoSerializer.get());
addType(Message.class, QMetaType.Type.UserType, "Message", MessageSerializer.get());
addType(Identity.class, QMetaType.Type.UserType, "Identity", new UserTypeSerializer<>(IdentitySerializer.get()));
addType(NetworkServer.class, QMetaType.Type.UserType, "Network::Server", new UserTypeSerializer<>(NetworkServerSerializer.get()));
addType(int.class, QMetaType.Type.UInt, IntSerializer.get());
addType(short.class, QMetaType.Type.UShort, ShortSerializer.get());
// TODO: Implement more custom quassel types
addType(DateTime.class, QMetaType.Type.QTime, new TimeSerializer());
addType(DateTime.class, QMetaType.Type.QTime, TimeSerializer.get());
addType(BigDecimal.class, QMetaType.Type.LongLong);
addType(BigDecimal.class, QMetaType.Type.ULongLong);
addType(double.class, QMetaType.Type.Double);
addType(char.class, QMetaType.Type.QChar, new CharSerializer());
addType(List.class, QMetaType.Type.QVariantList, new VariantListSerializer());
addType(Map.class, QMetaType.Type.QVariantMap, new VariantMapSerializer());
addType(List.class, QMetaType.Type.QStringList, new StringListSerializer());
addType(String.class, QMetaType.Type.QString, new StringSerializer());
addType(String.class, QMetaType.Type.QByteArray, new ByteArraySerializer());
addType(char.class, QMetaType.Type.QChar, CharSerializer.get());
addType(List.class, QMetaType.Type.QVariantList, VariantListSerializer.get());
addType(Map.class, QMetaType.Type.QVariantMap, VariantMapSerializer.get());
addType(List.class, QMetaType.Type.QStringList, StringListSerializer.get());
addType(String.class, QMetaType.Type.QString, StringSerializer.get());
addType(String.class, QMetaType.Type.QByteArray, ByteArraySerializer.get());
addType(void.class, QMetaType.Type.QBitArray);
addType(void.class, QMetaType.Type.QDate);
addType(DateTime.class, QMetaType.Type.QDateTime, new DateTimeSerializer());
addType(DateTime.class, QMetaType.Type.QDateTime, DateTimeSerializer.get());
addType(void.class, QMetaType.Type.QUrl);
addType(void.class, QMetaType.Type.QLocale);
addType(void.class, QMetaType.Type.QRect);
......@@ -113,15 +113,15 @@ public class QMetaTypeRegistry {
addType(void.class, QMetaType.Type.QQuaternion);
addType(void.class, QMetaType.Type.VoidStar, "void*");
addType(long.class, QMetaType.Type.Long, new LongSerializer());
addType(short.class, QMetaType.Type.Short, new ShortSerializer());
addType(byte.class, QMetaType.Type.Char, new ByteSerializer());
addType(long.class, QMetaType.Type.ULong, new LongSerializer());
addType(byte.class, QMetaType.Type.UChar, new ByteSerializer());
addType(long.class, QMetaType.Type.Long, LongSerializer.get());
addType(short.class, QMetaType.Type.Short, ShortSerializer.get());
addType(byte.class, QMetaType.Type.Char, ByteSerializer.get());
addType(long.class, QMetaType.Type.ULong, LongSerializer.get());
addType(byte.class, QMetaType.Type.UChar, ByteSerializer.get());
addType(void.class, QMetaType.Type.Float);
addType(void.class, QMetaType.Type.QObjectStar, "QObject*");
addType(void.class, QMetaType.Type.QWidgetStar, "QWidget*");
addType(QVariant.class, QMetaType.Type.QVariant, new VariantSerializer());
addType(QVariant.class, QMetaType.Type.QVariant, VariantSerializer.get());
}
// Disable Constructor
......@@ -158,11 +158,11 @@ public class QMetaTypeRegistry {
}
public static <T> T deserialize(final QMetaType.Type type, final ByteBuffer buffer) throws IOException {
return deserialize(((QMetaType<T>) typeSerializerMap.get(type)), buffer);
return deserialize((getMetaTypeByType(type)), buffer);
}
public static <T> T deserialize(final String type, final ByteBuffer buffer) throws IOException {
return deserialize(((QMetaType<T>) stringSerializerMap.get(type)), buffer);
return deserialize((getMetaTypeByString(type)), buffer);
}
public static <T> T deserialize(final QMetaType<T> type, final ByteBuffer buffer) throws IOException {
......@@ -191,42 +191,58 @@ public class QMetaTypeRegistry {
}
public static <T> QMetaType<T> getType(String typeName) {
return ((QMetaType<T>) stringSerializerMap.get(typeName));
return getMetaTypeByString(typeName);
}
private static <T> QMetaType<T> getMetaTypeByString(String typeName) {
QMetaType<T> result = stringSerializerMap.get(typeName);
if (result == null || result.serializer == null) {
throw new RuntimeException("Unknown type: " + typeName);
}
return result;
}
public static <T> QMetaType<T> getType(QMetaType.Type type) {
return ((QMetaType<T>) typeSerializerMap.get(type));
return getMetaTypeByType(type);
}
private static <T> QMetaType<T> getMetaTypeByType(QMetaType.Type type) {
QMetaType<T> result = typeSerializerMap.get(type);
if (result == null || result.serializer == null) {
throw new RuntimeException("Unknown type: " + type.toString());
}
return result;
}
public static <T> QMetaType<T> getTypeByObject(T type) {
if (type instanceof Void) return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.Void);
if (type instanceof Void) return getMetaTypeByType(QMetaType.Type.Void);
else if (type instanceof Boolean)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.Bool);
return getMetaTypeByType(QMetaType.Type.Bool);
else if (type instanceof Integer)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.Int);
return getMetaTypeByType(QMetaType.Type.Int);
else if (type instanceof Short)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.Short);
return getMetaTypeByType(QMetaType.Type.Short);
else if (type instanceof DateTime)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.QDateTime);
return getMetaTypeByType(QMetaType.Type.QDateTime);
else if (type instanceof Character)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.QChar);
return getMetaTypeByType(QMetaType.Type.QChar);
else if (type instanceof List) {
if (((List) type).size() > 0 && ((List) type).get(0) instanceof String)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.QStringList);
return getMetaTypeByType(QMetaType.Type.QStringList);
else if (((List) type).size() > 0 && ((List) type).get(0) instanceof QVariant)
return new QMetaType<>((Class) type.getClass(), QMetaType.Type.QVariantList, new VariantVariantListSerializer());
return new QMetaType<>((Class) type.getClass(), QMetaType.Type.QVariantList, (PrimitiveSerializer<T>) VariantVariantListSerializer.get());
else
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.QVariantList);
return getMetaTypeByType(QMetaType.Type.QVariantList);
} else if (type instanceof Map)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.QVariantMap);
return getMetaTypeByType(QMetaType.Type.QVariantMap);
else if (type instanceof String)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.QString);
return getMetaTypeByType(QMetaType.Type.QString);
else if (type instanceof Long)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.Long);
return getMetaTypeByType(QMetaType.Type.Long);
else if (type instanceof Byte)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.Char);
return getMetaTypeByType(QMetaType.Type.Char);
else if (type instanceof QVariant)
return (QMetaType<T>) typeSerializerMap.get(QMetaType.Type.QVariant);
return getMetaTypeByType(QMetaType.Type.QVariant);
else if (type instanceof Message) return stringSerializerMap.get("Message");
else if (type instanceof BufferInfo) return stringSerializerMap.get("BufferInfo");
else
......
......@@ -5,6 +5,12 @@ import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
public class BoolSerializer implements PrimitiveSerializer<Boolean> {
private static final BoolSerializer serializer = new BoolSerializer();
private BoolSerializer() {}
public static BoolSerializer get(){
return serializer;
}
@Override
public void serialize(final ByteChannel channel, final Boolean data) throws IOException {
final ByteBuffer buffer = ByteBuffer.allocate(1);
......
......@@ -7,23 +7,29 @@ import java.nio.channels.ByteChannel;
import de.kuschku.libquassel.primitives.types.BufferInfo;
public class BufferInfoSerializer implements PrimitiveSerializer<BufferInfo> {
private static final BufferInfoSerializer serializer = new BufferInfoSerializer();
private BufferInfoSerializer() {}
public static BufferInfoSerializer get(){
return serializer;
}
@Override
public void serialize(ByteChannel channel, BufferInfo data) throws IOException {
new IntSerializer().serialize(channel, data.id);
new IntSerializer().serialize(channel, data.networkId);
new ShortSerializer().serialize(channel, data.type.id);
new IntSerializer().serialize(channel, data.groupId);
new ByteArraySerializer().serialize(channel, data.name);
IntSerializer.get().serialize(channel, data.id);
IntSerializer.get().serialize(channel, data.networkId);
ShortSerializer.get().serialize(channel, data.type.id);
IntSerializer.get().serialize(channel, data.groupId);
ByteArraySerializer.get().serialize(channel, data.name);
}
@Override
public BufferInfo deserialize(final ByteBuffer buffer) throws IOException {
return new BufferInfo(
new IntSerializer().deserialize(buffer),
new IntSerializer().deserialize(buffer),
BufferInfo.Type.fromId(new ShortSerializer().deserialize(buffer)),
new IntSerializer().deserialize(buffer),
new ByteArraySerializer().deserialize(buffer)
IntSerializer.get().deserialize(buffer),
IntSerializer.get().deserialize(buffer),
BufferInfo.Type.fromId(ShortSerializer.get().deserialize(buffer)),
IntSerializer.get().deserialize(buffer),
ByteArraySerializer.get().deserialize(buffer)
);
}
}
package de.kuschku.libquassel.primitives.serializers;
import android.support.annotation.Nullable;
import android.util.Log;
import com.google.common.base.Charsets;
......@@ -9,23 +10,30 @@ import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
public class ByteArraySerializer implements PrimitiveSerializer<String> {
public final boolean trimLastByte;
private static final ByteArraySerializer serializerFalse = new ByteArraySerializer(false);
private static final ByteArraySerializer serializerTrue = new ByteArraySerializer(true);
public ByteArraySerializer() {
this(false);
public static ByteArraySerializer get() {
return get(false);
}
public static ByteArraySerializer get(boolean trimLastByte) {
if (trimLastByte) return serializerTrue;
else return serializerFalse;
}
public ByteArraySerializer(boolean trimLastByte) {
public final boolean trimLastByte;
private ByteArraySerializer(boolean trimLastByte) {
this.trimLastByte = trimLastByte;
}
@Override
public void serialize(final ByteChannel channel, final String data) throws IOException {
if (data == null) {
new IntSerializer().serialize(channel, 0xffffffff);
IntSerializer.get().serialize(channel, 0xffffffff);
} else {
final ByteBuffer contentBuffer = Charsets.ISO_8859_1.encode(data);
new IntSerializer().serialize(channel, contentBuffer.limit() + (trimLastByte ? 1 : 0));
IntSerializer.get().serialize(channel, contentBuffer.limit() + (trimLastByte ? 1 : 0));
channel.write(contentBuffer);
if (trimLastByte) channel.write(ByteBuffer.allocate(1));
}
......@@ -34,7 +42,7 @@ public class ByteArraySerializer implements PrimitiveSerializer<String> {
@Nullable
@Override
public String deserialize(final ByteBuffer buffer) throws IOException {
final int len = new IntSerializer().deserialize(buffer);
final int len = IntSerializer.get().deserialize(buffer);
if (len == 0xffffffff)
return null;
else if (len == 0)
......
......@@ -5,6 +5,15 @@ import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
public class ByteSerializer implements PrimitiveSerializer<Byte> {
private static final ByteSerializer serializer = new ByteSerializer();
private ByteSerializer() {
}
public static ByteSerializer get() {
return serializer;
}
@Override
public void serialize(final ByteChannel channel, final Byte data) throws IOException {
final ByteBuffer buffer = ByteBuffer.allocate(1);
......
......@@ -6,6 +6,15 @@ import java.nio.channels.ByteChannel;
import java.nio.charset.Charset;
public class CharSerializer implements PrimitiveSerializer<Character> {
private static final CharSerializer serializer = new CharSerializer();
private CharSerializer() {
}
public static CharSerializer get() {
return serializer;
}
@Override
public void serialize(final ByteChannel channel, final Character data) throws IOException {
final ByteBuffer buffer = Charset.forName("UTF-16BE").encode(String.valueOf(data.charValue()));
......
......@@ -9,6 +9,15 @@ import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
public class DateTimeSerializer implements PrimitiveSerializer<DateTime> {
private static final DateTimeSerializer serializer = new DateTimeSerializer();
private DateTimeSerializer() {
}
public static DateTimeSerializer get() {
return serializer;
}
@Override
public void serialize(final ByteChannel channel, final DateTime data) throws IOException {
final boolean isUTC;
......@@ -20,17 +29,17 @@ public class DateTimeSerializer implements PrimitiveSerializer<DateTime> {
throw new IllegalArgumentException("Serialization of timezones except for local and UTC is not supported");
new IntSerializer().serialize(channel, (int) DateTimeUtils.toJulianDayNumber(data.getMillis()));
new IntSerializer().serialize(channel, data.getMillisOfDay());
new BoolSerializer().serialize(channel, isUTC);
IntSerializer.get().serialize(channel, (int) DateTimeUtils.toJulianDayNumber(data.getMillis()));
IntSerializer.get().serialize(channel, data.getMillisOfDay());
BoolSerializer.get().serialize(channel, isUTC);
}
@Override
public DateTime deserialize(final ByteBuffer buffer) throws IOException {
final long julianDay = new IntSerializer().deserialize(buffer);
final int millisSinceMidnight = new IntSerializer().deserialize(buffer);
final long julianDay = IntSerializer.get().deserialize(buffer);
final int millisSinceMidnight = IntSerializer.get().deserialize(buffer);
final short zone = new ByteSerializer().deserialize(buffer);
final short zone = ByteSerializer.get().deserialize(buffer);
if (millisSinceMidnight == 0x73007300 && julianDay == 0x50006100 || millisSinceMidnight == -1 || julianDay == -1)
return new DateTime(0);
......
......@@ -5,6 +5,15 @@ import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
public class IntSerializer implements PrimitiveSerializer<Integer> {
private static final IntSerializer serializer = new IntSerializer();
private IntSerializer() {
}
public static IntSerializer get() {
return serializer;
}
@Override
public void serialize(final ByteChannel channel, final Integer data) throws IOException {
final ByteBuffer buffer = ByteBuffer.allocate(4);
......
......@@ -5,6 +5,15 @@ import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
public class LongSerializer implements PrimitiveSerializer<Long> {
private static final LongSerializer serializer = new LongSerializer();
private LongSerializer() {
}
public static LongSerializer get() {
return serializer;
}
@Override
public void serialize(final ByteChannel channel, final Long data) throws IOException {
final ByteBuffer buffer = ByteBuffer.allocate(8);
......
......@@ -9,27 +9,36 @@ import java.nio.channels.ByteChannel;
import de.kuschku.libquassel.message.Message;
public class MessageSerializer implements PrimitiveSerializer<Message> {
private static final MessageSerializer serializer = new MessageSerializer();
private MessageSerializer() {
}
public static MessageSerializer get() {
return serializer;
}
@Override
public void serialize(ByteChannel channel, Message data) throws IOException {
new IntSerializer().serialize(channel, data.messageId);
new IntSerializer().serialize(channel, (int) (data.time.getMillis() / 1000));
new IntSerializer().serialize(channel, data.type.value);
new ByteSerializer().serialize(channel, data.flags.flags);
new BufferInfoSerializer().serialize(channel, data.bufferInfo);
new ByteArraySerializer().serialize(channel, data.sender);
new ByteArraySerializer().serialize(channel, data.content);
IntSerializer.get().serialize(channel, data.messageId);
IntSerializer.get().serialize(channel, (int) (data.time.getMillis() / 1000));
IntSerializer.get().serialize(channel, data.type.value);
ByteSerializer.get().serialize(channel, data.flags.flags);
BufferInfoSerializer.get().serialize(channel, data.bufferInfo);
ByteArraySerializer.get().serialize(channel, data.sender);
ByteArraySerializer.get().serialize(channel, data.content);
}
@Override
public Message deserialize(final ByteBuffer buffer) throws IOException {
return new Message(
new IntSerializer().deserialize(buffer),
new DateTime(((long) new IntSerializer().deserialize(buffer)) * 1000),
Message.Type.fromId(new IntSerializer().deserialize(buffer)),
new Message.Flags(new ByteSerializer().deserialize(buffer)),
new BufferInfoSerializer().deserialize(buffer),
new ByteArraySerializer().deserialize(buffer),
new ByteArraySerializer().deserialize(buffer)
IntSerializer.get().deserialize(buffer),
new DateTime(((long) IntSerializer.get().deserialize(buffer)) * 1000),
Message.Type.fromId(IntSerializer.get().deserialize(buffer)),
new Message.Flags(ByteSerializer.get().deserialize(buffer)),
BufferInfoSerializer.get().deserialize(buffer),
ByteArraySerializer.get().deserialize(buffer),
ByteArraySerializer.get().deserialize(buffer)
);
}
}
......@@ -8,19 +8,28 @@ import de.kuschku.libquassel.ClientData;
import de.kuschku.libquassel.primitives.types.Protocol;
public class ProtocolSerializer implements PrimitiveSerializer<Protocol> {
private static final ProtocolSerializer serializer = new ProtocolSerializer();
private ProtocolSerializer() {
}
public static ProtocolSerializer get() {
return serializer;
}
@Override
public void serialize(ByteChannel channel, Protocol data) throws IOException {
new ByteSerializer().serialize(channel, data.protocolFlags.flags);
new ShortSerializer().serialize(channel, data.protocolData);
new ByteSerializer().serialize(channel, data.protocolVersion);
ByteSerializer.get().serialize(channel, data.protocolFlags.flags);
ShortSerializer.get().serialize(channel, data.protocolData);
ByteSerializer.get().serialize(channel, data.protocolVersion);
}
@Override
public Protocol deserialize(ByteBuffer buffer) throws IOException {
return new Protocol(
new ClientData.FeatureFlags(new ByteSerializer().deserialize(buffer)),
new ShortSerializer().deserialize(buffer),
new ByteSerializer().deserialize(buffer)
new ClientData.FeatureFlags(ByteSerializer.get().deserialize(buffer)),
ShortSerializer.get().deserialize(buffer),
ByteSerializer.get().deserialize(buffer)
);
}
}
......@@ -5,6 +5,15 @@ import java.nio.ByteBuffer;
import java.nio.channels.ByteChannel;
public class ShortSerializer implements PrimitiveSerializer<Short> {
private static final ShortSerializer serializer = new ShortSerializer();
private ShortSerializer() {
}
public static ShortSerializer get() {
return serializer;
}
@Override
public void serialize(final ByteChannel channel, final Short data) throws IOException {
final ByteBuffer buffer = ByteBuffer.allocate(2);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment