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