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 EventHTTP store the amount of data received from a HTTP/FTP server
035: * during the period between the last event and this one.
036: * @author Alexandre Fenyo
037: * @version $Id: EventHTTP.java,v 1.1 2007/03/08 18:21:32 fenyo Exp $
038: */
039:
040: public class EventHTTP extends EventGeneric {
041: private static Log log = LogFactory.getLog(EventHTTP.class);
042:
043: private final long bytes_received;
044:
045: private long cache_operand_1 = 0;
046: private long cache_operand_2 = 0;
047: private int cache_result = 0;
048:
049: /**
050: * Constructor.
051: * @param bytes_received bytes received during the period between the last event and this one.
052: */
053: // Queue thread
054: public EventHTTP(final long bytes_received) {
055: this .bytes_received = bytes_received;
056: }
057:
058: /**
059: * Returns the throughput in bit/s at the moment of this event.
060: * @param events every event.
061: * @param idx index of this event.
062: * @return int throughput.
063: */
064: public int getIntValue(final java.util.List<EventGeneric> events,
065: final int idx) {
066: if (idx == 0)
067: return 0;
068:
069: final EventHTTP prev_event = (EventHTTP) events.get(idx - 1);
070:
071: if (getDate().getTime() - prev_event.getDate().getTime() == 0)
072: return 0;
073:
074: if (getBytesReceived() == cache_operand_1
075: && getDate().getTime() - prev_event.getDate().getTime() == cache_operand_2) {
076: return cache_result;
077: }
078:
079: // cast to double to avoid overflow
080: final int ret = (int) (8 * 1000 * (double) getBytesReceived() / (getDate()
081: .getTime() - prev_event.getDate().getTime()));
082:
083: cache_operand_1 = getBytesReceived();
084: cache_operand_2 = getDate().getTime()
085: - prev_event.getDate().getTime();
086: cache_result = ret;
087:
088: return ret;
089: }
090:
091: /**
092: * Returns the numeric value stored with this event.
093: * @param none.
094: * @return long numeric value.
095: */
096: // Queue & AWT thread
097: public long getBytesReceived() {
098: return bytes_received;
099: }
100: }
|