You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by ve...@apache.org on 2012/08/02 18:16:36 UTC

svn commit: r1368570 - in /incubator/etch/trunk/binding-cpp/runtime: include/support/ src/main/ src/main/support/ src/test/ src/test/support/

Author: veithm
Date: Thu Aug  2 16:16:35 2012
New Revision: 1368570

URL: http://svn.apache.org/viewvc?rev=1368570&view=rev
Log:
ETCH-235 Implementation of AsyncResult classes

Added EtchAsyncResultNone for void methods
Added EtchAsyncResult for methods with return value

Change-Id: I07c6328256fd7d2399e46feb0568eb2fe93b1972

Added:
    incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResult.h
    incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResultNone.h
    incubator/etch/trunk/binding-cpp/runtime/src/main/support/EtchAsyncResultNone.cpp
    incubator/etch/trunk/binding-cpp/runtime/src/test/support/EtchAsyncResultTest.cpp
Removed:
    incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResultBase.h
Modified:
    incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
    incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt

Added: incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResult.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResult.h?rev=1368570&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResult.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResult.h Thu Aug  2 16:16:35 2012
@@ -0,0 +1,94 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#ifndef __ETCHASYNCRESULT_H__
+#define __ETCHASYNCRESULT_H__
+
+#include "support/EtchAsyncResultNone.h"
+
+/**
+ * Base class of AsyncResult implementations.
+ * @param <T> The type of object used to implement AsyncResult.
+ */
+template<typename T>
+class EtchAsyncResult : public EtchAsyncResultNone {
+public:
+
+  /**
+   * Constructor
+   * @param runtime
+   * @param mailbox
+   */
+  EtchAsyncResult(EtchRuntime* runtime = NULL, EtchMailbox* mailbox = NULL)
+   : EtchAsyncResultNone(runtime, mailbox), mResult(NULL), mHasResult(false) {
+  }
+
+  /**
+   * Destructor
+   */
+  virtual ~EtchAsyncResult() {
+  }
+
+  /**
+   * Check if a result is available
+   */
+  capu::bool_t hasResult() {
+    mMutex.lock();
+    while(!mHasMailboxStatus) {
+      // TODO wait
+      break;
+      mCond.wait(&mMutex);
+    }
+    mMutex.unlock();
+    return mHasResult;
+  }
+
+  /**
+   * Sets the result
+   */
+  status_t setResult(capu::SmartPointer<T> result) {
+    mResult = result;
+    mHasResult = true;
+    
+    mMutex.lock();
+    mHasMailboxStatus = true;
+    mCond.signal();
+    mMutex.unlock();
+    return ETCH_OK;
+  }
+
+  /**
+   * Returns the result
+   */
+  status_t getResult(capu::SmartPointer<T> &result) {
+    if(hasException()) {
+      return ETCH_ERROR;
+    }
+    if(hasResult()) {
+      result = mResult;
+      return ETCH_OK;
+    }
+    return ETCH_OK;
+  }
+
+private:
+   capu::bool_t mHasResult;
+   capu::SmartPointer<T> mResult;
+};
+
+#endif

