All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jukusoft.vertx.connection.clientserver.HandlerManagerImpl Maven / Gradle / Ivy

package com.jukusoft.vertx.connection.clientserver;

import com.carrotsearch.hppc.IntObjectHashMap;
import com.carrotsearch.hppc.IntObjectMap;
import com.jukusoft.vertx.serializer.SerializableObject;
import com.jukusoft.vertx.serializer.annotations.MessageType;
import com.jukusoft.vertx.serializer.utils.ByteUtils;

public class HandlerManagerImpl implements HandlerManager {

    protected IntObjectMap handlerMap = new IntObjectHashMap<>();

    /**
    * find handler for specific message
     *
     * @param cls class name of message
     *
     * @return handler or null, if no handler is registered
    */
    @Override
    public  MessageHandler findHandler (Class cls) {
        //get type
        MessageType type = getMessageTypeAnnotation(cls);

        //convert 2 bytes to an integer
        int i = ByteUtils.twoBytesToInt(type.type(), type.extendedType());

        return handlerMap.get(i);
    }

    @Override
    public  void register (Class cls, MessageHandler handler) {
        //get type
        MessageType type = getMessageTypeAnnotation(cls);

        //convert 2 bytes to an integer
        int i = ByteUtils.twoBytesToInt(type.type(), type.extendedType());

        this.handlerMap.put(i, handler);
    }

    @Override
    public  void unregister (Class cls) {
        //get type
        MessageType type = getMessageTypeAnnotation(cls);

        //convert 2 bytes to an integer
        int i = ByteUtils.twoBytesToInt(type.type(), type.extendedType());

        this.handlerMap.remove(i);
    }

    protected  MessageType getMessageTypeAnnotation (Class cls) {
        //get type
        MessageType type = cls.getAnnotation(MessageType.class);

        if (type == null) {
            throw new IllegalStateException("class '" + cls.getCanonicalName() + "' doesnt contains required annotation @MessageType!");
        }

        return type;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy