Skip to content

Commit

Permalink
Normalized thread names (openhab#9581)
Browse files Browse the repository at this point in the history
Related to openhab#8216

Signed-off-by: Hilbrand Bouwkamp <hilbrand@h72.nl>
  • Loading branch information
Hilbrand authored and thinkingstone committed Nov 7, 2021
1 parent f029da1 commit 0705f92
Show file tree
Hide file tree
Showing 22 changed files with 75 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ protected void scheduleConnectRetry(long waitMinutes) {

protected void startMsgReader() {
synchronized (msgReaderThreadLock) {
Thread mrt = new Thread(this::readerThread, "AD Reader");
Thread mrt = new Thread(this::readerThread, "OH-binding-" + getThing().getUID() + "-ADReader");
mrt.setDaemon(true);
mrt.start();
msgReaderThread = mrt;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
public class SocketChannelSession implements SocketSession {
private final Logger logger = LoggerFactory.getLogger(SocketChannelSession.class);

/**
* The uid of the calling thing
*/
private final String uid;
/**
* The host/ip address to connect to
*/
Expand Down Expand Up @@ -77,17 +81,19 @@ public class SocketChannelSession implements SocketSession {
/**
* Creates the socket session from the given host and port
*
* @param uid the thing uid of the calling thing
* @param host a non-null, non-empty host/ip address
* @param port the port number between 1 and 65535
*/
public SocketChannelSession(String host, int port) {
public SocketChannelSession(String uid, String host, int port) {
if (host == null || host.trim().length() == 0) {
throw new IllegalArgumentException("Host cannot be null or empty");
}

if (port < 1 || port > 65535) {
throw new IllegalArgumentException("Port must be between 1 and 65535");
}
this.uid = uid;
this.host = host;
this.port = port;
}
Expand Down Expand Up @@ -129,8 +135,12 @@ public void connect() throws IOException {
}

socketChannel.set(channel);
new Thread(dispatcher).start();
new Thread(responseReader).start();
Thread dispatcherThread = new Thread(dispatcher, "OH-binding-" + uid + "-dispatcher");
dispatcherThread.setDaemon(true);
dispatcherThread.start();
Thread responseReaderThread = new Thread(responseReader, "OH-binding-" + uid + "-responseReader");
responseReaderThread.setDaemon(true);
responseReaderThread.start();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ public void initialize() {
return;
}

session = new SocketChannelSession(config.getIpAddress(), 23);
session = new SocketChannelSession(getThing().getUID().getAsString(), config.getIpAddress(), 23);
atlonaHandler = new AtlonaPro3PortocolHandler(session, config, getCapabilities(),
new StatefulHandlerCallback(new AtlonaHandlerCallback() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ public void initialize() {
}, executor);

serialHandler = serialPortFuture
.thenApply(sp -> new BlueGigaSerialHandler(inputStream.get(), outputStream.get()));
.thenApply(sp -> new BlueGigaSerialHandler(getThing().getUID().getAsString(), inputStream.get(),
outputStream.get()));
transactionManager = serialHandler.thenApply(sh -> {
BlueGigaTransactionManager th = new BlueGigaTransactionManager(sh, executor);
sh.addHandlerListener(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ public class BlueGigaSerialHandler {
private final InputStream inputStream;
private final Thread parserThread;

public BlueGigaSerialHandler(final InputStream inputStream, final OutputStream outputStream) {
public BlueGigaSerialHandler(final String uid, final InputStream inputStream, final OutputStream outputStream) {
this.outputStream = outputStream;
this.inputStream = inputStream;

flush();
parserThread = createBlueGigaBLEHandler();
parserThread = createBlueGigaBLEHandler(uid);
parserThread.setUncaughtExceptionHandler((t, th) -> {
logger.warn("BluegigaSerialHandler terminating due to unhandled error", th);
});
Expand Down Expand Up @@ -323,7 +323,7 @@ private void inboundMessageHandlerLoop() {
logger.debug("BlueGiga BLE exited.");
}

private Thread createBlueGigaBLEHandler() {
return new Thread(this::inboundMessageHandlerLoop, "BlueGigaBLEHandler");
private Thread createBlueGigaBLEHandler(String uid) {
return new Thread(this::inboundMessageHandlerLoop, "OH-binding-" + uid + "-blueGigaBLEHandler");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public class CaddxCommunicator implements SerialPortEventListener {
private boolean unStuff = false;
private int tempAsciiByte = 0;

public CaddxCommunicator(SerialPortManager portManager, CaddxProtocol protocol, String serialPortName, int baudRate)
public CaddxCommunicator(String uid, SerialPortManager portManager, CaddxProtocol protocol, String serialPortName,
int baudRate)
throws UnsupportedCommOperationException, PortInUseException, IOException, TooManyListenersException {
this.portManager = portManager;
this.protocol = protocol;
Expand Down Expand Up @@ -101,7 +102,7 @@ public CaddxCommunicator(SerialPortManager portManager, CaddxProtocol protocol,
serialPort.notifyOnDataAvailable(true);
serialPort.addEventListener(this);

communicator = new Thread(this::messageDispatchLoop, "Caddx Communicator");
communicator = new Thread(this::messageDispatchLoop, "OH-binding-" + uid + "-caddxCommunicator");
communicator.setDaemon(true);
communicator.start();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ public void initialize() {
protocol);

try {
communicator = new CaddxCommunicator(portManager, protocol, serialPortName, baudRate);
communicator = new CaddxCommunicator(getThing().getUID().getAsString(), portManager, protocol,
serialPortName, baudRate);
} catch (IOException | TooManyListenersException | UnsupportedCommOperationException | PortInUseException e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR,
"Communication cannot be initialized. " + e.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,8 @@ public void openConnection() {
tcpOutput = new OutputStreamWriter(tcpSocket.getOutputStream(), "US-ASCII");
tcpInput = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));

Thread tcpListener = new Thread(new TCPListener());
Thread tcpListener = new Thread(new TCPListener(), "OH-binding-" + getThing().getUID() + "-tcplistener");
tcpListener.setDaemon(true);
tcpListener.start();

setConnected(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ public void openConnection() {
tcpOutput = new OutputStreamWriter(tcpSocket.getOutputStream(), "US-ASCII");
tcpInput = new BufferedReader(new InputStreamReader(tcpSocket.getInputStream()));

Thread tcpListener = new Thread(new TCPListener());
Thread tcpListener = new Thread(new TCPListener(), "OH-binding-" + getThing().getUID() + "-tcplistener");
tcpListener.setDaemon(true);
tcpListener.start();

setConnected(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.harmonyhub.internal.HarmonyHubBindingConstants;
import org.openhab.binding.harmonyhub.internal.handler.HarmonyHubHandler;
import org.openhab.core.config.discovery.AbstractDiscoveryService;
import org.openhab.core.config.discovery.DiscoveryResultBuilder;
Expand Down Expand Up @@ -222,7 +223,9 @@ public int getPort() {

public void start() {
running = true;
Thread localThread = new Thread(this::run, "HarmonyDiscoveryServer(tcp/" + getPort() + ")");
Thread localThread = new Thread(this::run,
"OH-binding-" + HarmonyHubBindingConstants.BINDING_ID + "discoveryServer");
localThread.setDaemon(true);
localThread.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ private synchronized void startServers() throws IOException {
for (TransferMode mode : availableInterfaces.values()) {
if (!rpcServers.containsKey(mode)) {
RpcServer rpcServer = mode == TransferMode.XML_RPC ? new XmlRpcServer(this, config)
: new BinRpcServer(this, config);
: new BinRpcServer(this, config, id);
rpcServers.put(mode, rpcServer);
rpcServer.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.IOException;

import org.openhab.binding.homematic.internal.HomematicBindingConstants;
import org.openhab.binding.homematic.internal.common.HomematicConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -28,12 +29,14 @@ public class BinRpcServer implements RpcServer {

private Thread networkServiceThread;
private BinRpcNetworkService networkService;
private HomematicConfig config;
private RpcEventListener listener;
private final HomematicConfig config;
private final RpcEventListener listener;
private final String id;

public BinRpcServer(RpcEventListener listener, HomematicConfig config) {
public BinRpcServer(RpcEventListener listener, HomematicConfig config, String id) {
this.listener = listener;
this.config = config;
this.id = id;
}

@Override
Expand All @@ -42,7 +45,8 @@ public void start() throws IOException {

networkService = new BinRpcNetworkService(listener, config);
networkServiceThread = new Thread(networkService);
networkServiceThread.setName("HomematicRpcServer");
networkServiceThread
.setName("OH-binding-" + HomematicBindingConstants.THING_TYPE_BRIDGE + ":" + id + "-rpcServer");
networkServiceThread.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.insteon.internal.InsteonBindingConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -52,7 +53,7 @@ private RequestQueueManager() {

private void setParamsAndStart(@Nullable Thread thread) {
if (thread != null) {
thread.setName("Insteon Request Queue Reader");
thread.setName("OH-binding-" + InsteonBindingConstants.BINDING_ID + "-requestQueueReader");
thread.setDaemon(true);
thread.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.insteon.internal.InsteonBindingConstants;
import org.openhab.binding.insteon.internal.device.InsteonDevice;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -112,7 +113,7 @@ public void start() {

private void setParamsAndStart(@Nullable Thread thread) {
if (thread != null) {
thread.setName("Insteon Poll Queue Reader");
thread.setName("OH-binding-" + InsteonBindingConstants.BINDING_ID + "-pollQueueReader");
thread.setDaemon(true);
thread.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public void start() {

private void setParamsAndStart(@Nullable Thread thread, String type) {
if (thread != null) {
thread.setName("Insteon " + logName + " " + type);
thread.setName("OH-binding-Insteon " + logName + " " + type);
thread.setDaemon(true);
thread.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.openhab.binding.insteon.internal.InsteonBindingConstants;
import org.openhab.binding.insteon.internal.driver.IOStream;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -102,7 +103,7 @@ public boolean open() {

private void setParamsAndStart(@Nullable Thread thread) {
if (thread != null) {
thread.setName("Insteon Hub Poller");
thread.setName("OH-binding-" + InsteonBindingConstants.BINDING_ID + "-hubPoller");
thread.setDaemon(true);
thread.start();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Set;

import org.openhab.binding.keba.internal.handler.KeContactHandler;
import org.openhab.binding.keba.internal.handler.KeContactTransceiver;
import org.openhab.core.thing.Thing;
import org.openhab.core.thing.ThingTypeUID;
import org.openhab.core.thing.binding.BaseThingHandlerFactory;
Expand All @@ -36,6 +37,8 @@ public class KebaHandlerFactory extends BaseThingHandlerFactory {

private static final Set<ThingTypeUID> SUPPORTED_THING_TYPES_UIDS = Collections.singleton(THING_TYPE_KECONTACTP20);

private final KeContactTransceiver transceiver = new KeContactTransceiver();

@Override
public boolean supportsThingType(ThingTypeUID thingTypeUID) {
return SUPPORTED_THING_TYPES_UIDS.contains(thingTypeUID);
Expand All @@ -46,7 +49,7 @@ protected ThingHandler createHandler(Thing thing) {
ThingTypeUID thingTypeUID = thing.getThingTypeUID();

if (thingTypeUID.equals(THING_TYPE_KECONTACTP20)) {
return new KeContactHandler(thing);
return new KeContactHandler(thing, transceiver);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public class KeContactHandler extends BaseThingHandler {

private final Logger logger = LoggerFactory.getLogger(KeContactHandler.class);

protected JsonParser parser = new JsonParser();
protected final JsonParser parser = new JsonParser();
private final KeContactTransceiver transceiver;

private ScheduledFuture<?> pollingJob;
private static KeContactTransceiver transceiver = new KeContactTransceiver();
private ExpiringCacheMap<String, ByteBuffer> cache;

private int maxPresetCurrent = 0;
Expand All @@ -87,8 +87,9 @@ public class KeContactHandler extends BaseThingHandler {
private int lastState = -1; // trigger a report100 at startup
private boolean isReport100needed = true;

public KeContactHandler(Thing thing) {
public KeContactHandler(Thing thing, KeContactTransceiver transceiver) {
super(thing);
this.transceiver = transceiver;
}

@Override
Expand All @@ -106,7 +107,7 @@ public void initialize() {

if (pollingJob == null || pollingJob.isCancelled()) {
try {
pollingJob = scheduler.scheduleWithFixedDelay(pollingRunnable, 0,
pollingJob = scheduler.scheduleWithFixedDelay(this::pollingRunnable, 0,
((BigDecimal) getConfig().get(POLLING_REFRESH_INTERVAL)).intValue(), TimeUnit.SECONDS);
} catch (Exception e) {
updateStatus(ThingStatus.OFFLINE, ThingStatusDetail.NONE,
Expand Down Expand Up @@ -147,7 +148,7 @@ protected Configuration getConfig() {
return super.getConfig();
}

private Runnable pollingRunnable = () -> {
private void pollingRunnable() {
try {
long stamp = System.currentTimeMillis();
if (!InetAddress.getByName(((String) getConfig().get(IP_ADDRESS))).isReachable(PING_TIME_OUT)) {
Expand Down Expand Up @@ -194,7 +195,7 @@ protected Configuration getConfig() {
} catch (InterruptedException e) {
logger.debug("Polling job has been interrupted for handler of thing '{}'.", getThing().getUID());
}
};
}

protected void onData(ByteBuffer byteBuffer) {
String response = new String(byteBuffer.array(), 0, byteBuffer.limit());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
* @author Karel Goderis - Initial contribution
*/

class KeContactTransceiver {
public class KeContactTransceiver {

public static final int LISTENER_PORT_NUMBER = 7090;
public static final int REMOTE_PORT_NUMBER = 7090;
Expand Down Expand Up @@ -74,7 +74,7 @@ public void start() {
selector = Selector.open();

if (transceiverThread == null) {
transceiverThread = new Thread(transceiverRunnable, "openHAB-Keba-Transceiver");
transceiverThread = new Thread(transceiverRunnable, "OH-binding-Keba-Transceiver");
transceiverThread.start();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,8 @@ private Utils() {
}

static Thread backgroundThread(Runnable r, String type, @Nullable Thing thing) {
String name = "OH-binding-" + LinuxInputBindingConstants.BINDING_ID + "-" + type;
if (thing != null) {
name += "-" + thing.getUID();
}
Thread t = new Thread(r, name);
String id = thing == null ? LinuxInputBindingConstants.BINDING_ID : thing.getUID().getAsString();
Thread t = new Thread(r, "OH-binding-" + id + "-" + type);
t.setDaemon(true);
return t;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public PrgBridgeHandler(Bridge bridge) {
}

final PrgBridgeConfig config = getPrgBridgeConfig();
session = new SocketSession(config.getIpAddress(), 23);
session = new SocketSession(getThing().getUID().getAsString(), config.getIpAddress(), 23);

protocolHandler = new PrgProtocolHandler(session, new PrgHandlerCallback() {
@Override
Expand Down
Loading

0 comments on commit 0705f92

Please sign in to comment.