Added: incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResultNone.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResultNone.h?rev=1368570&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResultNone.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/support/EtchAsyncResultNone.h Thu Aug  2 16:16:35 2012
@@ -0,0 +1,79 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#ifndef __ETCHASYNCRESULTNONE_H__
+#define __ETCHASYNCRESULTNONE_H__
+
+#include "capu/os/Mutex.h"
+#include "capu/os/CondVar.h"
+#include "support/EtchMailbox.h"
+#include "common/EtchException.h"
+
+class EtchRuntime;
+
+/**
+ * Base class of AsyncResultBase.
+ */
+class EtchAsyncResultNone : public EtchMailbox::EtchNotify {
+public:
+
+  /**
+   * Constructor
+   * @param runtime
+   */
+  explicit EtchAsyncResultNone(EtchRuntime* runtime = NULL, EtchMailbox* mailbox = NULL);
+
+  /**
+   * Destructor
+   */
+  virtual ~EtchAsyncResultNone();
+
+  /**
+   * Check if an exception occured
+   */
+  virtual capu::bool_t hasException();
+
+  /**
+   * Returns the exception
+   */
+  virtual capu::SmartPointer<EtchException> getException();
+
+  /**
+   * Sets the exception
+   * @param exception
+   */
+  void setException(capu::SmartPointer<EtchException> exception);
+
+  /**
+   * @see EtchNotify
+   */
+  virtual status_t mailboxStatus(EtchMailbox* mb, EtchObject* state, capu::bool_t closed);
+
+protected:
+  EtchRuntime* mRuntime;
+  EtchMailbox* mMailbox;
+  capu::Mutex mMutex;
+  capu::CondVar mCond;
+  capu::bool_t mHasMailboxStatus;
+  capu::bool_t mHasException;
+  capu::SmartPointer<EtchException> mException;
+};
+
+typedef capu::SmartPointer<EtchAsyncResultNone> EtchAsyncResultNonePtr;
+
+#endif

Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt?rev=1368570&r1=1368569&r2=1368570&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt Thu Aug  2 16:16:35 2012
@@ -115,9 +115,10 @@ SET(MAIN_INCLUDES
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchBinaryTaggedData.h
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchBinaryTaggedDataInput.h
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchBinaryTaggedDataOutput.h
-	${PROJECT_SOURCE_DIR}/include/serialization/EtchValidators.h
+    ${PROJECT_SOURCE_DIR}/include/serialization/EtchValidators.h
     ${PROJECT_SOURCE_DIR}/include/support/EtchRuntime.h
-    ${PROJECT_SOURCE_DIR}/include/support/EtchAsyncResultBase.h
+    ${PROJECT_SOURCE_DIR}/include/support/EtchAsyncResultNone.h
+    ${PROJECT_SOURCE_DIR}/include/support/EtchAsyncResult.h
     ${PROJECT_SOURCE_DIR}/include/support/EtchDefaultServerFactory.h
     ${PROJECT_SOURCE_DIR}/include/support/EtchDeliveryService.h
     ${PROJECT_SOURCE_DIR}/include/support/EtchMailbox.h
@@ -224,6 +225,7 @@ SET(MAIN_SOURCES
     support/EtchRuntime.cpp
     support/EtchServerStack.cpp
     support/EtchClientStack.cpp
+    support/EtchAsyncResultNone.cpp
     util/EtchCircularQueue.cpp
     util/EtchUtil.cpp
     util/EtchIdGenerator.cpp

Added: incubator/etch/trunk/binding-cpp/runtime/src/main/support/EtchAsyncResultNone.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/support/EtchAsyncResultNone.cpp?rev=1368570&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/support/EtchAsyncResultNone.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/support/EtchAsyncResultNone.cpp Thu Aug  2 16:16:35 2012
@@ -0,0 +1,77 @@
+/* EtchAsyncResultNone.cpp
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#include "support/EtchAsyncResultNone.h"
+
+#define MAILBOX_NOTIFY_TIMEOUT 2000
+
+EtchAsyncResultNone::EtchAsyncResultNone(EtchRuntime* runtime, EtchMailbox* mailbox)
+  : mRuntime(runtime), mMailbox(mailbox), mHasMailboxStatus(false), mHasException(false) {
+    if(mailbox != NULL) {
+      mMailbox->registerNotify(this, NULL, MAILBOX_NOTIFY_TIMEOUT);
+    }
+    
+}
+
+EtchAsyncResultNone::~EtchAsyncResultNone() {
+  if(mMailbox != NULL) {
+    mMailbox->unregisterNotify(this);
+    delete mMailbox;
+  }
+}
+
+capu::bool_t EtchAsyncResultNone::hasException() {
+  mMutex.lock();
+  while(!mHasMailboxStatus) {
+    // TODO wait
+    break;
+    mCond.wait(&mMutex);
+  }
+  mMutex.unlock();
+  return mHasException;
+}
+
+capu::SmartPointer<EtchException> EtchAsyncResultNone::getException() {
+  if (hasException())
+    return mException;
+  return NULL;
+}
+
+void EtchAsyncResultNone::setException(capu::SmartPointer<EtchException> exception) {
+  mException = exception;
+  mHasException = true;
+
+  mMutex.lock();
+  mHasMailboxStatus = true;
+  mCond.signal();
+  mMutex.unlock();
+}
+
+status_t EtchAsyncResultNone::mailboxStatus(EtchMailbox* mb, EtchObject* state, capu::bool_t closed) {
+
+  // TODO get data or exception from mailbox
+  // TODO call onResult( EtchObject* state);
+  // TODO call onEception( EtchObject* state);
+  // TODO setException(NULL);
+  // TODO call delegate
+  mMutex.lock();
+  mHasMailboxStatus = true;
+  mCond.signal();
+  mMutex.unlock();
+  return ETCH_OK;
+}

Modified: incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt?rev=1368570&r1=1368569&r2=1368570&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt Thu Aug  2 16:16:35 2012
@@ -93,6 +93,7 @@ add_executable (etch-cpp-test
     support/EtchMonitorTest.cpp
     support/EtchRemoteBaseTest.cpp
     support/EtchRuntimeTest.cpp
+    support/EtchAsyncResultTest.cpp
     ${GTEST}/src/gtest-all.cc
     ${GMOCK}/src/gmock-all.cc
     main.cpp

Added: incubator/etch/trunk/binding-cpp/runtime/src/test/support/EtchAsyncResultTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/support/EtchAsyncResultTest.cpp?rev=1368570&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/support/EtchAsyncResultTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/support/EtchAsyncResultTest.cpp Thu Aug  2 16:16:35 2012
@@ -0,0 +1,40 @@
+/* $Id$
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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.
+ */
+
+#include <gtest/gtest.h>
+#include "support/EtchAsyncResult.h"
+
+TEST(EtchAsyncResultTest, constructor) {
+  EtchAsyncResultNone* r1 = new EtchAsyncResultNone(NULL, NULL);
+  delete r1;
+
+  EtchAsyncResult<EtchObject>* r2 = new EtchAsyncResult<EtchObject>(NULL, NULL);
+  delete r2;
+}
+
+TEST(EtchAsyncResultTest, hasException) {
+  EtchAsyncResultNone* r1 = new EtchAsyncResultNone(NULL, NULL);
+  // TODO test with real mailbox and a thread
+  delete r1;
+}
+
+TEST(EtchAsyncResultTest, hasResult) {
+  EtchAsyncResult<EtchObject>* r1 = new EtchAsyncResult<EtchObject>(NULL, NULL);
+  // TODO test with real mailbox and a thread
+  delete r1;
+}