diff --git a/examples/java-matter-controller/BUILD.gn b/examples/java-matter-controller/BUILD.gn index 1c757260213e7c..55e595612f02e4 100644 --- a/examples/java-matter-controller/BUILD.gn +++ b/examples/java-matter-controller/BUILD.gn @@ -18,26 +18,12 @@ import("//build_overrides/chip.gni") import("${chip_root}/build/chip/java/rules.gni") import("${chip_root}/build/chip/tools.gni") -java_library("java") { - output_name = "JavaMatterController.jar" - deps = [ - "${chip_root}/src/controller/java", - "${chip_root}/third_party/java_deps:annotation", - ] - - sources = [ - "java/src/com/matter/controller/commands/common/FutureResult.java", - "java/src/com/matter/controller/commands/common/RealResult.java", - ] - - javac_flags = [ "-Xlint:deprecation" ] -} - kotlin_binary("java-matter-controller") { output_name = "java-matter-controller" deps = [ - ":java", + "${chip_root}/src/controller/java", "${chip_root}/src/controller/java:onboarding_payload", + "${chip_root}/third_party/java_deps:annotation", "${chip_root}/third_party/java_deps:kotlin-stdlib", ] @@ -48,8 +34,10 @@ kotlin_binary("java-matter-controller") { "java/src/com/matter/controller/commands/common/Command.kt", "java/src/com/matter/controller/commands/common/CommandManager.kt", "java/src/com/matter/controller/commands/common/CredentialsIssuer.kt", + "java/src/com/matter/controller/commands/common/FutureResult.kt", "java/src/com/matter/controller/commands/common/IPAddress.kt", "java/src/com/matter/controller/commands/common/MatterCommand.kt", + "java/src/com/matter/controller/commands/common/RealResult.kt", "java/src/com/matter/controller/commands/discover/DiscoverCommand.kt", "java/src/com/matter/controller/commands/discover/DiscoverCommissionablesCommand.kt", "java/src/com/matter/controller/commands/discover/DiscoverCommissionersCommand.kt", 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 deleted file mode 100644 index 188a0000db9141..00000000000000 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/FutureResult.java +++ /dev/null @@ -1,68 +0,0 @@ -/* - * 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 = Optional.empty(); - 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: " + realResult.get().getError()); - throw new RuntimeException("received failure test result"); - } - } - - public synchronized void clear() { - realResult = Optional.empty(); - } -} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/FutureResult.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/FutureResult.kt new file mode 100644 index 00000000000000..c7b16bdae2991c --- /dev/null +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/FutureResult.kt @@ -0,0 +1,75 @@ +/* + * 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.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. + */ +class RealResultException(message: String) : RuntimeException(message) + +class FutureResult { + private var realResult: RealResult? = null + private var timeoutMs: Long = 0 + private val logger = Logger.getLogger(FutureResult::class.java.name) + private val lock = Object() + + fun setTimeoutMs(timeoutMs: Long) { + this.timeoutMs = timeoutMs + } + + fun setRealResult(realResult: RealResult) { + synchronized(lock) { + if (this.realResult != null) { + throw RealResultException("Error, real result has been set!") + } + this.realResult = realResult + lock.notifyAll() + } + } + + fun waitResult() { + val start = System.currentTimeMillis() + synchronized(lock) { + while (realResult == null) { + try { + if (System.currentTimeMillis() > start + timeoutMs) { + throw RealResultException("Timeout!") + } + lock.wait() + } catch (e: InterruptedException) { + logger.log(Level.INFO, "Wait Result failed with exception: " + e.message) + } + } + if (realResult?.getResult() == false) { + logger.log(Level.INFO, "Error: ${realResult?.getError()}") + throw RealResultException("Received failure test result") + } + } + } + + fun clear() { + synchronized(lock) { realResult = null } + } +} diff --git a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt index 1467c905aed36c..e2b3acf7efa314 100644 --- a/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt +++ b/examples/java-matter-controller/java/src/com/matter/controller/commands/common/MatterCommand.kt @@ -96,11 +96,11 @@ abstract class MatterCommand( protected abstract fun runCommand() fun setSuccess() { - futureResult.setRealResult(RealResult.Success()) + futureResult.setRealResult(RealResult.success()) } fun setFailure(error: String?) { - futureResult.setRealResult(RealResult.Error(error)) + futureResult.setRealResult(RealResult.error(error)) } fun waitCompleteMs(timeoutMs: Long) { 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.kt similarity index 67% rename from examples/java-matter-controller/java/src/com/matter/controller/commands/common/RealResult.java rename to examples/java-matter-controller/java/src/com/matter/controller/commands/common/RealResult.kt index 1b8e5e2e2c6a1f..5cd5d80051efba 100644 --- 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.kt @@ -16,7 +16,7 @@ * */ -package com.matter.controller.commands.common; +package com.matter.controller.commands.common /** * Implements a Result where an error string is associated with its failure state. @@ -25,32 +25,26 @@ * 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; +class RealResult(private val result: Boolean, private val error: String?) { + constructor() : this(true, null) - public RealResult() { - this.result = true; - } - - public RealResult(String error) { - this.result = false; - this.error = error; - } + constructor(error: String?) : this(false, error) - public static RealResult Success() { - return new RealResult(); - } + companion object { + fun success(): RealResult { + return RealResult() + } - public static RealResult Error(String error) { - return new RealResult(error); + fun error(error: String?): RealResult { + return RealResult(error) + } } - public boolean getResult() { - return result; + fun getResult(): Boolean { + return result } - public String getError() { - return error; + fun getError(): String? { + return error } }