001package jmri.jmrix.can.adapters.gridconnect.net;
002
003import jmri.jmrix.can.CanSystemConnectionMemo;
004import jmri.jmrix.can.ConfigurationManager;
005import jmri.jmrix.can.TrafficController;
006import jmri.jmrix.can.adapters.gridconnect.GcTrafficController;
007import jmri.jmrix.can.adapters.gridconnect.canrs.MergTrafficController;
008import org.slf4j.Logger;
009import org.slf4j.LoggerFactory;
010
011/**
012 * Implements SerialPortAdapter for the OpenLCB system network connection.
013 * <p>
014 * This connects via a telnet connection. Normally controlled by the
015 * NetworkDriverFrame class.
016 *
017 * @author Bob Jacobsen Copyright (C) 2010
018 */
019public class NetworkDriverAdapter extends jmri.jmrix.AbstractNetworkPortController {
020
021    public NetworkDriverAdapter() {
022        super(new CanSystemConnectionMemo());
023        option1Name = "Gateway"; // NOI18N
024        options.put(option1Name, new Option(Bundle.getMessage("ConnectionGateway"), new String[]{"Pass All", "Filtering"}));
025        option2Name = "Protocol"; // NOI18N
026        options.put(option2Name, new Option(Bundle.getMessage("ConnectionProtocol"), jmri.jmrix.can.ConfigurationManager.getSystemOptions(), false));
027        super.setManufacturer(jmri.jmrix.openlcb.OlcbConnectionTypeList.OPENLCB); // overriden by MERG Connections.
028        allowConnectionRecovery = true;
029    }
030
031    /**
032     * Set up all of the other objects to operate with the CAN bus connected via
033     * this TCP/IP link.
034     */
035    @Override
036    public void configure() {
037        TrafficController tc;
038        switch (getOptionState(option2Name)) {
039            case ConfigurationManager.MERGCBUS:
040            case ConfigurationManager.SPROGCBUS:
041                // Register the CAN traffic controller being used for this connection
042                tc = new MergTrafficController();
043                 try {
044                    tc.setCanId(Integer.parseInt(getOptionState("CANID")));
045                } catch (NumberFormatException e) {
046                    log.error("Cannot parse CAN ID \"{}\" - check your preference settings", getOptionState("CANID"), e);
047                    log.error("Now using default CAN ID {}",tc.getCanid());
048                }
049                break;
050            default:
051                tc = new GcTrafficController();
052                break;
053        }
054        this.getSystemConnectionMemo().setTrafficController(tc);
055
056        // Now connect to the traffic controller
057        log.debug("Connecting port");
058        tc.connectPort(this);
059        this.getSystemConnectionMemo().setProtocol(getOptionState(option2Name));
060
061        // do central protocol-specific configuration
062        this.getSystemConnectionMemo().configureManagers();
063        if (socketConn != null) {
064            log.info("{} Connection via {} complete with {}",
065                this.getSystemConnectionMemo().getUserName(),
066                getOptionState(option2Name), socketConn.getInetAddress());
067        }
068    }
069
070    @Override
071    public boolean status() {
072        return opened;
073    }
074
075    @Override
076    public CanSystemConnectionMemo getSystemConnectionMemo() {
077        return (CanSystemConnectionMemo) super.getSystemConnectionMemo();
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    protected void resetupConnection() {
085        log.info("reconnected to Network after lost connection");
086        if (opened) {
087            this.getSystemConnectionMemo().getTrafficController().connectPort(this);
088        }
089    }
090
091    private final static Logger log = LoggerFactory.getLogger(NetworkDriverAdapter.class);
092
093}