View Javadoc

1   /*
2    *  XNap - A P2P framework and client.
3    *
4    *  See the file AUTHORS for copyright information.
5    *
6    *  This program is free software; you can redistribute it and/or modify
7    *  it under the terms of the GNU General Public License as published by
8    *  the Free Software Foundation.
9    *
10   *  This program is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   *  GNU General Public License for more details.
14   *
15   *  You should have received a copy of the GNU General Public License
16   *  along with this program; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package org.xnap.transfer;
21  
22  import java.util.Iterator;
23  import java.util.*;
24  
25  import org.apache.log4j.Logger;
26  import org.xnap.event.ListListener;
27  import org.xnap.event.ListSupport;
28  import org.xnap.util.*;
29  
30  /***
31   * This class manages transfers.
32   */
33  public abstract class AbstractTransferManager {
34  
35      //--- Constant(s) ---
36  
37      //--- Data field(s) ---
38  
39      protected static Preferences prefs = Preferences.getInstance();
40      protected Logger logger = Logger.getLogger(TransferQueue.class);
41  
42      private LinkedList transfers = new LinkedList();
43      protected ListSupport listeners = new ListSupport(this);
44      private TransferQueue queue = new TransferQueue();
45  	private AutoClearTask autoClearTask;
46      
47      //--- Constructor(s) ---
48  
49      public AbstractTransferManager()
50      {
51      }
52  
53      //--- Method(s) ---
54  
55      public void addListListener(ListListener listener)
56      {
57  		listeners.addListListener(listener);
58      }
59  
60      public void removeListListener(ListListener listener)
61      {
62  		listeners.removeListListener(listener);
63      }
64  
65      /***
66       * Removes all done transfers from this manager.
67       */
68      public synchronized void clearDone() 
69      {
70  		for (Iterator i = transfers.iterator(); i.hasNext();) {
71  			Transfer t = (Transfer)i.next();
72  			if (t.isDone()) {
73  				i.remove();
74  				t.cleared();
75  				listeners.fireItemRemoved(t);
76  			}
77  			else if (t instanceof TransferContainer){
78  				((TransferContainer)t).clearDone();
79  			}
80  		}
81      }
82  
83      /***
84       * Computes the currently used bandwidth.
85       * @return the rate in byte / s
86       */
87      public long getCurrentRate()
88  	{
89      	long currentRate = 0;
90      	Transfer[] transfers = getTransfers();
91  		for (int i = 0; i < transfers.length; i++) {
92  			// update transfer
93  			if (transfers[i].isRunning()) {
94  				long transferRate = transfers[i].getCurrentRate();
95  				if (transferRate > 0) {
96  					currentRate += transferRate;
97  				}
98  			}
99  		}
100 		return currentRate;
101     }
102     
103     public TransferQueue getQueue()
104     {
105 		return queue;
106     }
107 
108     /***
109      * Adds a transfer to this manager.
110      */
111     protected synchronized void add(Transfer t)
112     {
113 		transfers.add(t);
114 		listeners.fireItemAdded(t);
115     }
116 
117 	public synchronized Transfer[] getTransfers()
118 	{
119 		return (Transfer[])transfers.toArray(new Transfer[0]);
120 	}
121 
122     /***
123      * Removes <code>t</code> from this manager.
124      */
125     protected synchronized void remove(Transfer t)
126     {
127 		transfers.remove(t);
128 		listeners.fireItemRemoved(t);
129     }
130 
131 	/***
132 	 * Sets the interval for the auto clear task. 
133 	 *
134 	 * @param interval the clear interval in milli seconds; if -1,
135 	 * auto clearing is disabled */
136 	public synchronized void setAutoClearInterval(long interval)
137 	{
138 		if (interval == -1) {
139 			if (autoClearTask != null) {
140 				autoClearTask.cancel();
141 				autoClearTask = null;
142 			}
143 		}
144 		else {
145 			autoClearTask = new AutoClearTask(interval);
146 			Scheduler.run(0, 60 * 1000, autoClearTask);
147 		}
148 	}
149 
150 	//--- Inner Classes ---
151 
152 	private class AutoClearTask extends XNapTask {
153 
154 		private long autoClearInterval;
155 		private Hashtable doneTimeByTransfer = new Hashtable();
156 
157 		public AutoClearTask(long autoClearInterval)
158 		{
159 			this.autoClearInterval = autoClearInterval;
160 		}
161 
162 		public void run() 
163 		{
164 			synchronized (AbstractTransferManager.this) {
165 				for (Iterator i = transfers.iterator(); i.hasNext();) {
166 					Transfer t = (Transfer)i.next();
167 					if (t.isDone()) {
168 						Long time = (Long)doneTimeByTransfer.get(t);
169 						if (time != null || autoClearInterval == 0) {
170 							if (autoClearInterval == 0 
171 								|| time.longValue() >= autoClearInterval) {
172 
173 								i.remove();
174 								t.cleared();
175 								listeners.fireItemRemoved(t);
176 							}
177 						}
178 						else {
179 							doneTimeByTransfer.put
180 								(t, new Long(System.currentTimeMillis()));
181 						}
182 					}
183 				}
184 			}
185 		}
186 
187 	}
188 
189 }