KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > fenyo > gnetwatch > data > EventHTTPPages


1
2 /*
3  * GNetWatch
4  * Copyright 2006, 2007 Alexandre Fenyo
5  * gnetwatch@fenyo.net
6  *
7  * This file is part of GNetWatch.
8  *
9  * GNetWatch 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 2 of the License, or
12  * (at your option) any later version.
13  *
14  * GNetWatch 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 GNetWatch; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22  */

23
24 package net.fenyo.gnetwatch.data;
25
26 import net.fenyo.gnetwatch.*;
27 import net.fenyo.gnetwatch.targets.*;
28
29 import java.util.*;
30
31 import org.apache.commons.logging.Log;
32 import org.apache.commons.logging.LogFactory;
33
34 /**
35  * Events of type EventHTTP store the amount of pages/files received from a HTTP/FTP server
36  * during the period between the last event and this one.
37  * @author Alexandre Fenyo
38  * @version $Id: EventHTTPPages.java,v 1.1 2007/03/08 18:21:32 fenyo Exp $
39  */

40
41 public class EventHTTPPages extends EventGeneric {
42   private static Log log = LogFactory.getLog(EventHTTPPages.class);
43
44   private final long pages_received;
45
46   private long cache_operand_1 = 0;
47   private long cache_operand_2 = 0;
48   private int cache_result = 0;
49
50   /**
51    * Constructor.
52    * @param pages_received pages received during the period between the last event and this one.
53    */

54   // Queue thread
55
public EventHTTPPages(final long pages_received) {
56     this.pages_received = pages_received;
57   }
58
59   /**
60    * Returns the throughput in pages/s at the moment of this event.
61    * @param events every event.
62    * @param idx index of this event.
63    * @return int throughput.
64    */

65   public int getIntValue(final java.util.List JavaDoc<EventGeneric> events, final int idx) {
66     if (idx == 0) return 0;
67
68     final EventHTTPPages prev_event = (EventHTTPPages) events.get(idx - 1);
69
70     if (getDate().getTime() - prev_event.getDate().getTime() == 0)
71       return 0;
72
73     if (getPagesReceived() == cache_operand_1 &&
74         getDate().getTime() - prev_event.getDate().getTime() == cache_operand_2) {
75       return cache_result;
76     }
77
78     // cast to double to avoid overflow
79
final int ret = (int) (1000 * (double) getPagesReceived() /
80         (getDate().getTime() - prev_event.getDate().getTime()));
81
82     cache_operand_1 = getPagesReceived();
83     cache_operand_2 = getDate().getTime() - prev_event.getDate().getTime();
84     cache_result = ret;
85
86     return ret;
87   }
88
89   /**
90    * Returns the numeric value stored with this event.
91    * @param none.
92    * @return long numeric value.
93    */

94   // Queue & AWT thread
95
public long getPagesReceived() {
96     return pages_received;
97   }
98 }
99
Popular Tags