diff options
author | Alan Conway <aconway@apache.org> | 2007-08-27 15:52:47 +0000 |
---|---|---|
committer | Alan Conway <aconway@apache.org> | 2007-08-27 15:52:47 +0000 |
commit | d7b1ad93da22a7c1caf499b9eec7200a7ae52b6e (patch) | |
tree | 4943e75d94dc060bd8ba0206de9be3d85481b7a4 /cpp/src | |
parent | 366b7287350d47f788a0d1af1a1bf73328e4ec1f (diff) | |
download | qpid-python-d7b1ad93da22a7c1caf499b9eec7200a7ae52b6e.tar.gz |
* 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.
git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@570164 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Makefile.am | 4 | ||||
-rw-r--r-- | cpp/src/qpid/broker/SessionState.h | 57 | ||||
-rw-r--r-- | cpp/src/qpid/broker/SuspendedSessions.cpp | 58 | ||||
-rw-r--r-- | cpp/src/qpid/broker/SuspendedSessions.h | 59 | ||||
-rw-r--r-- | cpp/src/tests/Makefile.am | 5 | ||||
-rw-r--r-- | cpp/src/tests/Session.cpp | 59 |
6 files changed, 242 insertions, 0 deletions
diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am index a8383442b5..726bbd12c5 100644 --- a/cpp/src/Makefile.am +++ b/cpp/src/Makefile.am @@ -192,6 +192,9 @@ libqpidbroker_la_SOURCES = \ 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 @@ nobase_include_HEADERS = \ 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 \ diff --git a/cpp/src/qpid/broker/SessionState.h b/cpp/src/qpid/broker/SessionState.h new file mode 100644 index 0000000000..30f071ca55 --- /dev/null +++ b/cpp/src/qpid/broker/SessionState.h @@ -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*/ diff --git a/cpp/src/qpid/broker/SuspendedSessions.cpp b/cpp/src/qpid/broker/SuspendedSessions.cpp new file mode 100644 index 0000000000..cc90f04809 --- /dev/null +++ b/cpp/src/qpid/broker/SuspendedSessions.cpp @@ -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 + + + diff --git a/cpp/src/qpid/broker/SuspendedSessions.h b/cpp/src/qpid/broker/SuspendedSessions.h new file mode 100644 index 0000000000..03c5df27ed --- /dev/null +++ b/cpp/src/qpid/broker/SuspendedSessions.h @@ -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*/ diff --git a/cpp/src/tests/Makefile.am b/cpp/src/tests/Makefile.am index db9da392ea..34e7e973ac 100644 --- a/cpp/src/tests/Makefile.am +++ b/cpp/src/tests/Makefile.am @@ -19,6 +19,11 @@ CLEANFILES= # # 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 diff --git a/cpp/src/tests/Session.cpp b/cpp/src/tests/Session.cpp new file mode 100644 index 0000000000..445f612111 --- /dev/null +++ b/cpp/src/tests/Session.cpp @@ -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 (...) {} +} + + |