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.jtella;
21  
22  import org.xnap.XNap;
23  import org.xnap.search.AbstractSearch;
24  import org.xnap.search.SearchFilter;
25  import org.xnap.search.SearchHandler;
26  
27  import com.kenmccrary.jtella.GNUTellaConnection;
28  import com.kenmccrary.jtella.MessageReceiverAdapter;
29  import com.kenmccrary.jtella.SearchReplyMessage;
30  import com.kenmccrary.jtella.SearchSession;
31  
32  /***
33   * Implements a jtella search session.
34   */
35  public class JTellaSearch extends AbstractSearch  {
36  
37      // --- Constants(s) ---
38  
39      //--- Data Field(s) ---
40  
41      private SearchSession session;
42      private boolean stopped = true;
43  
44      //--- Constructor(s) ---
45  
46      public JTellaSearch(SearchFilter filter)
47      {
48  		super(filter);
49      }
50  
51      // --- Method(s) ---
52  
53      /***
54       * Returns a string that describes the current status. 
55       */
56      public String getStatus()
57      {
58  		return XNap.tr("receiving results") + "...";
59      }
60  
61      /***
62       * Returns true, if this search can not be canceled.
63       */
64      public boolean isDone()
65      {
66  		return stopped;
67      }
68  
69      /***
70       * Starts this search.
71       *
72       * @param handler the object that handles the results
73       */
74      public void start(SearchHandler handler)
75      {
76  		String text = (String)getFilter().get(SearchFilter.TEXT);
77  		GNUTellaConnection c = JTellaPlugin.getInstance().getConnection();
78  		session = c.createSearchSession(text, 100, 0, new Receiver(handler));
79  
80  		stopped = false;
81      }
82  
83      /***
84       * Cancels this search.
85       */
86      public void stop()
87      {
88  		session.close();
89  		stopped = true;
90      }
91  
92      // --- Inner Class(es) ---
93  
94      /***
95       * Receives search results.
96       */
97      private class Receiver extends MessageReceiverAdapter
98      {
99  
100 		private SearchHandler handler;
101 	
102 		public Receiver(SearchHandler handler)
103 		{
104 			this.handler = handler;
105 		}
106 
107 		/***
108 		 * Invoked when a result has been received.
109 		 */ 
110 		public void receiveSearchReply(SearchReplyMessage reply)
111 		{
112 			JTellaServant peer 
113 				= new JTellaServant(reply.getIPAddress(), reply.getPort());
114 			peer.setClientInfo(reply.getVendorCode());
115 			peer.setFileCount(reply.getFileCount());
116 			peer.setLinkSpeed(reply.getDownloadSpeed());
117 	    
118 			for (int i = 0 ; i <  reply.getFileCount(); i++) {
119 				JTellaSearchResult result 
120 					= new JTellaSearchResult(reply, i, peer);
121 				handler.resultReceived(result);
122 			}
123 		}
124     }
125 
126 }
127