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 options.put("LoconetUpdateSlotOnMessageCreation", // NOI18N 037 new Option(Bundle.getMessage("LoconetUpdateSlotOnMessageCreationLabel"), // I18N 038 new String[]{Bundle.getMessage("ButtonNo"),Bundle.getMessage("ButtonYes")} )); // I18N 039 040 } 041 042 public LnTcpDriverAdapter() { 043 this(new LocoNetSystemConnectionMemo()); 044 } 045 046 @Override 047 public void recover() { 048 if (allowConnectionRecovery && opened) { 049 log.info("Connection to {}:{} lost. Attempting to recover...", getHostName(), getPort()); 050 } 051 super.recover(); 052 } 053 054 // after reconnect, reattach the packetizer's streams and restart the receive thread 055 @Override 056 protected void resetupConnection() { 057 LnTrafficController tc = getSystemConnectionMemo().getLnTrafficController(); 058 if (tc instanceof LnOverTcpPacketizer) { 059 LnOverTcpPacketizer packets = (LnOverTcpPacketizer) tc; 060 packets.setLoconetUpdateSlotOnMessageCreation(Bundle.getMessage("ButtonYes").equals(getOptionState("LoconetUpdateSlotOnMessageCreation"))); 061 packets.connectPort(this); 062 packets.restartRcvThread(); 063 } 064 } 065 066 /** 067 * Set up all of the other objects to operate with a LocoNet connected via 068 * this class. 069 */ 070 @Override 071 public void configure() { 072 073 setCommandStationType(getOptionState(option2Name)); 074 setTurnoutHandling(getOptionState(option3Name)); 075 setTranspondingAvailable(getOptionState("TranspondingPresent")); 076 setInterrogateOnStart(getOptionState("InterrogateOnStart")); 077 setLoconetProtocolAutoDetect(getOptionState("LoconetProtocolAutoDetect")); 078 079 080 // connect to a packetizing traffic controller 081 LnOverTcpPacketizer packets = new LnOverTcpPacketizer(this.getSystemConnectionMemo()); 082 packets.setLoconetUpdateSlotOnMessageCreation(Bundle.getMessage("ButtonYes").equals(getOptionState("LoconetUpdateSlotOnMessageCreation"))); 083 packets.connectPort(this); 084 085 // create memo 086 this.getSystemConnectionMemo().setLnTrafficController(packets); 087 // do the common manager config 088 this.getSystemConnectionMemo().configureCommandStation(commandStationType, 089 mTurnoutNoRetry, mTurnoutExtraSpace, mTranspondingAvailable, mInterrogateAtStart, mLoconetProtocolAutoDetect); 090 this.getSystemConnectionMemo().configureManagers(); 091 092 // start operation 093 packets.startThreads(); 094 } 095 096 @Override 097 public boolean status() { 098 return opened; 099 } 100 101 @Override 102 public void configureOption1(String value) { 103 super.configureOption1(value); 104 log.debug("configureOption1: {}", value); 105 setCommandStationType(value); 106 } 107 108 private static final Logger log = LoggerFactory.getLogger(LnTcpDriverAdapter.class); 109 110}