Skip to content

Commit

Permalink
Use constructor injection (openhab#1210)
Browse files Browse the repository at this point in the history
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
  • Loading branch information
cweitkamp authored and wborn committed Nov 14, 2019
1 parent f682cb5 commit b34ef3c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 60 deletions.
3 changes: 2 additions & 1 deletion bundles/org.openhab.core.io.transport.mdns/pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?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>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ public class MDNSClientImpl implements MDNSClient, NetworkAddressChangeListener

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() {
final Set<InetAddress> addresses = new HashSet<>();
Expand Down Expand Up @@ -132,6 +137,7 @@ public Set<JmDNS> getClientInstances() {

@Activate
protected void activate() {
networkAddressService.addNetworkAddressChangeListener(this);
start();
}

Expand All @@ -152,6 +158,7 @@ private void start() {
public void deactivate() {
close();
activeServices.clear();
networkAddressService.removeNetworkAddressChangeListener(this);
}

@Override
Expand Down Expand Up @@ -273,15 +280,4 @@ public void onChanged(List<CidrAddress> added, List<CidrAddress> removed) {
close();
start();
}

@Reference
protected void setNetworkAddressService(NetworkAddressService networkAddressService) {
this.networkAddressService = networkAddressService;
networkAddressService.addNetworkAddressChangeListener(this);
}

protected void unsetNetworkAddressService(NetworkAddressService networkAddressService) {
networkAddressService.removeNetworkAddressChangeListener(this);
this.networkAddressService = null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
import java.util.concurrent.CopyOnWriteArraySet;
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.MDNSService;
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.Deactivate;
import org.osgi.service.component.annotations.Reference;
Expand All @@ -37,43 +37,36 @@
*/
@Component(immediate = true)
public class MDNSServiceImpl implements MDNSService {

private final Logger logger = LoggerFactory.getLogger(MDNSServiceImpl.class);
private MDNSClient mdnsClient;

private final Set<ServiceDescription> servicesToRegisterQueue = new CopyOnWriteArraySet<>();
private @Nullable MDNSClient mdnsClient;

public MDNSServiceImpl() {
}
private final Set<ServiceDescription> servicesToRegisterQueue = new CopyOnWriteArraySet<>();

@Reference(cardinality = ReferenceCardinality.OPTIONAL, policy = ReferencePolicy.DYNAMIC)
protected void setMDNSClient(MDNSClient client) {
this.mdnsClient = client;
// register queued services
if (!servicesToRegisterQueue.isEmpty()) {
Runnable runnable = new Runnable() {
@Override
public void run() {
logger.debug("Registering {} queued services", servicesToRegisterQueue.size());
for (ServiceDescription description : servicesToRegisterQueue) {
try {
MDNSClient localClient = mdnsClient;
if (localClient != null) {
localClient.registerService(description);
} else {
break;
}
} catch (IOException e) {
logger.error("{}", e.getMessage());
} catch (IllegalStateException e) {
logger.debug("Not registering service {}, because service is already deactivated!",
description.serviceType);
Executors.newSingleThreadExecutor().execute(() -> {
logger.debug("Registering {} queued services", servicesToRegisterQueue.size());
for (ServiceDescription description : servicesToRegisterQueue) {
try {
MDNSClient localClient = mdnsClient;
if (localClient != null) {
localClient.registerService(description);
} else {
break;
}
} catch (IOException e) {
logger.error("{}", e.getMessage());
} catch (IllegalStateException e) {
logger.debug("Not registering service {}, because service is already deactivated!",
description.serviceType);
}
servicesToRegisterQueue.clear();
}
};
Executors.newSingleThreadExecutor().execute(runnable);
servicesToRegisterQueue.clear();
});
}
}

Expand All @@ -84,25 +77,21 @@ protected void unsetMDNSClient(MDNSClient mdnsClient) {

@Override
public void registerService(final ServiceDescription description) {
if (mdnsClient == null) {
// queue the service to register it as soon as the mDNS client is
// available
MDNSClient localClient = mdnsClient;
if (localClient == null) {
// queue the service to register it as soon as the mDNS client is available
servicesToRegisterQueue.add(description);
} else {
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
mdnsClient.registerService(description);
} catch (IOException e) {
logger.error("{}", e.getMessage());
} catch (IllegalStateException e) {
logger.debug("Not registering service {}, because service is already deactivated!",
description.serviceType);
}
Executors.newSingleThreadExecutor().execute(() -> {
try {
localClient.registerService(description);
} catch (IOException e) {
logger.error("{}", e.getMessage());
} catch (IllegalStateException e) {
logger.debug("Not registering service {}, because service is already deactivated!",
description.serviceType);
}
};
Executors.newSingleThreadExecutor().execute(runnable);
});
}
}

Expand All @@ -122,10 +111,6 @@ protected void unregisterAllServices() {
}
}

@Activate
public void activate() {
}

@Deactivate
public void deactivate() {
unregisterAllServices();
Expand All @@ -134,5 +119,4 @@ public void deactivate() {
logger.debug("mDNS service has been stopped");
}
}

}

0 comments on commit b34ef3c

Please sign in to comment.