Skip to main content
summaryrefslogtreecommitdiffstats
blob: b1e02ea8c142264f06917a52e99f637967456fd8 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/****************************************************************************
 * Copyright (c) 2007 Composent, Inc. 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:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/
package org.eclipse.ecf.provider.jmdns.container;

import java.io.*;
import java.net.URI;
import java.util.*;
import javax.jmdns.*;
import javax.jmdns.ServiceInfo;
import org.eclipse.core.runtime.Assert;
import org.eclipse.ecf.core.ContainerConnectException;
import org.eclipse.ecf.core.events.*;
import org.eclipse.ecf.core.identity.ID;
import org.eclipse.ecf.core.identity.IDFactory;
import org.eclipse.ecf.core.security.IConnectContext;
import org.eclipse.ecf.core.util.*;
import org.eclipse.ecf.discovery.*;
import org.eclipse.ecf.discovery.identity.*;
import org.eclipse.ecf.discovery.service.IDiscoveryService;
import org.eclipse.ecf.internal.provider.jmdns.*;
import org.eclipse.ecf.provider.jmdns.identity.JMDNSNamespace;

public class JMDNSDiscoveryContainer extends AbstractDiscoveryContainerAdapter implements IDiscoveryService, ServiceListener, ServiceTypeListener {

	private static final String SCHEME_PROPERTY = "jmdns.ptcl"; //$NON-NLS-1$
	private static final String URI_PATH_PROPERTY = "path"; //$NON-NLS-1$
	private static final String NAMING_AUTHORITY_PROPERTY = "jmdns.namingauthority"; //$NON-NLS-1$

	public static final int DEFAULT_REQUEST_TIMEOUT = 3000;

	private static int instanceCount = 0;

	JmDNS jmdns = null;
	private ID targetID = null;

	List serviceTypes = null;

	/**
	 * Map of IServiceInfos (maps to SRV type records) discovered by mDNS.
	 * mDNS defines a two stage process where a client first discovers the PTR (announced by the advertiser) and then upon 
	 * request from the application layers above resolves the PTR into a SRV. 
	 * Deregistration is only send for PTR records, not for SRV records (SRV records have a very low TTL anyway). 
	 */
	final Map services = Collections.synchronizedMap(new HashMap());

	boolean disposed = false;
	final Object lock = new Object();

	SimpleFIFOQueue queue = null;
	Thread notificationThread = null;

	/**
	 * @since 4.0
	 */
	public JMDNSDiscoveryContainer() {
		super(JMDNSNamespace.NAME, new DiscoveryContainerConfig(IDFactory.getDefault().createStringID(JMDNSDiscoveryContainer.class.getName() + ";" + instanceCount++))); //$NON-NLS-1$  //$NON-NLS-2$
		serviceTypes = new ArrayList();
	}

	/****************** IContainer methods **************************/

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.core.IContainer#getConnectedID()
	 */
	public ID getConnectedID() {
		return this.targetID;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.AbstractDiscoveryContainerAdapter#dispose()
	 */
	public void dispose() {
		synchronized (lock) {
			super.dispose();
			disposed = true;
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.core.IContainer#connect(org.eclipse.ecf.core.identity.ID, org.eclipse.ecf.core.security.IConnectContext)
	 */
	public void connect(final ID targetID1, final IConnectContext joinContext) throws ContainerConnectException {
		synchronized (lock) {
			if (disposed)
				throw new ContainerConnectException("Container has been disposed"); //$NON-NLS-1$
			if (this.targetID != null)
				throw new ContainerConnectException("Already connected"); //$NON-NLS-1$
			this.targetID = (targetID1 == null) ? getConfig().getID() : targetID1;
			fireContainerEvent(new ContainerConnectingEvent(this.getID(), this.targetID, joinContext));
			initializeQueue();
			try {
				this.jmdns = JmDNS.create();
				jmdns.addServiceTypeListener(this);
			} catch (final IOException e) {
				Trace.catching(JMDNSPlugin.PLUGIN_ID, JMDNSDebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "connect", e); //$NON-NLS-1$
				if (this.jmdns != null) {
					jmdns.close();
					jmdns = null;
				}
				throw new ContainerConnectException("Cannot create JmDNS instance", e); //$NON-NLS-1$
			}
			fireContainerEvent(new ContainerConnectedEvent(this.getID(), this.targetID));
		}
	}

	private void initializeQueue() {
		queue = new SimpleFIFOQueue();
		notificationThread = new Thread(new Runnable() {
			public void run() {
				while (!disposed || queue.isStopped()) {
					if (Thread.currentThread().isInterrupted())
						break;
					final Runnable runnable = (Runnable) queue.dequeue();
					if (Thread.currentThread().isInterrupted() || runnable == null)
						break;
					try {
						runnable.run();
					} catch (final Throwable t) {
						JMDNSPlugin plugin = JMDNSPlugin.getDefault();
						if (plugin != null) {
							plugin.logException("handleRuntimeException", t); //$NON-NLS-1$
						}
					}
				}
				System.err.println("thread exited");
				return;
			}
		}, "JMDNS Discovery Thread"); //$NON-NLS-1$
		notificationThread.start();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.core.IContainer#disconnect()
	 */
	public void disconnect() {
		synchronized (lock) {
			if (getConnectedID() == null || disposed) {
				return;
			}
			final ID connectedID = getConnectedID();
			fireContainerEvent(new ContainerDisconnectingEvent(this.getID(), connectedID));
			queue.close();
			notificationThread.interrupt();
			notificationThread = null;
			this.targetID = null;
			serviceTypes.clear();
			jmdns.close();
			jmdns = null;
			fireContainerEvent(new ContainerDisconnectedEvent(this.getID(), connectedID));
		}
	}

	/************************* IDiscoveryContainerAdapter methods *********************/

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#getServiceInfo(org.eclipse.ecf.discovery.identity.IServiceID)
	 */
	public IServiceInfo getServiceInfo(final IServiceID service) {
		Assert.isNotNull(service);
		synchronized (lock) {
			try {
				// ECF discovery API defines identity to be the service type and the URI (location)
				// see https://bugs.eclipse.org/266723
				final ServiceInfo[] serviceInfos = jmdns.list(service.getServiceTypeID().getInternal());
				for (int i = 0; i < serviceInfos.length; i++) {
					ServiceInfo serviceInfo = serviceInfos[i];
					IServiceInfo iServiceInfo = createIServiceInfoFromServiceInfo(serviceInfo);
					Assert.isNotNull(iServiceInfo);
					Assert.isNotNull(iServiceInfo.getServiceID());
					if (iServiceInfo.getServiceID().equals(service)) {
						return iServiceInfo;
					}
				}
				return null;
			} catch (final Exception e) {
				Trace.catching(JMDNSPlugin.PLUGIN_ID, JMDNSDebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "getServiceInfo", e); //$NON-NLS-1$
				return null;
			}
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#getServices()
	 */
	public IServiceInfo[] getServices() {
		synchronized (lock) {
			final IServiceTypeID[] serviceTypeArray = getServiceTypes();
			final List results = new ArrayList();
			for (int i = 0; i < serviceTypeArray.length; i++) {
				final IServiceTypeID stid = serviceTypeArray[i];
				if (stid != null)
					results.addAll(Arrays.asList(getServices(stid)));
			}
			return (IServiceInfo[]) results.toArray(new IServiceInfo[] {});
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#getServices(org.eclipse.ecf.discovery.identity.IServiceTypeID)
	 */
	public IServiceInfo[] getServices(final IServiceTypeID type) {
		Assert.isNotNull(type);
		final List serviceInfos = new ArrayList();
		synchronized (lock) {
			// We don't know the naming authority yet (it's part of the service properties)
			for (final Iterator itr = serviceTypes.iterator(); itr.hasNext();) {
				final IServiceTypeID serviceType = (IServiceTypeID) itr.next();
				if (Arrays.equals(serviceType.getServices(), type.getServices()) && Arrays.equals(serviceType.getProtocols(), type.getProtocols()) && Arrays.equals(serviceType.getScopes(), type.getScopes())) {
					final ServiceInfo[] infos = jmdns.list(type.getInternal());
					for (int i = 0; i < infos.length; i++) {
						try {
							if (infos[i] != null) {
								final IServiceInfo si = createIServiceInfoFromServiceInfo(infos[i]);
								if (si != null)
									serviceInfos.add(si);
							}
						} catch (final Exception e) {
							Trace.catching(JMDNSPlugin.PLUGIN_ID, JMDNSDebugOptions.EXCEPTIONS_CATCHING, this.getClass(), "getServices", e); //$NON-NLS-1$
						}
					}
				}
			}
			return (IServiceInfo[]) serviceInfos.toArray(new IServiceInfo[] {});
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#getServiceTypes()
	 */
	public IServiceTypeID[] getServiceTypes() {
		synchronized (lock) {
			return (IServiceTypeID[]) serviceTypes.toArray(new IServiceTypeID[] {});
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#registerService(org.eclipse.ecf.discovery.IServiceInfo)
	 */
	public void registerService(final IServiceInfo serviceInfo) {
		Assert.isNotNull(serviceInfo);
		final ServiceInfo svcInfo = createServiceInfoFromIServiceInfo(serviceInfo);
		checkServiceInfo(svcInfo);
		try {
			jmdns.registerService(svcInfo);
		} catch (final IOException e) {
			throw new ECFRuntimeException("Exception registering service", e); //$NON-NLS-1$
		}
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.IDiscoveryContainerAdapter#unregisterService(org.eclipse.ecf.discovery.IServiceInfo)
	 */
	public void unregisterService(final IServiceInfo serviceInfo) {
		Assert.isNotNull(serviceInfo);
		final ServiceInfo si = createServiceInfoFromIServiceInfo(serviceInfo);
		jmdns.unregisterService(si);
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.AbstractDiscoveryContainerAdapter#unregisterAllServices()
	 */
	public void unregisterAllServices() {
		jmdns.unregisterAllServices();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.AbstractDiscoveryContainerAdapter#purgeCache()
	 */
	public IServiceInfo[] purgeCache() {
		synchronized (lock) {
			serviceTypes.clear();
		}
		return super.purgeCache();
	}

	/**************************** JMDNS listeners ***********************************/

	private void runInThread(final Runnable runnable) {
		queue.enqueue(runnable);
	}

	/* (non-Javadoc)
	 * @see javax.jmdns.ServiceTypeListener#serviceTypeAdded(javax.jmdns.ServiceEvent)
	 */
	public void serviceTypeAdded(final ServiceEvent arg0) {
		Trace.trace(JMDNSPlugin.PLUGIN_ID, "serviceTypeAdded(" + arg0.getType() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
		arg0.getDNS().addServiceListener(arg0.getType(), JMDNSDiscoveryContainer.this);
	}

	void fireTypeDiscovered(final IServiceTypeID serviceType) {
		fireServiceTypeDiscovered(new ServiceTypeContainerEvent(serviceType, getID()));
	}

	/* (non-Javadoc)
	 * @see javax.jmdns.ServiceListener#serviceAdded(javax.jmdns.ServiceEvent)
	 */
	public void serviceAdded(final ServiceEvent arg0) {
		Trace.trace(JMDNSPlugin.PLUGIN_ID, "serviceAdded(" + arg0.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
		runInThread(new Runnable() {
			public void run() {
				final String serviceType = arg0.getType();
				final String serviceName = arg0.getName();
				IServiceInfo aServiceInfo = null;
				synchronized (lock) {
					if (getConnectedID() == null || disposed) {
						return;
					}

					// explicitly get the service to determine the naming authority (part of the service properties)
					try {
						final ServiceInfo info = arg0.getDNS().getServiceInfo(serviceType, serviceName);
						aServiceInfo = createIServiceInfoFromServiceInfo(info);
						services.put(serviceType + serviceName, aServiceInfo);
						serviceTypes.add(aServiceInfo.getServiceID().getServiceTypeID());
					} catch (final Exception e) {
						Trace.trace(JMDNSPlugin.PLUGIN_ID, "Failed to resolve in serviceAdded(" + arg0.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
						return;
					}
				}
				fireTypeDiscovered(aServiceInfo.getServiceID().getServiceTypeID());
				fireDiscovered(aServiceInfo);
			}
		});
	}

	/* (non-Javadoc)
	 * @see javax.jmdns.ServiceListener#serviceRemoved(javax.jmdns.ServiceEvent)
	 */
	public void serviceRemoved(final ServiceEvent arg0) {
		Trace.trace(JMDNSPlugin.PLUGIN_ID, "serviceRemoved(" + arg0.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
		runInThread(new Runnable() {
			public void run() {
				if (getConnectedID() == null || disposed) {
					return;
				}
				final String serviceType = arg0.getType();
				final String serviceName = arg0.getName();
				IServiceInfo aServiceInfo = (IServiceInfo) services.remove(serviceType + serviceName);
				if (aServiceInfo == null) {
					Trace.trace(JMDNSPlugin.PLUGIN_ID, "Failed to resolve in serviceRemoved(" + arg0.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
					return;
				}
				fireUndiscovered(aServiceInfo);
			}
		});
	}

	void fireUndiscovered(final IServiceInfo serviceInfo) {
		fireServiceUndiscovered(new ServiceContainerEvent(serviceInfo, getID()));
	}

	/* (non-Javadoc)
	 * @see javax.jmdns.ServiceListener#serviceResolved(javax.jmdns.ServiceEvent)
	 */
	public void serviceResolved(final ServiceEvent arg0) {
		Trace.trace(JMDNSPlugin.PLUGIN_ID, "serviceResolved(" + arg0.getName() + ")"); //$NON-NLS-1$ //$NON-NLS-2$
	}

	void fireDiscovered(final IServiceInfo serviceInfo) {
		fireServiceDiscovered(new ServiceContainerEvent(serviceInfo, getID()));
	}

	/*******************************************/
	private void checkServiceInfo(final ServiceInfo serviceInfo) {
		final String serviceName = serviceInfo.getName();
		if (serviceName == null)
			throw new ECFRuntimeException("Service name cannot be null"); //$NON-NLS-1$
	}

	IServiceInfo createIServiceInfoFromServiceInfo(final ServiceInfo serviceInfo) throws Exception {
		Assert.isNotNull(serviceInfo);
		final int priority = serviceInfo.getPriority();
		final int weight = serviceInfo.getWeight();
		final Properties props = new Properties();
		String uriProtocol = null;
		String uriPath = null;
		String namingAuthority = IServiceTypeID.DEFAULT_NA;
		for (final Enumeration e = serviceInfo.getPropertyNames(); e.hasMoreElements();) {
			final String key = (String) e.nextElement();
			if (SCHEME_PROPERTY.equals(key)) {
				uriProtocol = serviceInfo.getPropertyString(key);
			} else if (NAMING_AUTHORITY_PROPERTY.equals(key)) {
				namingAuthority = serviceInfo.getPropertyString(key);
			} else if (URI_PATH_PROPERTY.equals(key)) {
				uriPath = serviceInfo.getPropertyString(key);
			} else {
				final byte[] bytes = serviceInfo.getPropertyBytes(key);
				try {
					final ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(bytes));
					final Object object = in.readObject();
					in.close();
					props.put(key, object);
				} catch (final StreamCorruptedException ioe) {
					props.put(key, serviceInfo.getPropertyString(key));
				} catch (final EOFException eofe) { // not all byte[] are serialized objs (e.g. a native service)
					props.put(key, serviceInfo.getPropertyString(key));
				}
			}
		}

		// proto
		final String proto = serviceInfo.getProtocol();
		// scopes
		final String domain = serviceInfo.getDomain();
		final String[] scopes = new String[] {domain};

		// uri
		String authority = serviceInfo.getHostAddress() + ":" + serviceInfo.getPort();
		final URI uri = new URI(uriProtocol == null ? proto : uriProtocol, authority, uriPath, null, null);
		// service type
		String st = serviceInfo.getType();
		final int end = st.indexOf(proto);
		String[] types = StringUtils.split(st.substring(1, end), "._");
		final IServiceTypeID sID = ServiceIDFactory.getDefault().createServiceTypeID(getServicesNamespace(), types, scopes, new String[] {proto}, namingAuthority);

		// service name
		final String name = serviceInfo.getName();

		return new org.eclipse.ecf.discovery.ServiceInfo(uri, name, sID, priority, weight, new ServiceProperties(props));
	}

	private ServiceInfo createServiceInfoFromIServiceInfo(final IServiceInfo serviceInfo) {
		if (serviceInfo == null)
			return null;
		final IServiceID sID = serviceInfo.getServiceID();
		final Hashtable props = new Hashtable();
		final IServiceProperties svcProps = serviceInfo.getServiceProperties();
		if (svcProps != null) {
			for (final Enumeration e = svcProps.getPropertyNames(); e.hasMoreElements();) {
				final String key = (String) e.nextElement();
				final Object val = svcProps.getProperty(key);
				if (val instanceof String) {
					props.put(key, val);
				} else if (val instanceof Serializable) {
					final ByteArrayOutputStream bos = new ByteArrayOutputStream();
					try {
						final ObjectOutputStream out = new ObjectOutputStream(bos);
						out.writeObject(val);
						out.close();
					} catch (final IOException e1) {
						e1.printStackTrace();
					}
					final byte[] buf = bos.toByteArray();
					props.put(key, buf);
					//				} else if (svcProps.getPropertyBytes(key) != null) {
					//					byte[] bytes = svcProps.getPropertyBytes(key);
					//					props.put(key, bytes);
				} else if (val != null) {
					props.put(key, val.toString());
				}
			}
		}
		// Add URI scheme to props
		final URI location = serviceInfo.getServiceID().getLocation();
		if (location != null) {
			props.put(SCHEME_PROPERTY, location.getScheme());
			props.put(URI_PATH_PROPERTY, location.getPath());
		}
		props.put(NAMING_AUTHORITY_PROPERTY, serviceInfo.getServiceID().getServiceTypeID().getNamingAuthority());
		final ServiceInfo si = ServiceInfo.create(sID.getServiceTypeID().getInternal(), serviceInfo.getServiceName(), location.getPort(), serviceInfo.getWeight(), serviceInfo.getPriority(), props);
		return si;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.ecf.discovery.AbstractDiscoveryContainerAdapter#getContainerName()
	 */
	public String getContainerName() {
		return JMDNSPlugin.NAME;
	}
}

Back to the top