001: /*
002: Copyright (C) 2004 David Bucciarelli (davibu@interfree.it)
003:
004: This program is free software; you can redistribute it and/or
005: modify it under the terms of the GNU General Public License
006: as published by the Free Software Foundation; either version 2
007: of the License, or (at your option) any later version.
008:
009: This program is distributed in the hope that it will be useful,
010: but WITHOUT ANY WARRANTY; without even the implied warranty of
011: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: GNU General Public License for more details.
013:
014: You should have received a copy of the GNU General Public License
015: along with this program; if not, write to the Free Software
016: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
017: */
018:
019: package org.homedns.dade.jcgrid;
020:
021: import java.io.*;
022:
023: public class WorkerStats implements Serializable {
024: private static final long serialVersionUID = 1L;
025:
026: public final static short STATUS_FREE = 0;
027: public final static short STATUS_BUSY = 1;
028:
029: private String name;
030:
031: private short status;
032: private String workingFor;
033: private long workingStarted;
034:
035: private long unitDone;
036: private double unitSec;
037:
038: public WorkerStats(String workerName) {
039: name = workerName;
040:
041: status = STATUS_FREE;
042: workingFor = "";
043: workingStarted = 0;
044:
045: unitDone = 0;
046: unitSec = 0.0;
047: }
048:
049: public synchronized String getName() {
050: return name;
051: }
052:
053: public synchronized short getStatus() {
054: return status;
055: }
056:
057: public synchronized String getWorkingFor() {
058: return workingFor;
059: }
060:
061: public synchronized long getWorkingStarted() {
062: return workingStarted;
063: }
064:
065: public synchronized long getUnitDone() {
066: return unitDone;
067: }
068:
069: public synchronized double getUnitSec() {
070: return unitSec;
071: }
072:
073: public synchronized void setStatusFree() {
074: status = STATUS_FREE;
075: workingFor = "";
076: workingStarted = 0;
077: }
078:
079: public synchronized void workBegin(String sessionName) {
080: status = STATUS_BUSY;
081: workingFor = sessionName;
082: workingStarted = System.currentTimeMillis();
083: }
084:
085: public synchronized void workEnd(WorkResult wr) {
086: long t = System.currentTimeMillis() - workingStarted + 1;
087:
088: this .setStatusFree();
089:
090: unitDone += wr.getUnitDone();
091:
092: if (unitDone == 0)
093: unitSec = 1000.0 * wr.getUnitDone() / t;
094: else {
095: double k = wr.getUnitDone() / (double) unitDone;
096: unitSec = unitSec * (1.0 - k)
097: + (1000.0 * wr.getUnitDone() / t) * k;
098: }
099: }
100: }
|