001package jmri.jmrix.cmri.serial.sim;
002
003import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
004import java.io.DataInputStream;
005import java.io.DataOutputStream;
006import java.io.PipedInputStream;
007
008import jmri.jmrix.cmri.CMRISystemConnectionMemo;
009import jmri.jmrix.cmri.serial.SerialTrafficController;
010import jmri.util.ImmediatePipedOutputStream;
011
012/**
013 * Extends the serialdriver.SimDriverAdapter class to act as simulated
014 * connection.
015 *
016 * @author Bob Jacobsen Copyright (C) 2002, 2008, 2011
017 */
018@SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD",
019        justification = "Access to 'self' OK until multiple instance pattern installed")
020public class SimDriverAdapter extends jmri.jmrix.cmri.serial.serialdriver.SerialDriverAdapter {
021
022    @Override
023    public String openPort(String portName, String appName) {
024            // don't even try to get port
025
026        opened = true;
027
028        return null; // normal operation
029    }
030
031    /**
032     * Can the port accept additional characters? Yes, always
033     */
034    @Override
035    public boolean okToSend() {
036        return true;
037    }
038
039    /**
040     * set up all of the other objects to operate connected to this port
041     */
042    @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD",
043            justification = "Access to 'self' OK until multiple instance pattern installed")
044
045    @Override
046    public void configure() {
047        // install a traffic controller that doesn't time out
048        SerialTrafficController tc = new SerialTrafficController() {
049            // timeout doesn't do anything
050            @SuppressFBWarnings(value = "ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD",
051                    justification = "only until multi-connect update done")
052            @Override
053            protected void handleTimeout(jmri.jmrix.AbstractMRMessage m, jmri.jmrix.AbstractMRListener l) {
054            }
055        };
056
057        // connect to the traffic controller
058        tc.connectPort(this);
059        ((CMRISystemConnectionMemo)getSystemConnectionMemo()).setTrafficController(tc);
060        ((CMRISystemConnectionMemo)getSystemConnectionMemo()).configureManagers();
061
062    }
063
064    // base class methods for the SerialPortController interface
065    @Override
066    public DataInputStream getInputStream() {
067        try {
068            return new DataInputStream(new PipedInputStream(new ImmediatePipedOutputStream()));
069        } catch (Exception e) {
070            return null;
071        }
072    }
073
074    @Override
075    public DataOutputStream getOutputStream() {
076        return new DataOutputStream(new java.io.OutputStream() {
077            @Override
078            public void write(int b) throws java.io.IOException {
079            }
080        });
081    }
082
083    @Override
084    public boolean status() {
085        return opened;
086    }
087
088    @Override
089    public String getCurrentPortName(){
090       return "";
091    }
092
093    // private control members
094    private boolean opened = false;
095
096}