702e135f56340c0a47cb3caad7ec93646dc1db70
[jSite2.git] / src / net / pterodactylus / jsite / core / Request.java
1 /*
2  * jSite2 - Request.java -
3  * Copyright © 2008 David Roden
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
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 net.pterodactylus.jsite.core;
21
22 import net.pterodactylus.util.beans.AbstractBean;
23
24 /**
25  * A request is an ongoing download or upload reported by the freenet node.
26  * 
27  * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
28  * @version $Id$
29  */
30 public class Request extends AbstractBean {
31
32         /**
33          * The type of a request.
34          * 
35          * @author David ‘Bombe’ Roden <bombe@freenetproject.org>
36          * @version $Id$
37          */
38         public enum Type {
39
40                 /** Type of request is unknown. */
41                 unknown,
42
43                 /** The request is a Get request. */
44                 get,
45
46                 /** The request is a Put request. */
47                 put,
48
49                 /** The request is a PutDir request. */
50                 putDir
51
52         }
53
54         /** Name of the “type” property. */
55         public static final String PROPERTY_TYPE = "type";
56
57         /** Name of the “client token” property. */
58         public static final String PROPERTY_CLIENT_TOKEN = "clientToken";
59
60         /** Name of the “finished” property. */
61         public static final String PROPERTY_FINISHED = "finished";
62
63         /** Name of the “successful” property. */
64         public static final String PROPERTY_SUCCESSFUL = "successful";
65
66         /** Name of the “fetchable” property. */
67         public static final String PROPERTY_FETCHABLE = "fetchable";
68
69         /** Name of the “URI” property. */
70         public static final String PROPERTY_URI = "uri";
71
72         /** Name of the “total blocks” property. */
73         public static final String PROPERTY_TOTAL_BLOCKS = "totalBlocks";
74
75         /** Name of the “required blocks” property. */
76         public static final String PROPERTY_REQUIRED_BLOCKS = "requiredBlocks";
77
78         /** Name of the “successful blocks” property. */
79         public static final String PROPERTY_SUCCESSFUL_BLOCKS = "successfulBlocks";
80
81         /** Name of the “failed blocks” property. */
82         public static final String PROPERTY_FAILED_BLOCKS = "failedBlocks";
83
84         /** Name of the “fatally failed blocks” property. */
85         public static final String PROPERTY_FATALLY_FAILED_BLOCKS = "fatallyFailedBlocks";
86
87         /** Name of the “total finalized” property. */
88         public static final String PROPERTY_TOTAL_FINALIZED = "totalFinalized";
89
90         /** The node the request belongs to. */
91         private final Node node;
92
93         /** The identifier of the request. */
94         private final String identifier;
95
96         /** The type of the request. */
97         private Type type;
98
99         /** The client token of the request. */
100         private String clientToken;
101
102         /** Whether the request is finished. */
103         private boolean finished;
104
105         /** Whether the request was finished successfully. */
106         private boolean successful;
107
108         /** Whether the data is already fetchable (in case of put requests). */
109         private boolean fetchable;
110
111         /** The generated URI. */
112         private String uri;
113
114         /** The total number of blocks. */
115         private int totalBlocks;
116
117         /** The required number of blocks. */
118         private int requiredBlocks;
119
120         /** The number of successful blocks. */
121         private int successfulBlocks;
122
123         /** The number of failedBlocks. */
124         private int failedBlocks;
125
126         /** The number of fatally failed blocks. */
127         private int fatallyFailedBlocks;
128
129         /** Whether the total number has been finalized. */
130         private boolean totalFinalized;
131
132         /**
133          * Creates a new request with the given identifier.
134          * 
135          * @param node
136          *            The node the request belongs to
137          * @param identifier
138          *            The identifier of the request
139          */
140         Request(Node node, String identifier) {
141                 this.node = node;
142                 this.identifier = identifier;
143         }
144
145         //
146         // EVENT MANAGEMENT
147         //
148
149         /**
150          * Returns the node the request belongs to.
151          * 
152          * @return The node the request belongs to
153          */
154         public Node getNode() {
155                 return node;
156         }
157
158         /**
159          * Returns the identifier of the request. It is unique per node.
160          * 
161          * @return The identifier of the request
162          */
163         public String getIdentifier() {
164                 return identifier;
165         }
166
167         /**
168          * Returns the type of the request.
169          * 
170          * @return The type of the request
171          */
172
173         public Type getType() {
174                 return type;
175         }
176
177         /**
178          * Sets the type of the request.
179          * 
180          * @param type
181          *            The type of the request
182          */
183         void setType(Type type) {
184                 Type oldType = this.type;
185                 this.type = type;
186                 fireIfPropertyChanged(PROPERTY_TYPE, oldType, type);
187         }
188
189         /**
190          * Returns the client token of the request.
191          * 
192          * @return The client token of the request
193          */
194         public String getClientToken() {
195                 return clientToken;
196         }
197
198         /**
199          * Sets the client token of the request.
200          * 
201          * @param clientToken
202          *            The client token of the request
203          */
204         void setClientToken(String clientToken) {
205                 String oldClientToken = this.clientToken;
206                 this.clientToken = clientToken;
207                 fireIfPropertyChanged(PROPERTY_CLIENT_TOKEN, oldClientToken, clientToken);
208         }
209
210         /**
211          * Returns whether the request has finished.
212          * 
213          * @see #isSuccessful()
214          * @return <code>true</code> if the request is finished,
215          *         <code>false</code> otherwise
216          */
217         public boolean isFinished() {
218                 return finished;
219         }
220
221         /**
222          * Sets whether the request has finished.
223          * 
224          * @param finished
225          *            <code>true</code> if the request has finished,
226          *            <code>false</code> otherwise
227          */
228         void setFinished(boolean finished) {
229                 boolean oldFinished = this.finished;
230                 this.finished = finished;
231                 firePropertyChange(PROPERTY_FINISHED, oldFinished, finished);
232         }
233
234         /**
235          * Returns whether the request finished successfully. This value will only
236          * have meaning if {@link #isFinished()} returns <code>true</code>.
237          * 
238          * @return <code>true</code> if the request finished successfully,
239          *         <code>false</code> otherwise
240          */
241         public boolean isSuccessful() {
242                 return successful;
243         }
244
245         /**
246          * Sets whether this request finished successfully.
247          * 
248          * @param successful
249          *            <code>true</code> if the request finished successfully,
250          *            <code>false</code> otherwise
251          */
252         void setSuccessful(boolean successful) {
253                 boolean oldSuccessful = this.successful;
254                 this.successful = successful;
255                 firePropertyChange(PROPERTY_SUCCESSFUL, oldSuccessful, successful);
256         }
257
258         /**
259          * Returns whether the data inserted by this {@link Type#put} or
260          * {@link Type#putDir} request is already fetchable by other clients.
261          * 
262          * @return <code>true</code> if the data is already fetchable,
263          *         <code>false</code> otherwise
264          */
265         public boolean isFetchable() {
266                 return fetchable;
267         }
268
269         /**
270          * Sets whether the data inserted by this {@link Type#put} or
271          * {@link Type#putDir} request is already fetchable by other clients.
272          * 
273          * @param fetchable
274          *            <code>true</code> if the data is already fetchable,
275          *            <code>false</code> otherwise
276          */
277         void setFetchable(boolean fetchable) {
278                 boolean oldFetchable = this.fetchable;
279                 this.fetchable = fetchable;
280                 firePropertyChange(PROPERTY_FETCHABLE, oldFetchable, fetchable);
281         }
282
283         /**
284          * Returns the URI generated by this request.
285          * 
286          * @return The generated URI
287          */
288         public String getURI() {
289                 return uri;
290         }
291
292         /**
293          * Sets the URI generated by this request.
294          * 
295          * @param uri
296          *            The generated URI
297          */
298         void setURI(String uri) {
299                 this.uri = uri;
300         }
301
302         /**
303          * Returns the total number of blocks of a request. Until
304          * {@link #isTotalFinalized()} returns <code>true</code> this value may
305          * change!
306          * 
307          * @return The total number of blocks of a request
308          */
309         public int getTotalBlocks() {
310                 return totalBlocks;
311         }
312
313         /**
314          * Sets the total number of blocks of a request.
315          * 
316          * @param totalBlocks
317          *            The total number of blocks
318          */
319         void setTotalBlocks(int totalBlocks) {
320                 int oldTotalBlocks = this.totalBlocks;
321                 this.totalBlocks = totalBlocks;
322                 fireIfPropertyChanged(PROPERTY_TOTAL_BLOCKS, oldTotalBlocks, totalBlocks);
323         }
324
325         /**
326          * @return the requiredBlocks
327          */
328         public int getRequiredBlocks() {
329                 return requiredBlocks;
330         }
331
332         /**
333          * @param requiredBlocks
334          *            the requiredBlocks to set
335          */
336         void setRequiredBlocks(int requiredBlocks) {
337                 int oldRequiredBlocks = this.requiredBlocks;
338                 this.requiredBlocks = requiredBlocks;
339                 fireIfPropertyChanged(PROPERTY_REQUIRED_BLOCKS, oldRequiredBlocks, requiredBlocks);
340         }
341
342         /**
343          * @return the successfulBlocks
344          */
345         public int getSuccessfulBlocks() {
346                 return successfulBlocks;
347         }
348
349         /**
350          * @param successfulBlocks
351          *            the successfulBlocks to set
352          */
353         void setSuccessfulBlocks(int successfulBlocks) {
354                 int oldSuccessfulBlocks = this.successfulBlocks;
355                 this.successfulBlocks = successfulBlocks;
356                 fireIfPropertyChanged(PROPERTY_SUCCESSFUL_BLOCKS, oldSuccessfulBlocks, successfulBlocks);
357         }
358
359         /**
360          * @return the failedBlocks
361          */
362         public int getFailedBlocks() {
363                 return failedBlocks;
364         }
365
366         /**
367          * @param failedBlocks
368          *            the failedBlocks to set
369          */
370         void setFailedBlocks(int failedBlocks) {
371                 int oldFailedBlocks = this.failedBlocks;
372                 this.failedBlocks = failedBlocks;
373                 fireIfPropertyChanged(PROPERTY_FAILED_BLOCKS, oldFailedBlocks, failedBlocks);
374         }
375
376         /**
377          * @return the fatallyFailedBlocks
378          */
379         public int getFatallyFailedBlocks() {
380                 return fatallyFailedBlocks;
381         }
382
383         /**
384          * @param fatallyFailedBlocks
385          *            the fatallyFailedBlocks to set
386          */
387         void setFatallyFailedBlocks(int fatallyFailedBlocks) {
388                 int oldFatallyFailedBlocks = this.fatallyFailedBlocks;
389                 this.fatallyFailedBlocks = fatallyFailedBlocks;
390                 fireIfPropertyChanged(PROPERTY_FATALLY_FAILED_BLOCKS, oldFatallyFailedBlocks, fatallyFailedBlocks);
391         }
392
393         /**
394          * @return the totalFinalized
395          */
396         public boolean isTotalFinalized() {
397                 return totalFinalized;
398         }
399
400         /**
401          * @param totalFinalized
402          *            the totalFinalized to set
403          */
404         void setTotalFinalized(boolean totalFinalized) {
405                 boolean oldTotalFinalized = this.totalFinalized;
406                 this.totalFinalized = totalFinalized;
407                 fireIfPropertyChanged(PROPERTY_TOTAL_FINALIZED, oldTotalFinalized, totalFinalized);
408         }
409
410 }