Skip to content

Commit

Permalink
Addressed comments by yunhanw-google
Browse files Browse the repository at this point in the history
  • Loading branch information
pgregorr-amazon committed Jan 25, 2024
1 parent 9c99ed1 commit 8185195
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,22 +91,4 @@ CompletableFuture<Void> VerifyOrEstablishConnection(
* CastingException will contain the error code and message from the CastingApp.
*/
CompletableFuture<Void> VerifyOrEstablishConnection();

// TODO: Implement in following PRs.

// ConnectionState getConnectionState();
//
// static class ConnectionState extends Observable {
// private boolean connected;
//
// void setConnected(boolean connected) {
// this.connected = connected;
// setChanged();
// notifyObservers(this.connected);
// }
//
// boolean isConnected() {
// return connected;
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,21 @@ JNI_METHOD(jobject, VerifyOrEstablishConnection)

ConnectCallback callback = [completableFutureObjGlobalRef](CHIP_ERROR err, CastingPlayer * playerPtr) {
ChipLogProgress(AppServer, "CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback called");
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturn(env != nullptr,
ChipLogError(AppServer, "CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback, env == nullptr"));
JniLocalReferenceManager manager(env);
jclass completableFutureClass = env->FindClass("java/util/concurrent/CompletableFuture");
VerifyOrReturn(
completableFutureClass != nullptr,
ChipLogError(AppServer,
"CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback, completableFutureClass == nullptr"));

if (completableFutureObjGlobalRef == nullptr)
{
ChipLogError(AppServer,
"PHILIPGREGOR CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback called, "
"completableFutureObjGlobalRef == nullptr");
ChipLogError(
AppServer,
"CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback, completableFutureObjGlobalRef == nullptr");
}

if (err == CHIP_NO_ERROR)
Expand All @@ -87,6 +94,10 @@ JNI_METHOD(jobject, VerifyOrEstablishConnection)
AppServer,
"CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback, Casting Player connection successful!");
jmethodID completeMethod = env->GetMethodID(completableFutureClass, "complete", "(Ljava/lang/Object;)Z");
VerifyOrReturn(
completeMethod != nullptr,
ChipLogError(AppServer,
"CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback, completeMethod == nullptr"));
chip::DeviceLayer::StackUnlock unlock;
env->CallBooleanMethod(completableFutureObjGlobalRef, completeMethod, nullptr);
}
Expand All @@ -97,11 +108,32 @@ JNI_METHOD(jobject, VerifyOrEstablishConnection)
err.Format());
jmethodID completeExceptionallyMethod =
env->GetMethodID(completableFutureClass, "completeExceptionally", "(Ljava/lang/Throwable;)Z");
VerifyOrReturn(
completeExceptionallyMethod != nullptr,
ChipLogError(
AppServer,
"CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback, completeExceptionallyMethod == nullptr"));
// Create a Throwable object (e.g., RuntimeException) to pass to completeExceptionallyMethod
jclass throwableClass = env->FindClass("java/lang/RuntimeException");
jclass throwableClass = env->FindClass("java/lang/RuntimeException");
VerifyOrReturn(
throwableClass != nullptr,
ChipLogError(AppServer,
"CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback, throwableClass == nullptr"));
jmethodID throwableConstructor = env->GetMethodID(throwableClass, "<init>", "(Ljava/lang/String;)V");
jstring errorMessage = env->NewStringUTF(err.Format());
jobject throwableObject = env->NewObject(throwableClass, throwableConstructor, errorMessage);
VerifyOrReturn(
throwableConstructor != nullptr,
ChipLogError(AppServer,
"CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback, throwableConstructor == nullptr"));
jstring errorMessage = env->NewStringUTF(err.Format());
VerifyOrReturn(
errorMessage != nullptr,
ChipLogError(AppServer,
"CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback, errorMessage == nullptr"));
jobject throwableObject = env->NewObject(throwableClass, throwableConstructor, errorMessage);
VerifyOrReturn(
throwableObject != nullptr,
ChipLogError(AppServer,
"CastingPlayer-JNI::VerifyOrEstablishConnection() ConnectCallback, throwableObject == nullptr"));
chip::DeviceLayer::StackUnlock unlock;
env->CallBooleanMethod(completableFutureObjGlobalRef, completeExceptionallyMethod, throwableObject);
}
Expand Down Expand Up @@ -142,7 +174,7 @@ JNI_METHOD(jobject, VerifyOrEstablishConnection)
ChipLogProgress(AppServer, "CastingPlayer-JNI::VerifyOrEstablishConnection() desiredEndpointFilter.productId: %d",
desiredEndpointFilter.productId);
// TODO: In following PRs. Translate the Java requiredDeviceTypes list to a C++ requiredDeviceTypes vector. For now we're
// passing an empty list of.
// passing an empty list of DeviceTypeStruct.

ChipLogProgress(AppServer,
"CastingPlayer-JNI::VerifyOrEstablishConnection() calling "
Expand Down

0 comments on commit 8185195

Please sign in to comment.