You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by ac...@apache.org on 2007/08/27 17:52:48 UTC

svn commit: r570164 - in /incubator/qpid/trunk/qpid/cpp/src: Makefile.am qpid/broker/SessionState.h qpid/broker/SuspendedSessions.cpp qpid/broker/SuspendedSessions.h tests/Makefile.am tests/Session.cpp

Author: aconway
Date: Mon Aug 27 08:52:47 2007
New Revision: 570164

URL: http://svn.apache.org/viewvc?rev=570164&view=rev
Log:

	* src/qpid/broker/SessionState.h: State of a session.
	* src/qpid/broker/SuspendedSessions.h, .cpp: Stores suspended sessions.
	* src/tests/Session.cpp: Test session classes.

Added:
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionState.h   (with props)
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.cpp   (with props)
    incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.h   (with props)
    incubator/qpid/trunk/qpid/cpp/src/tests/Session.cpp   (with props)
Modified:
    incubator/qpid/trunk/qpid/cpp/src/Makefile.am
    incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am

Modified: incubator/qpid/trunk/qpid/cpp/src/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/Makefile.am?rev=570164&r1=570163&r2=570164&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/Makefile.am Mon Aug 27 08:52:47 2007
@@ -192,6 +192,9 @@
   qpid/broker/RecoveredEnqueue.cpp \
   qpid/broker/RecoveredDequeue.cpp \
   qpid/broker/Reference.cpp \
+  qpid/broker/Session.h \
+  qpid/broker/SuspendedSessions.h \
+  qpid/broker/SuspendedSessions.cpp \
   qpid/broker/SemanticHandler.cpp \
   qpid/broker/Timer.cpp \
   qpid/broker/TopicExchange.cpp \
@@ -327,6 +330,7 @@
   qpid/framing/Buffer.h \
   qpid/framing/ChannelAdapter.h \
   qpid/framing/FieldTable.h \
+  qpid/framing/FrameDefaultVisitor.h \
   qpid/framing/FramingContent.h \
   qpid/framing/HeaderProperties.h \
   qpid/framing/InitiationHandler.h \

Added: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionState.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionState.h?rev=570164&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionState.h (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionState.h Mon Aug 27 08:52:47 2007
@@ -0,0 +1,57 @@
+#ifndef QPID_BROKER_SESSION_H
+#define QPID_BROKER_SESSION_H
+
+/*
+ *
+ * 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 "qpid/framing/Uuid.h"
+
+namespace qpid {
+namespace broker {
+
+/**
+ * State of a session.
+ */
+class SessionState
+{
+  public:
+    /** Create a new, active session. */
+    SessionState(uint32_t timeoutSeconds)
+        : id(true), active(true), timeout(timeoutSeconds) {}
+
+    const framing::Uuid& getId() const { return id; }
+    uint32_t getTimeout() const { return timeout; }
+
+    /** Call SuspendedSessions::resume to re-activate a suspended session. */ 
+    bool isActive() const { return active; }
+
+  private:
+  friend class SuspendedSessions;
+    framing::Uuid id;
+    bool active;
+    uint32_t timeout;
+};
+
+}} // namespace qpid::broker
+
+
+
+#endif  /*!QPID_BROKER_SESSION_H*/

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionState.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SessionState.h
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.cpp?rev=570164&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.cpp (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.cpp Mon Aug 27 08:52:47 2007
@@ -0,0 +1,58 @@
+/*
+ *
+ * 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 "SuspendedSessions.h"
+#include <boost/bind.hpp>
+
+namespace qpid {
+namespace broker {
+
+using namespace framing;
+using namespace sys;
+using namespace boost;
+typedef Mutex::ScopedLock Lock;
+
+void SuspendedSessions::suspend(SessionState& s) {
+    Lock l(lock);
+    assert(s.isActive());
+    AbsTime expires(now(), Duration(s.timeout*TIME_SEC));
+    suspended.insert(std::make_pair(expires, s));
+    s.active = false;
+}
+
+SessionState SuspendedSessions::resume(const Uuid& id)
+{
+    Lock l(lock);
+    Map::iterator expired = suspended.lower_bound(now());
+    suspended.erase(suspended.begin(), expired);
+
+    Map::iterator resume = std::find_if(
+        suspended.begin(), suspended.end(),
+        bind(&SessionState::getId, bind(&Map::value_type::second, _1))==id);
+    
+    if (resume == suspended.end())
+        throw Exception(QPID_MSG("Session timed out or invalid ID: " << id));
+    return resume->second;
+}
+
+}} // namespace qpid::broker
+
+
+

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.cpp
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.h
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.h?rev=570164&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.h (added)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.h Mon Aug 27 08:52:47 2007
@@ -0,0 +1,59 @@
+#ifndef QPID_BROKER_SUSPENDEDSESSIONS_H
+#define QPID_BROKER_SUSPENDEDSESSIONS_H
+
+/*
+ *
+ * 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 "qpid/broker/SessionState.h"
+#include "qpid/sys/Time.h"
+#include "qpid/sys/Mutex.h"
+
+#include <map>
+
+namespace qpid {
+namespace broker {
+
+/** Collection of suspended sessions.
+ * Thread safe.
+ */
+class SuspendedSessions {
+    typedef std::multimap<sys::AbsTime,SessionState> Map;
+
+    sys::Mutex lock;
+    Map suspended;
+        
+  public:
+    /** Suspend a session, start it's timeout counter.*/
+    void suspend(SessionState& session);
+        
+    /** Resume a suspended session.
+     *@throw Exception if timed out or non-existant.
+     */
+    SessionState resume(const framing::Uuid& id);
+};
+    
+
+
+}} // namespace qpid::broker
+
+
+
+#endif  /*!QPID_BROKER_SUSPENDEDSESSIONS_H*/

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.h
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/qpid/trunk/qpid/cpp/src/qpid/broker/SuspendedSessions.h
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am?rev=570164&r1=570163&r2=570164&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/Makefile.am Mon Aug 27 08:52:47 2007
@@ -19,6 +19,11 @@
 #
 # Unit test programs.
 # 
