001package jmri.jmrix.cmri.serial.cmrinetmetrics;
002
003import jmri.jmrix.cmri.serial.SerialListener;
004import jmri.jmrix.cmri.serial.SerialMessage;
005import jmri.jmrix.cmri.serial.SerialReply;
006
007/**
008 * Listener class for collecting CMRInet network traffic and error messages.
009 *
010 * @author Chuck Catania  Copyright (C) 2016, 2017, 2018
011 */
012
013public class CMRInetMetricsCollector implements SerialListener {
014    
015    /**
016     * Collected data instance.
017     */
018    private CMRInetMetricsData _data = new CMRInetMetricsData();
019    
020    //public CMRInetMetricsCollector(CMRISystemConnectionMemo memo) { // memo unused?
021    //    super();
022    //}
023    
024    public CMRInetMetricsCollector() {
025
026        _data.clearAllErrMetrics();
027        _data.clearAllDataMetrics();
028        
029    }
030
031    protected void init() {
032        log.info("CMRInetMetricsCollector - init");
033    }
034    
035    /**
036     * Expose collected data.
037     * @return collected data
038     */
039    public  CMRInetMetricsData getMetricData() { return _data; }
040    
041
042    /**
043     * Transmit packets.
044     * Monitor any transmit packets to collect metrics.
045     */
046    @Override
047    public synchronized void message(SerialMessage l) 
048    { 
049       int aPacketTypeID = 0;
050
051       if (l.getNumDataElements() < 2)
052       {
053           _data.incMetricErrValue(CMRInetMetricsData.CMRInetMetricTruncRecv);
054           return;
055        } 
056       
057       aPacketTypeID = l.getElement(1);
058       switch(aPacketTypeID)
059       {                     // Packet Type
060           case 0x41:        // (A) Datagram Ack
061           case 0x43:        // (C) Code Line
062           case 0x44:        // (D) Datagram Read
063           case 0x45:        // (E) EOT
064           break;
065               
066           case 0x49:        // (I) Initialize 
067            _data.incMetricDataValue(CMRInetMetricsData.CMRInetMetricInitMsgs);
068           break;
069               
070           case 0x4D:        // (M) NMRA Mast
071           break;
072               
073           case 0x50:        // (P) Poll
074            _data.startPollIntervalTimer();
075           break;
076
077           case 0x51:        // (Q) Query
078           case 0x52:        // (R) Read
079           case 0x54:        // (T) Transmit
080           case 0x57:        // (W) Datagram Write
081           break;
082               
083           default: 
084            _data.incMetricErrValue(CMRInetMetricsData.CMRInetMetricUnrecCommand);
085       }
086    }
087    
088    /**
089     * Receive packets.
090     * Monitor any read (reply) packets to collect metrics.
091     */
092    @Override
093    public synchronized void reply(SerialReply l) 
094    { 
095       int aPacketTypeID = 0;
096
097       if (l.getNumDataElements() < 2)
098       {
099           _data.incMetricErrValue(CMRInetMetricsData.CMRInetMetricTruncReply);
100           return;
101        } 
102       
103       aPacketTypeID = l.getElement(1);
104       switch(aPacketTypeID)
105       {                     // Packet Type
106           case 0x41:        // (A) Datagram Ack
107           case 0x43:        // (C) Code Line
108           case 0x44:        // (D) Datagram Read
109           break;
110               
111           case 0x45:        // (E) EOT
112           case 0x52:        // (R) Read
113            _data.computePollInterval();
114           break;
115               
116           case 0x49:        // (I) Initialize 
117           case 0x4D:        // (M) NMRA Mast
118           case 0x50:        // (P) Poll
119           case 0x51:        // (Q) Query
120           break;
121
122           case 0x54:        // (T) Transmit
123           case 0x57:        // (W) Datagram Write
124           break;
125               
126           default: 
127            _data.incMetricErrValue(CMRInetMetricsData.CMRInetMetricUnrecCommand);
128       }
129    }
130
131    private final static org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CMRInetMetricsCollector.class.getName());
132
133}