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

org.bidib.jbidibc.netbidib.client.pairingstates.TheirRequestPairingState Maven / Gradle / Ivy

package org.bidib.jbidibc.netbidib.client.pairingstates;

import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.enums.PairingResult;
import org.bidib.jbidibc.messages.message.netbidib.BidibLinkData;
import org.bidib.jbidibc.messages.message.netbidib.LocalLinkMessage;
import org.bidib.jbidibc.messages.message.netbidib.LocalProtocolSignatureMessage;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibCommandMessage;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibLinkData.PairingStatus;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TheirRequestPairingState extends AbstractPairingState {

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

    public TheirRequestPairingState(final DefaultPairingStateHandler pairingStateHandler,
        BidibLinkData remotePartnerLinkData, ProxyBidibLinkData clientLinkData) {
        super(pairingStateHandler, remotePartnerLinkData, clientLinkData);
    }

    @Override
    public void onLocalProtocolSignature(LocalProtocolSignatureMessage localProtocolSignatureMessage) {
        LOGGER.info("onLocalProtocolSignature, localProtocolSignatureMessage: {}", localProtocolSignatureMessage);

        String requestorName = localProtocolSignatureMessage.getRequestorName();

        remotePartnerLinkData.setRequestorName(requestorName);
        LOGGER.info("Set the requestor in remotePartnerLinkData: {}", remotePartnerLinkData);

    }

    @Override
    public void onLocalLink(LocalLinkMessage localLinkMessage) {

        switch (localLinkMessage.getLinkDescriptor()) {
            case BidibLibrary.BIDIB_LINK_STATUS_UNPAIRED:
                LOGGER
                    .info("Received the status unpaired from remote partner: {}",
                        ByteUtils.formatHexUniqueId(localLinkMessage.getSenderUniqueId()));

                signalPairingResult(PairingStatus.UNPAIRED);

                setNextPairingState(PairingStateEnum.Unpaired);
                break;
            default:
                super.onLocalLink(localLinkMessage);
                break;
        }
    }

    @Override
    public void pairingResult(PairingResult pairingResult) {
        LOGGER
            .info("Handle the pairingResult: {}, current partnerType: {}", pairingResult,
                clientLinkData.getPartnerType());

        if (PairingResult.PAIRED == pairingResult) {
            try {
                int timeout = clientLinkData.getRequestedPairingTimeout();
                // send the pairing request
                LocalLinkMessage pairingRequestMessage =
                    getRequestFactory()
                        .createLocalLinkPairingRequest(clientLinkData.getUniqueId(),
                            remotePartnerLinkData.getUniqueId(), timeout);
                publishMessage(pairingRequestMessage);

                clientLinkData.setPairingStatus(PairingStatus.PAIRING_REQUESTED);
                clientLinkData.setRequestedPairingTimeout(timeout);

                // send the status paired
                LOGGER.info("Send the status paired to the remote partner.");
                NetBidibCommandMessage statusPairedMessage =
                    getRequestFactory()
                        .createLocalLinkStatusPaired(clientLinkData.getUniqueId(), remotePartnerLinkData.getUniqueId());
                publishMessage(statusPairedMessage);

                // TODO is this correct ???
                clientLinkData.setPairingStatus(PairingStatus.PAIRED);

                setNextPairingState(PairingStateEnum.MyRequest);
            }
            catch (Exception ex) {
                LOGGER.warn("Send the status paired to partner failed.", ex);
            }
        }
        else {
            try {
                // send the status unpaired
                LOGGER.info("Send the status unpaired to the remote partner.");

                NetBidibCommandMessage unpairedRequest =
                    getRequestFactory()
                        .createLocalLinkStatusUnpaired(clientLinkData.getUniqueId(),
                            remotePartnerLinkData.getUniqueId());
                publishMessage(unpairedRequest);

                // TODO is this correct ???
                clientLinkData.setPairingStatus(PairingStatus.UNPAIRED);

                setNextPairingState(PairingStateEnum.Unpaired);
            }
            catch (Exception ex) {
                LOGGER.warn("Send the status paired to partner failed.", ex);
            }

        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy