diff --git a/examples/java-matter-controller/BUILD.gn b/examples/java-matter-controller/BUILD.gn index 9830b360195c1d..d4516b43aaa6bd 100644 --- a/examples/java-matter-controller/BUILD.gn +++ b/examples/java-matter-controller/BUILD.gn @@ -33,8 +33,10 @@ java_binary("java-matter-controller") { "java/src/com/matter/controller/commands/common/Command.java", "java/src/com/matter/controller/commands/common/CommandManager.java", "java/src/com/matter/controller/commands/common/CredentialsIssuer.java", + "java/src/com/matter/controller/commands/common/FutureResult.java", "java/src/com/matter/controller/commands/common/IPAddress.java", "java/src/com/matter/controller/commands/common/MatterCommand.java", + "java/src/com/matter/controller/commands/common/RealResult.java", "java/src/com/matter/controller/commands/discover/DiscoverCommand.java", "java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.java", "java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.java", diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/FutureResult.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/FutureResult.java new file mode 100644 index 00000000000000..4d3cdb92fad192 --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/FutureResult.java @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.matter.controller.commands.common; + +import java.util.Optional; +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * Implements the future result that encapculates the optional realResult, application would wait + * for realResult set by other thread wben receiving data from the other end. If the expected + * duration elapsed without receiving the expected realResult, the runtime exception would be + * raised. + */ +public class FutureResult { + private Optional realResult; + private long timeoutMs = 0; + private static Logger logger = Logger.getLogger(FutureResult.class.getName()); + + public void setTimeoutMs(long timeoutMs) { + this.timeoutMs = timeoutMs; + } + + public synchronized void setRealResult(RealResult realResult) { + if (this.realResult.isPresent()) { + throw new RuntimeException("error, real result has been set!"); + } + this.realResult = Optional.of(realResult); + notifyAll(); + } + + public synchronized void waitResult() { + long start = System.currentTimeMillis(); + while (!realResult.isPresent()) { + try { + if (System.currentTimeMillis() > (start + timeoutMs)) { + throw new RuntimeException("timeout!"); + } + wait(); + } catch (InterruptedException e) { + } + } + + if (!realResult.get().getResult()) { + logger.log(Level.INFO, "error: %s", realResult.get().getError()); + throw new RuntimeException("received failure test result"); + } + } +} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.java index b892c0bf678282..4d958c47e2f316 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.java @@ -22,6 +22,7 @@ import java.util.Optional; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; +import java.util.logging.Logger; public abstract class MatterCommand extends Command { private final ChipDeviceController mChipDeviceController; @@ -30,9 +31,10 @@ public abstract class MatterCommand extends Command { private final StringBuffer mPaaTrustStorePath = new StringBuffer(); private final StringBuffer mCDTrustStorePath = new StringBuffer(); private final AtomicLong mCommissionerNodeId = new AtomicLong(); - private final AtomicBoolean mUseMaxSizedCerts = new AtomicBoolean();; - private final AtomicBoolean mOnlyAllowTrustedCdKeys = new AtomicBoolean();; - private Optional mTestResult = Optional.empty(); + private final AtomicBoolean mUseMaxSizedCerts = new AtomicBoolean(); + private final AtomicBoolean mOnlyAllowTrustedCdKeys = new AtomicBoolean(); + private FutureResult mFutureResult = new FutureResult(); + private static Logger logger = Logger.getLogger(MatterCommand.class.getName()); public MatterCommand( ChipDeviceController controller, String commandName, CredentialsIssuer credIssuerCmds) { @@ -100,39 +102,16 @@ public void run() throws Exception { protected abstract void runCommand(); - public void setTestResult(String result) { - mTestResult = Optional.of(result); + public void setSuccess() { + mFutureResult.setRealResult(RealResult.Success()); } - public void expectSuccess(long timeout) { - expectResult("Success", timeout); + public void setFailure(String error) { + mFutureResult.setRealResult(RealResult.Error(error)); } - private void expectResult(String expectedResult, long timeout) { - long start = System.currentTimeMillis(); - while (!mTestResult.isPresent()) - try { - if (System.currentTimeMillis() > (start + timeout)) { - throw new RuntimeException("timeout!"); - } - Thread.sleep(100); - } catch (InterruptedException ex) { - } - - if (!mTestResult.isPresent()) { - throw new RuntimeException("received empty test result"); - } - - if (!mTestResult.get().equals(expectedResult)) { - if (!expectedResult.equals("Success")) { - System.out.format( - "%s command failed:%n Expected: %s%n Got: %s%n", - getName(), expectedResult, mTestResult); - throw new RuntimeException(getName()); - } else { - System.out.format("%s command failed: %s%n", getName(), mTestResult.get()); - } - } - mTestResult = Optional.empty(); + public void waitCompleteMs(long timeoutMs) { + mFutureResult.setTimeoutMs(timeoutMs); + mFutureResult.waitResult(); } } diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/RealResult.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/RealResult.java new file mode 100644 index 00000000000000..1b8e5e2e2c6a1f --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/RealResult.java @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2023 Project CHIP Authors + * All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package com.matter.controller.commands.common; + +/** + * Implements a Result where an error string is associated with its failure state. + * + *

A `Result` is just a booean of true/false that is exposed via `getResult`. This class will + * contain either a `true` value for `Success` or a `false` value in which case the failure will + * also have an error string explaining the reason of the failure associated with it. + */ +public class RealResult { + private boolean result; + private String error; + + public RealResult() { + this.result = true; + } + + public RealResult(String error) { + this.result = false; + this.error = error; + } + + public static RealResult Success() { + return new RealResult(); + } + + public static RealResult Error(String error) { + return new RealResult(error); + } + + public boolean getResult() { + return result; + } + + public String getError() { + return error; + } +} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.java index 928ba95dbd1c4a..a75b36a991bd0a 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairOnNetworkLongCommand.java @@ -27,6 +27,6 @@ protected void runCommand() { getSetupPINCode(), null); currentCommissioner().setCompletionListener(this); - expectSuccess(getTimeoutMillis()); + waitCompleteMs(getTimeoutMillis()); } } diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java index c6e209095bfca8..f26d2cadce3d1a 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/pairing/PairingCommand.java @@ -82,7 +82,7 @@ public void onStatusUpdate(int status) { public void onPairingComplete(int errorCode) { logger.log(Level.INFO, "onPairingComplete with error code: " + errorCode); if (errorCode != 0) { - setTestResult("Failure"); + setFailure("onPairingComplete failure"); } } @@ -95,9 +95,9 @@ public void onPairingDeleted(int errorCode) { public void onCommissioningComplete(long nodeId, int errorCode) { logger.log(Level.INFO, "onCommissioningComplete with error code: " + errorCode); if (errorCode == 0) { - setTestResult("Success"); + setSuccess(); } else { - setTestResult("Failure"); + setFailure("onCommissioningComplete failure"); } } @@ -124,7 +124,7 @@ public void onCloseBleComplete() { @Override public void onError(Throwable error) { - setTestResult(error.toString()); + setFailure(error.toString()); logger.log(Level.INFO, "onError with error: " + error.toString()); }