Unable to send UPnP search message in Java via jupnp

61 Views Asked by At

I am trying to send a UPnP search message in Java using jupnp. However, the documentation appears out of date and trying to send a search message results in the ControlPoint being null and therefore I am unable to send the message. This is my code so far:

package xyz.necrozma.upnpplugin;

import org.bukkit.plugin.java.JavaPlugin;

import org.jupnp.DefaultUpnpServiceConfiguration;
import org.jupnp.controlpoint.ControlPoint;
import org.jupnp.model.message.header.STAllHeader;
import org.jupnp.UpnpService;
import org.jupnp.UpnpServiceImpl;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public final class main extends JavaPlugin {

    private final Logger logger = LoggerFactory.getLogger(main.class);
    private UpnpService upnpService;

    private int port = 25565;

    @Override
    public void onEnable() {

        port = getServer().getPort();
        logger.info("Trying to open port " + port + " for UPNP");


        upnpService = new UpnpServiceImpl(new DefaultUpnpServiceConfiguration());

        logger.info("UPNP service created, searching for devices, please wait...");

        ControlPoint controlPoint = upnpService.getControlPoint();

        if (controlPoint != null) {
            controlPoint.search(new STAllHeader());

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

            logger.info("Search complete, found " + upnpService.getRegistry().getRemoteDevices().size() + " devices.");

        } else {
            logger.error("ControlPoint is null, unable to perform search.");
        }

        upnpService.getRegistry().getRemoteDevices().forEach((remoteDevice) -> {
            logger.info("Found device: " + remoteDevice.getDisplayString());
        });

        }

    @Override
    public void onDisable() {
        logger.info("UPNP service stopping...");
        if (upnpService != null) {
            upnpService.shutdown(); // Shut down the UPnP service
        }
    }
}

I am hoping someone has some experience or knowledge of how to use this library, as I would appreciate some guidance.

1

There are 1 best solutions below

0
Ethan James On

After reading through other examples, I have fixed the null ControlPoint error by adding upnpService.startup(); after creating the service.