~ubuntu-branches/ubuntu/quantal/pixelmed/quantal

« back to all changes in this revision

Viewing changes to com/pixelmed/network/NetworkConfigurationFromMulticastDNS.java

  • Committer: Package Import Robot
  • Author(s): Mathieu Malaterre
  • Date: 2011-10-26 15:58:36 UTC
  • mfrom: (1.2.5)
  • Revision ID: package-import@ubuntu.com-20111026155836-twt2wlpmu9zhk2s1
Tags: 20110928-1
* New upstream
* remove ioexception.patch applied upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (c) 2001-2010, David A. Clunie DBA Pixelmed Publishing. All rights reserved. */
 
1
/* Copyright (c) 2001-2011, David A. Clunie DBA Pixelmed Publishing. All rights reserved. */
2
2
 
3
3
package com.pixelmed.network;
4
4
 
7
7
import javax.jmdns.ServiceInfo;
8
8
import javax.jmdns.ServiceListener;
9
9
 
10
 
import javax.jmdns.impl.ServiceInfoImpl;
11
 
 
12
10
import java.io.IOException;
13
11
 
14
12
import java.lang.reflect.InvocationHandler;
16
14
import java.lang.reflect.Proxy;
17
15
 
18
16
import java.util.Enumeration;
19
 
import java.util.Hashtable;
 
17
//import java.util.Hashtable;
 
18
import java.util.HashMap;
 
19
import java.util.Map;
20
20
 
21
21
/**
22
22
 * <p>This class provides a dynamic registry of DICOM network parameters
32
32
public class NetworkConfigurationFromMulticastDNS extends NetworkConfigurationSource {
33
33
 
34
34
        /***/
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 $";
36
36
        
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/"
60
60
                
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]));
67
69
                                }
71
73
                                else if (methodName.equals("serviceResolved"))  {
72
74
                                        serviceResolved((ServiceEvent)(args[0]));
73
75
                                }
74
 
                                // assert 0;
75
76
                        }
 
77
            else {
 
78
                                // (000610)
 
79
                if (methodName.equals("hashCode"))  {
 
80
                    return new Integer(System.identityHashCode(proxy));
 
81
                }
 
82
                                else if (methodName.equals("equals")) {
 
83
                    return (proxy == args[0] ? Boolean.TRUE : Boolean.FALSE);
 
84
                }
 
85
                                else if (methodName.equals("toString")) {
 
86
                    return proxy.getClass().getName() + '@' + Integer.toHexString(proxy.hashCode());
 
87
                }
 
88
            }
76
89
                        return null; 
77
90
                }
78
91
         
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);
86
 
                        if (info == null) {
 
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 :(
93
 
                        }
 
105
                        // will come back with info later as serviceResolved() event
 
106
                        // unless there really is no info, which is of course a problem :(
94
107
                }
95
108
                
96
109
                public void serviceRemoved(ServiceEvent event) {
169
182
        public void activateDiscovery(int refreshInterval) {
170
183
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.activateDNSSelfDiscovery():");
171
184
                try {
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));
176
 
                        
 
189
                        jmDNS = (JmDNS)(methodToUse.invoke(null/*since static*/,argValues));
 
190
if (debugLevel > 1) System.err.println("NetworkConfigurationFromMulticastDNS.activateDNSSelfDiscovery(): created jmDNS = "+jmDNS);
 
191
 
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();
211
 
      try {
212
 
        jmDNS.close();          // needed, since otherwise application will not exit when main thread finished
213
 
      } catch ( IOException e ) {
214
 
        e.printStackTrace(System.err);
215
 
        jmDNS = null;
216
 
      }
 
226
                        try {
 
227
                                jmDNS.close();                  // needed, since otherwise application will not exit when main thread finished
 
228
                        } catch (Exception e) {         // (000609)
 
229
                                e.printStackTrace(System.err);
 
230
                                jmDNS = null;
 
231
                        }
217
232
                }
218
233
        }
219
234
        
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);
254
269
                        }
255
270
                        if (primaryDeviceType != null && primaryDeviceType.length() > 0) {
256
271
                                properties.put(PrimaryDeviceTypeTXTRecordPropertyName,primaryDeviceType);
257
272
                        }
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);
259
274
                        try {
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};
 
277
                                // (000611)
 
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);
265
283
                        }
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("/")) {
285
303
                                        path="/"+path;
286
304
                                }
287
305
                                properties.put(WADOPathTXTRecordPropertyName,path);
288
306
                        }
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);
290
308
                        try {
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};
 
311
                                // (000611)
 
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);
297
317
                        }
298
318
                        catch (Exception e) {   // may be ClassNotFoundException,NoSuchMethodException,InstantiationException,IOException