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.opennap.net;
21  
22  import javax.swing.Action;
23  
24  import org.xnap.XNap;
25  import org.xnap.plugin.opennap.net.msg.ExceptionListener;
26  import org.xnap.plugin.opennap.net.msg.MessageHandler;
27  import org.xnap.plugin.opennap.net.msg.MessageListener;
28  import org.xnap.plugin.opennap.net.msg.client.BrowseRequestMessage;
29  import org.xnap.plugin.opennap.net.msg.server.BrowseResponseMessage;
30  import org.xnap.plugin.opennap.net.msg.server.ServerMessage;
31  import org.xnap.plugin.opennap.net.msg.server.UserSignoffMessage;
32  import org.xnap.plugin.opennap.user.OpenNapUser;
33  import org.xnap.search.SearchFilter;
34  import org.xnap.search.SearchHandler;
35  import org.xnap.search.SearchResult;
36  
37  /***
38   * Implements an OpenNap search on set of servers.
39   */
40  public class OpenNapBrowse 
41  	implements ExceptionListener, MessageListener, OpenNapBrowseInterface {
42  
43      // --- Constants(s) ---
44  
45      //--- Data Field(s) ---
46      
47      private SearchHandler handler;
48  	private boolean done;
49  	private boolean failed;
50  
51  	private OpenNapUser user;
52      private BrowseRequestMessage request;
53  
54  	
55      
56      //--- Constructor(s) ---
57  
58      public OpenNapBrowse(OpenNapUser user)
59      {
60  		this.user = user;
61  
62  		request = new BrowseRequestMessage(user.getName());
63  		request.setExceptionListener(this);
64      }
65  
66      // --- Method(s) ---
67  
68      public void exceptionThrown(Exception e)
69      {
70  		failed = true;
71  		finished();
72      }
73  
74  	/***
75  	 * Returns <code>null</code>.
76  	 */
77  	public Action[] getActions()
78  	{
79  		return null;
80  	}
81  
82      /***
83       * Returns <code>null</code>.
84       */
85      public SearchFilter getFilter()
86      {
87  		return null;
88      }
89  
90      /***
91       * Returns the name of the user that is browsed.
92       */
93      public String getName()
94      {
95  		return user.getName();
96      }
97  
98  	public BrowseRequestMessage getRequest()
99  	{
100 		return request;
101 	}
102 
103     /***
104      * Returns <code>true</code>.
105      */
106     public boolean showTree()
107     {
108 		return true;
109     }
110 
111     /***
112      * Returns a string that describes the current status. 
113      */
114     public String getStatus()
115     {
116 		return (isDone()) 
117 			? XNap.tr("finished")
118 			: XNap.tr("receiving results") + "...";
119     }
120 
121     /***
122      * Returns true, if this search can not be canceled.
123      */
124     public boolean isDone()
125     {
126 		return done;
127     }
128 
129 	public boolean isFailed()
130 	{
131 		return failed;
132 	}
133 
134     /***
135      * Starts this search.
136      *
137      * @param handler the object that handles the results
138      */
139     public void start(SearchHandler handler)
140     {
141 		this.handler = handler;
142 
143 		done = false;
144 		failed = false;
145 
146 		OpenNapServerRunner r = user.getServer().getRunner();
147 		if (r != null) {
148 			r.enqueue(this);
149 			MessageHandler.subscribe(UserSignoffMessage.TYPE, this);
150 		}
151 		else {
152 			getRequest().failed();
153 		}
154     }
155 
156     /***
157      * Cancels this search.
158      */
159     public void stop()
160     {
161 		MessageHandler.unsubscribe(UserSignoffMessage.TYPE, this);
162 		done = true;
163     }
164 
165 	/***
166 	 * Invoked by {@link OpenNapServer}.
167 	 */
168     void finished()
169     {
170 		done = true;
171 		handler.stateChanged(this);
172     }
173 
174     void received(BrowseResponseMessage msg)
175     {
176 		if (done) {
177 			return;
178 		}
179 
180  		OpenNapUser user = msg.getServer().getUser(msg.nick);
181 
182 		SearchResult result = new OpenNapSearchResult
183 			(null, msg.filesize, msg.bitrate, msg.frequency, 
184 			 msg.length, user, msg.filename, msg.md5);
185 
186 		handler.resultReceived(result);
187     }
188 
189 	/***
190 	 *  @see xnap.plugin.nap.net.msg.server.MessageListener#messageReceived(xnap.plugin.nap.net.msg.server.ServerMessage)
191 	 */
192 	public void messageReceived(ServerMessage msg) 
193 	{
194 		// the user has signed off
195 		failed = true;
196 		finished();
197 	}
198 
199     // --- Inner Class(es) ---
200 
201 }
202