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.limewire;
21  
22  import java.util.Hashtable;
23  
24  import org.xnap.search.MediaType;
25  import org.xnap.search.Search;
26  import org.xnap.search.SearchFilter;
27  import org.xnap.search.SearchManager;
28  import org.xnap.search.SearchProvider;
29  
30  import com.limegroup.gnutella.GUID;
31  import com.limegroup.gnutella.RemoteFileDesc;
32  import com.limegroup.gnutella.RouterService;
33  import com.limegroup.gnutella.search.HostData;
34  import com.sun.java.util.collections.Set;
35  
36  public class LimeWireSearchManager implements SearchProvider {
37      
38      //--- Constant(s) ---
39  
40      //--- Data field(s) ---
41  
42      //private static Logger logger = Logger.getLogger(LimeWireSearchManager.class);
43  
44  	private Hashtable mediaTypeByType = new Hashtable();
45  	private Hashtable searchByGUID = new Hashtable();
46  
47      //--- Constructor(s) ---
48      
49      public LimeWireSearchManager()
50      {
51  		mediaTypeByType.put
52  			(com.limegroup.gnutella.MediaType.getAudioMediaType(),
53  			 SearchManager.MEDIA_AUDIO);
54  		mediaTypeByType.put
55  			(com.limegroup.gnutella.MediaType.getVideoMediaType(), 
56  			 SearchManager.MEDIA_VIDEO);
57  		mediaTypeByType.put
58  			(com.limegroup.gnutella.MediaType.getImageMediaType(), 
59  			 SearchManager.MEDIA_IMAGES);
60      }
61  
62      //--- Method(s) ---
63  
64      public MediaType[] getSupportedMediaTypes()
65      {
66  		return new MediaType[] {
67  			SearchManager.MEDIA_ANYTHING, SearchManager.MEDIA_AUDIO, 
68  			SearchManager.MEDIA_DOCUMENTS, SearchManager.MEDIA_IMAGES, 
69  			SearchManager.MEDIA_SOFTWARE, SearchManager.MEDIA_VIDEO,
70  		};
71      }
72  
73      public String getName()
74      {
75  		return LimeWirePlugin.getInstance().getInfo().getName();
76      }
77  
78  	public void received(RemoteFileDesc file, HostData data, 
79  						 Set locations)
80  	{
81  		LimeWireSearch search
82  			= (LimeWireSearch)searchByGUID.get(new GUID(data.getMessageGUID()));
83  		if (search != null) {
84  			LimeWireSearchResult result
85  				= new LimeWireSearchResult(file, data, locations);
86  			search.received(result);
87  		}
88  	}
89  
90      /***
91       * Invoked when a search is requested.
92       */
93      public Search search(SearchFilter filter)
94      {
95  		// from this point on it is not possible to cleanly disable
96  		// the plugin anymore, since plugin object will get
97  		// distributed all over the place
98  		LimeWirePlugin.getInstance().getInfo().setDisableable(false);
99  
100 		GUID guid = new GUID(RouterService.newQueryGUID());
101 		DefaultLimeWireSearch search = new DefaultLimeWireSearch(filter, guid);
102 		searchByGUID.put(guid, search);
103 		return search;							
104     }
105 
106 	/***
107 	 * @param desc
108 	 */
109 	public void browse(RemoteFileDesc desc) 
110 	{
111 		browse(desc.getHost(), desc.getPort(), new GUID(desc.getClientGUID()), 
112 			   desc.getPushProxies());
113 	}
114 
115 	public void browse(String host, int port, GUID clientGUID, Set proxies)
116 	{
117 		GUID guid = new GUID(RouterService.newQueryGUID());
118 		LimeWireBrowse browse = new LimeWireBrowse(host, port, guid,
119 												   clientGUID, proxies);
120 		searchByGUID.put(guid, browse);
121 		SearchManager.getInstance().handle(browse);
122 	}
123 
124 	/***
125 	 * @param guid
126 	 */
127 	public void failed(GUID guid) 
128 	{
129 		LimeWireSearch search = (LimeWireSearch)searchByGUID.get(guid);
130 		if (search != null) {
131 			search.failed();
132 		}
133 	}
134     
135 }