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.msg.client;
21  
22  import java.util.StringTokenizer;
23  
24  import org.xnap.plugin.opennap.OpenNapPlugin;
25  import org.xnap.plugin.opennap.util.OpenNapServerVersion;
26  import org.xnap.search.MediaType;
27  import org.xnap.search.SearchFilter;
28  import org.xnap.search.SearchManager;
29  
30  public class SearchRequestMessage extends ClientMessage {
31  
32      //--- Constant(s) ---
33      
34      public static final int TYPE = 200;
35  
36      //--- Constructor(s) ---
37  
38      public SearchRequestMessage(SearchFilter filter, int maxResults)
39      {
40  		super(TYPE);
41  
42  		StringBuffer sb = new StringBuffer();
43  
44  		sb.append(getSearchText(filter.getText(), false));
45  
46  		if (OpenNapPlugin.getPreferences().getLimitSearchResultsPerServer()
47  			&& maxResults > 0) {
48  			sb.append(" MAX_RESULTS ");
49  			sb.append(maxResults);
50  		}
51  	
52  		if (filter.getMediaType() == null 
53  			|| filter.getMediaType() == SearchManager.MEDIA_ANYTHING
54  			|| filter.getMediaType() == SearchManager.MEDIA_AUDIO) {
55  			sb.append(compareString("BITRATE", 
56  									(Long)filter.get("nap.minBitrate"), 
57  									(Long)filter.get("nap.maxBitrate")));
58  			sb.append(compareString("FREQUENCY", 
59  									(Long)filter.get("nap.minFrequency"), 
60  									(Long)filter.get("nap.maxFrequency")));
61  		}
62  	
63  
64  		data = sb.toString().trim();
65  
66  		if (filter.getMediaType() != null) {
67  			sb.append(mediaString(filter.getMediaType()));
68  		}
69  		sb.append(getSearchText(filter.getText(), true));
70  		sb.append(compareString("SIZE", 
71  								(Long)filter.get(SearchFilter.MIN_FILESIZE), 
72  								(Long)filter.get(SearchFilter.MAX_FILESIZE)));
73  		  
74  		add(OpenNapServerVersion.OPENNAP044, sb.toString().trim());
75  		add(OpenNapServerVersion.SLAVANAP1, sb.toString().trim());
76  
77  		//  	sb.append(" SHOW_QUEUE");
78  		//  	sb.append(" SHOW_SOFTWARE");
79  	
80  		add(OpenNapServerVersion.SLAVANAP2, sb.toString().trim());
81      }
82  
83      // --- Method(s) ---
84  
85      public static String compareString(String key, Long min, Long max)
86      {
87  		if (min == null && max == null) {
88  			return "";
89  		}
90  
91  		StringBuffer sb = new StringBuffer();
92  		sb.append(" ");
93  		sb.append(key);
94  		sb.append(" \"");
95  
96  		if (min != null && max != null && min.equals(max)) {
97  			sb.append("EQUAL TO");		
98  			sb.append("\" \"");
99  			sb.append(min.intValue());
100 			sb.append("\"");
101 		}
102 		else if (min != null) {
103 			sb.append("AT LEAST");
104 			sb.append("\" \"");
105 			sb.append(min.intValue());
106 			sb.append("\"");
107 		}
108 		else if (max != null) {
109 			sb.append("AT BEST");
110 			sb.append("\" \"");
111 			sb.append(max.intValue());
112 			sb.append("\"");
113 		
114 		}
115 			
116 		return sb.toString();
117     }
118 	
119     public static String mediaString(MediaType mt)
120     {
121 		String realm = OpenNapPlugin.getSearchManager().getRealm(mt);
122 		if (realm != null) {
123 			StringBuffer sb = new StringBuffer();
124 			sb.append(" TYPE ");
125 			sb.append(realm);
126 			
127 			if (mt == SearchManager.MEDIA_AUDIO) {
128 				sb.append(" TYPE mp3");
129 			}
130 			
131 			return sb.toString();
132 		}
133 
134 		return "";
135     }
136 
137     public static String getSearchText(String s, boolean exclude)
138     {
139 		StringBuffer sb = new StringBuffer(s.length());
140 		StringTokenizer t = new StringTokenizer(s);
141 		while (t.hasMoreTokens()) {
142 			String token = t.nextToken();
143 			if (token.startsWith("-")) {
144 				if (exclude) {
145 					sb.append(token);
146 					sb.append(" ");
147 				}
148 			}
149 			else {
150 				if (!exclude) {
151 					sb.append(token);
152 					sb.append(" ");
153 				}	
154 			}
155 		}
156 
157 		if (sb.length() == 0) {
158 			return "";
159 		}
160 		else {
161 			// trim
162 			sb.deleteCharAt(sb.length() - 1);
163 			sb.insert(0, (exclude) ? " FILENAME EXCLUDES \"" 
164 					  : " FILENAME CONTAINS \"");
165 			sb.append("\"");
166 			return sb.toString();
167 		}
168     }
169 }