org.bidib.jbidibc.netbidib.client.pairingstates.PairedPairingState Maven / Gradle / Ivy
package org.bidib.jbidibc.netbidib.client.pairingstates;
import org.bidib.jbidibc.messages.BidibLibrary;
import org.bidib.jbidibc.messages.message.BidibCommandMessage;
import org.bidib.jbidibc.messages.message.LocalLogoffMessage;
import org.bidib.jbidibc.messages.message.LocalLogonMessage;
import org.bidib.jbidibc.messages.message.netbidib.BidibLinkData;
import org.bidib.jbidibc.messages.message.netbidib.LocalLinkMessage;
import org.bidib.jbidibc.messages.message.netbidib.NetBidibLinkData.LogonStatus;
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 PairedPairingState extends AbstractPairingState {
private static final Logger LOGGER = LoggerFactory.getLogger(PairedPairingState.class);
public PairedPairingState(final DefaultPairingStateHandler pairingStateHandler, BidibLinkData remotePartnerLinkData,
ProxyBidibLinkData clientLinkData) {
super(pairingStateHandler, remotePartnerLinkData, clientLinkData);
}
@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 onLocalLogon(LocalLogonMessage localLogonMessage) {
LOGGER.info("onLocalLogon, localLogonMessage: {}", localLogonMessage);
super.onLocalLogon(localLogonMessage);
// TODO generate the local addr
int localNodeAddr = 0; // was 1 before
long uniqueId = localLogonMessage.getSenderUniqueId();
LOGGER
.info("LocalLogonMessage uniqueId: {}, remotePartnerLinkData.uniqueId: {}, use localNodeAddr: {}", uniqueId,
remotePartnerLinkData.getUniqueId(), localNodeAddr);
try {
final BidibCommandMessage message = getRequestFactory().createLocalLogonAckRequest(localNodeAddr, uniqueId);
publishMessage(message);
remotePartnerLinkData.setLogonStatus(LogonStatus.LOGGED_ON);
}
catch (Exception ex) {
LOGGER.warn("Send the status paired to partner failed.", ex);
}
publishLocalLogon(localNodeAddr, uniqueId);
}
@Override
public void onLocalLogoff(LocalLogoffMessage localLogoffMessage) {
LOGGER.info("onLocalLogoff, localLogoffMessage: {}", localLogoffMessage);
super.onLocalLogoff(localLogoffMessage);
remotePartnerLinkData.setLogonStatus(LogonStatus.LOGGED_OFF);
publishLocalLogoff();
}
}