001: /*
002: * GNetWatch
003: * Copyright 2006, 2007 Alexandre Fenyo
004: * gnetwatch@fenyo.net
005: *
006: * This file is part of GNetWatch.
007: *
008: * GNetWatch is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU General Public License as published by
010: * the Free Software Foundation; either version 2 of the License, or
011: * (at your option) any later version.
012: *
013: * GNetWatch is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with GNetWatch; if not, write to the Free Software
020: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
021: */
022:
023: package net.fenyo.gnetwatch.data;
024:
025: import net.fenyo.gnetwatch.*;
026: import net.fenyo.gnetwatch.targets.*;
027:
028: import java.util.*;
029:
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032:
033: /**
034: * Events of type EventsBytesExchanged store the amount of data exchanged
035: * during the period between the last event and this one.
036: * @author Alexandre Fenyo
037: * @version $Id: EventBytesExchanged.java,v 1.8 2007/03/03 00:38:20 fenyo Exp $
038: */
039:
040: public class EventBytesExchanged extends EventGeneric {
041: private static Log log = LogFactory
042: .getLog(EventBytesExchanged.class);
043:
044: private final long bytes_exchanged;
045:
046: private long cache_operand_1 = 0;
047: private long cache_operand_2 = 0;
048: private int cache_result = 0;
049:
050: /**
051: * Constructor.
052: * @param bytes_exchanged bytes exchanged during the period between the last event and this one.
053: */
054: // Queue thread
055: public EventBytesExchanged(final long bytes_exchanged) {
056: this .bytes_exchanged = bytes_exchanged;
057: }
058:
059: /**
060: * Returns the throughput in bit/s at the moment of this event.
061: * @param events every event.
062: * @param idx index of this event.
063: * @return int throughput.
064: */
065: public int getIntValue(final java.util.List<EventGeneric> events,
066: final int idx) {
067: if (idx == 0)
068: return 0;
069:
070: final EventBytesExchanged prev_event = (EventBytesExchanged) events
071: .get(idx - 1);
072:
073: if (getDate().getTime() - prev_event.getDate().getTime() == 0)
074: return 0;
075:
076: if (getBytesExchanged() == cache_operand_1
077: && getDate().getTime() - prev_event.getDate().getTime() == cache_operand_2) {
078: return cache_result;
079: }
080:
081: // cast to double to avoid overflow
082: final int ret = (int) (8 * 1000 * (double) getBytesExchanged() / (getDate()
083: .getTime() - prev_event.getDate().getTime()));
084:
085: cache_operand_1 = getBytesExchanged();
086: cache_operand_2 = getDate().getTime()
087: - prev_event.getDate().getTime();
088: cache_result = ret;
089:
090: return ret;
091: }
092:
093: /**
094: * Returns the numeric value stored with this event.
095: * @param none.
096: * @return long numeric value.
097: */
098: // Queue & AWT thread
099: public long getBytesExchanged() {
100: return bytes_exchanged;
101: }
102: }
|