001package jmri.jmrix.rfid.merg.concentrator;
002
003import jmri.jmrix.AbstractMRListener;
004import jmri.jmrix.AbstractMRMessage;
005import jmri.jmrix.AbstractMRReply;
006import jmri.jmrix.rfid.RfidMessage;
007import jmri.jmrix.rfid.RfidSystemConnectionMemo;
008import jmri.jmrix.rfid.RfidTrafficController;
009import org.slf4j.Logger;
010import org.slf4j.LoggerFactory;
011
012/**
013 * Converts Stream-based I/O to/from messages. The "SerialInterface" side
014 * sends/receives message objects.
015 * <p>
016 * The connection to a SerialPortController is via a pair of *Streams, which
017 * then carry sequences of characters for transmission. Note that this
018 * processing is handled in an independent thread.
019 * <p>
020 * This maintains a list of nodes, but doesn't currently do anything with it.
021 *
022 * @author Bob Jacobsen Copyright (C) 2001, 2003, 2005, 2006, 2008
023 * @author Matthew Harris Copyright (C) 2011
024 * @since 2.11.4
025 */
026public class ConcentratorTrafficController extends RfidTrafficController {
027
028    private final String range;
029
030    public ConcentratorTrafficController(RfidSystemConnectionMemo memo, String range) {
031        super();
032        adapterMemo = memo;
033        this.range = range;
034        logDebug = log.isDebugEnabled();
035
036        // not polled at all, so allow unexpected messages, and
037        // use poll delay just to spread out startup
038        setAllowUnexpectedReply(true);
039        mWaitBeforePoll = 1000;  // can take a long time to send
040
041    }
042
043    @Override
044    public void sendInitString() {
045        String init = adapterMemo.getProtocol().initString();
046        if (init.length() > 0) {
047            sendRfidMessage(new ConcentratorMessage(init, 0), null);
048        }
049    }
050
051    @Override
052    public RfidMessage getRfidMessage(int length) {
053        return new ConcentratorMessage(length);
054    }
055
056    @Override
057    protected void forwardToPort(AbstractMRMessage m, AbstractMRListener reply) {
058        if (logDebug) {
059            log.debug("forward {}", m);
060        }
061        sendInterlock = ((RfidMessage) m).getInterlocked();
062        super.forwardToPort(m, reply);
063    }
064
065    @Override
066    protected AbstractMRReply newReply() {
067        ConcentratorReply reply = new ConcentratorReply(adapterMemo.getTrafficController());
068        return reply;
069    }
070
071    @Override
072    public String getRange() {
073        return range;
074    }
075
076    @Override
077    protected boolean endOfMessage(AbstractMRReply msg) {
078        return adapterMemo.getProtocol().endOfMessage(msg);
079    }
080
081    boolean sendInterlock = false; // send the 00 interlock when CRC received
082
083    private static final Logger log = LoggerFactory.getLogger(ConcentratorTrafficController.class);
084
085}