Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace runnable wrappers by lambda expressions/method references #1380

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>zigbee</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
Expand Up @@ -284,21 +284,18 @@ public void commandReceived(ZigBeeCommand command) {
}
});

Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
shutdown = true;
try {
System.in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
mainThread.interrupt();
mainThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
shutdown = true;
try {
System.in.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
mainThread.interrupt();
mainThread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}));
}
Expand Down Expand Up @@ -945,74 +942,62 @@ public boolean process(final ZigBeeApi zigbeeApi, final String[] args, final Pri
return false;
}

new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (true) {
print("STRESSING 1 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(167);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
int cnt = 0;
while (true) {
print("STRESSING 1 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(167);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (true) {
print("STRESSING 2 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(107);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
int cnt = 0;
while (true) {
print("STRESSING 2 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(107);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (true) {
print("STRESSING 3 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(131);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
int cnt = 0;
while (true) {
print("STRESSING 3 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(131);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();

new Thread(new Runnable() {
@Override
public void run() {
int cnt = 0;
while (true) {
print("STRESSING 4 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(187);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread(() -> {
int cnt = 0;
while (true) {
print("STRESSING 4 CNT: " + cnt++, out);
ZclOnOffCluster cluster = (ZclOnOffCluster) endpoint
.getInputCluster(ZclOnOffCluster.CLUSTER_ID);
cluster.onCommand();
try {
Thread.sleep(187);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
Expand Down Expand Up @@ -1089,46 +1074,42 @@ public String getSyntax() {
*/
@Override
public boolean process(final ZigBeeApi zigbeeApi, final String[] args, final PrintStream out) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
int cnts = 1000;
int errors = 0;
int cnt = 0;
List<Integer> lqi = new ArrayList<>();
while (cnt < cnts) {
print("LQI Poll CNT: " + cnt++, out);
final ManagementLqiRequest neighborRequest = new ManagementLqiRequest(0);
neighborRequest.setDestinationAddress(new ZigBeeEndpointAddress(0));

CommandResult response;
try {
response = networkManager.sendTransaction(neighborRequest, neighborRequest).get();
final ManagementLqiResponse neighborResponse = response.getResponse();

if (neighborResponse == null || neighborResponse.getStatus() != ZdoStatus.SUCCESS) {
errors++;
continue;
}
if (neighborResponse.getNeighborTableList().isEmpty()) {
print("No neighbors", out);
continue;
}
lqi.add(neighborResponse.getNeighborTableList().get(0).getLqi());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
new Thread(() -> {
int cnts = 1000;
int errors = 0;
int cnt = 0;
List<Integer> lqi = new ArrayList<>();
while (cnt < cnts) {
print("LQI Poll CNT: " + cnt++, out);
final ManagementLqiRequest neighborRequest = new ManagementLqiRequest(0);
neighborRequest.setDestinationAddress(new ZigBeeEndpointAddress(0));

CommandResult response;
try {
response = networkManager.sendTransaction(neighborRequest, neighborRequest).get();
final ManagementLqiResponse neighborResponse = response.getResponse();

if (neighborResponse == null || neighborResponse.getStatus() != ZdoStatus.SUCCESS) {
errors++;
continue;
}
if (neighborResponse.getNeighborTableList().isEmpty()) {
print("No neighbors", out);
continue;
}
lqi.add(neighborResponse.getNeighborTableList().get(0).getLqi());
} catch (InterruptedException | ExecutionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
IntSummaryStatistics stats = lqi.stream().mapToInt((x) -> x).summaryStatistics();

print("LQI Polling Complete", out);
print("Errors: " + errors, out);
print("Min : " + stats.getMin(), out);
print("Max : " + stats.getMax(), out);
print("Avg : " + stats.getAverage(), out);

}
IntSummaryStatistics stats = lqi.stream().mapToInt((x) -> x).summaryStatistics();

print("LQI Polling Complete", out);
print("Errors: " + errors, out);
print("Min : " + stats.getMin(), out);
print("Max : " + stats.getMax(), out);
print("Avg : " + stats.getAverage(), out);
}).start();

return true;
Expand Down Expand Up @@ -1157,12 +1138,7 @@ public String getSyntax() {
*/
@Override
public boolean process(final ZigBeeApi zigbeeApi, final String[] args, final PrintStream out) throws Exception {
new Thread(new Runnable() {
@Override
public void run() {
networkManager.reinitialize();
}
}).start();
new Thread(networkManager::reinitialize).start();

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -630,17 +630,14 @@ private void scheduleNetworkStatePolling() {
return;
}

pollingTimer = executorService.scheduleWithFixedDelay(new Runnable() {
@Override
public void run() {
// Don't poll the state if the network is down
// or we've sent a command to the dongle within the pollRate
if (!networkStateUp || (lastSendCommand + pollRate > System.currentTimeMillis())) {
return;
}
// Don't wait for the response. This is running in a single thread scheduler
frameHandler.queueFrame(new EzspNetworkStateRequest());
pollingTimer = executorService.scheduleWithFixedDelay(() -> {
// Don't poll the state if the network is down
// or we've sent a command to the dongle within the pollRate
if (!networkStateUp || (lastSendCommand + pollRate > System.currentTimeMillis())) {
return;
}
// Don't wait for the response. This is running in a single thread scheduler
frameHandler.queueFrame(new EzspNetworkStateRequest());
}, pollRate, pollRate, TimeUnit.MILLISECONDS);
}

Expand Down Expand Up @@ -808,40 +805,37 @@ public void sendCommand(final int msgTag, final ZigBeeApsFrame apsFrame) {

// The response from the SendXxxcast messages returns the network layer sequence number
// We need to correlate this with the messageTag
executorService.execute(new Runnable() {
@Override
public void run() {
synchronized (isConfiguredSync) {
if (!isConfigured) {
logger.debug("EZSP Dongle is not configured. Frame not sent: {}", apsFrame);
return;
}
executorService.execute(() -> {
synchronized (isConfiguredSync) {
if (!isConfigured) {
logger.debug("EZSP Dongle is not configured. Frame not sent: {}", apsFrame);
return;
}

lastSendCommand = System.currentTimeMillis();
frameHandler.sendEzspTransaction(transaction);

EmberStatus status = null;
if (transaction.getResponse() instanceof EzspSendUnicastResponse) {
fragmentationApsCounters.put(msgTag,
((EzspSendUnicastResponse) transaction.getResponse()).getSequence());
status = ((EzspSendUnicastResponse) transaction.getResponse()).getStatus();
} else if (transaction.getResponse() instanceof EzspSendBroadcastResponse) {
status = ((EzspSendBroadcastResponse) transaction.getResponse()).getStatus();
} else if (transaction.getResponse() instanceof EzspSendMulticastResponse) {
status = ((EzspSendMulticastResponse) transaction.getResponse()).getStatus();
} else {
logger.debug("Unable to get response from {} :: {}", transaction.getRequest(),
transaction.getResponse());
return;
}
lastSendCommand = System.currentTimeMillis();
frameHandler.sendEzspTransaction(transaction);

EmberStatus status = null;
if (transaction.getResponse() instanceof EzspSendUnicastResponse) {
fragmentationApsCounters.put(msgTag,
((EzspSendUnicastResponse) transaction.getResponse()).getSequence());
status = ((EzspSendUnicastResponse) transaction.getResponse()).getStatus();
} else if (transaction.getResponse() instanceof EzspSendBroadcastResponse) {
status = ((EzspSendBroadcastResponse) transaction.getResponse()).getStatus();
} else if (transaction.getResponse() instanceof EzspSendMulticastResponse) {
status = ((EzspSendMulticastResponse) transaction.getResponse()).getStatus();
} else {
logger.debug("Unable to get response from {} :: {}", transaction.getRequest(),
transaction.getResponse());
return;
}

// If this is EMBER_SUCCESS, then do nothing as the command is still not transmitted.
// If there was an error, then we let the system know we've failed already!
if (status == EmberStatus.EMBER_SUCCESS) {
return;
}
zigbeeTransportReceive.receiveCommandState(msgTag, ZigBeeTransportProgressState.TX_NAK);
// If this is EMBER_SUCCESS, then do nothing as the command is still not transmitted.
// If there was an error, then we let the system know we've failed already!
if (status == EmberStatus.EMBER_SUCCESS) {
return;
}
zigbeeTransportReceive.receiveCommandState(msgTag, ZigBeeTransportProgressState.TX_NAK);
}
});
}
Expand Down Expand Up @@ -941,18 +935,15 @@ public void handlePacket(EzspFrame response) {

// Message has been completed by the NCP
if (response instanceof EzspMessageSentHandler) {
executorService.execute(new Runnable() {
@Override
public void run() {
EzspMessageSentHandler sentHandler = (EzspMessageSentHandler) response;
ZigBeeTransportProgressState sentHandlerState;
if (sentHandler.getStatus() == EmberStatus.EMBER_SUCCESS) {
sentHandlerState = ZigBeeTransportProgressState.RX_ACK;
} else {
sentHandlerState = ZigBeeTransportProgressState.RX_NAK;
}
zigbeeTransportReceive.receiveCommandState(sentHandler.getMessageTag(), sentHandlerState);
executorService.execute(() -> {
EzspMessageSentHandler sentHandler = (EzspMessageSentHandler) response;
ZigBeeTransportProgressState sentHandlerState;
if (sentHandler.getStatus() == EmberStatus.EMBER_SUCCESS) {
sentHandlerState = ZigBeeTransportProgressState.RX_ACK;
} else {
sentHandlerState = ZigBeeTransportProgressState.RX_NAK;
}
zigbeeTransportReceive.receiveCommandState(sentHandler.getMessageTag(), sentHandlerState);
});
return;
}
Expand Down
Loading