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

org.bidib.jbidibc.netbidib.client.NetMessageReceiver Maven / Gradle / Ivy

package org.bidib.jbidibc.netbidib.client;

import java.io.ByteArrayOutputStream;

import org.bidib.jbidibc.core.AbstractMessageReceiver;
import org.bidib.jbidibc.core.node.NodeRegistry;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.exception.ProtocolException;
import org.bidib.jbidibc.messages.message.BidibMessage;
import org.bidib.jbidibc.messages.message.BidibMessageInterface;
import org.bidib.jbidibc.messages.message.LocalLogoffMessage;
import org.bidib.jbidibc.messages.message.LocalLogonAckMessage;
import org.bidib.jbidibc.messages.message.LocalLogonMessage;
import org.bidib.jbidibc.messages.message.ResponseFactory;
import org.bidib.jbidibc.messages.message.netbidib.LocalLinkMessage;
import org.bidib.jbidibc.messages.message.netbidib.LocalProtocolSignatureMessage;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.netbidib.client.listener.NetBidibLocalMessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class NetMessageReceiver extends AbstractMessageReceiver {
    private static final Logger LOGGER = LoggerFactory.getLogger(NetMessageReceiver.class);

    private ByteArrayOutputStream output = new ByteArrayOutputStream(2048);

    private NetBidibLocalMessageListener netBidibLocalMessageListener;

    /**
     * Create a new instance of MessageReceiver.
     * 
     * @param nodeRegistry
     *            the node registry
     * @param responseFactory
     *            the response factory
     * @param checkCRC
     *            flag if we must check the CRC
     */
    public NetMessageReceiver(final NodeRegistry nodeRegistry, final ResponseFactory responseFactory,
        boolean checkCRC) {

        super(nodeRegistry, responseFactory, checkCRC);
    }

    public void setNetBidibLocalMessageListener(NetBidibLocalMessageListener netBidibLocalMessageListener) {
        this.netBidibLocalMessageListener = netBidibLocalMessageListener;
    }

    @Override
    public void enable() {
        LOGGER.info("enable is called.");
        // escapeHot.set(false);

        MSG_RAW_LOGGER.info("++++ Enable the message receiver.");

        try {
            output.reset();
        }
        catch (Exception ex) {
            LOGGER.warn("Reset buffered received data failed.", ex);
        }

        super.enable();
    }

    @Override
    public void disable() {
        LOGGER.info("Disable is called.");
        super.disable();

        MSG_RAW_LOGGER.info("++++ Disable the message receiver.");

    }

    private void logRX(final BidibMessageInterface bidibCommand, byte[] messageArray) {

        if (MSG_RX_LOGGER.isInfoEnabled()) {

            StringBuilder sb = new StringBuilder("<< ");
            sb.append(bidibCommand);
            sb.append(" : ");
            sb.append(ByteUtils.bytesToHex(messageArray));

            MSG_RX_LOGGER.info(sb.toString());
        }
    }

    @Override
    protected void processMessage(byte[] messageArray) throws ProtocolException {

        BidibMessage message = null;

        try {
            // let the request factory create the commands
            BidibMessageInterface bidibCommand = getResponseFactory().create(messageArray);
            LOGGER.trace("Received command: {}", bidibCommand);

            switch (ByteUtils.getInt(bidibCommand.getType())) {
                case BidibLibrary.MSG_LOCAL_PROTOCOL_SIGNATURE:
                    logRX(bidibCommand, messageArray);

                    // respond the protocol signature
                    LocalProtocolSignatureMessage localProtocolSignatureMessage =
                        (LocalProtocolSignatureMessage) bidibCommand;
                    String requestor = localProtocolSignatureMessage.getRequestorName();
                    LOGGER.info("Received MSG_LOCAL_PROTOCOL_SIGNATURE from requestor: {}", requestor);

                    if (netBidibLocalMessageListener != null) {
                        netBidibLocalMessageListener.onLocalProtocolSignature(localProtocolSignatureMessage);
                    }
                    else {
                        LOGGER.warn("No netBidibLocalMessageListener available. Discard message.");
                    }

                    break;
                case BidibLibrary.MSG_LOCAL_LINK:
                    logRX(bidibCommand, messageArray);

                    LocalLinkMessage localLinkMessage = (LocalLinkMessage) bidibCommand;
                    processMsgLocalLink(localLinkMessage);
                    break;

                case BidibLibrary.MSG_LOCAL_LOGON_ACK:
                    logRX(bidibCommand, messageArray);
                    super.processMessage(bidibCommand);

                    LocalLogonAckMessage localLogonAckMessage = (LocalLogonAckMessage) bidibCommand;
                    processMsgLocalLogonAck(localLogonAckMessage);
                    break;
                case BidibLibrary.MSG_LOCAL_LOGON:
                    logRX(bidibCommand, messageArray);
                    super.processMessage(bidibCommand);

                    LocalLogonMessage localLogonMessage = (LocalLogonMessage) bidibCommand;
                    processMsgLocalLogon(localLogonMessage);
                    break;
                case BidibLibrary.MSG_LOCAL_LOGOFF:
                    logRX(bidibCommand, messageArray);
                    super.processMessage(bidibCommand);

                    LocalLogoffMessage localLogoffMessage = (LocalLogoffMessage) bidibCommand;
                    processMsgLocalLogoff(localLogoffMessage);
                    break;
                default:
                    LOGGER.trace("Processing BiDiB node related command: {}", bidibCommand);
                    logRX(bidibCommand, messageArray);
                    super.processMessage(bidibCommand);
                    break;
            }
        }
        catch (ProtocolException ex) {
            LOGGER.warn("Process received messages failed: {}", ByteUtils.bytesToHex(messageArray), ex);

            StringBuilder sb = new StringBuilder("<< received invalid: ");
            sb.append(message);
            sb.append(" : ");
            sb.append(ByteUtils.bytesToHex(messageArray));

            MSG_RX_LOGGER.warn(sb.toString());

            throw ex;
        }
        catch (Exception ex) {
            LOGGER.warn("Process received messages failed: {}", ByteUtils.bytesToHex(messageArray), ex);
        }

        // TODO Auto-generated method stub
        // super.processMessage(messageArray);
    }

    private void processMsgLocalLink(final LocalLinkMessage localLinkMessage) throws ProtocolException {
        LOGGER.info("Received MSG_LOCAL_LINK: {}", localLinkMessage);

        if (netBidibLocalMessageListener != null) {
            netBidibLocalMessageListener.onLocalLink(localLinkMessage);
        }
        else {
            LOGGER.warn("No netBidibLocalMessageListener available. Discard message.");
        }
    }

    private void processMsgLocalLogonAck(final LocalLogonAckMessage localLogonAckMessage) {
        LOGGER.info("Received MSG_LOCAL_LOGON_ACK: {}", localLogonAckMessage);

        int nodeAddress = localLogonAckMessage.getNodeAddress();
        long uniqueId = localLogonAckMessage.getSenderUniqueId();

        LOGGER.info("Current nodeAddress: {}, uniqueId: {}", nodeAddress, ByteUtils.formatHexUniqueId(uniqueId));

        if (netBidibLocalMessageListener != null) {
            netBidibLocalMessageListener.onLocalLogonAck(localLogonAckMessage);
        }
        else {
            LOGGER.warn("No netBidibLocalMessageListener available. Discard message.");
        }
    }

    private void processMsgLocalLogon(final LocalLogonMessage localLogonMessage) {
        LOGGER.info("Received MSG_LOCAL_LOGON: {}", localLogonMessage);

        long uniqueId = localLogonMessage.getSenderUniqueId();

        LOGGER.info("Current uniqueId: {}", ByteUtils.formatHexUniqueId(uniqueId));

        if (netBidibLocalMessageListener != null) {
            netBidibLocalMessageListener.onLocalLogon(localLogonMessage);
        }
        else {
            LOGGER.warn("No netBidibLocalMessageListener available. Discard message.");
        }
    }

    private void processMsgLocalLogoff(final LocalLogoffMessage localLogoffMessage) {
        LOGGER.info("Received MSG_LOCAL_LOGOFF: {}", localLogoffMessage);

        Long uniqueId = localLogoffMessage.getSenderUniqueId();

        LOGGER.info("Current uniqueId: {}", ByteUtils.formatHexUniqueId(uniqueId));

        if (netBidibLocalMessageListener != null) {
            netBidibLocalMessageListener.onLocalLogoff(localLogoffMessage);
        }
        else {
            LOGGER.warn("No netBidibLocalMessageListener available. Discard message.");
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy