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  package org.xnap.plugin.freeway;
20  
21  import java.awt.event.ActionEvent;
22  import java.io.File;
23  import java.io.IOException;
24  
25  import javax.swing.Action;
26  import javax.swing.Icon;
27  
28  import org.apache.log4j.Logger;
29  import org.gnu.freeway.protocol.afs.esed2.DownloadUtil;
30  import org.gnu.freeway.protocol.afs.esed2.ProgressModel;
31  import org.gnu.freeway.protocol.afs.esed2.ProgressStats;
32  import org.gnu.freeway.protocol.afs.esed2.RequestManager;
33  import org.gnu.freeway.protocol.afs.esed2.RootNode;
34  import org.gnu.freeway.util.AbstractAction;
35  import org.gnu.freeway.util.ConfigurationService;
36  import org.gnu.freeway.util.CronService;
37  import org.gnu.freeway.util.Task;
38  import org.xnap.XNap;
39  import org.xnap.peer.Peer;
40  import org.xnap.plugin.Plugin;
41  import org.xnap.transfer.AbstractDownload;
42  import org.xnap.transfer.action.AbstractDeleteAction;
43  import org.xnap.util.FileHelper;
44  
45  /***
46   * 
47   */
48  public class FreewayDownload extends AbstractDownload implements ProgressModel
49  {
50  
51  	private RootNode node;
52  	private long startTime;
53  	private RequestManager reqMan = null;
54  	private FreewayPlugin plugin = FreewayPlugin.getInstance();
55  	private Task downloader = null;
56  
57  	private File file;
58  	private long bytesTransferred = 0;
59  	private long filesize;
60  	private boolean done = false;
61  	
62  		
63  	private static Logger logger = Logger.getLogger(FreewayDownload.class);
64  	
65  	/***
66  	 * @param node
67  	 */
68  	public FreewayDownload(RootNode node) 
69  	{
70  		this.node = node;
71  		filesize = node.getFileIdentifier().getFileLength();
72  		try {
73  			file = FileHelper.createIncompleteFile(node.getFileName());
74  		}
75  		catch (IOException ie) {
76  			logger.debug("Could not create incomplete file", ie);
77  		}
78  
79  		downloader = new Task("GNUNet Download", new DownloadJob());
80  		downloader.launch();
81  		transferStarted();  
82  	}
83  
84  	/*
85  	 * @see org.xnap.transfer.AbstractTransfer#getBytesTransferred()
86  	 */
87  	protected long getBytesTransferred() 
88  	{
89  		return bytesTransferred;
90  	}
91  
92  	/*
93  	 * @see org.xnap.transfer.Transfer#getActions()
94  	 */
95  	public Action[] getActions() 
96  	{
97  		return new Action[] { new FreewayDeleteAction() };
98  	}
99  
100 	/*
101 	 * @see org.xnap.transfer.Transfer#getFile()
102 	 */
103 	public File getFile() 
104 	{
105 		return file;
106 	}
107 
108 	public long getFilesize()
109 	{
110 		return filesize;
111 	}
112 
113 	/*
114 	 * @see org.xnap.transfer.Transfer#getPeer()
115 	 */
116 	public Peer getPeer() 
117 	{
118 		return null;
119 	}
120 
121 	/*
122 	 * @see org.xnap.transfer.Transfer#getPlugin()
123 	 */
124 	public Plugin getPlugin() 
125 	{
126 		return plugin;
127 	}
128 
129 	/*
130 	 * @see org.xnap.transfer.Transfer#getStatus()
131 	 */
132 	public String getStatus() 
133 	{
134 		return  done ? XNap.tr("Finished") : XNap.tr("Running");
135 	}
136 
137 	public Icon getIcon()
138 	{
139 		return FreewayPlugin.ICON_16;
140 	}
141 
142 	/*
143 	 * @see org.xnap.transfer.Transfer#getTotalBytesTransferred()
144 	 */
145 	public long getTotalBytesTransferred() 
146 	{
147 		return bytesTransferred;
148 	}
149 
150 	/*
151 	 * @see org.xnap.transfer.Transfer#isDone()
152 	 */
153 	public boolean isDone() 
154 	{
155 		return done;
156 	}
157 
158 	public void transferStopped()
159 	{
160 		if (reqMan != null) {
161 			reqMan.destroy();
162 		}
163 		super.transferStopped();
164 	}
165 
166 	/*
167 	 * @see org.xnap.transfer.Transfer#isRunning()
168 	 */
169 	public boolean isRunning() 
170 	{
171 		return !done;
172 	}
173 
174 	public void progress(ProgressStats stats, Object obj) 
175 	{
176 		bytesTransferred = stats.progress;
177 				
178 		if (filesize != stats.filesize) {
179 			logger.debug("Inconsistent filesizes");
180 		}
181 		
182 		if (stats.filesize == stats.progress) {
183 			done = true;
184 			transferStopped();
185 		}
186 	}
187 	
188 	private class DownloadJob extends AbstractAction
189 	{
190 		
191 		public void perform() throws Throwable 
192 		{
193 			/* this starts the "real" download thread in the background,
194 			   with "modelCallback" called back to tell us about the progress */
195 			reqMan = new DownloadUtil().downloadFile
196 				(plugin.getPreferences(),
197 				 (ConfigurationService)plugin.service(ConfigurationService.class),
198 				 (CronService)plugin.service(CronService.class),
199 				 plugin.getPolicy(),
200 				 node.getFileIdentifier(), 
201 				 file.getAbsolutePath(), 
202 				 FreewayDownload.this,
203 				 this);
204 			
205 			if (reqMan == null) {
206 				logger.debug("Download could not be started");
207 				done = true;
208 				return;
209 			}
210 			
211 		}
212 		
213 	}
214 
215 	private class FreewayDeleteAction extends AbstractDeleteAction
216 	{
217 		public FreewayDeleteAction()
218 		{
219 			setEnabled(false);
220 		}
221 		
222 		public void actionPerformed(ActionEvent e)
223 		{
224 //			if (downloader != null) {
225 //				downloader.
226 //			}
227 //			
228 //			if (reqMan != null) {
229 //				reqMan.destroy();
230 //				file.delete();
231 //			}
232 		}
233 	}
234 }