KickJava   Java API By Example, From Geeks To Geeks.

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


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 EventFlood store the amount of data flooded to a target
36  * during the period between the last event and this one.
37  * @author Alexandre Fenyo
38  * @version $Id: EventFlood.java,v 1.7 2007/03/08 18:21:32 fenyo Exp $
39  */

40
41 public class EventFlood extends EventGeneric {
42   private static Log log = LogFactory.getLog(EventFlood.class);
43
44   private final long bytes_sent;
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 bytes_sent bytes flooded during the period between the last event and this one.
53    */

54   // Queue thread
55
public EventFlood(final long bytes_sent) {
56     this.bytes_sent = bytes_sent;
57   }
58
59   /**
60    * Returns the throughput in bit/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 EventFlood prev_event = (EventFlood) events.get(idx - 1);
69
70     if (getDate().getTime() - prev_event.getDate().getTime() == 0)
71       return 0;
72
73     if (getBytesSent() == 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) (8 * 1000 * (double) getBytesSent() /
80         (getDate().getTime() - prev_event.getDate().getTime()));
81
82     cache_operand_1 = getBytesSent();
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 getBytesSent() {
96     return bytes_sent;
97   }
98 }
99
Popular Tags