001package jmri.jmrix.lenz;
002
003import org.slf4j.Logger;
004import org.slf4j.LoggerFactory;
005
006/**
007 * The XNetTimeSlotListener listens for two messages from the computer interface:
008 * <ol>
009 * <li>"Command Station No Longer Providing a timeslot for communications" (01 05 04)</li>
010 * <li>"Command Station is providing a timeslot for communications again." (01 07 06)</li>
011 * </ol>
012 * <p>
013 * when the first message is received, the associated port controller's 
014 * setTimeSlot methodis called with a "false" parameter.  When the second 
015 * is true, it is called with a "true paramter.
016 *
017 * @author Paul Bender Copyright (C) 2017
018 */
019public class XNetTimeSlotListener implements XNetListener {
020
021    private XNetPortController port;
022
023    public XNetTimeSlotListener(XNetPortController p){
024       port = p;
025       log.debug("Time Slot Listener created");
026    }
027
028    /**
029     * Member function that will be invoked by an XNetInterface implementation to
030     * forward an XNet message from the layout.
031     *
032     * @param msg The received XNet message. Note that this same object may be
033     *            presented to multiple users. It should not be modified here.
034     */
035    @Override
036    public void message(XNetReply msg){
037        log.debug("Time Slot Listener received {}",msg);
038        if(msg.isTimeSlotErrorMessage()){
039           if(msg.isTimeSlotRevoked()){
040              log.debug("Time Slot Revoked Received");
041              port.setTimeSlot(false);
042           } else if(msg.isTimeSlotRestored()) {
043              log.debug("Time Slot Restored Received");
044              port.setTimeSlot(true);
045           } else {
046              log.debug("Message Sent while we had no timeslot");
047              port.setTimeSlot(false);
048           }
049        }
050    }
051
052    /**
053     * Member function that will be invoked by an XNetInterface implementation to
054     * forward an XNet message sent to the layout. Normally, this function will
055     * do nothing.
056     *
057     * @param msg The received XNet message. Note that this same object may be
058     *            presented to multiple users. It should not be modified here.
059     */
060    @Override
061    public void message(XNetMessage msg){
062       // do nothing
063    }
064
065    /**
066     * Member function invoked by an XNetInterface implementation to notify a
067     * sender that an outgoing message timed out and was dropped from the
068     * queue.
069     */
070    @Override
071    public void notifyTimeout(XNetMessage msg){
072       // do nothing
073    }
074
075    private static final Logger log = LoggerFactory.getLogger(XNetTimeSlotListener.class);
076
077}