1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.xnap.plugin.opennap.net;
21
22 import org.xnap.XNap;
23 import org.xnap.plugin.opennap.OpenNapPlugin;
24 import org.xnap.plugin.opennap.net.msg.ExceptionListener;
25 import org.xnap.plugin.opennap.net.msg.client.SearchRequestMessage;
26 import org.xnap.plugin.opennap.net.msg.server.SearchResponseMessage;
27 import org.xnap.plugin.opennap.user.OpenNapUser;
28 import org.xnap.search.AbstractSearch;
29 import org.xnap.search.SearchFilter;
30 import org.xnap.search.SearchHandler;
31
32 /***
33 * Implements an OpenNap search on a set of servers.
34 */
35 public class OpenNapSearch extends AbstractSearch implements ExceptionListener
36 {
37
38
39
40 public static int PRIORITY_HIGH = 2;
41 public static int PRIORITY_LOW = 1;
42
43
44
45 private SearchHandler handler;
46 private OpenNapServer[] servers;
47 private int pending = 0;
48 private int failed = 0;
49 private SearchRequestMessage request;
50 private int priority;
51
52
53
54 public OpenNapSearch(SearchFilter filter, int priority, OpenNapServer[] servers)
55 {
56 super(filter);
57
58 this.priority = priority;
59 this.servers = servers;
60
61 request = new SearchRequestMessage
62 (filter, OpenNapPlugin.getPreferences().getMaxSearchResultsPerServer());
63 request.setExceptionListener(this);
64 }
65
66
67
68 public void exceptionThrown(Exception e)
69 {
70 synchronized (this) {
71 failed++;
72 }
73 finished();
74 }
75
76 public int getPriority()
77 {
78 return priority;
79 }
80
81 public SearchRequestMessage getRequest()
82 {
83 return request;
84 }
85
86 /***
87 * Returns a string that describes the current status.
88 */
89 public String getStatus()
90 {
91 if (isDone()) {
92 return XNap.tr("OpenNap search finished");
93 }
94 else {
95 return XNap.tr("{0} OpenNap servers pending",
96 new Integer(pending));
97 }
98 }
99
100 /***
101 * Returns true, if this search can not be canceled.
102 */
103 public boolean isDone()
104 {
105 return pending == 0;
106 }
107
108 /***
109 * Starts this search.
110 *
111 * @param handler the object that handles the results
112 */
113 public void start(SearchHandler handler)
114 {
115 this.handler = handler;
116
117 pending = servers.length;
118 for (int i = 0; i < servers.length; i++) {
119 OpenNapServerRunner r = servers[i].getRunner();
120 if (r != null) {
121 r.enqueue(this);
122 }
123 else {
124 getRequest().failed();
125 }
126 }
127 }
128
129 /***
130 * Cancels this search.
131 */
132 public void stop()
133 {
134 for (int i = 0; i < servers.length; i++) {
135 OpenNapServerRunner r = servers[i].getRunner();
136 if (r != null) {
137 r.dequeue(this);
138 }
139 }
140 pending = 0;
141 }
142
143 void finished()
144 {
145 synchronized (this) {
146 pending--;
147 }
148 handler.stateChanged(this);
149 }
150
151 void received(SearchResponseMessage msg)
152 {
153 OpenNapUser user = msg.getServer().getUser(msg.nick);
154 OpenNapSearchResult result
155 = new OpenNapSearchResult(getFilter(), msg.filesize, msg.bitrate,
156 msg.frequency, msg.length, user,
157 msg.filename, msg.md5);
158
159 handler.resultReceived(result);
160 }
161
162
163
164 }
165