Use constructor injection (#1210)
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>pull/1219/head
parent
f682cb56e6
commit
b34ef3cf13
|
@ -1,5 +1,6 @@
|
||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
|
||||||
<modelVersion>4.0.0</modelVersion>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
|
|
@ -56,7 +56,12 @@ public class MDNSClientImpl implements MDNSClient, NetworkAddressChangeListener
|
||||||
|
|
||||||
private final Set<ServiceDescription> activeServices = ConcurrentHashMap.newKeySet();
|
private final Set<ServiceDescription> activeServices = ConcurrentHashMap.newKeySet();
|
||||||
|
|
||||||
private NetworkAddressService networkAddressService;
|
private final NetworkAddressService networkAddressService;
|
||||||
|
|
||||||
|
@Activate
|
||||||
|
public MDNSClientImpl(final @Reference NetworkAddressService networkAddressService) {
|
||||||
|
this.networkAddressService = networkAddressService;
|
||||||
|
}
|
||||||
|
|
||||||
private Set<InetAddress> getAllInetAddresses() {
|
private Set<InetAddress> getAllInetAddresses() {
|
||||||
final Set<InetAddress> addresses = new HashSet<>();
|
final Set<InetAddress> addresses = new HashSet<>();
|
||||||
|
@ -132,6 +137,7 @@ public class MDNSClientImpl implements MDNSClient, NetworkAddressChangeListener
|
||||||
|
|
||||||
@Activate
|
@Activate
|
||||||
protected void activate() {
|
protected void activate() {
|
||||||
|
networkAddressService.addNetworkAddressChangeListener(this);
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,6 +158,7 @@ public class MDNSClientImpl implements MDNSClient, NetworkAddressChangeListener
|
||||||
public void deactivate() {
|
public void deactivate() {
|
||||||
close();
|
close();
|
||||||
activeServices.clear();
|
activeServices.clear();
|
||||||
|
networkAddressService.removeNetworkAddressChangeListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -273,15 +280,4 @@ public class MDNSClientImpl implements MDNSClient, NetworkAddressChangeListener
|
||||||
close();
|
close();
|
||||||
start();
|
start();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Reference
|
|
||||||
protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
|
|
||||||
this.networkAddressService = networkAddressService;
|
|
||||||
networkAddressService.addNetworkAddressChangeListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
|
|
||||||
networkAddressService.removeNetworkAddressChangeListener(this);
|
|
||||||
this.networkAddressService = null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,10 +17,10 @@ import java.util.Set;
|
||||||
import java.util.concurrent.CopyOnWriteArraySet;
|
import java.util.concurrent.CopyOnWriteArraySet;
|
||||||
import java.util.concurrent.Executors;
|
import java.util.concurrent.Executors;
|
||||||
|
|
||||||
|
import org.eclipse.jdt.annotation.Nullable;
|
||||||
import org.eclipse.smarthome.io.transport.mdns.MDNSClient;
|
import org.eclipse.smarthome.io.transport.mdns.MDNSClient;
|
||||||
import org.eclipse.smarthome.io.transport.mdns.MDNSService;
|
import org.eclipse.smarthome.io.transport.mdns.MDNSService;
|
||||||
import org.eclipse.smarthome.io.transport.mdns.ServiceDescription;
|
import org.eclipse.smarthome.io.transport.mdns.ServiceDescription;
|
||||||
import org.osgi.service.component.annotations.Activate;
|
|
||||||
import org.osgi.service.component.annotations.Component;
|
import org.osgi.service.component.annotations.Component;
|
||||||
import org.osgi.service.component.annotations.Deactivate;
|
import org.osgi.service.component.annotations.Deactivate;
|
||||||
import org.osgi.service.component.annotations.Reference;
|
import org.osgi.service.component.annotations.Reference;
|
||||||
|
@ -37,43 +37,36 @@ import org.slf4j.LoggerFactory;
|
||||||
*/
|
*/
|
||||||
@Component(immediate = true)
|
@Component(immediate = true)
|
||||||
public class MDNSServiceImpl implements MDNSService {
|
public class MDNSServiceImpl implements MDNSService {
|
||||||
|
|
||||||
private final Logger logger = LoggerFactory.getLogger(MDNSServiceImpl.class);
|
private final Logger logger = LoggerFactory.getLogger(MDNSServiceImpl.class);
|
||||||
private MDNSClient mdnsClient;
|
|
||||||
|
private @Nullable MDNSClient mdnsClient;
|
||||||
|
|
||||||
private final Set<ServiceDescription> servicesToRegisterQueue = new CopyOnWriteArraySet<>();
|
private final Set<ServiceDescription> servicesToRegisterQueue = new CopyOnWriteArraySet<>();
|
||||||
|
|
||||||
public MDNSServiceImpl() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
|
@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
|
||||||
protected void setMDNSClient(MDNSClient client) {
|
protected void setMDNSClient(MDNSClient client) {
|
||||||
this.mdnsClient = client;
|
this.mdnsClient = client;
|
||||||
// register queued services
|
// register queued services
|
||||||
if (!servicesToRegisterQueue.isEmpty()) {
|
if (!servicesToRegisterQueue.isEmpty()) {
|
||||||
Runnable runnable = new Runnable() {
|
Executors.newSingleThreadExecutor().execute(() -> {
|
||||||
@Override
|
logger.debug("Registering {} queued services", servicesToRegisterQueue.size());
|
||||||
public void run() {
|
for (ServiceDescription description : servicesToRegisterQueue) {
|
||||||
logger.debug("Registering {} queued services", servicesToRegisterQueue.size());
|
try {
|
||||||
for (ServiceDescription description : servicesToRegisterQueue) {
|
MDNSClient localClient = mdnsClient;
|
||||||
try {
|
if (localClient != null) {
|
||||||
MDNSClient localClient = mdnsClient;
|
localClient.registerService(description);
|
||||||
if (localClient != null) {
|
} else {
|
||||||
localClient.registerService(description);
|
break;
|
||||||
} else {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} catch (IOException e) {
|
|
||||||
logger.error("{}", e.getMessage());
|
|
||||||
} catch (IllegalStateException e) {
|
|
||||||
logger.debug("Not registering service {}, because service is already deactivated!",
|
|
||||||
description.serviceType);
|
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
logger.error("{}", e.getMessage());
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
logger.debug("Not registering service {}, because service is already deactivated!",
|
||||||
|
description.serviceType);
|
||||||
}
|
}
|
||||||
servicesToRegisterQueue.clear();
|
|
||||||
}
|
}
|
||||||
};
|
servicesToRegisterQueue.clear();
|
||||||
Executors.newSingleThreadExecutor().execute(runnable);
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -84,25 +77,21 @@ public class MDNSServiceImpl implements MDNSService {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void registerService(final ServiceDescription description) {
|
public void registerService(final ServiceDescription description) {
|
||||||
if (mdnsClient == null) {
|
MDNSClient localClient = mdnsClient;
|
||||||
// queue the service to register it as soon as the mDNS client is
|
if (localClient == null) {
|
||||||
// available
|
// queue the service to register it as soon as the mDNS client is available
|
||||||
servicesToRegisterQueue.add(description);
|
servicesToRegisterQueue.add(description);
|
||||||
} else {
|
} else {
|
||||||
Runnable runnable = new Runnable() {
|
Executors.newSingleThreadExecutor().execute(() -> {
|
||||||
@Override
|
try {
|
||||||
public void run() {
|
localClient.registerService(description);
|
||||||
try {
|
} catch (IOException e) {
|
||||||
mdnsClient.registerService(description);
|
logger.error("{}", e.getMessage());
|
||||||
} catch (IOException e) {
|
} catch (IllegalStateException e) {
|
||||||
logger.error("{}", e.getMessage());
|
logger.debug("Not registering service {}, because service is already deactivated!",
|
||||||
} catch (IllegalStateException e) {
|
description.serviceType);
|
||||||
logger.debug("Not registering service {}, because service is already deactivated!",
|
|
||||||
description.serviceType);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
});
|
||||||
Executors.newSingleThreadExecutor().execute(runnable);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -122,10 +111,6 @@ public class MDNSServiceImpl implements MDNSService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Activate
|
|
||||||
public void activate() {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Deactivate
|
@Deactivate
|
||||||
public void deactivate() {
|
public void deactivate() {
|
||||||
unregisterAllServices();
|
unregisterAllServices();
|
||||||
|
@ -134,5 +119,4 @@ public class MDNSServiceImpl implements MDNSService {
|
||||||
logger.debug("mDNS service has been stopped");
|
logger.debug("mDNS service has been stopped");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue