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.util.Collection;
23  import java.util.Hashtable;
24  import java.util.Iterator;
25  
26  import org.xnap.plugin.overnet.net.msg.*;
27  import org.xnap.plugin.overnet.*;
28  import org.xnap.plugin.overnet.net.msg.core.GapsMessage;
29  import org.xnap.plugin.overnet.net.msg.core.MessageListener;
30  import org.xnap.plugin.overnet.net.msg.core.NewDownloadMessage;
31  import org.xnap.plugin.overnet.net.msg.core.UpdateDownloadMessage;
32  import org.xnap.transfer.DownloadManager;
33  
34  public class DownloadTable implements MessageListener
35  {
36  	//--- Constant(s) ---
37  
38  	//--- Data Field(s) ---
39  
40  	private DownloadManager manager = DownloadManager.getInstance();
41  
42  	private Hashtable indices = new Hashtable();
43  	private int index = 0;
44  
45  	//--- Constructor(s) ---
46  
47  	public DownloadTable()
48  	{
49  		MessageHandler handler = OvernetPlugin.getMessageHandler();
50  		handler.subscribe(NewDownloadMessage.TYPE, this);
51  		handler.subscribe(UpdateDownloadMessage.TYPE, this);
52  		handler.subscribe(GapsMessage.TYPE, this);
53  	}
54  
55  	public void messageReceived(OvernetMessage msg)
56  	{
57  		if (msg instanceof NewDownloadMessage) {
58  			NewDownloadMessage ndm = (NewDownloadMessage)msg;
59  			OvernetDownload od = new OvernetDownload(ndm);
60  			addDownload(od, ndm.id);
61  		}
62  		else if (msg instanceof UpdateDownloadMessage) {
63  			UpdateDownloadMessage um = (UpdateDownloadMessage)msg;
64  			OvernetDownload dl = getDownload(um.index);
65  			if (dl != null) {
66  				dl.update(um);
67  			}
68  		}
69  		else if (msg instanceof GapsMessage) {
70  			GapsMessage gm = (GapsMessage)msg;
71  			OvernetDownload dl = getDownload(gm.index);
72  			if (dl != null) {
73  				dl.updateGaps(gm);
74  			}
75  		}
76  	}
77  
78  	private OvernetDownload getDownload(int index)
79  	{
80  		return (OvernetDownload)indices.get(new Integer(index));
81  	}
82  
83  	/***
84  	 * New implementation for cores >= 0.49.5, the core provides the id itself.
85  	 */
86  	private void addDownload(OvernetDownload download, int id)
87  	{
88  		indices.put(id != 0 ? new Integer(id) : new Integer(index++),
89  					download);
90  		manager.add(download);
91  	}
92  
93  	public void dispose()
94  	{
95  		Collection c = indices.values();
96  
97  		for (Iterator i = c.iterator(); i.hasNext();) {
98  			OvernetDownload od = (OvernetDownload)i.next();
99  			od.transferStopped();
100 			manager.remove(od);
101 		}
102 		indices.clear();
103 		index = 0;
104 	}
105 }
106