001package jmri.jmrix.cmri.serial.cmrinetmetrics;
002
003/**
004 * CMRInet metric data variables and access methods.
005 * The metric data is not persistent between runs.
006 * 
007 * @author Chuck Catania  Copyright (C) 2016, 2018
008 */
009    public class CMRInetMetricsData
010    {
011        /**
012         * CMRInet packet types
013         */
014        public static int _DGACK    = 0x41;
015        public static int _CODELINE = 0x43;
016        public static int _DGREAD   = 0x44;
017        public static int _EOT      = 0x45;
018        public static int _INIT     = 0x49;
019        public static int _POLL     = 0x50;
020        public static int _QUERY    = 0x51;
021        public static int _READ     = 0x52;
022        public static int _TRANSMIT = 0x54;
023        public static int _DGWRITE  = 0x57;
024        
025        /**
026         * Data items for network error counts
027         */
028        //-------------------------------------------------------------------
029        public static final String[] CMRInetMetricErrName = {
030                                                    "Timeout",
031                                                    "Truncated Receive",
032                                                    "Truncated Reply",
033                                                    "Unrecognized Response",
034                                                    "Unrecognized Command"
035                                                    };
036        
037        public final static int CMRInetMetricTimeout       = 0;
038        public final static int CMRInetMetricTruncRecv     = 1;
039        public final static int CMRInetMetricTruncReply    = 2;
040        public final static int CMRInetMetricUnrecResponse = 3;
041        public final static int CMRInetMetricUnrecCommand  = 4;
042        public final static int CMRInetMetricErrLAST       = CMRInetMetricUnrecCommand+1;
043    
044        public int CMRInetMetricErrCount[]           = {
045                                                     0,0, 
046                                                     0,0, 
047                                                     0,0
048                                                    };
049       
050        /**
051         * Data array for network data counts
052         */
053        public static final String[] CMRInetMetricDataName = {
054                                                    "Poll/Response Time (ms)",
055                                                    "Init Messages",
056                                                   };
057        
058        //-------------------------------------------------------------------
059        public static int CMRInetMetricPollResponse  = 0;
060        public static int CMRInetMetricInitMsgs      = 1;
061        public static int CMRInetMetricDataLAST      = CMRInetMetricInitMsgs+1;
062        
063        public int CMRInetMetricDataCount[] = {0,0};
064        
065        
066        /**
067         * Variables used for poll/response measurements
068         * 
069         */
070        public long pollTicks;
071        public int  pollIntervalMS;
072        public int  pollCnt;
073        public int  pollCntMax = 10;
074    
075        public CMRInetMetricsData() {   
076            super();
077        }
078        
079        /**
080         * Methods for Metric Error data.
081         * 
082         */
083        
084        /**
085         * Get the error count.
086         * 
087         * @param metricName metric index.
088         * @return error count.
089         */
090        synchronized public int getMetricErrValue( int metricName ){
091            return CMRInetMetricErrCount[metricName];
092        }
093        // Set the error count
094        synchronized public void setMetricErrValue( int metricName, int value ){
095            CMRInetMetricErrCount[metricName] = value;
096        }
097        // Increment the error count
098        synchronized public void incMetricErrValue( int metricName ){
099            CMRInetMetricErrCount[metricName]++;
100        }
101        // Zero the particular error count
102        synchronized public void zeroMetricErrValue( int metricName ){
103            CMRInetMetricErrCount[metricName] = 0;
104        }
105        // Zero the all of the error counts
106        synchronized public void clearAllErrMetrics(){
107            for (int i=0; i!=CMRInetMetricErrLAST; i++)
108             CMRInetMetricErrCount[i] = 0;
109        }
110        // Get Error Count
111        synchronized public int getMetricErrorCount(int metricName){
112             return CMRInetMetricErrCount[metricName];
113        }
114       
115        /**
116         * Methods for Metric data
117         */
118        
119        /**
120         * Get the metric value.
121         * @param metricName index.
122         * @return data value.
123         */
124        synchronized public int getMetricDataValue( int metricName ){
125            return CMRInetMetricDataCount[metricName];
126        }
127        // Set the metric value
128        synchronized public void setMetricDataValue( int metricName, int value ){
129            CMRInetMetricDataCount[metricName] = value;
130        }
131        // Increment the metric value
132        synchronized public void incMetricDataValue( int metricName ){
133            CMRInetMetricDataCount[metricName]++;
134        }
135        // Zero the particular metric value
136        synchronized public void zeroMetricDataValue( int metricName ){
137            CMRInetMetricDataCount[metricName] = 0;
138        }
139        // Set the metric error count
140        synchronized public void setMetricErrorValue( int metricName, int value ){
141            CMRInetMetricErrCount[metricName] = value;
142        }
143        // Zero the all of the metric values
144        synchronized public void clearAllDataMetrics(){
145            for (int i=0; i!=CMRInetMetricDataLAST; i++)
146             CMRInetMetricDataCount[i] = 0;
147            
148            pollTicks = 0;
149            pollIntervalMS = 0;
150            pollCnt = 0;
151        }
152
153        /**
154         * Methods to manage the poll/response metric
155         * Start the poll timer when a poll message is seen
156         * 
157         */
158        public void startPollIntervalTimer()
159        {
160            pollTicks = System.currentTimeMillis();
161        }
162        
163        /**
164         * Compute the poll/reply interval in milliseconds
165         * Average over pollCnt polls to keep the Tablemodel update rate low
166         * 
167         */
168        public void computePollInterval()
169        {
170            long curTicks = System.currentTimeMillis();
171
172            if (pollCnt++ <= pollCntMax)
173            {
174                if (pollTicks == 0) pollTicks = curTicks;
175                pollIntervalMS = pollIntervalMS + (int)(curTicks-pollTicks);
176                pollTicks = curTicks;
177            }
178            else
179            {
180                pollIntervalMS = pollIntervalMS/pollCntMax;
181                CMRInetMetricDataCount[CMRInetMetricPollResponse] = pollIntervalMS;
182                pollIntervalMS = 0;
183                pollCnt = 0;
184            }
185        }        
186  }
187