001package jmri.jmrix.bidib.bidibovertcp;
002
003import java.io.DataInputStream;
004import java.io.DataOutputStream;
005import java.io.IOException;
006import java.util.Set;
007
008import jmri.jmrix.bidib.BiDiBNetworkPortController;
009import jmri.jmrix.bidib.BiDiBTrafficController;
010import org.bidib.jbidibc.core.MessageListener;
011import org.bidib.jbidibc.core.NodeListener;
012import org.bidib.jbidibc.core.node.listener.TransferListener;
013import org.bidib.jbidibc.messages.ConnectionListener;
014import org.bidib.jbidibc.net.serialovertcp.NetBidib;
015import org.bidib.jbidibc.messages.helpers.Context;
016
017import org.slf4j.Logger;
018import org.slf4j.LoggerFactory;
019
020/**
021 * Implements BiDiBPortController for the BiDiBOverTcp system network
022 * connection.
023 * <p>
024 * This connects a DCC++ via a telnet connection. Normally controlled by the
025 * DCCppTcpDriverFrame class.
026 * 
027 *
028 * @author Bob Jacobsen Copyright (C) 2001, 2002, 2003
029 * @author Alex Shepherd Copyright (C) 2003, 2006
030 * @author Mark Underwood Copyright (C) 2015
031 * @author Eckart Meyer Copyright (C) 2023
032 *
033 * Based on DCCppNetworkDriverAdapter.
034 */
035public class BiDiBOverTcpAdapter extends BiDiBNetworkPortController {
036
037    public BiDiBOverTcpAdapter() {
038        //super(new BiDiBSystemConnectionMemo());
039        setManufacturer(jmri.jmrix.bidib.BiDiBConnectionTypeList.BIDIB);
040    }
041    
042    @Override
043    public void connect(String host, int port) throws IOException {
044        setHostName(host);
045        setPort(port);
046        connect();
047    }
048
049    /**
050     * This methods is called from network connection config and creates the BiDiB object from jbidibc and opens it.
051     * The connectPort method of the traffic controller is called for generic initialisation.
052     * 
053     */
054    @Override
055    public void connect() {// throws IOException {
056        log.debug("connect() starts to {}:{}", getHostName(), getPort());
057        opened = false;
058        Context ctx = getContext();
059        log.debug("Context: {}", ctx);
060        bidib = NetBidib.createInstance(getContext());
061        BiDiBTrafficController tc = new BiDiBTrafficController(bidib);
062        context = tc.connnectPort(this); //must be done before configuring managers since they may need features from the device
063        log.debug("memo: {}, bidib over TCP: {}", this.getSystemConnectionMemo(), bidib);
064        this.getSystemConnectionMemo().setBiDiBTrafficController(tc);
065        if (context != null) {
066            opened = true;
067        }
068        else {
069            opened = false;
070            log.warn("No device found on port {} ({}})",
071                    getCurrentPortName(), getCurrentPortName());
072        }
073    }
074
075    
076    @Override
077    public void configure() {
078        log.debug("configure");
079        this.getSystemConnectionMemo().configureManagers();
080    }
081
082    /**
083     * {@inheritDoc}
084     */
085    @Override
086    public void registerAllListeners(ConnectionListener connectionListener, Set<NodeListener> nodeListeners,
087                Set<MessageListener> messageListeners, Set<TransferListener> transferListeners) {
088        
089        NetBidib b = (NetBidib)bidib;
090        b.setConnectionListener(connectionListener);
091        b.registerListeners(nodeListeners, messageListeners, transferListeners);
092    }
093    
094    // base class methods for the BiDiBNetworkPortController interface
095    // not used but must be implemented
096
097    @Override
098    public DataInputStream getInputStream() {
099        return null;
100    }
101
102    @Override
103    public DataOutputStream getOutputStream() {
104        return null;
105    }
106
107
108    private final static Logger log = LoggerFactory.getLogger(BiDiBOverTcpAdapter.class);
109
110    
111}