upnpmonitor script added that executes a simple test program.
[upnpmonitor] / monitor / src / main / java / org / wamblee / upnpmonitor / Main.java
1 package org.wamblee.upnpmonitor;
2
3 import java.util.logging.Logger;
4
5 import org.teleal.cling.UpnpService;
6 import org.teleal.cling.UpnpServiceImpl;
7 import org.teleal.cling.controlpoint.ControlPoint;
8 import org.teleal.cling.model.message.header.DeviceTypeHeader;
9 import org.teleal.cling.model.meta.Device;
10 import org.teleal.cling.model.types.DeviceType;
11 import org.teleal.cling.registry.DefaultRegistryListener;
12 import org.teleal.cling.registry.Registry;
13 import org.teleal.cling.registry.RegistryListener;
14
15 public class Main {
16
17     private static final Logger LOGGER = Logger.getLogger(Main.class.getName());
18
19     public static class UpnpStack {
20         private UpnpService upnpService;
21         private ControlPoint controlPoint;
22         private DeviceType deviceType;
23
24         public UpnpStack() {
25             RegistryListener listener = new DefaultRegistryListener() {
26                 @Override
27                 public void deviceAdded(Registry aRegistry, Device aDevice) {
28                     super.deviceAdded(aRegistry, aDevice);
29                     System.out.println("Device added: " +
30                         aDevice.getDisplayString());
31                     System.out.println(aDevice.getType());
32                 }
33
34                 @Override
35                 public void deviceRemoved(Registry aRegistry, Device aDevice) {
36                     super.deviceRemoved(aRegistry, aDevice);
37                     System.out.println("Device removed: " +
38                         aDevice.getDisplayString());
39                 }
40             };
41             upnpService = new UpnpServiceImpl(listener);
42             controlPoint = upnpService.getControlPoint();
43             deviceType = new DeviceType("schemas-upnp-org", "MediaServer");
44
45         }
46
47         public void search() {
48             controlPoint.search(new DeviceTypeHeader(deviceType));
49         }
50
51         public void shutdown() {
52             upnpService.shutdown();
53         }
54     }
55
56     public static void main(String[] aArgs) throws Exception {
57
58         Runtime.getRuntime().addShutdownHook(new Thread() {
59             @Override
60             public void run() {
61                 System.out.println("Shutdown hook");
62             }
63         });
64
65         for (;;) {
66             UpnpStack stack = new UpnpStack();
67             stack.search();
68             Thread.sleep(10000);
69             stack.shutdown();
70
71         }
72
73     }
74 }