This projects provide UPnP discovery and description functionality. Currently in version 1.0.1, the library can be included as a maven artifact from the not Alexa repository:
Repository | https://maven.pkg.github.com/notalexa/repo |
GroupId | not.alexa |
ArtifactId | jlib-upnp |
Version | 1.0.1 |
Currently, the library is restricted to UPnP 1.0 and IPv4 multicast. The library can be used both on the client side to discover devices and on the server side to publish devices. For the second use case, the library includes a very simple HTTP server which is started automatically if the port is specified. Note that the server is really simple. A connection is used once and no TLS is provided. The server is intended to deliver just the device descriptions published and assumes living in a local network.
The following parameters are configurable:
Multicast Address | The multicast address used for sending and receiving messages. |
Multicast Port | The multicast port used for sending and receiving messages. |
HTTP Port | The TCP port used for delivering descriptions via HTTP. (Server only) |
Time To Live | The validity of an UPnP Message in seconds. This parameter defaults to 300 (seconds) and the server broadcasts the message again before the time expires. (Server only) |
MX | Seconds to delay responds. This parameter defaults to 5 (seconds). Set by the client, the server chooses a random delay within this interval to wait for a respond. (Client only) |
Bye Bye on shutdown | iftrue , the server sends a bye bye message for each published device on shutdown. (Server only) |
When run in server mode, the library allows multiple methods to resolve the content of the device descriptor:
- as a constant
- via an URL
- via a classloader resource stream
The following code snippet asks for a device with URN urn:schemas-upnp-org:device:notalexa-butler:1
overriding the default MX value by 2 (seconds):
CountDownLatch waitLatch=new CountDownLatch(1);
try(UPnP upnp=new UPnP().setMX(2).start()) {
UPnPScanner scanner=upnp.startScan(new UPnPMessage(null,UPnP.getDefaultDeviceURN("notalexa-butler",1),null),new UPnPCallback() {
@Override
public void onMessageReceived(UPnPScanner scanner,InetAddress from,boolean reply,int searchId, UPnPMessage msg) {
registerButler(msg);
waitLatch.countDown();
}
@Override
public void onSearchTimedOut(UPnPScanner scanner,int searchId) {
waitLatch.countDown();
}
});
scanner.search(0);
waitLatch.await();
}
Assume that the file butler.xml
can be resolved as a classloader resource. The following snippet publish the butler service with a random UUID using a http server listening at port 8080:
try(UPnP upnp=new UPnP().setHttpPort(8080).start()) {
UPnPMessage msg=new UPnPMessage(UUID.randomUUID().toString(),UPnP.getDefaultDeviceURN("notalexa-butler",1), new ClassLoaderLocationDescriptor("butler.xml"));
upnp.publish(msg);
shutdownLatch.await();
}