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

se.laz.casual.network.protocol.decoding.CasualMessageDecoder Maven / Gradle / Ivy

There is a newer version: 3.2.46
Show newest version
/*
 * Copyright (c) 2017 - 2023, The casual project. All rights reserved.
 *
 * This software is licensed under the MIT license, https://opensource.org/licenses/MIT
 */

package se.laz.casual.network.protocol.decoding;


import se.laz.casual.api.network.protocol.messages.CasualNWMessage;
import se.laz.casual.api.network.protocol.messages.CasualNetworkTransmittable;
import se.laz.casual.network.protocol.decoding.decoders.CasualNWMessageHeaderDecoder;
import se.laz.casual.network.protocol.decoding.decoders.MessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.NetworkDecoder;
import se.laz.casual.network.protocol.decoding.decoders.conversation.ConnectReplyMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.conversation.ConnectRequestMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.conversation.DisconnectMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.conversation.RequestMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.domain.CasualDomainConnectReplyMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.domain.CasualDomainConnectRequestMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.domain.CasualDomainDiscoveryReplyMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.domain.CasualDomainDiscoveryRequestMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.domain.DomainDisconnectReplyMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.domain.DomainDisconnectRequestMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.domain.DomainDiscoveryTopologyUpdateMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.queue.CasualDequeueReplyMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.queue.CasualDequeueRequestMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.queue.CasualEnqueueReplyMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.queue.CasualEnqueueRequestMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.service.CasualServiceCallReplyMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.service.CasualServiceCallRequestMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.transaction.CasualTransactionResourceCommitReplyMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.transaction.CasualTransactionResourceCommitRequestMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.transaction.CasualTransactionResourcePrepareReplyMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.transaction.CasualTransactionResourcePrepareRequestMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.transaction.CasualTransactionResourceRollbackReplyMessageDecoder;
import se.laz.casual.network.protocol.decoding.decoders.transaction.CasualTransactionResourceRollbackRequestMessageDecoder;
import se.laz.casual.network.protocol.messages.CasualNWMessageHeader;
import se.laz.casual.network.protocol.messages.CasualNWMessageImpl;

public final class CasualMessageDecoder
{
    private static int maxSingleBufferByteSize = Integer.MAX_VALUE;
    private CasualMessageDecoder()
    {}

    /**
     * Number of maximum bytes before any chunk reading takes place
     * Defaults to Integer.MAX_VALUE
     * @return maximum number of bytes for a single buffer payload.
     */
    public static int getMaxSingleBufferByteSize()
    {
        return maxSingleBufferByteSize;
    }

    public static CasualNWMessageHeader networkHeaderToCasualHeader(final byte[] message)
    {
        return CasualNWMessageHeaderDecoder.fromNetworkBytes(message);
    }

    public static  CasualNWMessage read(final byte[] data, CasualNWMessageHeader header)
    {
        NetworkDecoder networkReader = getDecoder( header );
        return readMessage( data, header, networkReader );
    }

    @SuppressWarnings({"unchecked", "squid:MethodCyclomaticComplexity"})
    static  NetworkDecoder getDecoder(CasualNWMessageHeader header )
    {
        switch(header.getType())
        {
            case DOMAIN_DISCOVERY_REQUEST:
                return (NetworkDecoder) CasualDomainDiscoveryRequestMessageDecoder.of();
            case DOMAIN_DISCOVERY_REPLY:
                return (NetworkDecoder) CasualDomainDiscoveryReplyMessageDecoder.of();
            case DOMAIN_DISCONNECT_REQUEST:
                return (NetworkDecoder) DomainDisconnectRequestMessageDecoder.of();
            case DOMAIN_DISCONNECT_REPLY:
                return (NetworkDecoder) DomainDisconnectReplyMessageDecoder.of();
            case DOMAIN_DISCOVERY_TOPOLOGY_UPDATE:
                return (NetworkDecoder) DomainDiscoveryTopologyUpdateMessageDecoder.of();
            case DOMAIN_CONNECT_REQUEST:
                return (NetworkDecoder) CasualDomainConnectRequestMessageDecoder.of();
            case DOMAIN_CONNECT_REPLY:
                return (NetworkDecoder) CasualDomainConnectReplyMessageDecoder.of();
            case SERVICE_CALL_REQUEST:
                // We may want to use some other size for chunking of service payload
                CasualServiceCallRequestMessageDecoder.setMaxPayloadSingleBufferByteSize(getMaxSingleBufferByteSize());
                return (NetworkDecoder) CasualServiceCallRequestMessageDecoder.of();
            case SERVICE_CALL_REPLY:
                // We may want to use some other size for chunking of service payload
                CasualServiceCallReplyMessageDecoder.setMaxPayloadSingleBufferByteSize(getMaxSingleBufferByteSize());
                return (NetworkDecoder) CasualServiceCallReplyMessageDecoder.of();
            case ENQUEUE_REQUEST:
                return (NetworkDecoder) CasualEnqueueRequestMessageDecoder.of();
            case ENQUEUE_REPLY:
                return (NetworkDecoder) CasualEnqueueReplyMessageDecoder.of();
            case DEQUEUE_REQUEST:
                return (NetworkDecoder) CasualDequeueRequestMessageDecoder.of();
            case DEQUEUE_REPLY:
                return (NetworkDecoder) CasualDequeueReplyMessageDecoder.of();
            case PREPARE_REQUEST:
                return (NetworkDecoder) CasualTransactionResourcePrepareRequestMessageDecoder.of();
            case PREPARE_REQUEST_REPLY:
                return (NetworkDecoder) CasualTransactionResourcePrepareReplyMessageDecoder.of();
            case COMMIT_REQUEST:
                return (NetworkDecoder) CasualTransactionResourceCommitRequestMessageDecoder.of();
            case COMMIT_REQUEST_REPLY:
                return (NetworkDecoder) CasualTransactionResourceCommitReplyMessageDecoder.of();
            case REQUEST_ROLLBACK:
                return (NetworkDecoder) CasualTransactionResourceRollbackRequestMessageDecoder.of();
            case REQUEST_ROLLBACK_REPLY:
                return (NetworkDecoder) CasualTransactionResourceRollbackReplyMessageDecoder.of();
            case CONVERSATION_CONNECT:
                return (NetworkDecoder) ConnectRequestMessageDecoder.of();
            case CONVERSATION_CONNECT_REPLY:
                return (NetworkDecoder) ConnectReplyMessageDecoder.of();
            case CONVERSATION_REQUEST:
                return (NetworkDecoder) RequestMessageDecoder.of();
            case CONVERSATION_DISCONNECT:
                return (NetworkDecoder) DisconnectMessageDecoder.of();
            default:
                throw new UnsupportedOperationException("Unknown messagetype: " + header.getType());
        }
    }

    private static  CasualNWMessage readMessage(final byte[] data, final CasualNWMessageHeader header, NetworkDecoder nr )
    {
        final MessageDecoder reader = MessageDecoder.of(nr, getMaxSingleBufferByteSize() );
        final T msg = reader.read(data);
        return CasualNWMessageImpl.of(header.getCorrelationId(), msg);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy