View Javadoc
1   package info.mikethomas.fahview.v6project.model;
2   
3   /*
4    * #%L
5    * This file is part of FAHView-v6project.
6    * %%
7    * Copyright (C) 2011 - 2017 Mike Thomas <mikepthomas@outlook.com>
8    * %%
9    * FAHView is free software: you can redistribute it and/or modify
10   * it under the terms of the GNU General Public License as published by
11   * the Free Software Foundation, either version 3 of the License, or
12   * (at your option) any later version.
13   * %
14   * FAHView is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17   * GNU General Public License for more details.
18   * %
19   * You should have received a copy of the GNU General Public License
20   * along with FAHView.  If not, see <http://www.gnu.org/licenses/>.
21   * #L%
22   */
23  
24  import info.mikethomas.fahview.v6project.utilities.QueueReader;
25  import java.util.logging.Level;
26  import java.util.logging.Logger;
27  
28  /**
29   * Class to represent data stored about a Folding@home queue.
30   *
31   * @author <a href="mailto:mikepthomas@outlook.com">Michael Thomas</a>
32   * @version 6.00
33   */
34  public class QueueImpl implements Queue {
35  
36      private int version; // QueueImpl (client) version (as of v2.17)
37      private int current; // Current index number
38      private QueueIndexImpl[] index; // QueueImpl Indices
39      private int pfract; // Performance fraction (as of v3.24)
40      private int punits; // Performance fraction unit weight (as of v3.24)
41      private int drate; // Download rate sliding average (as of v4.00)
42      private int dunits; // Download rate unit weight (as of v4.00)
43      private int urate; // Upload rate sliding average (as of v4.00)
44      private int uunits; // Upload rate unit weight (as of v4.00)
45      private int sent; // Results successfully sent (after upload failures) (as of v5.00)
46      private byte[] z7156; // All zeros after queue conversion (as of v5.00)
47      
48      private QueueReader reader;
49  
50      /**
51       * Constructor to initialize variables
52       *
53       * @param reader QueueReader for {@code queue.dat}
54       */
55      public QueueImpl(QueueReader reader) {
56          this.reader = reader;
57          update();
58      }
59  
60      /** {@inheritDoc} */
61      @Override
62      public int getVersion() {
63          return version;
64      }
65  
66      /** {@inheritDoc} */
67      @Override
68      public int getCurrent() {
69          return current;
70      }
71  
72      /** {@inheritDoc} */
73      @Override
74      public QueueIndexImpl getQueueIndex(int index) {
75          return this.index[index];
76      }
77  
78      /** {@inheritDoc} */
79      @Override
80      public int getPfract() {
81          return pfract;
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public int getPunits() {
87          return punits;
88      }
89  
90      /** {@inheritDoc} */
91      @Override
92      public int getDrate() {
93          return drate;
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public int getDunits() {
99          return dunits;
100     }
101 
102     /** {@inheritDoc} */
103     @Override
104     public int getUrate() {
105         return urate;
106     }
107 
108     /** {@inheritDoc} */
109     @Override
110     public int getUunits() {
111         return uunits;
112     }
113 
114     /** {@inheritDoc} */
115     @Override
116     public int getSent() {
117         return sent;
118     }
119 
120     /**
121      * Set the value of QueueImpl (client) version
122      */
123     protected void setVersion() {
124         version = (int) reader.readLEUInt(VERSION_POS, VERSION_LENGTH);
125     }
126 
127     /**
128      * Set the value of Current index number
129      */
130     protected void setCurrent() {
131         current = (int) reader.readLEUInt(CURRENT_POS, CURRENT_LENGTH);
132     }
133     
134     /**
135      * Set the QueueImpl Indices
136      */
137     protected void setIndex() {
138         try {
139             index = new QueueIndexImpl[10];
140             index[0] = new QueueIndexImpl(0, reader);
141             index[1] = new QueueIndexImpl(1, reader);
142             index[2] = new QueueIndexImpl(2, reader);
143             index[3] = new QueueIndexImpl(3, reader);
144             index[4] = new QueueIndexImpl(4, reader);
145             index[5] = new QueueIndexImpl(5, reader);
146             index[6] = new QueueIndexImpl(6, reader);
147             index[7] = new QueueIndexImpl(7, reader);
148             index[8] = new QueueIndexImpl(8, reader);
149             index[9] = new QueueIndexImpl(9, reader);
150         }
151         catch (InstantiationException ex) {
152             Logger.getLogger(QueueImpl.class.getName()).log(Level.SEVERE, null, ex);
153         }
154     }
155 
156     /**
157      * Set the value of Performance fraction
158      */
159     protected void setPfract() {
160         pfract = (int) reader.readLEUInt(PFRACT_POS, PFRACT_LENGTH);
161     }
162 
163     /**
164      * Set the value of Performance fraction unit weight
165      */
166     protected void setPunits() {
167         punits = (int) reader.readLEUInt(PUNITS_POS, PUNITS_LENGTH);
168     }
169 
170     /**
171      * Set the value of Download rate sliding average
172      */
173     protected void setDrate() {
174         drate = (int) reader.readLEUInt(DRATE_POS, DRATE_LENGTH);
175     }
176 
177     /**
178      * Set the value of Download rate unit weight
179      */
180     protected void setDunits() {
181         dunits = (int) reader.readLEUInt(DUNITS_POS, DUNITS_LENGTH);
182     }
183 
184     /**
185      * Set the value of Upload rate sliding average
186      */
187     protected void setUrate() {
188         urate = (int) reader.readLEUInt(URATE_POS, URATE_LENGTH);
189     }
190 
191     /**
192      * Set the value of Upload rate unit weight
193      */
194     protected void setUunits() {
195         uunits = (int) reader.readLEUInt(UUNITS_POS, UUNITS_LENGTH);
196     }
197 
198     /**
199      * Set the value of Results Sent
200      */
201     protected void setSent() {
202         sent = (int) reader.readLEUInt(SENT_POS, SENT_LENGTH);
203     }
204 
205     /**
206      * Set the value of z7156
207      */
208     protected void setZ7156() {
209         z7156 = reader.read(Z7156_POS, Z7156_LENGTH);
210         boolean notZero = false;
211         for (int i = 0; i < z7156.length; i++) {
212             if (z7156[i] != 0) {
213                 notZero = true;
214             }
215         }
216         if (notZero) {
217             System.err.println("WARNING: Z7156 is not all zero");
218         }
219     }
220     
221     /**
222      * {@inheritDoc}
223      *
224      * Generate a String representation of the QueueImpl
225      */
226     @Override
227     public String toString() {
228         String result = "";
229         result += "queue.version\t" + getVersion();
230         result += "\nqueue.current\t" + getCurrent();
231         result += "\n" + getQueueIndex(0);
232         result += "\n" + getQueueIndex(1);
233         result += "\n" + getQueueIndex(2);
234         result += "\n" + getQueueIndex(3);
235         result += "\n" + getQueueIndex(4);
236         result += "\n" + getQueueIndex(5);
237         result += "\n" + getQueueIndex(6);
238         result += "\n" + getQueueIndex(7);
239         result += "\n" + getQueueIndex(8);
240         result += "\n" + getQueueIndex(9);
241         result += "\nqueue.pfract\t" + getPfract();
242         result += "\nqueue.punits\t" + getPunits();
243         result += "\nqueue.drate\t" + getDrate();
244         result += "\nqueue.dunits\t" + getDunits();
245         result += "\nqueue.urate\t" + getUrate();
246         result += "\nqueue.uunits\t" + getUunits();
247         result += "\nqueue.sent\t" + getSent();
248         return result;
249     }
250     
251     /** {@inheritDoc} */
252     @Override
253     public final void update() {
254         setVersion();
255         setCurrent();
256         setIndex();
257         setPfract();
258         setPunits();
259         setDrate();
260         setDunits();
261         setUrate();
262         setUunits();
263         setSent();
264         setZ7156();
265     }
266 }