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 org.apache.log4j.Logger;
23  import org.xnap.XNap;
24  import org.xnap.plugin.overnet.net.msg.*;
25  import org.xnap.plugin.overnet.*;
26  import org.xnap.plugin.overnet.net.msg.client.SearchMessage;
27  import org.xnap.plugin.overnet.net.msg.core.MessageListener;
28  import org.xnap.plugin.overnet.net.msg.core.SearchResultMessage;
29  import org.xnap.search.AbstractSearch;
30  import org.xnap.search.SearchFilter;
31  import org.xnap.search.SearchHandler;
32  
33  public class OvernetSearch extends AbstractSearch implements MessageListener
34  {
35  	//--- Constant(s) ---
36  
37  	//--- Data field(s) ---
38  
39  	private boolean stopped;
40  	private SearchHandler handler;
41  	/***
42  	 * Remember the last search object in order to unsubscribe it before the
43  	 * new one starts.
44  	 */
45  	private static OvernetSearch lastSearch = null;
46  
47      private static Logger logger = Logger.getLogger(OvernetSearch.class);
48  	
49  		//--- Constructor(s) ---
50  
51      public OvernetSearch(SearchFilter filter)
52  	{
53  		super(filter);
54  	}
55  
56      //--- Method(s) ---
57  
58  	/***
59       * Returns a string that describes the current status. 
60       */
61      public String getStatus()
62      {
63  		if (isDone()) {
64  			return XNap.tr("finished");
65  		}
66  		else {
67  			return XNap.tr("receiving results") + "...";
68  		}
69      }
70  
71      /***
72       * Returns true, if this search can not be canceled.
73       */
74      public boolean isDone()
75      {
76  		return stopped;
77      }
78  
79      /***
80       * Starts this search.
81       *
82       * @param handler the object that handles the results
83       */
84      public void start(SearchHandler handler)
85      {
86  		if (lastSearch != null) {
87  			lastSearch.stop();
88  		}
89  		stopped = false;
90  		this.handler = handler;
91  		lastSearch = this;
92  		OvernetPlugin.getMessageHandler().subscribe
93  			(SearchResultMessage.TYPE, this);
94  		OvernetCore.send(new SearchMessage(getFilter().getText()));
95      }
96  
97      /***
98       * Cancels this search.
99       */
100     public void stop()
101     {
102 		OvernetPlugin.getMessageHandler().unsubscribe
103 			(SearchResultMessage.TYPE, this);
104 		stopped = true;
105     }
106 
107 	public void messageReceived(OvernetMessage msg)
108 	{
109 		if (!stopped && msg instanceof SearchResultMessage) {
110 			SearchResultMessage sm = (SearchResultMessage)msg;
111 			handler.resultReceived(new OvernetSearchResult(sm));
112 		}
113 	}
114 }
115