+TESTS+=Session
+check_PROGRAMS+=Session
+Session_SOURCES=Session.cpp
+Session_LDADD=-lboost_unit_test_framework $(lib_broker)
+
 TESTS+=Blob
 check_PROGRAMS+=Blob
 Blob_SOURCES=Blob.cpp ../qpid/framing/Blob.cpp

Added: incubator/qpid/trunk/qpid/cpp/src/tests/Session.cpp
URL: http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/Session.cpp?rev=570164&view=auto
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/Session.cpp (added)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/Session.cpp Mon Aug 27 08:52:47 2007
@@ -0,0 +1,59 @@
+/*
+ *
+ * Copyright (c) 2006 The Apache Software Foundation
+ *
+ * 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.
+ *
+ */
+
+#include "qpid/broker/SuspendedSessions.h"
+
+#define BOOST_AUTO_TEST_MAIN
+#include <boost/test/auto_unit_test.hpp>
+
+using namespace std;
+using namespace qpid::framing;
+using namespace qpid::broker;
+using namespace qpid::sys;
+
+BOOST_AUTO_TEST_CASE(testSuspendedSessions) {
+    SuspendedSessions suspended;
+
+    SessionState s(0);          // 0 timeout
+    BOOST_CHECK(s.isActive());
+    suspended.suspend(s);
+    BOOST_CHECK(!s.isActive());
+    try {
+        s = suspended.resume(s.getId());
+        BOOST_FAIL("Expected session to be timed out.");
+    } catch (...) {}
+
+    s = SessionState(1);        // New session, 1 sec timeout.
+    try {
+        suspended.resume(s.getId());
+        BOOST_FAIL("Expeced exception: non-existent session.");
+    } catch (...) {}
+    suspended.suspend(s);
+    BOOST_CHECK(!s.isActive());
+    s = suspended.resume(s.getId());
+    BOOST_CHECK(s.isActive());
+
+    suspended.suspend(s);       // Real timeout
+    sleep(2);
+    try {
+        suspended.resume(s.getId());
+        BOOST_FAIL("Expeced timeout.");
+    } catch (...) {}
+}
+
+

Propchange: incubator/qpid/trunk/qpid/cpp/src/tests/Session.cpp
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: incubator/qpid/trunk/qpid/cpp/src/tests/Session.cpp
------------------------------------------------------------------------------
    svn:keywords = Rev Date