Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8d5d9311f420cc872985e31ef37491b6a20571fd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/****************************************************************************
 * Copyright (c) 2005, 2010 Jan S. Rellermeyer, Systems Group,
 * Department of Computer Science, ETH Zurich and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Jan S. Rellermeyer - initial API and implementation
 *    Markus Alexander Kuppe - enhancements and bug fixes
 *    Md.Jamal MohiUddin (Ubiquitous Computing, C-DAC Hyderabad) - IPv6 support
 *    P Sowjanya (Ubiquitous Computing, C-DAC Hyderabad) - IPv6 support
 *
*****************************************************************************/
package ch.ethz.iks.slp.impl;

import java.util.ArrayList;
import java.util.Dictionary;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import ch.ethz.iks.slp.ServiceType;
import ch.ethz.iks.slp.ServiceURL;

/**
 * Utility class.
 * 
 * @author Jan S. Rellermeyer, ETH Z�rich
 * @since 0.1
 */
final class SLPUtils {

	/**
	 * hidden constructor.
	 */
	private SLPUtils() {

	}

	/**
	 * get a <code>List</code> of attribute/value pairs in String
	 * representation from a <code>Dictionary</code>.
	 * 
	 * @param attributes
	 *            the <code>Dictionary</code>
	 * @return the <code>List</code>.
	 */
	static List dictToAttrList(final Dictionary attributes) {
		List attList = new ArrayList();
		if (attributes != null) {
			for (Enumeration keys = attributes.keys(); keys.hasMoreElements();) {
				Object key = keys.nextElement();
				StringBuffer buffer = new StringBuffer();
				buffer.append("(");
				buffer.append(key);
				buffer.append("=");
				buffer.append(attributes.get(key));
				buffer.append(")");
				attList.add(buffer.toString());
			}
		}
		return attList;
	}

	/**
	 * get a <code>Dictionary</code> of attributes and their values from an
	 * attribute <code>List</code>.
	 * 
	 * @since 0.6
	 * @param attrList
	 *            the attribute list.
	 * @return the <code>Dictionary</code>.
	 */
	static Dictionary attrListToDict(final List attrList) {
		Dictionary dict = new Hashtable();

		for (Iterator iter = attrList.iterator(); iter.hasNext();) {
			String attrStr = (String) iter.next();
			attrStr = attrStr.substring(1, attrStr.length() - 1);
			int pos = attrStr.indexOf("=");
			if (pos > -1) {
				String key = attrStr.substring(0, pos).trim();
				String value = attrStr.substring(pos + 1).trim();
				dict.put(key, value);
			}
		}

		return dict;
	}

	/**
	 * add a value to a value list in a Map.
	 * 
	 * @param map
	 *            the map.
	 * @param key
	 *            the key.
	 * @param value
	 *            the value to be added to the list.
	 */
	static void addValue(final Map map, final Object key, final Object value) {

		List values;
		if ((values = (List) map.get(key)) == null) {
			values = new ArrayList();
		}
		if (values.contains(value)) {
			return;
		}
		values.add(value);
		map.put(key, values);
	}

	/**
	 * remove a value from a value list in a Map.
	 * 
	 * @param map
	 *            the map.
	 * @param key
	 *            the key.
	 * @param value
	 *            the value to be removed from the list.
	 */
	static void removeValue(final Map map, final Object key, final Object value) {
		List values;
		if ((values = (List) map.get(key)) == null) {
			return;
		}
		values.remove(value);
		if (!values.isEmpty()) {
			map.put(key, values);
		} else {
			map.remove(key);
		}
	}

	/**
	 * remove a value from all keys where it occurs.
	 * 
	 * @param map
	 *            the map.
	 * @param value
	 *            the value.
	 */
	static void removeValueFromAll(final Map map, final Object value) {
		final Object[] keys = map.keySet().toArray();
		for (int i = 0; i < keys.length; i++) {
			List list = (List) map.get(keys[i]);
			list.remove(value);
			if (list.isEmpty()) {
				map.remove(keys[i]);
			}
		}
	}

