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

svn commit: r1368508 - in /incubator/etch/trunk/binding-cpp/runtime: include/support/EtchMonitor.h src/main/CMakeLists.txt src/main/support/EtchMonitor.cpp src/test/CMakeLists.txt src/test/support/EtchMonitorTest.cpp

Author: fitzner
Date: Thu Aug  2 15:04:41 2012
New Revision: 1368508

URL: http://svn.apache.org/viewvc?rev=1368508&view=rev
Log:
ETCH-149: EtchMonitor Implementation

Change-Id: Ia81de91f0752092874c3651fc1ce247e9ab13057

Added:
    incubator/etch/trunk/binding-cpp/runtime/include/support/EtchMonitor.h
    incubator/etch/trunk/binding-cpp/runtime/src/main/support/EtchMonitor.cpp
    incubator/etch/trunk/binding-cpp/runtime/src/test/support/EtchMonitorTest.cpp
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/EtchMonitor.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/support/EtchMonitor.h?rev=1368508&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/support/EtchMonitor.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/support/EtchMonitor.h Thu Aug  2 15:04:41 2012
@@ -0,0 +1,173 @@
+/* $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 __ETCHMONITOR_H__
+#define __ETCHMONITOR_H__
+
+#include "capu/os/Mutex.h"
+#include "capu/os/CondVar.h"
+#include "capu/os/Time.h"
+#include "common/EtchString.h"
+
+/**
+ * A class which we can use to monitor conditions.
+ */
+class EtchMonitor {
+public:
+
+  /**
+   * Constructs the Monitor.
+   *
+   * @param description a description of this monitor.
+   * @param initialValue the initial value of this monitor.
+   */
+  EtchMonitor(EtchString& description, EtchString& initialValue);
+
+  /**
+   * Destructor
+   */
+  virtual ~EtchMonitor();
+
+  /**
+   * @return the description of this monitor.
+   */
+  EtchString getDescription();
+
+  /**
+   * @return the current value of the monitor.
+   */
+  EtchString get();
+
+  /**
+   * Sets the current value.
+   *
+   * @param newValue the value to be set.
+   * @param the old value.
+   */
+  status_t set(EtchString& value, EtchString& oldValue);
+
+  /**
+   * Waits until value equals the desired value and
+   * then sets the value. Will wait forever.
+   *
+   * @param desiredValue the value we want.
+   * @param newValue the value to be set.
+   * @param the old value
+   * @return ETCH_OK if it was successfully
+   */
+  status_t waitUntilEqAndSet(EtchString& desiredValue, EtchString& newValue, EtchString &old);
+
+  /**
+   * Waits until value equals the desired value and
+   * then sets the value.
+   *
+   * @param desiredValue the value we want.
+   * @param maxDelay the max amount of time in ms to wait.
+   * If 0 is specified, we will wait forever.
+   * @param newValue the value to be set.
+   * @param the old value
+   * @return ETCH_OK if it was successfully
+   */
+  status_t waitUntilEqAndSet(EtchString& desiredValue, capu::int32_t maxDelay, EtchString& newValue, EtchString &old);
+
+  /**
+   * Waits until value equals the desired value. Will wait forever.
+   *
+   * @param desiredValue the value we want.
+   * @return ETCH_OK if it was successfully
+   */
+  status_t waitUntilEq(EtchString& desiredValue);
+
+  /**
+   * Waits until value equals the desired value.
+   *
+   * @param desiredValue the value we want.
+   *
+   * @param maxDelay the max amount of time in ms to wait.
+   * If 0 is specified, we will wait forever.
+   * @return ETCH_OK if it was successfully
+   */
+  status_t waitUntilEq(EtchString& desiredValue, capu::int32_t maxDelay);
+
+  /**
+   * Waits until value does not equal the undesired value and then
+   * sets the value. Will wait forever.
+   *
+   * @param undesiredValue the value we do not want.
+   * @param newValue the value to be set.
+   * @param the old value
+   * @return ETCH_OK if it was successfully
+   */
+  status_t waitUntilNotEqAndSet(EtchString& undesiredValue, EtchString& newValue, EtchString& old);
+
+  /**
+   * Waits until value does not equal the undesired value and then
+   * sets the value.
+   *
+   * @param undesiredValue the value we do not want.
+   * @param maxDelay the max amount of time in ms to wait.
+   * If 0 is specified, we will wait forever.
+   * @param newValue the value to be set.
+   * @param the old value
+   * @return ETCH_OK if it was successfully, ETCH_TIMEOUT if a timeout occurs
+   */
+  status_t waitUntilNotEqAndSet(EtchString& undesiredValue, capu::int32_t maxDelay, EtchString& newValue, EtchString& old);
+
+  /**
+   * Waits until value does not equal the undesired value. Will
+   * wait forever.
+   *
+   * @param undesiredValue the value we do not want.
+   * @param the current value.
+   * @return ETCH_OK if it was successfully
+   */
+
+  status_t waitUntilNotEq(EtchString& undesiredValue, EtchString& current);
+
+  /**
+   * Waits until value does not equal the undesired value.
+   *
+   * @param undesiredValue the value we do not want.
+   * @param maxDelay the max amount of time in ms to wait.
+   * If 0 is specified, we will wait forever.
+   * @param the current value.
+   * @return ETCH_OK if it was successfully, ETCH_TIMEOUT if a timeout occurs
+   */
+  status_t waitUntilNotEq(EtchString& undesiredValue, capu::uint32_t maxDelay, EtchString& current);
+
+private:
+  /////////////////////
+  // PRIVATE METHODS //
+  /////////////////////
+
+  /**
+   * Compares the specified values.
+   *
+   * @param v1 a value to compare, which may be null.
+   * @param v2 another value to compare, which may be null.
+   * @return true if the values are equal, false otherwise.
+   */
+  capu::bool_t eq(EtchString& v1, EtchString& v2);
+
+  EtchString mDescription;
+  EtchString mValue;
+  capu::Mutex mMutex;
+  capu::CondVar mCv;
+};
+
+#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=1368508&r1=1368507&r2=1368508&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 15:04:41 2012
@@ -126,6 +126,7 @@ SET(MAIN_INCLUDES
     ${PROJECT_SOURCE_DIR}/include/transport/EtchTcpTransportFactory.h
     ${PROJECT_SOURCE_DIR}/include/support/EtchTransportHelper.h
     ${PROJECT_SOURCE_DIR}/include/support/EtchPlainMailbox.h
+    ${PROJECT_SOURCE_DIR}/include/support/EtchMonitor.h
     ${PROJECT_SOURCE_DIR}/include/util/EtchUtil.h
     ${PROJECT_SOURCE_DIR}/include/util/EtchCircularQueue.h
     ${PROJECT_SOURCE_DIR}/include/util/EtchIdGenerator.h
@@ -206,6 +207,7 @@ SET(MAIN_SOURCES
     transport/EtchTransportFactory.cpp
     transport/EtchTcpTransportFactory.cpp
     support/EtchTransportHelper.cpp
+    support/EtchMonitor.cpp
     util/EtchCircularQueue.cpp
     util/EtchUtil.cpp
     util/EtchIdGenerator.cpp

Added: incubator/etch/trunk/binding-cpp/runtime/src/main/support/EtchMonitor.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/support/EtchMonitor.cpp?rev=1368508&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/support/EtchMonitor.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/support/EtchMonitor.cpp Thu Aug  2 15:04:41 2012
@@ -0,0 +1,133 @@
+/* $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 "capu/os/NumericLimits.h"
+#include "support/EtchMonitor.h"
+
+EtchMonitor::EtchMonitor(EtchString& description, EtchString& initialValue) {
+  mDescription = description;
+  mValue = initialValue;
+}
+
+EtchMonitor::~EtchMonitor() {}
+
+EtchString EtchMonitor::getDescription() {
+  return mDescription;
+}
+
+EtchString EtchMonitor::get() {
+  return mValue;
+}
+
+status_t EtchMonitor::set(EtchString& value, EtchString& oldValue) {
+  mMutex.lock();
+  oldValue = mValue;
+  mValue = value;
+  mCv.broadcast();
+  mMutex.unlock();
+  return ETCH_OK;
+}
+
+status_t EtchMonitor::waitUntilEqAndSet(EtchString& desiredValue, EtchString& newValue, EtchString &old) {
+  return waitUntilEqAndSet(desiredValue, 0, newValue, old);
+}
+
+status_t EtchMonitor::waitUntilEqAndSet(EtchString& desiredValue, capu::int32_t maxDelay, EtchString& newValue, EtchString &old) {
+  mMutex.lock();
+  if (waitUntilEq(desiredValue, maxDelay) != ETCH_OK) {
+    mMutex.unlock();
+    return ETCH_TIMEOUT;
+  }
+  set(newValue, old);
+  mMutex.unlock();
+  return ETCH_OK;
+}
+
+status_t EtchMonitor::waitUntilEq(EtchString& desiredValue) {
+  return waitUntilEq(desiredValue, 0);
+}
+
+status_t EtchMonitor::waitUntilEq(EtchString& desiredValue, capu::int32_t maxDelay) {
+  mMutex.lock();
+  
+  capu::uint32_t now = capu::Time::GetMilliseconds();
+  capu::uint32_t end = (maxDelay > 0) ? now + maxDelay : capu::NumericLimitMax<capu::uint32_t>();
+
+  capu::int32_t d = end - now;
+  while (!eq(mValue, desiredValue) && d > 0) {
+    mCv.wait(&mMutex, d);
+    now = capu::Time::GetMilliseconds();
+    d = end - now;
+  }
+
+  if (!eq(mValue, desiredValue)) {
+    mMutex.unlock();
+    return ETCH_TIMEOUT;
+  }
+
+  mMutex.unlock();
+  return ETCH_OK;
+}
+
+status_t EtchMonitor::waitUntilNotEqAndSet(EtchString& undesiredValue, EtchString& newValue, EtchString& old) {
+  return waitUntilNotEqAndSet(undesiredValue, 0, newValue, old);
+}
+
+status_t EtchMonitor::waitUntilNotEqAndSet(EtchString& undesiredValue, capu::int32_t maxDelay, EtchString& newValue, EtchString& old) {
+  mMutex.lock();
+  EtchString str;
+  if (waitUntilNotEq(undesiredValue, maxDelay, str) != ETCH_OK) {
+    mMutex.unlock();
+    return ETCH_TIMEOUT;
+  }
+  set(newValue, old);
+  mMutex.unlock();
+  return ETCH_OK;
+}
+
+status_t EtchMonitor::waitUntilNotEq(EtchString& undesiredValue, EtchString& current) {
+  return waitUntilNotEq(undesiredValue, 0, current);
+}
+
+status_t EtchMonitor::waitUntilNotEq(EtchString& undesiredValue, capu::uint32_t maxDelay, EtchString& current) {
+  mMutex.lock();
+
+  capu::uint32_t now = capu::Time::GetMilliseconds();
+  capu::uint32_t end = (maxDelay > 0) ? now + maxDelay : capu::NumericLimitMax<capu::uint32_t>();
+
+  capu::int32_t d = end - now;
+  while (eq(mValue, undesiredValue) && d > 0) {
+    mCv.wait(&mMutex, d);
+
+    now = capu::Time::GetMilliseconds();
+    d = end - now;
+  }
+
+  if (eq(mValue, undesiredValue)) {
+    mMutex.unlock();
+    return ETCH_TIMEOUT;
+  }
+
+  current = mValue;
+  mMutex.unlock();
+  return ETCH_OK;
+}
+
+capu::bool_t EtchMonitor::eq(EtchString& v1, EtchString& v2) {
+  return v1.equals(&v2);
+}
\ No newline at end of file

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=1368508&r1=1368507&r2=1368508&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 15:04:41 2012
@@ -85,6 +85,7 @@ add_executable (etch-cpp-test
     serialization/EtchBinaryTaggedDataInputOutputTest.cpp
     support/EtchFreePoolTest.cpp
     support/EtchPlainMailboxTest.cpp
+    support/EtchMonitorTest.cpp
     util/EtchCircularQueueTest.cpp
     util/EtchUtilTest.cpp
     ${GTEST}/src/gtest-all.cc

Added: incubator/etch/trunk/binding-cpp/runtime/src/test/support/EtchMonitorTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/support/EtchMonitorTest.cpp?rev=1368508&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/support/EtchMonitorTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/support/EtchMonitorTest.cpp Thu Aug  2 15:04:41 2012
@@ -0,0 +1,131 @@
+/* $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/EtchMonitor.h"
+
+TEST(EtchMonitorTest, constructorTest) {
+  EtchMonitor *m = new EtchMonitor(EtchString("desc"), EtchString("init"));
+  EXPECT_TRUE(m != NULL);
+  delete m;
+}
+
+TEST(EtchMonitorTest, setTest) {
+  EtchString init("init");
+  EtchMonitor *m = new EtchMonitor(EtchString("desc"), init);
+  EtchString tmp1("tmp1");
+  EtchString tmp2("tmp2");
+  EtchString tmp3("tmp3");
+  EtchString old;
+  EXPECT_TRUE(m != NULL);
+
+  m->set(tmp1, old);
+  EXPECT_TRUE(old.equals(&init));
+  m->set(tmp2, old);
+  EXPECT_TRUE(old.equals(&tmp1));
+  m->set(tmp3, old);
+  EXPECT_TRUE(old.equals(&tmp2));
+
+  delete m;
+}
+
+TEST(EtchMonitorTest, waitUntilEqTest) {
+  EtchString init("init");
+  EtchMonitor *m = new EtchMonitor(EtchString("desc"), init);
+  EtchString tmp1("tmp1");
+  EtchString tmp2("tmp2");
+  EtchString old;
+
+  EXPECT_EQ(ETCH_OK, m->waitUntilEq(init));
+  EXPECT_EQ(ETCH_OK, m->waitUntilEq(init, 0));
+  m->set(tmp1, old);
+  EXPECT_EQ(ETCH_OK, m->waitUntilEq(tmp1));
+  EXPECT_EQ(ETCH_OK, m->waitUntilEq(tmp1, 0));
+
+  m->set(tmp2, old);
+  EXPECT_EQ(ETCH_OK, m->waitUntilEq(tmp2));
+  EXPECT_EQ(ETCH_OK, m->waitUntilEq(tmp2, 0));
+  EXPECT_EQ(ETCH_TIMEOUT, m->waitUntilEq(tmp1, 2000));
+  delete m;
+}
+
+TEST(EtchMonitorTest, waitUntilNotEqTest) {
+  EtchString init("init");
+  EtchMonitor *m = new EtchMonitor(EtchString("desc"), init);
+  EtchString tmp1("tmp1");
+  EtchString tmp2("tmp2");
+  EtchString current;
+
+  EXPECT_EQ(ETCH_OK, m->waitUntilNotEq(tmp1, current));
+  EXPECT_EQ(ETCH_OK, m->waitUntilNotEq(tmp1, 0, current));
+  m->set(tmp1, current);
+  EXPECT_EQ(ETCH_OK, m->waitUntilNotEq(tmp2, current));
+  EXPECT_EQ(ETCH_OK, m->waitUntilNotEq(tmp2, 0, current));
+
+  m->set(tmp2, current);
+  EXPECT_EQ(ETCH_TIMEOUT, m->waitUntilNotEq(tmp2, current));
+  EXPECT_EQ(ETCH_TIMEOUT, m->waitUntilNotEq(tmp2, 2000, current));
+  EXPECT_EQ(ETCH_OK, m->waitUntilNotEq(tmp1, 2000, current));
+
+  delete m;
+}
+
+TEST(EtchMonitorTest, waitUntilEqAndSetTest) {
+  EtchString init("init");
+  EtchMonitor *m = new EtchMonitor(EtchString("desc"), init);
+  EtchString tmp1("tmp1");
+  EtchString tmp2("tmp2");
+  EtchString current;
+
+  EXPECT_EQ(ETCH_OK, m->waitUntilEqAndSet(init, tmp1, current));
+  EXPECT_EQ(ETCH_OK, m->waitUntilEqAndSet(tmp1, 0, tmp1, current));
+  m->set(tmp1, current);
+  EXPECT_EQ(ETCH_OK, m->waitUntilEqAndSet(tmp1, tmp2, current));
+  EXPECT_EQ(ETCH_OK, m->waitUntilEqAndSet(tmp2, 0, tmp2, current));
+
+  m->set(tmp2, current);
+  EXPECT_EQ(ETCH_TIMEOUT, m->waitUntilEqAndSet(tmp1, tmp2, current));
+  EXPECT_EQ(ETCH_TIMEOUT, m->waitUntilEqAndSet(init, 2000, tmp2, current));
+  EXPECT_EQ(ETCH_OK, m->waitUntilEqAndSet(tmp2, 2000, tmp2, current));
+
+  delete m;
+}
+
+TEST(EtchMonitorTest, waitUntilNotEqAndSetTest) {
+  EtchString init("init");
+  EtchMonitor *m = new EtchMonitor(EtchString("desc"), init);
+  EtchString tmp1("tmp1");
+  EtchString tmp2("tmp2");
+  EtchString current;
+
+  EXPECT_EQ(ETCH_OK, m->waitUntilNotEqAndSet(tmp1, init, current));
+  EXPECT_EQ(ETCH_OK, m->waitUntilNotEqAndSet(tmp1, 0, init, current));
+  m->set(tmp1, current);
+  EXPECT_EQ(ETCH_OK, m->waitUntilNotEqAndSet(tmp2, tmp1, current));
+  EXPECT_EQ(ETCH_OK, m->waitUntilNotEqAndSet(tmp2, 0, tmp2, current));
+
+  m->set(tmp2, current);
+  EXPECT_EQ(ETCH_TIMEOUT, m->waitUntilNotEqAndSet(tmp2, tmp1, current));
+  EXPECT_EQ(ETCH_TIMEOUT, m->waitUntilNotEqAndSet(tmp2, 2000, tmp2, current));
+  EXPECT_EQ(ETCH_OK, m->waitUntilNotEqAndSet(tmp1, 2000, tmp2, current));
+
+  delete m;
+}