View Javadoc
1   package example.binarylight;
2   
3   import org.fourthline.cling.UpnpService;
4   import org.fourthline.cling.UpnpServiceImpl;
5   import org.fourthline.cling.binding.*;
6   import org.fourthline.cling.binding.annotations.*;
7   import org.fourthline.cling.model.*;
8   import org.fourthline.cling.model.meta.*;
9   import org.fourthline.cling.model.types.*;
10  
11  import java.io.IOException;
12  
13  public class BinaryLightServer implements Runnable {
14  
15      public static void main(String[] args) throws Exception {
16          // Start a user thread that runs the UPnP stack
17          Thread serverThread = new Thread(new BinaryLightServer());
18          serverThread.setDaemon(false);
19          serverThread.start();
20      }
21  
22      public void run() {
23          try {
24  
25              final UpnpService upnpService = new UpnpServiceImpl();
26  
27              Runtime.getRuntime().addShutdownHook(new Thread() {
28                  @Override
29                  public void run() {
30                      upnpService.shutdown();
31                  }
32              });
33  
34              // Add the bound local device to the registry
35              upnpService.getRegistry().addDevice(
36                      createDevice()
37              );
38  
39          } catch (Exception ex) {
40              System.err.println("Exception occured: " + ex);
41              ex.printStackTrace(System.err);
42              System.exit(1);
43          }
44      }
45  
46      // DOC: CREATEDEVICE
47      LocalDevice createDevice()
48              throws ValidationException, LocalServiceBindingException, IOException {
49  
50          DeviceIdentity identity =
51                  new DeviceIdentity(
52                          UDN.uniqueSystemIdentifier("Demo Binary Light")
53                  );
54  
55          DeviceType type =
56                  new UDADeviceType("BinaryLight", 1);
57  
58          DeviceDetails details =
59                  new DeviceDetails(
60                          "Friendly Binary Light",
61                          new ManufacturerDetails("ACME"),
62                          new ModelDetails(
63                                  "BinLight2000",
64                                  "A demo light with on/off switch.",
65                                  "v1"
66                          )
67                  );
68  
69          Icon icon =
70                  new Icon(
71                          "image/png", 48, 48, 8,
72                          getClass().getResource("icon.png")
73                  );
74  
75          LocalService<SwitchPower> switchPowerService =
76                  new AnnotationLocalServiceBinder().read(SwitchPower.class);
77  
78          switchPowerService.setManager(
79                  new DefaultServiceManager(switchPowerService, SwitchPower.class)
80          );
81  
82          return new LocalDevice(identity, type, details, icon, switchPowerService);
83  
84          /* Several services can be bound to the same device:
85          return new LocalDevice(
86                  identity, type, details, icon,
87                  new LocalService[] {switchPowerService, myOtherService}
88          );
89          */
90          
91      }
92      // DOC: CREATEDEVICE
93  }