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

org.bidib.wizard.gateway.netbidib.NetBidibServerHandlerMessage Maven / Gradle / Ivy

package org.bidib.wizard.gateway.netbidib;

import java.util.function.Consumer;

import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.HostAdapter;
import org.bidib.jbidibc.messages.SequenceNumberProvider;
import org.bidib.jbidibc.messages.message.BidibMessageInterface;
import org.bidib.jbidibc.messages.message.LocalBidibUpResponse;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibLinkData;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibLinkData.LogonStatus;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.netbidib.exception.PairingDeniedException;
import org.bidib.jbidibc.netbidib.server.NetBidibServerHandler;
import org.bidib.jbidibc.netbidib.server.RoleTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.netty.buffer.Unpooled;
import io.netty.channel.group.ChannelGroup;

public class NetBidibServerHandlerMessage extends NetBidibServerHandler {

    private static final Logger LOGGER = LoggerFactory.getLogger(NetBidibServerHandlerMessage.class);

    private static final Logger MSG_TX_NET_LOGGER = LoggerFactory.getLogger("TX_NET");

    public NetBidibServerHandlerMessage(ChannelGroup channelGroup, HostAdapter hostAdapter,
        String backendPortName, NetBidibLinkData serverLinkData,
        Consumer> lazyInitializationCallback, final RoleTypeEnum roleType,
        final NetBidibLinkData pairedPartner) {
        super(channelGroup, hostAdapter, backendPortName, serverLinkData, lazyInitializationCallback,
            message -> message, roleType, pairedPartner);
    }

    @Override
    public synchronized void publishBidibMessage(final SequenceNumberProvider node, BidibMessageInterface message) {

        // publish the message to the netty channel

        // TODO skip already prepared data

        // do some inspection and filter out the MSG_BIDIB_LOCAL_UP
        if (handleLocalBidibUpResponse()) {
            try {
                if (message instanceof LocalBidibUpResponse) {
                    LocalBidibUpResponse localBidibUpResponse = (LocalBidibUpResponse) message;
                    LOGGER.info("Check if we must not deliver LocalBidibUpResponse: {}", localBidibUpResponse);

                    boolean sendPairingStatusIfRequired = processLocalBidibUpResponseFromBackend(localBidibUpResponse);

                    if (sendPairingStatusIfRequired) {
                        LOGGER.info("Check if we must send the current pairing status.");

                        if (serverLinkData.getUniqueId() != null) {
                            try {
                                LOGGER.info("Current pairingStatus: {}", pairedPartner.getPairingStatus());
                                switch (pairedPartner.getPairingStatus()) {
                                    case UNPAIRED:
                                        LOGGER.info("The partner is not paired. Send the STATUS_UNPAIRED.");
                                        publishPairedStatus(ctx, BidibLibrary.BIDIB_LINK_STATUS_UNPAIRED);
                                        break;
                                    case PAIRED:
                                        LOGGER.info("The partner is paired. Send the STATUS_PAIRED.");
                                        publishPairedStatus(ctx, BidibLibrary.BIDIB_LINK_STATUS_PAIRED);
                                        break;
                                    default:
                                        break;
                                }
                            }
                            catch (PairingDeniedException pde) {
                                LOGGER.info("The pairing is denied.", pde);
                            }
                        }
                        else {
                            LOGGER.info("The uniqueId of the server link is not yet available.");
                        }
                    }
                    return;
                }
            }
            catch (Exception ex) {
                LOGGER.warn("Check if message is local response failed.");
            }
        }

        synchronized (pairedPartnerLock) {
            if (LogonStatus.LOGGED_OFF == pairedPartner.getLogonStatus()) {
                LOGGER
                    .warn(
                        "The paired partner is not available or not logged on. Discard the message. Current pairedPartner: {}",
                        pairedPartner);
                return;
            }
        }

        if (node != null) {
            // set the correct sequence number here
            message.setSendMsgNum(node.getNextSendMessageNum());
        }

        byte[] content = message.getContent();
        LOGGER.info("Publish the message data to the connected partner: {}", ByteUtils.bytesToHex(content));

        if (MSG_TX_NET_LOGGER.isInfoEnabled()) {
            // log the bidib message and the content of the "output" stream
            StringBuilder sb = new StringBuilder(">>net>> ");
            sb.append(message);
            sb.append(" : ");
            sb.append(ByteUtils.bytesToHex(content));
            MSG_TX_NET_LOGGER.info(sb.toString());
        }

        // write the message content to the context / connection to the remote partner
        ctx.writeAndFlush(Unpooled.copiedBuffer(content));
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy