View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling;
17  
18  import org.fourthline.cling.model.message.header.STAllHeader;
19  import org.fourthline.cling.model.meta.LocalDevice;
20  import org.fourthline.cling.model.meta.RemoteDevice;
21  import org.fourthline.cling.registry.Registry;
22  import org.fourthline.cling.registry.RegistryListener;
23  
24  /**
25   * Runs a simple UPnP discovery procedure.
26   */
27  public class Main {
28  
29      public static void main(String[] args) throws Exception {
30  
31          // UPnP discovery is asynchronous, we need a callback
32          RegistryListener listener = new RegistryListener() {
33  
34              public void remoteDeviceDiscoveryStarted(Registry registry,
35                                                       RemoteDevice device) {
36                  System.out.println(
37                          "Discovery started: " + device.getDisplayString()
38                  );
39              }
40  
41              public void remoteDeviceDiscoveryFailed(Registry registry, RemoteDevice device, Exception ex) {
42                  System.out.println(
43                          "Discovery failed: " + device.getDisplayString() + " => " + ex
44                  );
45              }
46  
47              public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
48                  System.out.println(
49                          "Remote device available: " + device.getDisplayString()
50                  );
51              }
52  
53              public void remoteDeviceUpdated(Registry registry, RemoteDevice device) {
54                  System.out.println(
55                          "Remote device updated: " + device.getDisplayString()
56                  );
57              }
58  
59              public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
60                  System.out.println(
61                          "Remote device removed: " + device.getDisplayString()
62                  );
63              }
64  
65              public void localDeviceAdded(Registry registry, LocalDevice device) {
66                  System.out.println(
67                          "Local device added: " + device.getDisplayString()
68                  );
69              }
70  
71              public void localDeviceRemoved(Registry registry, LocalDevice device) {
72                  System.out.println(
73                          "Local device removed: " + device.getDisplayString()
74                  );
75              }
76  
77              public void beforeShutdown(Registry registry) {
78                  System.out.println(
79                          "Before shutdown, the registry has devices: " + registry.getDevices().size()
80                  );
81              }
82  
83              public void afterShutdown() {
84                  System.out.println("Shutdown of registry complete!");
85  
86              }
87          };
88  
89          // This will create necessary network resources for UPnP right away
90          System.out.println("Starting Cling...");
91          UpnpService upnpService = new UpnpServiceImpl(listener);
92  
93          // Send a search message to all devices and services, they should respond soon
94          System.out.println("Sending SEARCH message to all devices...");
95          upnpService.getControlPoint().search(new STAllHeader());
96  
97          // Let's wait 10 seconds for them to respond
98          System.out.println("Waiting 10 seconds before shutting down...");
99          Thread.sleep(10000);
100 
101         // Release all resources and advertise BYEBYE to other UPnP devices
102         System.out.println("Stopping Cling...");
103         upnpService.shutdown();
104     }
105 }
106