	/**
	 * get the current timestamp as defined in RFC 2608.
	 * 
	 * @return the current timestamp.
	 */
	static int getTimestamp() {
		long systemTime = System.currentTimeMillis();
		systemTime /= 1000;
		return (int) systemTime;
	}

	/**
	 * find case insensitive matching between a key List and a Dictionary of
	 * attributes.
	 * 
	 * @param keyList
	 *            the key List.
	 * @param attributes
	 *            the attribute Dictionary.
	 * @return a List of matches.
	 */
	static List findMatches(final List keyList, final Dictionary attributes) {
		List results = new ArrayList();
		Set caseInsensitiveKeyList = new HashSet();
		List wildcards = new ArrayList();
		if (!keyList.isEmpty()) {
			for (Iterator keys = keyList.iterator(); keys.hasNext();) {
				String key = (String) keys.next();
				if (key.indexOf("*") == -1) {
					caseInsensitiveKeyList.add(key.toLowerCase());
				} else {
					wildcards.add(key);
				}
			}
		}

		for (Enumeration keys = attributes.keys(); keys.hasMoreElements();) {
			String key = (String) keys.nextElement();
			if (keyList.isEmpty()
					|| caseInsensitiveKeyList.contains(key.toLowerCase())) {
				results.add("(" + key + "=" + attributes.get(key).toString()
						+ ")");
				continue;
			}
			for (Iterator iter = wildcards.iterator(); iter.hasNext();) {
				String wildcard = (String) iter.next();
				if (equalsWithWildcard(wildcard.toCharArray(), 0, key
						.toCharArray(), 0)) {
					results.add("(" + key + "="
							+ attributes.get(key).toString() + ")");
					continue;
				}
			}

		}
		return results;
	}

	/**
	 * equality check with wildcards
	 * 
	 * @param val
	 *            the value
	 * @param valIndex
	 *            the current position within the value
	 * @param attr
	 *            the attribute
	 * @param attrIndex
	 *            the current position within the attribute.
	 * @return true if equals.
	 */
	private static boolean equalsWithWildcard(char[] val, int valIndex,
			char[] attr, int attrIndex) {
		if (val.length == valIndex) {
			return attr.length == attrIndex;
		}
		if (val[valIndex] == '*') {
			valIndex++;
			do {
				if (equalsWithWildcard(val, valIndex, attr, attrIndex)) {
					return true;
				}
				attrIndex++;
			} while (attr.length - attrIndex > -1);
			return false;
		} else {
			return (attr.length == attrIndex || attr[attrIndex] != val[valIndex]) ? false
					: equalsWithWildcard(val, ++valIndex, attr, ++attrIndex);
		}
	}
	
	static String hash(ServiceURL aURL) {
		String service = extractService(aURL.toString());
		return hash(service);
	}

	static String hash(ServiceType aType) {
		String service = extractService(aType.toString());
		return hash(service);
	}
	
	/* Hash Algorithm for SLP Service Type String(RFC 3111) */
	private static String hash(String pc) {
		String ip = "FF02::1:1";
		
		/*
		 * An unsigned 32-bit value v is initialized to 0.Each byte of the
		 * character is considered consecutively
		 */
		int h = 0;

		for (int i = 0; i < pc.length(); i++) {
			/*
			 * The current value v is multiplied by 33.then the value of the
			 * current string byte is added ,Each byte in the service type
			 * string is processed in this manner
			 */
			h *= 33;
			h += pc.charAt(i);
		}
		/* The result is contained in the low order 10 bits of v */
		int j = (0x3ff & h);
		// Concatenating the value with the IP
		return ip + Integer.toHexString(j);
	}

	private static String extractService(String url) {
		int position = url.indexOf(':', 0);
		if (position == -1) {
			return url;
		}
		position = url.indexOf(':', position + 1);
		if (position == -1) {
			return url;
		}
		return url.substring(0, position);
	}
}

Back to the top