org.bidib.jbidibc.netbidib.client.pairingstates.InitialPairingState 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.NetBidibCommandMessage;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibLinkData.PairingStatus;
import org.bidib.jbidibc.messages.utils.ByteUtils;
import org.bidib.jbidibc.messages.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class InitialPairingState extends AbstractPairingState {
private static final Logger LOGGER = LoggerFactory.getLogger(InitialPairingState.class);
private final Object emitterLock = new Object();
public InitialPairingState(final DefaultPairingStateHandler pairingStateHandler,
BidibLinkData remotePartnerLinkData, ProxyBidibLinkData clientLinkData) {
super(pairingStateHandler, remotePartnerLinkData, clientLinkData);
}
@Override
public void sendNetBidibStartupSequence() {
String requestorName = clientLinkData.getRequestorName();
LOGGER.info("Send the startup sequence. Configured local requestor: {}", requestorName);
try {
// let the pairing state handler send the initial message
NetBidibCommandMessage localProtocolSignatureMessage =
getRequestFactory().createLocalProtocolSignature(requestorName);
synchronized (emitterLock) {
publishMessage(localProtocolSignatureMessage);
this.clientLinkData.setInitialEmitterSent(true);
if (StringUtils.isNotBlank(remotePartnerLinkData.getRequestorName())) {
LOGGER
.info("The requestorName of the remote partner is available already: {}",
remotePartnerLinkData.getRequestorName());
publishInitialLinkData();
}
}
}
catch (ProtocolException ex) {
LOGGER.warn("Send LocalProtocolSignature to remote partner failed.", ex);
// TODO: handle exception
}
}
@Override
public void onLocalProtocolSignature(LocalProtocolSignatureMessage localProtocolSignatureMessage) {
LOGGER.info("onLocalProtocolSignature, localProtocolSignatureMessage: {}", localProtocolSignatureMessage);
String requestorName = localProtocolSignatureMessage.getRequestorName();
synchronized (emitterLock) {
remotePartnerLinkData.setRequestorName(requestorName);
LOGGER.info("Set the requestor in remotePartnerLinkData: {}", remotePartnerLinkData);
if (this.clientLinkData.getInitialEmitterSent()) {
LOGGER.info("The emitter was published already. Publish the initial link data.");
publishInitialLinkData();
}
}
}
private void publishInitialLinkData() {
// send my emitter
try {
long uniqueId = clientLinkData.getUniqueId();
LOGGER.info("Publish the initial link data, uniqueId: {}", uniqueId);
LocalLinkMessage bidibCommand = getRequestFactory().createLocalLinkDescriptorUid(uniqueId);
publishMessage(bidibCommand);
bidibCommand = getRequestFactory().createLocalLinkProdString(clientLinkData.getProdString());
publishMessage(bidibCommand);
bidibCommand = getRequestFactory().createLocalLinkUserString(clientLinkData.getUserString());
publishMessage(bidibCommand);
bidibCommand = getRequestFactory().createLocalLinkProtocolVersion(clientLinkData.getProtocolVersion());
publishMessage(bidibCommand);
bidibCommand = getRequestFactory().createLocalLinkNetBidibRole(clientLinkData.getNetBidibRole());
publishMessage(bidibCommand);
}
catch (ProtocolException ex) {
LOGGER.warn("Send descriptor UID to remote partner failed.", ex);
// TODO: handle exception
}
LOGGER.info("Send the link data uniqueId, prod string, user string and protocol version to the server.");
}
@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());
boolean forcePaired = false;
// TODO check if we are paired already on our side
if (forcePaired || isPaired(remotePartnerLinkData)) {
LOGGER.info("The remote partner is paired already. Send the status paired.");
try {
// send the status paired
LocalLinkMessage bidibCommand =
getRequestFactory()
.createLocalLinkStatusPaired(clientLinkData.getUniqueId(),
remotePartnerLinkData.getUniqueId());
publishMessage(bidibCommand);
clientLinkData.setPairingStatus(PairingStatus.PAIRED);
setNextPairingState(PairingStateEnum.MyRequest);
}
catch (ProtocolException ex) {
LOGGER.warn("Send LocalLinkStatusPaired to remote partner failed.", ex);
// TODO: handle exception
}
}
else {
LOGGER.info("Send the status unpaired.");
try {
// send the status unpaired
LocalLinkMessage bidibCommand =
getRequestFactory()
.createLocalLinkStatusUnpaired(clientLinkData.getUniqueId(),
remotePartnerLinkData.getUniqueId());
publishMessage(bidibCommand);
setNextPairingState(PairingStateEnum.Unpaired);
}
catch (ProtocolException ex) {
LOGGER.warn("Send LocalLinkStatusUnPaired to remote partner failed.", ex);
// TODO: handle exception
}
}
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_DESCRIPTOR_ROLE:
LOGGER.info("Received the partner ROLE: {}", localLinkMessage.getNetBidibRole());
remotePartnerLinkData.setNetBidibRole(localLinkMessage.getNetBidibRole());
break;
default:
super.onLocalLink(localLinkMessage);
break;
}
}
}