001package jmri.jmrix.loconet.loconetovertcp;
002
003import jmri.jmrix.loconet.LnNetworkPortController;
004import jmri.jmrix.loconet.LnTrafficController;
005import jmri.jmrix.loconet.LocoNetSystemConnectionMemo;
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 * Implements SerialPortAdapter for the LocoNetOverTcp system network
011 * connection.
012 * <p>
013 * This connects a LocoNet via a telnet connection. Normally controlled by the
014 * LnTcpDriverFrame class.
015 *
016 * @author Bob Jacobsen Copyright (C) 2001, 2002, 2003
017 * @author Alex Shepherd Copyright (C) 2003, 2006
018 */
019public class LnTcpDriverAdapter extends LnNetworkPortController {
020
021    public LnTcpDriverAdapter(LocoNetSystemConnectionMemo m) {
022        super(m);
023        allowConnectionRecovery = true;
024        reconnectMaxAttempts = -1; // retry indefinitely
025        option2Name = "CommandStation";
026        option3Name = "TurnoutHandle";
027        options.put(option2Name, new Option(Bundle.getMessage("CommandStationTypeLabel"), commandStationNames, false));
028        options.put(option3Name, new Option(Bundle.getMessage("TurnoutHandling"),
029                new String[]{Bundle.getMessage("HandleNormal"), Bundle.getMessage("HandleSpread"), Bundle.getMessage("HandleOneOnly"), Bundle.getMessage("HandleBoth")})); // I18N
030        options.put("TranspondingPresent", new Option(Bundle.getMessage("TranspondingPresent"),
031                new String[]{Bundle.getMessage("ButtonNo"), Bundle.getMessage("ButtonYes")} )); // NOI18N
032        options.put("InterrogateOnStart", new Option(Bundle.getMessage("InterrogateOnStart"),
033                new String[]{Bundle.getMessage("ButtonYes"), Bundle.getMessage("ButtonNo")} )); // NOI18N
034        options.put("LoconetProtocolAutoDetect", new Option(Bundle.getMessage("LoconetProtocolAutoDetectLabel"),
035                new String[]{Bundle.getMessage("LoconetProtocolAutoDetect"),Bundle.getMessage("ButtonNo")} )); // NOI18N
036
037    }
038
039    public LnTcpDriverAdapter() {
040        this(new LocoNetSystemConnectionMemo());
041    }
042
043    @Override
044    public void recover() {
045        if (allowConnectionRecovery && opened) {
046            log.info("Connection to {}:{} lost. Attempting to recover...", getHostName(), getPort());
047        }
048        super.recover();
049    }
050
051    // after reconnect, reattach the packetizer's streams and restart the receive thread
052    @Override
053    protected void resetupConnection() {
054        LnTrafficController tc = getSystemConnectionMemo().getLnTrafficController();
055        if (tc instanceof LnOverTcpPacketizer) {
056            LnOverTcpPacketizer packets = (LnOverTcpPacketizer) tc;
057            packets.connectPort(this);
058            packets.restartRcvThread();
059        }
060    }
061
062    /**
063     * Set up all of the other objects to operate with a LocoNet connected via
064     * this class.
065     */
066    @Override
067    public void configure() {
068
069        setCommandStationType(getOptionState(option2Name));
070        setTurnoutHandling(getOptionState(option3Name));
071        setTranspondingAvailable(getOptionState("TranspondingPresent"));
072        setInterrogateOnStart(getOptionState("InterrogateOnStart"));
073        setLoconetProtocolAutoDetect(getOptionState("LoconetProtocolAutoDetect"));
074
075
076        // connect to a packetizing traffic controller
077        LnOverTcpPacketizer packets = new LnOverTcpPacketizer(this.getSystemConnectionMemo());
078        packets.connectPort(this);
079
080        // create memo
081        this.getSystemConnectionMemo().setLnTrafficController(packets);
082        // do the common manager config
083        this.getSystemConnectionMemo().configureCommandStation(commandStationType,
084                mTurnoutNoRetry, mTurnoutExtraSpace, mTranspondingAvailable, mInterrogateAtStart, mLoconetProtocolAutoDetect);
085        this.getSystemConnectionMemo().configureManagers();
086
087        // start operation
088        packets.startThreads();
089    }
090
091    @Override
092    public boolean status() {
093        return opened;
094    }
095
096    @Override
097    public void configureOption1(String value) {
098        super.configureOption1(value);
099        log.debug("configureOption1: {}", value);
100        setCommandStationType(value);
101    }
102
103    private static final Logger log = LoggerFactory.getLogger(LnTcpDriverAdapter.class);
104
105}