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.plugin.gift.net;
21  
22  import java.io.File;
23  
24  import javax.swing.Action;
25  
26  import org.xnap.peer.Peer;
27  import org.xnap.plugin.Plugin;
28  import org.xnap.plugin.gift.GiFTPlugin;
29  import org.xnap.plugin.gift.net.event.UploadUpdatedEvent;
30  import org.xnap.plugin.gift.net.event.listener.UploadEventListener;
31  import org.xnap.transfer.AbstractUpload;
32  import org.xnap.transfer.DefaultSegment;
33  import org.xnap.transfer.Segment;
34  
35  /***
36   * @author Tammo
37   */
38  public class GiFTUpload extends AbstractUpload implements UploadEventListener {
39  
40  	private String filename;
41  	private GiFTUser user;
42  	private String url;
43  	private String status;
44      private DefaultSegment segment;
45  
46  	private String gid;
47  	
48  	private boolean failed;
49  	private boolean done;
50  
51      private boolean marked;
52  
53  	/***
54  	 * 
55  	 */
56  	public GiFTUpload(
57  		GiFTUser user,
58  		String url, long filesize, long transmit) {
59  		super();
60  		this.user = user;
61  		this.url = url;
62  
63  		segment = new DefaultSegment(filesize);
64  		segment.setTransferred(transmit);		
65  		// TODO: this should be initialized when the upload
66  		// acctually starts
67  		transferStarted();
68  	}
69  
70  	public void setStatus(String status) {
71  		this.status = status;
72  		if (status.equalsIgnoreCase("complete")) {
73  			done = true;
74  		} else if (
75  			status.startsWith("Cancelled")
76  				|| status.startsWith("Timed out")
77  				|| status.startsWith("Orphaned")) {
78  			done = true;
79  			failed = true;
80  			
81  			segment.setEnd(segment.getStart() 
82  				       + segment.getTransferred());
83  		}
84  		else {
85  		    done = false;
86  		    failed = false;
87  		}
88  
89  		stateChanged();
90  	}
91  
92  	/***
93  	 * @see xnap.transfer.Transfer#getActions()
94  	 */
95  	public Action[] getActions() {
96  		return new Action[] {};
97  	}
98  
99  	/***
100 	 * @see xnap.transfer.Transfer#getTotalBytesTransferred()
101 	 */
102 	public long getBytesTransferred() {
103 		return segment.getTransferred();
104 	}
105 
106 	/***
107 	 * @see xnap.transfer.Transfer#getFile()
108 	 */
109 	public File getFile() {
110 		return null;
111 	}
112 
113 	/***
114 	 * @see xnap.transfer.Transfer#getPeer()
115 	 */
116 	public Peer getPeer() {
117 		return user;
118 	}
119 
120     public Plugin getPlugin()
121     {
122 	return GiFTPlugin.getInstance();
123     }
124 
125     public Segment[] getSegments()
126     {
127 	return new Segment[] { segment };
128     }
129 
130 	/***
131 	 * @see xnap.transfer.Transfer#getTotalBytesTransferred()
132 	 */
133 	public long getTotalBytesTransferred() {
134 		return segment.getTransferred();
135 	}
136 
137 	/***
138 	 * @see xnap.transfer.Transfer#getStatus()
139 	 */
140 	public String getStatus() {
141 		return status;
142 	}
143 
144 	/***
145 	 * @see xnap.transfer.Transfer#isDone()
146 	 */
147 	public boolean isDone() {
148 		return done;
149 	}
150 
151 	/***
152 	 * @see xnap.transfer.Transfer#isFailed()
153 	 */
154 	public boolean isFailed() {
155 		return failed;
156 	}
157 
158     public boolean isMarked()
159     {
160 	return marked;
161     }
162 
163 	/***
164 	 * @see xnap.transfer.Transfer#isRunning()
165 	 */
166 	public boolean isRunning() {
167 		return !failed && !done; // or !(failed || done);
168 	}
169 
170 	/***
171 	 * @param l
172 	 */
173 	public void updated(String status, long start, long transmit, 
174 			    long total) {
175 	    segment.setStart(start);
176 	    segment.setEnd(start + total);
177 	    segment.setTransferred(transmit);
178 
179 	    // this will throw the state changed event
180 	    setStatus(status);
181 	}
182 
183 	/***
184 	 * @see xnap.transfer.Transfer#getFilename()
185 	 */
186 	public String getFilename() {
187 		return this.filename;
188 	}
189 
190 	/***
191 	 * @see xnap.transfer.Transfer#getFilesize()
192 	 */
193 	public long getFilesize() {
194 	    return segment.getEnd() - segment.getStart();
195 	}
196 
197     public void setMarked(boolean marked)
198     {
199 		this.marked = marked;
200     }
201     
202     public void setGID(String gid) {
203     	this.gid = gid; 
204     }
205     
206     public String getGID() {
207     	return gid;
208     }
209 
210 	/***
211 	 * @see xnap.plugin.gift.net.event.listener.UploadEventListener#uploadUpdated(xnap.plugin.gift.net.event.UploadUpdatedEvent)
212 	 */
213 	public void uploadUpdated(UploadUpdatedEvent evt) {
214 		segment.setStart(0);
215 		segment.setEnd(evt.getSize());
216 		segment.setTransferred(evt.getTransmit());
217 
218 		// this will throw the state changed event
219 		setStatus(evt.getState());
220 	}
221 
222 	/***
223 	 * @see xnap.plugin.gift.net.event.listener.UploadEventListener#uploadCanceled()
224 	 */
225 	public void uploadCanceled() {
226 		// TODO
227 		
228 	}
229 
230 }