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.apache.log4j.Logger;
27  import org.xnap.plugin.overnet.net.msg.*;
28  import org.xnap.plugin.overnet.*;
29  import org.xnap.plugin.overnet.net.msg.core.ChangeUploadSlotMessage;
30  import org.xnap.plugin.overnet.net.msg.core.MessageListener;
31  import org.xnap.plugin.overnet.net.msg.core.RemoveUploadSlotMessage;
32  import org.xnap.plugin.overnet.net.msg.core.UpdateUploadMessage;
33  import org.xnap.transfer.UploadManager;
34  
35  public class UploadTable implements MessageListener
36  {
37  	//--- Constant(s) ---
38  
39  	//--- Data field(s) ---
40  
41  	private UploadManager manager = UploadManager.getInstance();
42  
43  	private Hashtable indices = new Hashtable();
44  
45      private static Logger logger = Logger.getLogger(UploadTable.class);
46  	
47  		//--- Constructor(s) ---
48  
49      public UploadTable()
50  	{
51  		MessageHandler handler = OvernetPlugin.getMessageHandler();
52  		handler.subscribe(ChangeUploadSlotMessage.TYPE, this);
53  		handler.subscribe(UpdateUploadMessage.TYPE, this);
54  		handler.subscribe(RemoveUploadSlotMessage.TYPE, this);
55  	}
56  
57      //--- Method(s) ---
58  
59  	public void messageReceived(OvernetMessage msg)
60  	{
61  		if (msg instanceof UpdateUploadMessage) {
62  			UpdateUploadMessage um = (UpdateUploadMessage)msg;
63  			OvernetUpload ul = getUpload(um.index);
64  			if (ul != null) {
65  				logger.debug("new upload speed " + um.speed);
66  				ul.update(um.speed);
67  			}
68  		}
69  		else if (msg instanceof ChangeUploadSlotMessage) {
70  			ChangeUploadSlotMessage cm = (ChangeUploadSlotMessage)msg;
71  			OvernetUpload ul =  getUpload(cm.index);
72  			if (ul != null) {
73  				ul.updateInfo(cm);
74  			}
75  			else {
76  				ul = new OvernetUpload(cm);
77  				addUpload(cm.index, ul);
78  			}
79  		}
80  		else if (msg instanceof RemoveUploadSlotMessage) {
81  			RemoveUploadSlotMessage rm = (RemoveUploadSlotMessage)msg;
82  			removeUpload(rm.index);
83  		}
84  	}
85  
86  	public OvernetUpload getUpload(int index)
87  	{
88  		return (OvernetUpload)indices.get(new Integer(index));
89  	}
90  
91  	public void addUpload(int index, OvernetUpload ul)
92  	{
93  		indices.put(new Integer(index), ul);
94  		manager.add(ul);
95  	}
96  
97  	public void removeUpload(int index)
98  	{
99  		OvernetUpload ul = (OvernetUpload)indices.remove(new Integer(index));
100 		if (ul != null) {
101 			ul.setDone(true);
102 		}
103 	}
104 
105 	public void dispose()
106 	{
107 		Collection c = indices.values();
108 
109 		for (Iterator i = c.iterator(); i.hasNext();) {
110 			OvernetUpload up = (OvernetUpload)i.next();
111 			manager.remove(up);
112 		}
113 		indices.clear();
114 	}
115 }