001package jmri.jmrix.jmriclient; 002 003import jmri.jmrix.AbstractMRListener; 004import jmri.jmrix.AbstractMRMessage; 005import jmri.jmrix.AbstractMRReply; 006import jmri.jmrix.AbstractMRTrafficController; 007 008/** 009 * Converts Stream-based I/O to/from JMRIClient messages. The 010 * "JMRIClientInterface" side sends/receives message objects. 011 * <p> 012 * The connection to a JMRIClientPortController is via a pair of *Streams, which 013 * then carry sequences of characters for transmission. Note that this 014 * processing is handled in an independent thread. 015 * <p> 016 * This handles the state transitions, based on the necessary state in each 017 * message. 018 * 019 * @author Bob Jacobsen Copyright (C) 2001 020 */ 021public class JMRIClientTrafficController extends AbstractMRTrafficController 022 implements JMRIClientInterface { 023 024 /** 025 * Create a new JMRIClientTrafficController instance. 026 */ 027 public JMRIClientTrafficController() { 028 super(); 029 setAllowUnexpectedReply(true); 030 } 031 032 // The methods to implement the JMRIClientInterface 033 @Override 034 public synchronized void addJMRIClientListener(JMRIClientListener l) { 035 this.addListener(l); 036 } 037 038 @Override 039 public synchronized void removeJMRIClientListener(JMRIClientListener l) { 040 this.removeListener(l); 041 } 042 043 /** 044 * Forward a JMRIClientMessage to all registered JMRIClientInterface 045 * listeners. 046 */ 047 @Override 048 protected void forwardMessage(AbstractMRListener client, AbstractMRMessage m) { 049 ((JMRIClientListener) client).message((JMRIClientMessage) m); 050 } 051 052 /** 053 * Forward a JMRIClientReply to all registered JMRIClientInterface 054 * listeners. 055 */ 056 @Override 057 protected void forwardReply(AbstractMRListener client, AbstractMRReply m) { 058 ((JMRIClientListener) client).reply((JMRIClientReply) m); 059 } 060 061 @Override 062 protected AbstractMRMessage pollMessage() { 063 return null; 064 } 065 066 @Override 067 protected AbstractMRListener pollReplyHandler() { 068 return null; 069 } 070 071 /** 072 * Forward a preformatted message to the actual interface. 073 */ 074 @Override 075 public void sendJMRIClientMessage(JMRIClientMessage m, JMRIClientListener reply) { 076 sendMessage(m, reply); 077 } 078 079 @Override 080 protected AbstractMRMessage enterProgMode() { 081 return JMRIClientMessage.getProgMode(); 082 } 083 084 @Override 085 protected AbstractMRMessage enterNormalMode() { 086 return JMRIClientMessage.getExitProgMode(); 087 } 088 089 @Override 090 protected AbstractMRReply newReply() { 091 return new JMRIClientReply(); 092 } 093 094 @Override 095 protected boolean endOfMessage(AbstractMRReply msg) { 096 int index = msg.getNumDataElements() - 1; 097 if (msg.getElement(index) == 0x0D) { 098 return true; 099 } 100 if (msg.getElement(index) == 0x0A) { 101 return true; 102 } else { 103 return false; 104 } 105 } 106}