001package jmri.jmrix.marklin.networkdriver;
002
003import java.io.DataInputStream;
004import java.io.DataOutputStream;
005
006import jmri.jmrix.ConnectionStatus;
007import jmri.jmrix.marklin.MarklinPortController;
008import jmri.jmrix.marklin.MarklinSystemConnectionMemo;
009import jmri.jmrix.marklin.MarklinTrafficController;
010import jmri.util.com.rbnb.UDPInputStream;
011import jmri.util.com.rbnb.UDPOutputStream;
012
013/**
014 * Implements NetworkPortAdapter for the Marklin system network connection.
015 * <p>
016 * This connects a Marklin command station via a UDP connection. Normally
017 * controlled by the NetworkDriverFrame class.
018 *
019 * @author Bob Jacobsen Copyright (C) 2001, 2002, 2003, 2008
020 * @author Kevin Dickerson Copyright (C) 2012
021 */
022public class NetworkDriverAdapter extends MarklinPortController {
023
024    public NetworkDriverAdapter() {
025        super(new MarklinSystemConnectionMemo());
026        allowConnectionRecovery = true;
027        manufacturerName = jmri.jmrix.marklin.MarklinConnectionTypeList.MARKLIN;
028        m_port = 15731;
029    }
030
031    @Override //ports are fixed and not user set
032    public void setPort(int p) {
033    }
034
035    @Override //ports are fixed and not user set
036    public void setPort(String p) {
037    }
038
039    @Override
040    public void connect() {
041        opened = false;
042
043        if (m_HostName == null) {
044            log.error("No host name or port set :{}:{}", m_HostName, m_port);
045            return;
046        }
047        try {
048            opened = true;
049        } catch (Exception e) {
050            log.error("a error opening network connection", e);
051            if (m_port != 0) {
052                ConnectionStatus.instance().setConnectionState(
053                        null, m_HostName + ":" + m_port, ConnectionStatus.CONNECTION_DOWN);
054            } else {
055                ConnectionStatus.instance().setConnectionState(
056                        null, m_HostName, ConnectionStatus.CONNECTION_DOWN);
057            }
058            throw (e);
059        }
060        if (opened && m_port != 0) {
061            ConnectionStatus.instance().setConnectionState(
062                    null, m_HostName + ":" + m_port, ConnectionStatus.CONNECTION_UP);
063        } else if (opened) {
064            ConnectionStatus.instance().setConnectionState(
065                    null, m_HostName, ConnectionStatus.CONNECTION_UP);
066        }
067    }
068
069    @Override
070    public DataInputStream getInputStream() {
071        if (!opened) {
072            log.error("getInputStream called before load(), stream not available");
073            if (m_port != 0) {
074                ConnectionStatus.instance().setConnectionState(
075                        null, m_HostName + ":" + m_port, ConnectionStatus.CONNECTION_DOWN);
076            } else {
077                ConnectionStatus.instance().setConnectionState(
078                        null, m_HostName, ConnectionStatus.CONNECTION_DOWN);
079            }
080        }
081        try {
082            return new DataInputStream(new UDPInputStream(null, 15730));
083        } catch (java.io.IOException ex1) {
084            log.error("an Exception getting input stream", ex1);
085            return null;
086        }
087    }
088
089    @Override
090    public DataOutputStream getOutputStream() {
091        if (!opened) {
092            log.error("getOutputStream called before load(), stream not available");
093        }
094        try {
095            return new DataOutputStream(new UDPOutputStream(m_HostName, 15731));
096        } catch (java.io.IOException e) {
097            log.error("getOutputStream exception", e);
098            if (m_port != 0) {
099                ConnectionStatus.instance().setConnectionState(
100                        null, m_HostName + ":" + m_port, ConnectionStatus.CONNECTION_DOWN);
101            } else {
102                ConnectionStatus.instance().setConnectionState(
103                        null, m_HostName, ConnectionStatus.CONNECTION_DOWN);
104            }
105        }
106        return null;
107    }
108
109    /**
110     * Set up all of the other objects to operate with a Marklin command station
111     * connected to this port.
112     */
113    @Override
114    public void configure() {
115        // connect to the traffic controller
116        MarklinTrafficController control = new MarklinTrafficController();
117        control.connectPort(this);
118        control.setAdapterMemo(this.getSystemConnectionMemo());
119        this.getSystemConnectionMemo().setMarklinTrafficController(control);
120        this.getSystemConnectionMemo().configureManagers();
121    }
122
123    @Override
124    public boolean status() {
125        return opened;
126    }
127
128    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(NetworkDriverAdapter.class);
129
130}