Skip to content

Instantly share code, notes, and snippets.

@balazs
Created January 29, 2015 21:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balazs/f5cca91c366e7b9c7146 to your computer and use it in GitHub Desktop.
Save balazs/f5cca91c366e7b9c7146 to your computer and use it in GitHub Desktop.
From e13d1e16c160cf369c608209a032bcfc683dac29 Mon Sep 17 00:00:00 2001
From: Balazs Kelemen <b.kelemen@samsung.com>
Date: Thu, 29 Jan 2015 15:58:54 -0500
Subject: [PATCH] Cleanup mapping in GWCliendDriver
We should watch for consequent registrations before callback dispatched.
Also we should remove mapping once we are done with it.
Straighten the lookup by CHECK's and use STL iterator api's to avoid
double lookups.
Change-Id: I98b7ad0df89862d5bf2b716fb5c80c46b5fbc908
Signed-off-by: Balazs Kelemen <b.kelemen@samsung.com>
---
components/gwclient_driver/gwclient_driver.cc | 63 ++++++++++++++++++++-------
1 file changed, 47 insertions(+), 16 deletions(-)
diff --git a/components/gwclient_driver/gwclient_driver.cc b/components/gwclient_driver/gwclient_driver.cc
index e17fcf6..3426645 100644
--- a/components/gwclient_driver/gwclient_driver.cc
+++ b/components/gwclient_driver/gwclient_driver.cc
@@ -37,43 +37,72 @@ GWClientDriver::~GWClientDriver() {
void GWClientDriver::Register(
const std::string& app_id,
const GWClientDriver::RegisterCallback& callback) {
- LOG(ERROR) << __PRETTY_FUNCTION__ << " app_id = " << app_id;
+ LOG(ERROR) << __PRETTY_FUNCTION__ << " app_id=" << app_id;
+ auto insert_result = register_callbacks_.insert(std::make_pair(app_id, callback));
+ if (!insert_result.second) {
+ // Previous register operation is still in progress, bail out.
+ // TODO: pass specific error reason (async operation pending).
+ callback.Run(std::string(), REGISTER_FAIL);
+ return;
+ }
+
JNIEnv* env = AttachCurrentThread();
Java_GWClientDriver_register(
env, java_ref_.obj(),
ConvertUTF8ToJavaString(env, app_id).Release());
- register_callbacks_[app_id] = callback;
}
void GWClientDriver::Unregister(
const std::string& app_id,
const GWClientDriver::UnregisterCallback& callback) {
LOG(ERROR) << __PRETTY_FUNCTION__ << " app_id = " << app_id;
+ auto insert_result = unregister_callbacks_.insert(std::make_pair(app_id, callback));
+ if (!insert_result.second) {
+ // Previous unregister operation is still in progress, bail out.
+ // TODO: pass specific error reason (async operation pending).
+ callback.Run(REGISTER_FAIL);
+ return;
+ }
+
JNIEnv* env = AttachCurrentThread();
Java_GWClientDriver_unregister(
env, java_ref_.obj(),
ConvertUTF8ToJavaString(env, app_id).Release());
- unregister_callbacks_[app_id] = callback;
- unregister_callbacks_[app_id].Run(gwclient::REGISTER_SUCCESS);
}
void GWClientDriver::OnRegisterFinished(JNIEnv* env,
- jobject obj,
- jstring j_app_id,
- jstring j_registration_id,
- jboolean success) {
+ jobject obj,
+ jstring j_app_id,
+ jstring j_registration_id,
+ jboolean success) {
std::string app_id = ConvertJavaStringToUTF8(env, j_app_id);
std::string registration_id = ConvertJavaStringToUTF8(env, j_registration_id);
- LOG(ERROR) << __PRETTY_FUNCTION__ << app_id << registration_id;
- register_callbacks_[app_id].Run(registration_id, gwclient::REGISTER_SUCCESS);
+ LOG(ERROR) << __PRETTY_FUNCTION__ << " appid=" << app_id
+ <<", registration_id=" << registration_id;
+
+ auto iterator = register_callbacks_.find(app_id);
+ CHECK(iterator != register_callbacks_.end());
+ CHECK(iterator->first == app_id);
+ RegisterCallback callback = iterator->second;
+ register_callbacks_.erase(iterator);
+
+ callback.Run(registration_id, success ? REGISTER_SUCCESS : REGISTER_FAIL);
}
void GWClientDriver::OnUnregisterFinished(JNIEnv* env,
- jobject obj,
- jstring j_app_id,
- jboolean success) {
+ jobject obj,
+ jstring j_app_id,
+ jboolean success) {
std::string app_id = ConvertJavaStringToUTF8(env, j_app_id);
- LOG(ERROR) << __PRETTY_FUNCTION__ << app_id;
+ LOG(ERROR) << __PRETTY_FUNCTION__ << " appid=" << app_id;
+
+ auto iterator = unregister_callbacks_.find(app_id);
+ CHECK(iterator != unregister_callbacks_.end());
+ CHECK(iterator->first == app_id);
+ UnregisterCallback callback = iterator->second;
+ unregister_callbacks_.erase(iterator);
+
+ callback.Run(success ? REGISTER_SUCCESS : REGISTER_FAIL);
}
void GWClientDriver::OnMessageReceived(JNIEnv* env,
@@ -82,7 +111,9 @@ void GWClientDriver::OnMessageReceived(JNIEnv* env,
jstring j_message) {
std::string app_id = ConvertJavaStringToUTF8(env, j_app_id);
std::string message = ConvertJavaStringToUTF8(env, j_message);
- LOG(ERROR) << __PRETTY_FUNCTION__ << app_id;
+ LOG(ERROR) << __PRETTY_FUNCTION__ << " app_id=" << app_id
+ << ", message=" << message;
+
delegate_->OnMessage(app_id, message);
}
@@ -90,7 +121,7 @@ void GWClientDriver::OnMessagesDeleted(JNIEnv* env,
jobject obj,
jstring j_app_id) {
std::string app_id = ConvertJavaStringToUTF8(env, j_app_id);
- LOG(ERROR) << __PRETTY_FUNCTION__;
+ LOG(ERROR) << __PRETTY_FUNCTION__ << " app_id=" << app_id;
}
// static
--
2.2.2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment