001package jmri.jmrix.jmriclient.networkdriver;
002
003import java.util.ResourceBundle;
004import jmri.jmrix.jmriclient.JMRIClientPortController;
005import jmri.jmrix.jmriclient.JMRIClientTrafficController;
006import jmri.util.zeroconf.ZeroConfClient;
007import org.slf4j.Logger;
008import org.slf4j.LoggerFactory;
009
010/**
011 * Implements NetworkPortAdapter for the jmriclient system network connection.
012 * <p>
013 * This connects a JMRI Simple Server (daemon) via a telnet connection.
014 *
015 * @author Paul Bender Copyright (C) 2010
016 */
017public class NetworkDriverAdapter extends JMRIClientPortController {
018
019    static final ResourceBundle rb = ResourceBundle.getBundle("jmri.jmrix.jmriclient.JMRIClientConfigurationBundle");
020
021    public NetworkDriverAdapter() {
022        super(new jmri.jmrix.jmriclient.JMRIClientSystemConnectionMemo());
023        setPort(2048); // set the default port on construction
024    }
025
026    /**
027     * set up all of the other objects to operate with an JMRI Simple server connected
028     * to this port
029     */
030    @Override
031    public void configure() {
032        // connect to the traffic controller
033        JMRIClientTrafficController control = new JMRIClientTrafficController();
034        control.connectPort(this);
035        this.getSystemConnectionMemo().setJMRIClientTrafficController(control);
036        this.getSystemConnectionMemo().configureManagers();
037    }
038
039    @Override
040    public boolean status() {
041        return opened;
042    }
043
044    // private control members
045    private boolean opened = false;
046
047    private boolean mDNSConfigure = false;
048
049    /*
050     * Set whether or not this adapter should be
051     * configured automatically via MDNS.
052     * @param autoconfig boolean value.
053     */
054    @Override
055    public void setMdnsConfigure(boolean autoconfig) {
056        log.debug("Setting LIUSB Ethernet adapter autoconfiguration to: {}", autoconfig);
057        mDNSConfigure = autoconfig;
058    }
059
060    /*
061     * Get whether or not this adapter is configured
062     * to use autoconfiguration via MDNS
063     * @return true if configured using MDNS.
064     */
065    @Override
066    public boolean getMdnsConfigure() {
067        return mDNSConfigure;
068    }
069
070    /*
071     * set the server's host name and port
072     * using mdns autoconfiguration.
073     */
074    @Override
075    public void autoConfigure() {
076        log.info("Configuring JMRIClient interface via JmDNS");
077        if (getHostName().equals(rb.getString("defaultMDNSServerName"))) {
078            setHostName(""); // reset the hostname to none.
079        }
080        String serviceType = rb.getString("defaultMDNSServiceType");
081        log.debug("Listening for service: {}", serviceType);
082
083        if (mdnsClient == null) {
084            mdnsClient = new ZeroConfClient();
085            mdnsClient.startServiceListener(serviceType);
086        }
087        try {
088            // if there is a hostname set, use the host name (which can
089            // be changed) to find the service.
090            String qualifiedHostName = m_HostName
091                    + "." + rb.getString("defaultMDNSDomainName");
092            setHostAddress(mdnsClient.getServiceOnHost(serviceType,
093                    qualifiedHostName).getHostAddresses()[0]);
094        } catch (java.lang.NullPointerException npe) {
095            // if there is no hostname set, use the service name (which can't
096            // be changed) to find the service.
097            String qualifiedServiceName = rb.getString("defaultMDNSServiceName")
098                    + "." + serviceType;
099            setHostAddress(mdnsClient.getServicebyAdName(serviceType,
100                    qualifiedServiceName).getHostAddresses()[0]);
101        }
102    }
103    ZeroConfClient mdnsClient = null;
104
105    /*
106     * Get the ZeroConf/mDNS advertisement name.
107     * this value is fixed on the LIUSB-Ethernet, so return the default
108     * value.
109     */
110    @Override
111    public String getAdvertisementName() {
112        return rb.getString("defaultMDNSServiceName");
113    }
114
115    /*
116     * Get the ZeroConf/mDNS service type.
117     * this value is fixed on the LIUSB-Ethernet, so return the default
118     * value.
119     */
120    @Override
121    public String getServiceType() {
122        return rb.getString("defaultMDNSServiceType");
123    }
124
125    private final static Logger log = LoggerFactory.getLogger(NetworkDriverAdapter.class);
126
127}