001package jmri.jmrix.can.cbus.simulator; 002 003import javax.annotation.OverridingMethodsMustInvokeSuper; 004import jmri.jmrix.AbstractMessage; 005import jmri.jmrix.can.CanMessage; 006import jmri.jmrix.can.CanReply; 007import jmri.jmrix.can.CanSystemConnectionMemo; 008import jmri.jmrix.can.cbus.CbusSend; 009 010/** 011 * Simulating event request responses. 012 * 013 * @author Steve Young Copyright (C) 2018 014 * @see CbusSimulator 015 * @since 4.15.2 016 */ 017public class CbusSimCanListener extends jmri.jmrix.can.cbus.node.CbusNodeCanListener { 018 019 private int _networkDelay; 020 private boolean _processIn; 021 private boolean _processOut; 022 private boolean _sendIn; 023 private boolean _sendOut; 024 public CbusSend send; 025 026 /** 027 * Create a CanListener with Common Simulation setting attributes. 028 * @param memo System Connection 029 * @param node Node ( if a CbusDummyNode ), else use null 030 */ 031 public CbusSimCanListener( CanSystemConnectionMemo memo, jmri.jmrix.can.cbus.node.CbusNode node ){ 032 super(memo,node); 033 send = new CbusSend(memo); 034 _processIn=false; 035 _processOut=true; 036 _sendIn=true; 037 _sendOut=false; 038 _networkDelay = 50; 039 } 040 041 /** 042 * Set the simulated network delay. 043 * @param delay Delay in ms 044 */ 045 public final void setDelay( int delay){ 046 _networkDelay = delay; 047 } 048 049 /** 050 * Get the simulated network delay. 051 * Defaults to 50ms 052 * @return delay in ms 053 */ 054 public final int getDelay(){ 055 return _networkDelay; 056 } 057 058 /** 059 * Set if to Listen for CanReply Frames incoming to JMRI. 060 * @param newval true to listen, else false 061 */ 062 public final void setProcessIn( boolean newval){ 063 _processIn = newval; 064 } 065 066 /** 067 * Set if to Listen for CanMessage Frames outgoing to JMRI. 068 * @param newval true to listen, else false 069 */ 070 public final void setProcessOut( boolean newval){ 071 _processOut = newval; 072 } 073 074 /** 075 * Set if to Send Frames from the Sim as Incoming CanReply. 076 * @param newval true to send as incoming 077 */ 078 public final void setSendIn( boolean newval){ 079 _sendIn = newval; 080 } 081 082 /** 083 * Set if to Send Frames from the Sim as Outgoing CanMessage. 084 * @param newval true to send as outgoing 085 */ 086 public final void setSendOut( boolean newval){ 087 _sendOut = newval; 088 } 089 090 /** 091 * Get if to Listen for CanReply Frames incoming to JMRI. 092 * Defaults to false 093 * @return true to listen, else false 094 */ 095 public final boolean getProcessIn() { 096 return _processIn; 097 } 098 099 /** 100 * Get if to Listen for CanMessage Frames outgoing to JMRI. 101 * Defaults to true 102 * @return true to listen, else false 103 */ 104 public final boolean getProcessOut() { 105 return _processOut; 106 } 107 108 /** 109 * Get if to Send Frames from the Sim as Incoming CanReply. 110 * Defaults to true 111 * @return true to send as incoming 112 */ 113 public final boolean getSendIn() { 114 return _sendIn; 115 } 116 117 /** 118 * Get if to Send Frames from the Sim as Outgoing CanMessage. 119 * Defaults to false 120 * @return true to send as outgoing 121 */ 122 public final boolean getSendOut() { 123 return _sendOut; 124 } 125 126 /** 127 * Method to be overridden by extending methods. 128 * @param m CanFrame or CanReply to process 129 */ 130 protected void startProcessFrame(AbstractMessage m){} 131 132 /** 133 * Forwards non-extended CanMessage according to #getProcessOut 134 * {@inheritDoc} 135 */ 136 @Override 137 public final void message(CanMessage m) { 138 if ( !m.extendedOrRtr() && _processOut ) { 139 startProcessFrame(m); 140 } 141 } 142 143 /** 144 * Forwards non-extended CanReply according to #getProcessIn 145 * {@inheritDoc} 146 */ 147 @Override 148 public final void reply(CanReply r) { 149 if ( !r.extendedOrRtr() && _processIn ) { 150 startProcessFrame(r); 151 } 152 } 153 154 /** 155 * {@inheritDoc} 156 */ 157 @OverridingMethodsMustInvokeSuper 158 @Override 159 public void dispose(){ 160 removeTc(memo); 161 send = null; 162 } 163 164}