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.overnet.net;
21  
22  import java.io.File;
23  
24  import javax.swing.Action;
25  import javax.swing.Icon;
26  
27  import org.apache.log4j.Logger;
28  import org.xnap.XNap;
29  import org.xnap.peer.Peer;
30  import org.xnap.plugin.Plugin;
31  import org.xnap.plugin.overnet.OvernetPlugin;
32  import org.xnap.plugin.overnet.net.msg.core.ChangeUploadSlotMessage;
33  import org.xnap.plugin.overnet.peer.OvernetPeer;
34  import org.xnap.transfer.AbstractTransfer;
35  import org.xnap.transfer.Upload;
36  
37  public class OvernetUpload extends AbstractTransfer implements Upload
38  {
39  	//--- Constant(s) ---
40  
41  	//--- Data field(s) ---
42  
43  	private OvernetPeer peer;
44  	private String filename;
45  	private byte[] hash;
46  	private long currentRate = -1;
47  	private boolean done = false;
48  	
49      private static Logger logger = Logger.getLogger(OvernetUpload.class);
50  	
51  	//--- Constructor(s) ---
52  
53      public OvernetUpload(ChangeUploadSlotMessage um)
54  	{
55  		filename = um.filename;
56  		hash = um.fileHash;
57  		peer = new OvernetPeer(um.peer, um.userHash);
58  	}
59  
60      //--- Method(s) ---
61  
62  	public long getTotalBytesTransferred()
63  	{
64  		return -1;
65  	}
66  
67  	public long getBytesTransferred()
68  	{
69  		return -1;
70  	}
71  
72  	public String getFilename()
73  	{
74  		return filename;
75  	}
76  
77  	public long getFilesize()
78  	{
79  		return -1;
80  	}
81  
82  	/***
83  	 * Update the current rate.
84  	 */
85  	public void update(float speed)
86  	{
87  		currentRate = (int)(1024 * speed);
88  	}
89  
90  	/***
91  	 * Update the properties of this upload, like filename, peer name etc.
92  	 */
93  	public void updateInfo(ChangeUploadSlotMessage cm)
94  	{
95  		if (cm.filename != null) {
96  			if (filename == null || !filename.equals(cm.filename)) {
97  				filename = cm.filename;
98  			}
99  		}
100 		if (cm.peer != null) {
101 			if (peer.getName() == null || !peer.getName().equals(cm.peer)) {
102 				peer = new OvernetPeer(cm.peer, cm.userHash);
103 			}
104 		}
105 	}
106 
107 	public long getCurrentRate()
108 	{
109 		return currentRate;
110 	}
111 
112 	public Action[] getActions()
113 	{
114 		return null;
115 	}
116 
117 	public File getFile()
118 	{
119 		return null;
120 	}
121 
122 	public Icon getIcon()
123 	{
124 		return OvernetPlugin.ICON_16;
125 	}
126 
127 	public Peer getPeer()
128 	{
129 		return peer;
130 	}
131 
132 	public Plugin getPlugin()
133 	{
134 		return OvernetPlugin.getInstance();
135 	}
136 
137 	public String getStatus()
138 	{
139 		if (isDone()) {
140 			return XNap.tr("Finished");
141 		}
142 		else {
143 			return XNap.tr("Uploading") + "...";
144 		}
145 	}
146 
147 	public boolean isDone()
148 	{
149 		return done;
150 	}
151 
152 	public void setDone(boolean state)
153 	{
154 		done = state;
155 		if (state) {
156 			currentRate = 0;
157 		}
158 	}
159 	
160 	public boolean isRunning()
161 	{
162 		return !done;
163 	}
164 
165 	public boolean isFailed()
166 	{
167 		return false;
168 	}
169 }
170