1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
38
39
40
41 private SearchSession session;
42 private boolean stopped = true;
43
44
45
46 public JTellaSearch(SearchFilter filter)
47 {
48 super(filter);
49 }
50
51
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
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