1 //
2 // DownloadItem.java
3 // CocoDonkey
4 //
5 // Created by Fred Bonzoun on Sat May 11 2002.
6 // Copyright (c) 2002 Bonzoun. All rights reserved.
7 //
8 // This library is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU Lesser General Public License as published
10 // by the Free Software Foundation; either version 2.1 of the License, or
11 // (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 //
22
23 package net.bonzoun.cocodonkey;
24
25 public class DownloadItem extends GenericItem {
26
27 protected float percent;
28 protected int downloaded;
29 protected float rate;
30 protected String chunks;
31 protected int nbConn;
32 protected RateCalculator rateCalculator = new RateCalculator();
33
34 protected int completion;
35
36 /*package*/ DownloadItem(int id, String name, float percent, int downloaded, int size, float rate) {
37 super(id, name);
38 this.percent = percent;
39 this.downloaded = downloaded;
40 this.size = size;
41 this.rate = rate;
42 completion = 0;
43 }
44
45 /*package*/ DownloadItem(int id, String name, int size, String hash) {
46 super(id, name);
47 this.size = size;
48 this.hash = hash;
49 this.percent = 100;
50 completion = 1;
51 }
52
53 public boolean isFinished() {
54 return completion==1;
55 }
56
57 public boolean updateWith(DownloadItem item) {
58 boolean modified = false;
59 if (completion!=1 && item.completion!=1) {
60 /*if (id!=item.id) {
61 modified = true;
62 id=item.id;
63 }
64 if (name!=item.name) {
65 modified = true;
66 name=item.name;
67 }*/
68 if (percent!=item.percent) {
69 modified = true;
70 percent=item.percent;
71 }
72 if (downloaded!=item.downloaded) {
73 modified = true;
74 downloaded=item.downloaded;
75 }
76 /*if (size!=item.size) {
77 modified = true;
78 size=item.size;
79 }*/
80 if (rate!=item.rate) {
81 modified = true;
82 rate=item.rate;
83 }
84 }
85 else if (completion!=1 && item.completion==1) {
86 // We have got a finished item
87 modified = true;
88 if (completion==0) {
89 name=item.name;
90 hash=item.hash;
91 }
92 completion = 1;
93 }
94 return modified;
95 }
96
97 public void completeData(String name, int total, String hash, int downloaded) {
98 if (completion!=1) {
99 this.name=name;
100 this.hash=hash;
101 completion = 2;
102 }
103 }
104
105 public float percent() {
106 return percent;
107 }
108
109 public int downloaded() {
110 if (isFinished())
111 return size();
112 else
113 return downloaded;
114 }
115
116 public String prettyDownloaded() {
117 return displaySize(downloaded());
118 }
119
120 public float rate() {
121 return rate;
122 }
123
124 public int timeLeft() {
125 rateCalculator.addRate(rate());
126 float meanRate = rateCalculator.meanRate();
127
128 if (meanRate > 0) {
129 int remaining = size() - downloaded();
130 return (int) (Math.round((float) remaining / (1024 * meanRate) ));
131 } else {
132 return -1;
133 }
134 }
135
136 public void setChunks(String chunks) {
137 this.chunks = chunks;
138 }
139
140 public String chunks() {
141 return chunks;
142 }
143
144 public float chunksFoundPercent() {
145 return chunksPercentOf('V') + chunksPercentOf('p') + chunksPercentOf('!') + chunksPercentOf('?');
146 }
147
148 public float chunksNotFoundPercent() {
149 return chunksPercentOf('_') + chunksPercentOf('.');
150 }
151
152 private float chunksPercentOf(char t) {
153 if (chunks==null || chunks.length()==0)
154 return 0;
155 int n = 0, l = chunks.length();
156 for(int i=0; i<l; i++) {
157 char c = chunks.charAt(i);
158 if (c==t)
159 n++;
160 }
161 return (float)100*n/l;
162 }
163
164 public void setNbConn(int n) {
165 nbConn = n;
166 }
167
168 public int nbConn() {
169 return nbConn;
170 }
171
172 public static class ComparePercent extends MyComparator {
173 public final int compare(Object o1, Object o2) {
174 DownloadItem item1 = (DownloadItem)o1;
175 DownloadItem item2 = (DownloadItem)o2;
176 if (item1.percent()<item2.percent())
177 return -1;
178 else if (item1.percent()==item2.percent())
179 return 0;
180 else
181 return 1;
182 }
183 }
184
185 public static class CompareRate extends MyComparator {
186 public final int compare(Object o1, Object o2) {
187 DownloadItem item1 = (DownloadItem)o1;
188 DownloadItem item2 = (DownloadItem)o2;
189 if (item1.rate()<item2.rate())
190 return -1;
191 else if (item1.rate()==item2.rate())
192 return 0;
193 else
194 return 1;
195 }
196 }
197
198 public static class CompareDownloaded extends MyComparator {
199 public final int compare(Object o1, Object o2) {
200 DownloadItem item1 = (DownloadItem)o1;
201 DownloadItem item2 = (DownloadItem)o2;
202 if (item1.downloaded()<item2.downloaded())
203 return -1;
204 else if (item1.downloaded()==item2.downloaded())
205 return 0;
206 else
207 return 1;
208 }
209 }
210 }
211
212 // $Log: DownloadItem.java,v $
213 // Revision 1.6 2002/06/29 11:22:52 fortun
214 // Handles the cached chunk states
215 //
216 // Revision 1.5 2002/06/09 15:43:01 fortun
217 // Added chunksFoundPercent() method
218 // Uses the RateCalculator
219 //
220 // Revision 1.4 2002/06/08 12:29:18 fortun
221 // Handles finished items
222 //
223 // Revision 1.3 2002/06/08 12:20:07 fortun
224 // Estimation of the time remaining
225 //
226 // Revision 1.2 2002/05/27 22:09:40 fortun
227 // Added some comparators (for sorting)
228 //
229 // Revision 1.1.1.1 2002/05/21 21:26:13 fortun
230 //
231 //