org.bidib.jbidibc.netbidib.client.pairingstates.UnpairedPairingState Maven / Gradle / Ivy
package org.bidib.jbidibc.netbidib.client.pairingstates;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.exception.ProtocolException;
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.NetBidibLinkData.PairingStatus;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UnpairedPairingState extends AbstractPairingState {
private static final Logger LOGGER = LoggerFactory.getLogger(UnpairedPairingState.class);
public UnpairedPairingState(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_DESCRIPTOR_UID:
LOGGER
.info("Received the partner UID: {}",
ByteUtils.formatHexUniqueId(localLinkMessage.getSenderUniqueId()));
remotePartnerLinkData.setUniqueId(localLinkMessage.getSenderUniqueId());
break;
case BidibLibrary.BIDIB_LINK_DESCRIPTOR_P_VERSION:
LOGGER.info("Received the partner P_VERSION: {}", localLinkMessage.getProtocolVersion());
remotePartnerLinkData.setProtocolVersion(localLinkMessage.getProtocolVersion());
break;
case BidibLibrary.BIDIB_LINK_DESCRIPTOR_PROD_STRING:
LOGGER.info("Received the partner PROD_STRING: {}", localLinkMessage.getProdString());
remotePartnerLinkData.setProdString(localLinkMessage.getProdString());
break;
case BidibLibrary.BIDIB_LINK_DESCRIPTOR_USER_STRING:
LOGGER.info("Received the partner USER_STRING: {}", localLinkMessage.getProdString());
remotePartnerLinkData.setUserString(localLinkMessage.getProdString());
break;
case BidibLibrary.BIDIB_LINK_PAIRING_REQUEST:
LOGGER
.info("Received a pairing request, senderUID: {}, receiverUID: {}, pairingTimeout: {}",
ByteUtils.formatHexUniqueId(localLinkMessage.getSenderUniqueId()),
ByteUtils.formatHexUniqueId(localLinkMessage.getReceiverUniqueId()),
localLinkMessage.getPairingTimeout());
remotePartnerLinkData.setPairingStatus(PairingStatus.PAIRING_REQUESTED);
remotePartnerLinkData.setRequestedPairingTimeout(localLinkMessage.getPairingTimeout());
setNextPairingState(PairingStateEnum.TheirRequest);
// TODO signal the pairing request to the ui
signalPairingRequest();
break;
case BidibLibrary.BIDIB_LINK_STATUS_UNPAIRED:
LOGGER
.info("Received the status unpaired from remote partner: {}",
ByteUtils.formatHexUniqueId(localLinkMessage.getSenderUniqueId()));
signalPairingResult(PairingStatus.UNPAIRED);
// TODO we must notify the user
// setNextPairingState(PairingStateEnum.MyRequest);
break;
case BidibLibrary.BIDIB_LINK_STATUS_PAIRED:
LOGGER
.info("Received the status paired from remote partner: {}",
ByteUtils.formatHexUniqueId(localLinkMessage.getSenderUniqueId()));
signalPairingResult(PairingStatus.UNPAIRED);
// LOGGER.info("Send the paired state to the remote partner.");
// try {
// // send the paired state
// LocalLinkMessage pairedStateMessage =
// getRequestFactory()
// .createLocalLinkStatusPaired(clientLinkData.getUniqueId(),
// remotePartnerLinkData.getUniqueId());
// publishMessage(pairedStateMessage);
// }
// catch (ProtocolException ex) {
// LOGGER.warn("Send LocalLinkStatusPaired to remote partner failed.", ex);
// // TODO: handle exception
// }
//
// signalPairingResult(PairingStatus.PAIRED);
//
// setNextPairingState(PairingStateEnum.Paired);
break;
default:
super.onLocalLink(localLinkMessage);
break;
}
}
@Override
public void initiatePairing(int timeout) {
LOGGER.info("initPairing. Prepare the pairing request and send it to the remote partner, timeout: {}", timeout);
// TODO make the timeout configurable
// int timeout = 30;
try {
// send the pairing request
LocalLinkMessage pairingRequestMessage =
getRequestFactory()
.createLocalLinkPairingRequest(clientLinkData.getUniqueId(), remotePartnerLinkData.getUniqueId(),
timeout);
publishMessage(pairingRequestMessage);
clientLinkData.setPairingStatus(PairingStatus.PAIRING_REQUESTED);
clientLinkData.setRequestedPairingTimeout(timeout);
setNextPairingState(PairingStateEnum.MyRequest);
// // send the paired state
// LocalLinkMessage pairedStateMessage =
// getRequestFactory()
// .createLocalLinkStatusPaired(clientLinkData.getUniqueId(), remotePartnerLinkData.getUniqueId());
// publishMessage(pairedStateMessage);
}
catch (ProtocolException ex) {
LOGGER.warn("Send LocalLinkPairingRequest to remote partner failed.", ex);
// TODO: handle exception
}
}
}