7
7
import javax.jmdns.ServiceInfo;
8
8
import javax.jmdns.ServiceListener;
10
import javax.jmdns.impl.ServiceInfoImpl;
12
10
import java.io.IOException;
14
12
import java.lang.reflect.InvocationHandler;
16
14
import java.lang.reflect.Proxy;
18
16
import java.util.Enumeration;
19
import java.util.Hashtable;
17
//import java.util.Hashtable;
18
import java.util.HashMap;
22
22
* <p>This class provides a dynamic registry of DICOM network parameters
32
32
public class NetworkConfigurationFromMulticastDNS extends NetworkConfigurationSource {
35
private static final String identString = "@(#) $Header: /userland/cvs/pixelmed/imgbook/com/pixelmed/network/NetworkConfigurationFromMulticastDNS.java,v 1.9 2010/05/31 11:33:40 dclunie Exp $";
35
private static final String identString = "@(#) $Header: /userland/cvs/pixelmed/imgbook/com/pixelmed/network/NetworkConfigurationFromMulticastDNS.java,v 1.10 2011/09/03 11:30:39 dclunie Exp $";
37
37
private static final String DICOMServiceName = "_dicom._tcp.local.";
38
38
private static final String ACRNEMAServiceName = "_acr-nema._tcp.local.";
59
59
// See "http://java.sun.com/products/jfc/tsc/articles/generic-listener2/"
61
61
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
62
if (method.getDeclaringClass() == javax.jmdns.ServiceListener.class) {
63
String methodName = method.getName();
62
Class declaringClass = method.getDeclaringClass();
63
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.OurJmDNSServiceListener.invoke(): class = "+declaringClass);
64
String methodName = method.getName();
64
65
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.OurJmDNSServiceListener.invoke(): methodName = "+methodName);
66
if (declaringClass == javax.jmdns.ServiceListener.class) {
65
67
if (methodName.equals("serviceAdded")) {
66
68
serviceAdded((ServiceEvent)(args[0]));
71
73
else if (methodName.equals("serviceResolved")) {
72
74
serviceResolved((ServiceEvent)(args[0]));
79
if (methodName.equals("hashCode")) {
80
return new Integer(System.identityHashCode(proxy));
82
else if (methodName.equals("equals")) {
83
return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
85
else if (methodName.equals("toString")) {
86
return proxy.getClass().getName() + '@' + Integer.toHexString(proxy.hashCode());
83
96
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.OurJmDNSServiceListener: Service added : Type = "+type);
84
97
ServiceInfo info = event.getInfo();
85
98
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.OurJmDNSServiceListener: Service added : Info = "+info);
99
// Always request info, even if not null, since info may not be complete (000612)
87
100
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.OurJmDNSServiceListener: issuing request for info");
88
//jmDNS.requestServiceInfo(type,name,10000/*timeout in ms*/);
89
jmDNS.requestServiceInfo(type,name);
101
//jmDNS.requestServiceInfo(type,name,10000/*timeout in ms*/);
102
//jmDNS.requestServiceInfo(type,name);
103
jmDNS.requestServiceInfo(type,name,true/*persistent*/); // the persistent flag seems to be the key to actually getting anything back ! (000612)
90
104
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.OurJmDNSServiceListener: back from issuing request for info");
91
//will come back with info later as serviceResolved() event
92
// unless there really is no info, which is of course a problem :(
105
// will come back with info later as serviceResolved() event
106
// unless there really is no info, which is of course a problem :(
96
109
public void serviceRemoved(ServiceEvent event) {
169
182
public void activateDiscovery(int refreshInterval) {
170
183
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.activateDNSSelfDiscovery():");
172
Class classToUse = Thread.currentThread().getContextClassLoader().loadClass("javax.jmdns.impl.JmDNSImpl");
185
Class classToUse = Thread.currentThread().getContextClassLoader().loadClass("javax.jmdns.JmDNS");
173
186
Class [] argTypes = {};
187
java.lang.reflect.Method methodToUse = classToUse.getDeclaredMethod("create",argTypes); // (000611)
174
188
Object[] argValues = {};
175
jmDNS = (JmDNS)(classToUse.getConstructor(argTypes).newInstance(argValues));
189
jmDNS = (JmDNS)(methodToUse.invoke(null/*since static*/,argValues));
190
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.activateDNSSelfDiscovery(): created jmDNS = "+jmDNS);
177
192
//ServiceListener listener = new OurJmDNSServiceListener();
178
193
InvocationHandler listenerInvocationHandler = new OurJmDNSServiceListener();
179
194
ServiceListener listener = (ServiceListener) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
208
223
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.deActivateDiscovery():");
209
224
if (jmDNS != null) {
210
225
jmDNS.unregisterAllServices();
212
jmDNS.close(); // needed, since otherwise application will not exit when main thread finished
213
} catch ( IOException e ) {
214
e.printStackTrace(System.err);
227
jmDNS.close(); // needed, since otherwise application will not exit when main thread finished
228
} catch (Exception e) { // (000609)
229
e.printStackTrace(System.err);
248
263
public void registerDicomService(String calledApplicationEntityTitle,int port,String primaryDeviceType) {
249
264
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.registerDicomService():");
250
265
if (jmDNS != null) {
251
Hashtable properties = new Hashtable();
266
Map<String,String> properties = new HashMap<String,String>(); // (000611)
252
267
if (calledApplicationEntityTitle != null && calledApplicationEntityTitle.length() > 0) {
253
268
properties.put(AETTXTRecordPropertyName,calledApplicationEntityTitle);
255
270
if (primaryDeviceType != null && primaryDeviceType.length() > 0) {
256
271
properties.put(PrimaryDeviceTypeTXTRecordPropertyName,primaryDeviceType);
258
//ServiceInfo info = new ServiceInfoImpl(DICOMServiceName,calledApplicationEntityTitle,port,0/*weight*/,0/*priority*/,properties);
273
//ServiceInfo info = ServiceInfo.create(DICOMServiceName,calledApplicationEntityTitle,port,0/*weight*/,0/*priority*/,properties);
260
Class classToUse = Thread.currentThread().getContextClassLoader().loadClass("javax.jmdns.impl.ServiceInfoImpl");
261
Class [] argTypes = {String.class,String.class,java.lang.Integer.TYPE,java.lang.Integer.TYPE,java.lang.Integer.TYPE,Hashtable.class};
275
Class classToUse = Thread.currentThread().getContextClassLoader().loadClass("javax.jmdns.ServiceInfo");
276
Class [] argTypes = {String.class,String.class,java.lang.Integer.TYPE,java.lang.Integer.TYPE,java.lang.Integer.TYPE,Map.class};
278
java.lang.reflect.Method methodToUse = classToUse.getDeclaredMethod("create",argTypes);
262
279
Object[] argValues = {DICOMServiceName,calledApplicationEntityTitle,port,0/*weight*/,0/*priority*/,properties};
263
ServiceInfo info = (ServiceInfo)(classToUse.getConstructor(argTypes).newInstance(argValues));
280
ServiceInfo info = (ServiceInfo)(methodToUse.invoke(null/*since static*/,argValues));
281
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.registerDicomService(): created ServiceInfo = "+info);
264
282
jmDNS.registerService(info);
266
284
catch (Exception e) { // may be ClassNotFoundException,NoSuchMethodException,InstantiationException,IOException
279
297
public void registerWADOService(String instanceName,int port,String path) {
280
298
//System.err.println("NetworkConfigurationFromMulticastDNS.registerWADOService():");
281
299
if (jmDNS != null) {
282
Hashtable properties = new Hashtable();
300
Map<String,String> properties = new HashMap<String,String>(); // (000611)
283
301
if (path != null && path.length() > 0) {
284
302
if (!path.startsWith("/")) {
287
305
properties.put(WADOPathTXTRecordPropertyName,path);
289
//ServiceInfo info = new ServiceInfoImpl(WADOServiceName,instanceName,port,0/*weight*/,0/*priority*/,properties);
307
//ServiceInfo info = ServiceInfo.create(WADOServiceName,instanceName,port,0/*weight*/,0/*priority*/,properties);
291
Class classToUse = Thread.currentThread().getContextClassLoader().loadClass("javax.jmdns.impl.ServiceInfoImpl");
292
Class [] argTypes = {String.class,String.class,java.lang.Integer.TYPE,java.lang.Integer.TYPE,java.lang.Integer.TYPE,Hashtable.class};
309
Class classToUse = Thread.currentThread().getContextClassLoader().loadClass("javax.jmdns.ServiceInfo");
310
Class [] argTypes = {String.class,String.class,java.lang.Integer.TYPE,java.lang.Integer.TYPE,java.lang.Integer.TYPE,Map.class};
312
java.lang.reflect.Method methodToUse = classToUse.getDeclaredMethod("create",argTypes);
293
313
Object[] argValues = {WADOServiceName,instanceName,port,0/*weight*/,0/*priority*/,properties};
294
ServiceInfo info = (ServiceInfo)(classToUse.getConstructor(argTypes).newInstance(argValues));
295
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.registerWADOService(): info = "+info);
314
ServiceInfo info = (ServiceInfo)(methodToUse.invoke(null/*since static*/,argValues));
315
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.registerWADOService(): created ServiceInfo = "+info);
296
316
jmDNS.registerService(info);
298
318
catch (Exception e) { // may be ClassNotFoundException,NoSuchMethodException,InstantiationException,IOException