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 pages/files received from a HTTP/FTP server
035: * during the period between the last event and this one.
036: * @author Alexandre Fenyo
037: * @version $Id: EventHTTPPages.java,v 1.1 2007/03/08 18:21:32 fenyo Exp $
038: */
039:
040: public class EventHTTPPages extends EventGeneric {
041: private static Log log = LogFactory.getLog(EventHTTPPages.class);
042:
043: private final long pages_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 pages_received pages received during the period between the last event and this one.
052: */
053: // Queue thread
054: public EventHTTPPages(final long pages_received) {
055: this .pages_received = pages_received;
056: }
057:
058: /**
059: * Returns the throughput in pages/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 EventHTTPPages prev_event = (EventHTTPPages) events
070: .get(idx - 1);
071:
072: if (getDate().getTime() - prev_event.getDate().getTime() == 0)
073: return 0;
074:
075: if (getPagesReceived() == cache_operand_1
076: && getDate().getTime() - prev_event.getDate().getTime() == cache_operand_2) {
077: return cache_result;
078: }
079:
080: // cast to double to avoid overflow
081: final int ret = (int) (1000 * (double) getPagesReceived() / (getDate()
082: .getTime() - prev_event.getDate().getTime()));
083:
084: cache_operand_1 = getPagesReceived();
085: cache_operand_2 = getDate().getTime()
086: - prev_event.getDate().getTime();
087: cache_result = ret;
088:
089: return ret;
090: }
091:
092: /**
093: * Returns the numeric value stored with this event.
094: * @param none.
095: * @return long numeric value.
096: */
097: // Queue & AWT thread
098: public long getPagesReceived() {
099: return pages_received;
100: }
101: }
|