From ca3a7cd64822e874076bd23e9981af077eb47b03 Mon Sep 17 00:00:00 2001 From: Alan Conway Date: Fri, 13 Apr 2007 20:58:27 +0000 Subject: Moved src/ source code to src/qpid directory: - allows rhm package to build consistently against checked-out or installed qpid. - consistent correspondence between source paths and C++ namespaces. - consistent use of #include in source and by users. - allows header files to split over multiple directories, e.g. separating generated code, separating public API from private files. git-svn-id: https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid@528668 13f79535-47bb-0310-9956-ffa450edef68 --- cpp/src/Exception.cpp | 46 --- cpp/src/Exception.h | 97 ----- cpp/src/ExceptionHolder.cpp | 32 -- cpp/src/ExceptionHolder.h | 67 ---- cpp/src/Makefile.am | 91 ++--- cpp/src/QpidError.cpp | 42 --- cpp/src/QpidError.h | 78 ---- cpp/src/SharedObject.h | 55 --- cpp/src/broker/AccumulatedAck.cpp | 57 --- cpp/src/broker/AccumulatedAck.h | 57 --- cpp/src/broker/AutoDelete.cpp | 86 ----- cpp/src/broker/AutoDelete.h | 55 --- cpp/src/broker/Broker.cpp | 122 ------ cpp/src/broker/Broker.h | 108 ------ cpp/src/broker/BrokerAdapter.cpp | 388 ------------------- cpp/src/broker/BrokerAdapter.h | 222 ----------- cpp/src/broker/BrokerChannel.cpp | 347 ----------------- cpp/src/broker/BrokerChannel.h | 159 -------- cpp/src/broker/BrokerExchange.h | 51 --- cpp/src/broker/BrokerMessage.cpp | 244 ------------ cpp/src/broker/BrokerMessage.h | 137 ------- cpp/src/broker/BrokerMessageBase.h | 182 --------- cpp/src/broker/BrokerMessageMessage.cpp | 320 ---------------- cpp/src/broker/BrokerMessageMessage.h | 102 ----- cpp/src/broker/BrokerQueue.cpp | 284 -------------- cpp/src/broker/BrokerQueue.h | 152 -------- cpp/src/broker/BrokerSingleton.cpp | 36 -- cpp/src/broker/BrokerSingleton.h | 52 --- cpp/src/broker/CompletionHandler.h | 39 -- cpp/src/broker/Configuration.cpp | 252 ------------- cpp/src/broker/Configuration.h | 171 --------- cpp/src/broker/Connection.cpp | 128 ------- cpp/src/broker/Connection.h | 108 ------ cpp/src/broker/ConnectionFactory.cpp | 43 --- cpp/src/broker/ConnectionFactory.h | 47 --- cpp/src/broker/ConnectionToken.h | 38 -- cpp/src/broker/Consumer.h | 37 -- cpp/src/broker/Content.h | 64 ---- cpp/src/broker/Deliverable.h | 37 -- cpp/src/broker/DeliverableMessage.cpp | 33 -- cpp/src/broker/DeliverableMessage.h | 41 -- cpp/src/broker/DeliveryRecord.cpp | 87 ----- cpp/src/broker/DeliveryRecord.h | 63 ---- cpp/src/broker/DirectExchange.cpp | 71 ---- cpp/src/broker/DirectExchange.h | 57 --- cpp/src/broker/ExchangeRegistry.cpp | 76 ---- cpp/src/broker/ExchangeRegistry.h | 47 --- cpp/src/broker/FanOutExchange.cpp | 56 --- cpp/src/broker/FanOutExchange.h | 60 --- cpp/src/broker/HandlerImpl.h | 71 ---- cpp/src/broker/HeadersExchange.cpp | 119 ------ cpp/src/broker/HeadersExchange.h | 65 ---- cpp/src/broker/InMemoryContent.cpp | 72 ---- cpp/src/broker/InMemoryContent.h | 45 --- cpp/src/broker/LazyLoadedContent.cpp | 68 ---- cpp/src/broker/LazyLoadedContent.h | 50 --- cpp/src/broker/MessageBuilder.cpp | 74 ---- cpp/src/broker/MessageBuilder.h | 57 --- cpp/src/broker/MessageHandlerImpl.cpp | 243 ------------ cpp/src/broker/MessageHandlerImpl.h | 130 ------- cpp/src/broker/MessageStore.h | 129 ------- cpp/src/broker/MessageStoreModule.cpp | 114 ------ cpp/src/broker/MessageStoreModule.h | 68 ---- cpp/src/broker/NameGenerator.cpp | 32 -- cpp/src/broker/NameGenerator.h | 39 -- cpp/src/broker/NullMessageStore.cpp | 110 ------ cpp/src/broker/NullMessageStore.h | 65 ---- cpp/src/broker/Persistable.h | 62 ---- cpp/src/broker/PersistableExchange.h | 44 --- cpp/src/broker/PersistableMessage.h | 53 --- cpp/src/broker/PersistableQueue.h | 45 --- cpp/src/broker/Prefetch.h | 42 --- cpp/src/broker/QueuePolicy.cpp | 69 ---- cpp/src/broker/QueuePolicy.h | 54 --- cpp/src/broker/QueueRegistry.cpp | 78 ---- cpp/src/broker/QueueRegistry.h | 96 ----- cpp/src/broker/RecoverableMessage.h | 58 --- cpp/src/broker/RecoverableQueue.h | 51 --- cpp/src/broker/RecoveryManager.h | 45 --- cpp/src/broker/RecoveryManagerImpl.cpp | 143 ------- cpp/src/broker/RecoveryManagerImpl.h | 55 --- cpp/src/broker/Reference.cpp | 53 --- cpp/src/broker/Reference.h | 114 ------ cpp/src/broker/TopicExchange.cpp | 154 -------- cpp/src/broker/TopicExchange.h | 100 ----- cpp/src/broker/TransactionalStore.h | 60 --- cpp/src/broker/TxAck.cpp | 54 --- cpp/src/broker/TxAck.h | 57 --- cpp/src/broker/TxBuffer.cpp | 56 --- cpp/src/broker/TxBuffer.h | 107 ------ cpp/src/broker/TxOp.h | 42 --- cpp/src/broker/TxPublish.cpp | 60 --- cpp/src/broker/TxPublish.h | 78 ---- cpp/src/client/AckMode.h | 102 ----- cpp/src/client/BasicMessageChannel.cpp | 345 ----------------- cpp/src/client/BasicMessageChannel.h | 90 ----- cpp/src/client/ClientAdapter.cpp | 70 ---- cpp/src/client/ClientAdapter.h | 66 ---- cpp/src/client/ClientChannel.cpp | 341 ----------------- cpp/src/client/ClientChannel.h | 356 ------------------ cpp/src/client/ClientConnection.cpp | 156 -------- cpp/src/client/ClientExchange.cpp | 34 -- cpp/src/client/ClientExchange.h | 106 ------ cpp/src/client/ClientMessage.h | 64 ---- cpp/src/client/ClientQueue.cpp | 58 --- cpp/src/client/ClientQueue.h | 103 ------ cpp/src/client/Connection.h | 179 --------- cpp/src/client/Connector.cpp | 188 ---------- cpp/src/client/Connector.h | 98 ----- cpp/src/client/IncomingMessage.cpp | 168 --------- cpp/src/client/IncomingMessage.h | 136 ------- cpp/src/client/MessageChannel.h | 94 ----- cpp/src/client/MessageListener.cpp | 24 -- cpp/src/client/MessageListener.h | 49 --- cpp/src/client/MessageMessageChannel.cpp | 431 ---------------------- cpp/src/client/MessageMessageChannel.h | 82 ---- cpp/src/client/MethodBodyInstances.h | 100 ----- cpp/src/client/ResponseHandler.cpp | 79 ---- cpp/src/client/ResponseHandler.h | 75 ---- cpp/src/client/ReturnedMessageHandler.cpp | 24 -- cpp/src/client/ReturnedMessageHandler.h | 49 --- cpp/src/doxygen_mainpage.h | 45 --- cpp/src/framing/AMQBody.cpp | 33 -- cpp/src/framing/AMQBody.h | 59 --- cpp/src/framing/AMQContentBody.cpp | 43 --- cpp/src/framing/AMQContentBody.h | 53 --- cpp/src/framing/AMQDataBlock.h | 42 --- cpp/src/framing/AMQFrame.cpp | 139 ------- cpp/src/framing/AMQFrame.h | 78 ---- cpp/src/framing/AMQHeaderBody.cpp | 75 ---- cpp/src/framing/AMQHeaderBody.h | 60 --- cpp/src/framing/AMQHeartbeatBody.cpp | 29 -- cpp/src/framing/AMQHeartbeatBody.h | 47 --- cpp/src/framing/AMQMethodBody.cpp | 59 --- cpp/src/framing/AMQMethodBody.h | 84 ----- cpp/src/framing/AMQRequestBody.cpp | 66 ---- cpp/src/framing/AMQRequestBody.h | 78 ---- cpp/src/framing/AMQResponseBody.cpp | 65 ---- cpp/src/framing/AMQResponseBody.h | 85 ----- cpp/src/framing/BasicHeaderProperties.cpp | 103 ------ cpp/src/framing/BasicHeaderProperties.h | 115 ------ cpp/src/framing/BodyHandler.cpp | 60 --- cpp/src/framing/BodyHandler.h | 61 --- cpp/src/framing/Buffer.cpp | 183 --------- cpp/src/framing/Buffer.h | 86 ----- cpp/src/framing/ChannelAdapter.cpp | 109 ------ cpp/src/framing/ChannelAdapter.h | 113 ------ cpp/src/framing/Correlator.cpp | 42 --- cpp/src/framing/Correlator.h | 68 ---- cpp/src/framing/FieldTable.cpp | 150 -------- cpp/src/framing/FieldTable.h | 90 ----- cpp/src/framing/FramingContent.cpp | 75 ---- cpp/src/framing/FramingContent.h | 41 -- cpp/src/framing/HeaderProperties.h | 46 --- cpp/src/framing/InitiationHandler.cpp | 24 -- cpp/src/framing/InitiationHandler.h | 41 -- cpp/src/framing/InputHandler.h | 39 -- cpp/src/framing/MethodContext.cpp | 31 -- cpp/src/framing/MethodContext.h | 75 ---- cpp/src/framing/OutputHandler.h | 39 -- cpp/src/framing/ProtocolInitiation.cpp | 63 ---- cpp/src/framing/ProtocolInitiation.h | 54 --- cpp/src/framing/ProtocolVersion.cpp | 44 --- cpp/src/framing/ProtocolVersion.h | 57 --- cpp/src/framing/ProtocolVersionException.cpp | 33 -- cpp/src/framing/ProtocolVersionException.h | 56 --- cpp/src/framing/Proxy.cpp | 32 -- cpp/src/framing/Proxy.h | 51 --- cpp/src/framing/Requester.cpp | 40 -- cpp/src/framing/Requester.h | 68 ---- cpp/src/framing/Responder.cpp | 43 --- cpp/src/framing/Responder.h | 61 --- cpp/src/framing/Value.cpp | 122 ------ cpp/src/framing/Value.h | 171 --------- cpp/src/framing/amqp_framing.h | 36 -- cpp/src/framing/amqp_types.h | 57 --- cpp/src/framing/amqp_types_full.h | 36 -- cpp/src/make-gen-src-mk.sh | 5 +- cpp/src/qpid/Exception.cpp | 46 +++ cpp/src/qpid/Exception.h | 97 +++++ cpp/src/qpid/ExceptionHolder.cpp | 32 ++ cpp/src/qpid/ExceptionHolder.h | 67 ++++ cpp/src/qpid/QpidError.cpp | 42 +++ cpp/src/qpid/QpidError.h | 78 ++++ cpp/src/qpid/SharedObject.h | 55 +++ cpp/src/qpid/broker/AccumulatedAck.cpp | 57 +++ cpp/src/qpid/broker/AccumulatedAck.h | 57 +++ cpp/src/qpid/broker/AutoDelete.cpp | 86 +++++ cpp/src/qpid/broker/AutoDelete.h | 55 +++ cpp/src/qpid/broker/Broker.cpp | 122 ++++++ cpp/src/qpid/broker/Broker.h | 108 ++++++ cpp/src/qpid/broker/BrokerAdapter.cpp | 388 +++++++++++++++++++ cpp/src/qpid/broker/BrokerAdapter.h | 222 +++++++++++ cpp/src/qpid/broker/BrokerChannel.cpp | 347 +++++++++++++++++ cpp/src/qpid/broker/BrokerChannel.h | 159 ++++++++ cpp/src/qpid/broker/BrokerExchange.h | 51 +++ cpp/src/qpid/broker/BrokerMessage.cpp | 244 ++++++++++++ cpp/src/qpid/broker/BrokerMessage.h | 137 +++++++ cpp/src/qpid/broker/BrokerMessageBase.h | 182 +++++++++ cpp/src/qpid/broker/BrokerMessageMessage.cpp | 320 ++++++++++++++++ cpp/src/qpid/broker/BrokerMessageMessage.h | 102 +++++ cpp/src/qpid/broker/BrokerQueue.cpp | 284 ++++++++++++++ cpp/src/qpid/broker/BrokerQueue.h | 152 ++++++++ cpp/src/qpid/broker/BrokerSingleton.cpp | 36 ++ cpp/src/qpid/broker/BrokerSingleton.h | 52 +++ cpp/src/qpid/broker/CompletionHandler.h | 39 ++ cpp/src/qpid/broker/Configuration.cpp | 252 +++++++++++++ cpp/src/qpid/broker/Configuration.h | 171 +++++++++ cpp/src/qpid/broker/Connection.cpp | 128 +++++++ cpp/src/qpid/broker/Connection.h | 108 ++++++ cpp/src/qpid/broker/ConnectionFactory.cpp | 43 +++ cpp/src/qpid/broker/ConnectionFactory.h | 47 +++ cpp/src/qpid/broker/ConnectionToken.h | 38 ++ cpp/src/qpid/broker/Consumer.h | 37 ++ cpp/src/qpid/broker/Content.h | 64 ++++ cpp/src/qpid/broker/Deliverable.h | 37 ++ cpp/src/qpid/broker/DeliverableMessage.cpp | 33 ++ cpp/src/qpid/broker/DeliverableMessage.h | 41 ++ cpp/src/qpid/broker/DeliveryRecord.cpp | 87 +++++ cpp/src/qpid/broker/DeliveryRecord.h | 63 ++++ cpp/src/qpid/broker/DirectExchange.cpp | 71 ++++ cpp/src/qpid/broker/DirectExchange.h | 57 +++ cpp/src/qpid/broker/ExchangeRegistry.cpp | 76 ++++ cpp/src/qpid/broker/ExchangeRegistry.h | 47 +++ cpp/src/qpid/broker/FanOutExchange.cpp | 56 +++ cpp/src/qpid/broker/FanOutExchange.h | 60 +++ cpp/src/qpid/broker/HandlerImpl.h | 71 ++++ cpp/src/qpid/broker/HeadersExchange.cpp | 119 ++++++ cpp/src/qpid/broker/HeadersExchange.h | 65 ++++ cpp/src/qpid/broker/InMemoryContent.cpp | 72 ++++ cpp/src/qpid/broker/InMemoryContent.h | 45 +++ cpp/src/qpid/broker/LazyLoadedContent.cpp | 68 ++++ cpp/src/qpid/broker/LazyLoadedContent.h | 50 +++ cpp/src/qpid/broker/MessageBuilder.cpp | 74 ++++ cpp/src/qpid/broker/MessageBuilder.h | 57 +++ cpp/src/qpid/broker/MessageHandlerImpl.cpp | 243 ++++++++++++ cpp/src/qpid/broker/MessageHandlerImpl.h | 130 +++++++ cpp/src/qpid/broker/MessageStore.h | 129 +++++++ cpp/src/qpid/broker/MessageStoreModule.cpp | 114 ++++++ cpp/src/qpid/broker/MessageStoreModule.h | 68 ++++ cpp/src/qpid/broker/NameGenerator.cpp | 32 ++ cpp/src/qpid/broker/NameGenerator.h | 39 ++ cpp/src/qpid/broker/NullMessageStore.cpp | 110 ++++++ cpp/src/qpid/broker/NullMessageStore.h | 65 ++++ cpp/src/qpid/broker/Persistable.h | 62 ++++ cpp/src/qpid/broker/PersistableExchange.h | 44 +++ cpp/src/qpid/broker/PersistableMessage.h | 53 +++ cpp/src/qpid/broker/PersistableQueue.h | 45 +++ cpp/src/qpid/broker/Prefetch.h | 42 +++ cpp/src/qpid/broker/QueuePolicy.cpp | 69 ++++ cpp/src/qpid/broker/QueuePolicy.h | 54 +++ cpp/src/qpid/broker/QueueRegistry.cpp | 78 ++++ cpp/src/qpid/broker/QueueRegistry.h | 96 +++++ cpp/src/qpid/broker/RecoverableMessage.h | 58 +++ cpp/src/qpid/broker/RecoverableQueue.h | 51 +++ cpp/src/qpid/broker/RecoveryManager.h | 45 +++ cpp/src/qpid/broker/RecoveryManagerImpl.cpp | 143 +++++++ cpp/src/qpid/broker/RecoveryManagerImpl.h | 55 +++ cpp/src/qpid/broker/Reference.cpp | 53 +++ cpp/src/qpid/broker/Reference.h | 114 ++++++ cpp/src/qpid/broker/TopicExchange.cpp | 154 ++++++++ cpp/src/qpid/broker/TopicExchange.h | 100 +++++ cpp/src/qpid/broker/TransactionalStore.h | 60 +++ cpp/src/qpid/broker/TxAck.cpp | 54 +++ cpp/src/qpid/broker/TxAck.h | 57 +++ cpp/src/qpid/broker/TxBuffer.cpp | 56 +++ cpp/src/qpid/broker/TxBuffer.h | 107 ++++++ cpp/src/qpid/broker/TxOp.h | 42 +++ cpp/src/qpid/broker/TxPublish.cpp | 60 +++ cpp/src/qpid/broker/TxPublish.h | 78 ++++ cpp/src/qpid/client/AckMode.h | 102 +++++ cpp/src/qpid/client/BasicMessageChannel.cpp | 345 +++++++++++++++++ cpp/src/qpid/client/BasicMessageChannel.h | 90 +++++ cpp/src/qpid/client/ClientAdapter.cpp | 70 ++++ cpp/src/qpid/client/ClientAdapter.h | 66 ++++ cpp/src/qpid/client/ClientChannel.cpp | 341 +++++++++++++++++ cpp/src/qpid/client/ClientChannel.h | 356 ++++++++++++++++++ cpp/src/qpid/client/ClientConnection.cpp | 156 ++++++++ cpp/src/qpid/client/ClientExchange.cpp | 34 ++ cpp/src/qpid/client/ClientExchange.h | 106 ++++++ cpp/src/qpid/client/ClientMessage.h | 64 ++++ cpp/src/qpid/client/ClientQueue.cpp | 58 +++ cpp/src/qpid/client/ClientQueue.h | 103 ++++++ cpp/src/qpid/client/Connection.h | 179 +++++++++ cpp/src/qpid/client/Connector.cpp | 188 ++++++++++ cpp/src/qpid/client/Connector.h | 98 +++++ cpp/src/qpid/client/IncomingMessage.cpp | 168 +++++++++ cpp/src/qpid/client/IncomingMessage.h | 136 +++++++ cpp/src/qpid/client/MessageChannel.h | 94 +++++ cpp/src/qpid/client/MessageListener.cpp | 24 ++ cpp/src/qpid/client/MessageListener.h | 49 +++ cpp/src/qpid/client/MessageMessageChannel.cpp | 431 ++++++++++++++++++++++ cpp/src/qpid/client/MessageMessageChannel.h | 82 ++++ cpp/src/qpid/client/MethodBodyInstances.h | 100 +++++ cpp/src/qpid/client/ResponseHandler.cpp | 79 ++++ cpp/src/qpid/client/ResponseHandler.h | 75 ++++ cpp/src/qpid/client/ReturnedMessageHandler.cpp | 24 ++ cpp/src/qpid/client/ReturnedMessageHandler.h | 49 +++ cpp/src/qpid/doxygen_mainpage.h | 45 +++ cpp/src/qpid/framing/AMQBody.cpp | 33 ++ cpp/src/qpid/framing/AMQBody.h | 59 +++ cpp/src/qpid/framing/AMQContentBody.cpp | 43 +++ cpp/src/qpid/framing/AMQContentBody.h | 53 +++ cpp/src/qpid/framing/AMQDataBlock.h | 42 +++ cpp/src/qpid/framing/AMQFrame.cpp | 139 +++++++ cpp/src/qpid/framing/AMQFrame.h | 78 ++++ cpp/src/qpid/framing/AMQHeaderBody.cpp | 75 ++++ cpp/src/qpid/framing/AMQHeaderBody.h | 60 +++ cpp/src/qpid/framing/AMQHeartbeatBody.cpp | 29 ++ cpp/src/qpid/framing/AMQHeartbeatBody.h | 47 +++ cpp/src/qpid/framing/AMQMethodBody.cpp | 59 +++ cpp/src/qpid/framing/AMQMethodBody.h | 84 +++++ cpp/src/qpid/framing/AMQRequestBody.cpp | 66 ++++ cpp/src/qpid/framing/AMQRequestBody.h | 78 ++++ cpp/src/qpid/framing/AMQResponseBody.cpp | 65 ++++ cpp/src/qpid/framing/AMQResponseBody.h | 85 +++++ cpp/src/qpid/framing/BasicHeaderProperties.cpp | 103 ++++++ cpp/src/qpid/framing/BasicHeaderProperties.h | 115 ++++++ cpp/src/qpid/framing/BodyHandler.cpp | 60 +++ cpp/src/qpid/framing/BodyHandler.h | 61 +++ cpp/src/qpid/framing/Buffer.cpp | 183 +++++++++ cpp/src/qpid/framing/Buffer.h | 86 +++++ cpp/src/qpid/framing/ChannelAdapter.cpp | 109 ++++++ cpp/src/qpid/framing/ChannelAdapter.h | 113 ++++++ cpp/src/qpid/framing/Correlator.cpp | 42 +++ cpp/src/qpid/framing/Correlator.h | 68 ++++ cpp/src/qpid/framing/FieldTable.cpp | 150 ++++++++ cpp/src/qpid/framing/FieldTable.h | 90 +++++ cpp/src/qpid/framing/FramingContent.cpp | 75 ++++ cpp/src/qpid/framing/FramingContent.h | 41 ++ cpp/src/qpid/framing/HeaderProperties.h | 46 +++ cpp/src/qpid/framing/InitiationHandler.cpp | 24 ++ cpp/src/qpid/framing/InitiationHandler.h | 41 ++ cpp/src/qpid/framing/InputHandler.h | 39 ++ cpp/src/qpid/framing/MethodContext.cpp | 31 ++ cpp/src/qpid/framing/MethodContext.h | 75 ++++ cpp/src/qpid/framing/OutputHandler.h | 39 ++ cpp/src/qpid/framing/ProtocolInitiation.cpp | 63 ++++ cpp/src/qpid/framing/ProtocolInitiation.h | 54 +++ cpp/src/qpid/framing/ProtocolVersion.cpp | 44 +++ cpp/src/qpid/framing/ProtocolVersion.h | 57 +++ cpp/src/qpid/framing/ProtocolVersionException.cpp | 33 ++ cpp/src/qpid/framing/ProtocolVersionException.h | 56 +++ cpp/src/qpid/framing/Proxy.cpp | 32 ++ cpp/src/qpid/framing/Proxy.h | 51 +++ cpp/src/qpid/framing/Requester.cpp | 40 ++ cpp/src/qpid/framing/Requester.h | 68 ++++ cpp/src/qpid/framing/Responder.cpp | 43 +++ cpp/src/qpid/framing/Responder.h | 61 +++ cpp/src/qpid/framing/Value.cpp | 122 ++++++ cpp/src/qpid/framing/Value.h | 171 +++++++++ cpp/src/qpid/framing/amqp_framing.h | 36 ++ cpp/src/qpid/framing/amqp_types.h | 57 +++ cpp/src/qpid/framing/amqp_types_full.h | 36 ++ cpp/src/qpid/shared_ptr.h | 49 +++ cpp/src/qpid/sys/Acceptor.h | 47 +++ cpp/src/qpid/sys/AtomicCount.h | 53 +++ cpp/src/qpid/sys/Condition.h | 128 +++++++ cpp/src/qpid/sys/ConnectionInputHandler.h | 45 +++ cpp/src/qpid/sys/ConnectionInputHandlerFactory.h | 46 +++ cpp/src/qpid/sys/ConnectionOutputHandler.h | 41 ++ cpp/src/qpid/sys/Module.h | 161 ++++++++ cpp/src/qpid/sys/Monitor.h | 55 +++ cpp/src/qpid/sys/Mutex.h | 165 +++++++++ cpp/src/qpid/sys/ProducerConsumer.cpp | 141 +++++++ cpp/src/qpid/sys/ProducerConsumer.h | 165 +++++++++ cpp/src/qpid/sys/Runnable.cpp | 32 ++ cpp/src/qpid/sys/Runnable.h | 50 +++ cpp/src/qpid/sys/ScopedIncrement.h | 59 +++ cpp/src/qpid/sys/ShutdownHandler.h | 37 ++ cpp/src/qpid/sys/Socket.h | 88 +++++ cpp/src/qpid/sys/Thread.h | 142 +++++++ cpp/src/qpid/sys/ThreadSafeQueue.h | 98 +++++ cpp/src/qpid/sys/Time.cpp | 60 +++ cpp/src/qpid/sys/Time.h | 58 +++ cpp/src/qpid/sys/TimeoutHandler.h | 39 ++ cpp/src/qpid/sys/apr/APRAcceptor.cpp | 120 ++++++ cpp/src/qpid/sys/apr/APRBase.cpp | 90 +++++ cpp/src/qpid/sys/apr/APRBase.h | 78 ++++ cpp/src/qpid/sys/apr/APRPool.cpp | 41 ++ cpp/src/qpid/sys/apr/APRPool.h | 50 +++ cpp/src/qpid/sys/apr/APRSocket.cpp | 78 ++++ cpp/src/qpid/sys/apr/APRSocket.h | 48 +++ cpp/src/qpid/sys/apr/LFProcessor.cpp | 179 +++++++++ cpp/src/qpid/sys/apr/LFProcessor.h | 121 ++++++ cpp/src/qpid/sys/apr/LFSessionContext.cpp | 179 +++++++++ cpp/src/qpid/sys/apr/LFSessionContext.h | 90 +++++ cpp/src/qpid/sys/apr/Socket.cpp | 86 +++++ cpp/src/qpid/sys/apr/Thread.cpp | 33 ++ cpp/src/qpid/sys/posix/EventChannel.cpp | 325 ++++++++++++++++ cpp/src/qpid/sys/posix/EventChannel.h | 176 +++++++++ cpp/src/qpid/sys/posix/EventChannelAcceptor.cpp | 149 ++++++++ cpp/src/qpid/sys/posix/EventChannelConnection.cpp | 229 ++++++++++++ cpp/src/qpid/sys/posix/EventChannelConnection.h | 102 +++++ cpp/src/qpid/sys/posix/EventChannelThreads.cpp | 119 ++++++ cpp/src/qpid/sys/posix/EventChannelThreads.h | 92 +++++ cpp/src/qpid/sys/posix/PosixAcceptor.cpp | 48 +++ cpp/src/qpid/sys/posix/Socket.cpp | 118 ++++++ cpp/src/qpid/sys/posix/Thread.cpp | 28 ++ cpp/src/qpid/sys/posix/check.cpp | 39 ++ cpp/src/qpid/sys/posix/check.h | 62 ++++ cpp/src/shared_ptr.h | 49 --- cpp/src/sys/Acceptor.h | 47 --- cpp/src/sys/AtomicCount.h | 53 --- cpp/src/sys/Condition.h | 128 ------- cpp/src/sys/ConnectionInputHandler.h | 45 --- cpp/src/sys/ConnectionInputHandlerFactory.h | 46 --- cpp/src/sys/ConnectionOutputHandler.h | 41 -- cpp/src/sys/Module.h | 161 -------- cpp/src/sys/Monitor.h | 55 --- cpp/src/sys/Mutex.h | 165 --------- cpp/src/sys/ProducerConsumer.cpp | 141 ------- cpp/src/sys/ProducerConsumer.h | 165 --------- cpp/src/sys/Runnable.cpp | 32 -- cpp/src/sys/Runnable.h | 50 --- cpp/src/sys/ScopedIncrement.h | 59 --- cpp/src/sys/ShutdownHandler.h | 37 -- cpp/src/sys/Socket.h | 88 ----- cpp/src/sys/Thread.h | 142 ------- cpp/src/sys/ThreadSafeQueue.h | 98 ----- cpp/src/sys/Time.cpp | 60 --- cpp/src/sys/Time.h | 58 --- cpp/src/sys/TimeoutHandler.h | 39 -- cpp/src/sys/apr/APRAcceptor.cpp | 120 ------ cpp/src/sys/apr/APRBase.cpp | 90 ----- cpp/src/sys/apr/APRBase.h | 78 ---- cpp/src/sys/apr/APRPool.cpp | 41 -- cpp/src/sys/apr/APRPool.h | 50 --- cpp/src/sys/apr/APRSocket.cpp | 78 ---- cpp/src/sys/apr/APRSocket.h | 48 --- cpp/src/sys/apr/LFProcessor.cpp | 179 --------- cpp/src/sys/apr/LFProcessor.h | 121 ------ cpp/src/sys/apr/LFSessionContext.cpp | 179 --------- cpp/src/sys/apr/LFSessionContext.h | 90 ----- cpp/src/sys/apr/Socket.cpp | 86 ----- cpp/src/sys/apr/Thread.cpp | 33 -- cpp/src/sys/posix/EventChannel.cpp | 325 ---------------- cpp/src/sys/posix/EventChannel.h | 176 --------- cpp/src/sys/posix/EventChannelAcceptor.cpp | 149 -------- cpp/src/sys/posix/EventChannelConnection.cpp | 229 ------------ cpp/src/sys/posix/EventChannelConnection.h | 102 ----- cpp/src/sys/posix/EventChannelThreads.cpp | 119 ------ cpp/src/sys/posix/EventChannelThreads.h | 92 ----- cpp/src/sys/posix/PosixAcceptor.cpp | 48 --- cpp/src/sys/posix/Socket.cpp | 118 ------ cpp/src/sys/posix/Thread.cpp | 28 -- cpp/src/sys/posix/check.cpp | 39 -- cpp/src/sys/posix/check.h | 62 ---- cpp/src/tests/APRBaseTest.cpp | 2 +- cpp/src/tests/AccumulatedAckTest.cpp | 2 +- cpp/src/tests/BrokerChannelTest.cpp | 18 +- cpp/src/tests/ClientChannelTest.cpp | 14 +- cpp/src/tests/ConfigurationTest.cpp | 2 +- cpp/src/tests/EventChannelConnectionTest.cpp | 12 +- cpp/src/tests/EventChannelTest.cpp | 10 +- cpp/src/tests/EventChannelThreadsTest.cpp | 4 +- cpp/src/tests/ExchangeTest.cpp | 12 +- cpp/src/tests/FieldTableTest.cpp | 2 +- cpp/src/tests/FramingTest.cpp | 28 +- cpp/src/tests/HeaderTest.cpp | 2 +- cpp/src/tests/HeadersExchangeTest.cpp | 6 +- cpp/src/tests/InMemoryContentTest.cpp | 6 +- cpp/src/tests/InProcessBroker.h | 12 +- cpp/src/tests/LazyLoadedContentTest.cpp | 8 +- cpp/src/tests/MessageBuilderTest.cpp | 10 +- cpp/src/tests/MessageHandlerTest.cpp | 6 +- cpp/src/tests/MessageTest.cpp | 6 +- cpp/src/tests/MockChannel.h | 10 +- cpp/src/tests/MockConnectionInputHandler.h | 8 +- cpp/src/tests/ProducerConsumerTest.cpp | 8 +- cpp/src/tests/QueuePolicyTest.cpp | 2 +- cpp/src/tests/QueueRegistryTest.cpp | 2 +- cpp/src/tests/QueueTest.cpp | 4 +- cpp/src/tests/ReferenceTest.cpp | 10 +- cpp/src/tests/TopicExchangeTest.cpp | 2 +- cpp/src/tests/TxAckTest.cpp | 6 +- cpp/src/tests/TxBufferTest.cpp | 2 +- cpp/src/tests/TxPublishTest.cpp | 6 +- cpp/src/tests/ValueTest.cpp | 2 +- cpp/src/tests/client_test.cpp | 14 +- cpp/src/tests/echo_service.cpp | 14 +- cpp/src/tests/topic_listener.cpp | 14 +- cpp/src/tests/topic_publisher.cpp | 16 +- 483 files changed, 20566 insertions(+), 20562 deletions(-) delete mode 100644 cpp/src/Exception.cpp delete mode 100644 cpp/src/Exception.h delete mode 100644 cpp/src/ExceptionHolder.cpp delete mode 100644 cpp/src/ExceptionHolder.h delete mode 100644 cpp/src/QpidError.cpp delete mode 100644 cpp/src/QpidError.h delete mode 100644 cpp/src/SharedObject.h delete mode 100644 cpp/src/broker/AccumulatedAck.cpp delete mode 100644 cpp/src/broker/AccumulatedAck.h delete mode 100644 cpp/src/broker/AutoDelete.cpp delete mode 100644 cpp/src/broker/AutoDelete.h delete mode 100644 cpp/src/broker/Broker.cpp delete mode 100644 cpp/src/broker/Broker.h delete mode 100644 cpp/src/broker/BrokerAdapter.cpp delete mode 100644 cpp/src/broker/BrokerAdapter.h delete mode 100644 cpp/src/broker/BrokerChannel.cpp delete mode 100644 cpp/src/broker/BrokerChannel.h delete mode 100644 cpp/src/broker/BrokerExchange.h delete mode 100644 cpp/src/broker/BrokerMessage.cpp delete mode 100644 cpp/src/broker/BrokerMessage.h delete mode 100644 cpp/src/broker/BrokerMessageBase.h delete mode 100644 cpp/src/broker/BrokerMessageMessage.cpp delete mode 100644 cpp/src/broker/BrokerMessageMessage.h delete mode 100644 cpp/src/broker/BrokerQueue.cpp delete mode 100644 cpp/src/broker/BrokerQueue.h delete mode 100644 cpp/src/broker/BrokerSingleton.cpp delete mode 100644 cpp/src/broker/BrokerSingleton.h delete mode 100644 cpp/src/broker/CompletionHandler.h delete mode 100644 cpp/src/broker/Configuration.cpp delete mode 100644 cpp/src/broker/Configuration.h delete mode 100644 cpp/src/broker/Connection.cpp delete mode 100644 cpp/src/broker/Connection.h delete mode 100644 cpp/src/broker/ConnectionFactory.cpp delete mode 100644 cpp/src/broker/ConnectionFactory.h delete mode 100644 cpp/src/broker/ConnectionToken.h delete mode 100644 cpp/src/broker/Consumer.h delete mode 100644 cpp/src/broker/Content.h delete mode 100644 cpp/src/broker/Deliverable.h delete mode 100644 cpp/src/broker/DeliverableMessage.cpp delete mode 100644 cpp/src/broker/DeliverableMessage.h delete mode 100644 cpp/src/broker/DeliveryRecord.cpp delete mode 100644 cpp/src/broker/DeliveryRecord.h delete mode 100644 cpp/src/broker/DirectExchange.cpp delete mode 100644 cpp/src/broker/DirectExchange.h delete mode 100644 cpp/src/broker/ExchangeRegistry.cpp delete mode 100644 cpp/src/broker/ExchangeRegistry.h delete mode 100644 cpp/src/broker/FanOutExchange.cpp delete mode 100644 cpp/src/broker/FanOutExchange.h delete mode 100644 cpp/src/broker/HandlerImpl.h delete mode 100644 cpp/src/broker/HeadersExchange.cpp delete mode 100644 cpp/src/broker/HeadersExchange.h delete mode 100644 cpp/src/broker/InMemoryContent.cpp delete mode 100644 cpp/src/broker/InMemoryContent.h delete mode 100644 cpp/src/broker/LazyLoadedContent.cpp delete mode 100644 cpp/src/broker/LazyLoadedContent.h delete mode 100644 cpp/src/broker/MessageBuilder.cpp delete mode 100644 cpp/src/broker/MessageBuilder.h delete mode 100644 cpp/src/broker/MessageHandlerImpl.cpp delete mode 100644 cpp/src/broker/MessageHandlerImpl.h delete mode 100644 cpp/src/broker/MessageStore.h delete mode 100644 cpp/src/broker/MessageStoreModule.cpp delete mode 100644 cpp/src/broker/MessageStoreModule.h delete mode 100644 cpp/src/broker/NameGenerator.cpp delete mode 100644 cpp/src/broker/NameGenerator.h delete mode 100644 cpp/src/broker/NullMessageStore.cpp delete mode 100644 cpp/src/broker/NullMessageStore.h delete mode 100644 cpp/src/broker/Persistable.h delete mode 100644 cpp/src/broker/PersistableExchange.h delete mode 100644 cpp/src/broker/PersistableMessage.h delete mode 100644 cpp/src/broker/PersistableQueue.h delete mode 100644 cpp/src/broker/Prefetch.h delete mode 100644 cpp/src/broker/QueuePolicy.cpp delete mode 100644 cpp/src/broker/QueuePolicy.h delete mode 100644 cpp/src/broker/QueueRegistry.cpp delete mode 100644 cpp/src/broker/QueueRegistry.h delete mode 100644 cpp/src/broker/RecoverableMessage.h delete mode 100644 cpp/src/broker/RecoverableQueue.h delete mode 100644 cpp/src/broker/RecoveryManager.h delete mode 100644 cpp/src/broker/RecoveryManagerImpl.cpp delete mode 100644 cpp/src/broker/RecoveryManagerImpl.h delete mode 100644 cpp/src/broker/Reference.cpp delete mode 100644 cpp/src/broker/Reference.h delete mode 100644 cpp/src/broker/TopicExchange.cpp delete mode 100644 cpp/src/broker/TopicExchange.h delete mode 100644 cpp/src/broker/TransactionalStore.h delete mode 100644 cpp/src/broker/TxAck.cpp delete mode 100644 cpp/src/broker/TxAck.h delete mode 100644 cpp/src/broker/TxBuffer.cpp delete mode 100644 cpp/src/broker/TxBuffer.h delete mode 100644 cpp/src/broker/TxOp.h delete mode 100644 cpp/src/broker/TxPublish.cpp delete mode 100644 cpp/src/broker/TxPublish.h delete mode 100644 cpp/src/client/AckMode.h delete mode 100644 cpp/src/client/BasicMessageChannel.cpp delete mode 100644 cpp/src/client/BasicMessageChannel.h delete mode 100644 cpp/src/client/ClientAdapter.cpp delete mode 100644 cpp/src/client/ClientAdapter.h delete mode 100644 cpp/src/client/ClientChannel.cpp delete mode 100644 cpp/src/client/ClientChannel.h delete mode 100644 cpp/src/client/ClientConnection.cpp delete mode 100644 cpp/src/client/ClientExchange.cpp delete mode 100644 cpp/src/client/ClientExchange.h delete mode 100644 cpp/src/client/ClientMessage.h delete mode 100644 cpp/src/client/ClientQueue.cpp delete mode 100644 cpp/src/client/ClientQueue.h delete mode 100644 cpp/src/client/Connection.h delete mode 100644 cpp/src/client/Connector.cpp delete mode 100644 cpp/src/client/Connector.h delete mode 100644 cpp/src/client/IncomingMessage.cpp delete mode 100644 cpp/src/client/IncomingMessage.h delete mode 100644 cpp/src/client/MessageChannel.h delete mode 100644 cpp/src/client/MessageListener.cpp delete mode 100644 cpp/src/client/MessageListener.h delete mode 100644 cpp/src/client/MessageMessageChannel.cpp delete mode 100644 cpp/src/client/MessageMessageChannel.h delete mode 100644 cpp/src/client/MethodBodyInstances.h delete mode 100644 cpp/src/client/ResponseHandler.cpp delete mode 100644 cpp/src/client/ResponseHandler.h delete mode 100644 cpp/src/client/ReturnedMessageHandler.cpp delete mode 100644 cpp/src/client/ReturnedMessageHandler.h delete mode 100644 cpp/src/doxygen_mainpage.h delete mode 100644 cpp/src/framing/AMQBody.cpp delete mode 100644 cpp/src/framing/AMQBody.h delete mode 100644 cpp/src/framing/AMQContentBody.cpp delete mode 100644 cpp/src/framing/AMQContentBody.h delete mode 100644 cpp/src/framing/AMQDataBlock.h delete mode 100644 cpp/src/framing/AMQFrame.cpp delete mode 100644 cpp/src/framing/AMQFrame.h delete mode 100644 cpp/src/framing/AMQHeaderBody.cpp delete mode 100644 cpp/src/framing/AMQHeaderBody.h delete mode 100644 cpp/src/framing/AMQHeartbeatBody.cpp delete mode 100644 cpp/src/framing/AMQHeartbeatBody.h delete mode 100644 cpp/src/framing/AMQMethodBody.cpp delete mode 100644 cpp/src/framing/AMQMethodBody.h delete mode 100644 cpp/src/framing/AMQRequestBody.cpp delete mode 100644 cpp/src/framing/AMQRequestBody.h delete mode 100644 cpp/src/framing/AMQResponseBody.cpp delete mode 100644 cpp/src/framing/AMQResponseBody.h delete mode 100644 cpp/src/framing/BasicHeaderProperties.cpp delete mode 100644 cpp/src/framing/BasicHeaderProperties.h delete mode 100644 cpp/src/framing/BodyHandler.cpp delete mode 100644 cpp/src/framing/BodyHandler.h delete mode 100644 cpp/src/framing/Buffer.cpp delete mode 100644 cpp/src/framing/Buffer.h delete mode 100644 cpp/src/framing/ChannelAdapter.cpp delete mode 100644 cpp/src/framing/ChannelAdapter.h delete mode 100644 cpp/src/framing/Correlator.cpp delete mode 100644 cpp/src/framing/Correlator.h delete mode 100644 cpp/src/framing/FieldTable.cpp delete mode 100644 cpp/src/framing/FieldTable.h delete mode 100644 cpp/src/framing/FramingContent.cpp delete mode 100644 cpp/src/framing/FramingContent.h delete mode 100644 cpp/src/framing/HeaderProperties.h delete mode 100644 cpp/src/framing/InitiationHandler.cpp delete mode 100644 cpp/src/framing/InitiationHandler.h delete mode 100644 cpp/src/framing/InputHandler.h delete mode 100644 cpp/src/framing/MethodContext.cpp delete mode 100644 cpp/src/framing/MethodContext.h delete mode 100644 cpp/src/framing/OutputHandler.h delete mode 100644 cpp/src/framing/ProtocolInitiation.cpp delete mode 100644 cpp/src/framing/ProtocolInitiation.h delete mode 100644 cpp/src/framing/ProtocolVersion.cpp delete mode 100644 cpp/src/framing/ProtocolVersion.h delete mode 100644 cpp/src/framing/ProtocolVersionException.cpp delete mode 100644 cpp/src/framing/ProtocolVersionException.h delete mode 100644 cpp/src/framing/Proxy.cpp delete mode 100644 cpp/src/framing/Proxy.h delete mode 100644 cpp/src/framing/Requester.cpp delete mode 100644 cpp/src/framing/Requester.h delete mode 100644 cpp/src/framing/Responder.cpp delete mode 100644 cpp/src/framing/Responder.h delete mode 100644 cpp/src/framing/Value.cpp delete mode 100644 cpp/src/framing/Value.h delete mode 100644 cpp/src/framing/amqp_framing.h delete mode 100644 cpp/src/framing/amqp_types.h delete mode 100644 cpp/src/framing/amqp_types_full.h create mode 100644 cpp/src/qpid/Exception.cpp create mode 100644 cpp/src/qpid/Exception.h create mode 100644 cpp/src/qpid/ExceptionHolder.cpp create mode 100644 cpp/src/qpid/ExceptionHolder.h create mode 100644 cpp/src/qpid/QpidError.cpp create mode 100644 cpp/src/qpid/QpidError.h create mode 100644 cpp/src/qpid/SharedObject.h create mode 100644 cpp/src/qpid/broker/AccumulatedAck.cpp create mode 100644 cpp/src/qpid/broker/AccumulatedAck.h create mode 100644 cpp/src/qpid/broker/AutoDelete.cpp create mode 100644 cpp/src/qpid/broker/AutoDelete.h create mode 100644 cpp/src/qpid/broker/Broker.cpp create mode 100644 cpp/src/qpid/broker/Broker.h create mode 100644 cpp/src/qpid/broker/BrokerAdapter.cpp create mode 100644 cpp/src/qpid/broker/BrokerAdapter.h create mode 100644 cpp/src/qpid/broker/BrokerChannel.cpp create mode 100644 cpp/src/qpid/broker/BrokerChannel.h create mode 100644 cpp/src/qpid/broker/BrokerExchange.h create mode 100644 cpp/src/qpid/broker/BrokerMessage.cpp create mode 100644 cpp/src/qpid/broker/BrokerMessage.h create mode 100644 cpp/src/qpid/broker/BrokerMessageBase.h create mode 100644 cpp/src/qpid/broker/BrokerMessageMessage.cpp create mode 100644 cpp/src/qpid/broker/BrokerMessageMessage.h create mode 100644 cpp/src/qpid/broker/BrokerQueue.cpp create mode 100644 cpp/src/qpid/broker/BrokerQueue.h create mode 100644 cpp/src/qpid/broker/BrokerSingleton.cpp create mode 100644 cpp/src/qpid/broker/BrokerSingleton.h create mode 100644 cpp/src/qpid/broker/CompletionHandler.h create mode 100644 cpp/src/qpid/broker/Configuration.cpp create mode 100644 cpp/src/qpid/broker/Configuration.h create mode 100644 cpp/src/qpid/broker/Connection.cpp create mode 100644 cpp/src/qpid/broker/Connection.h create mode 100644 cpp/src/qpid/broker/ConnectionFactory.cpp create mode 100644 cpp/src/qpid/broker/ConnectionFactory.h create mode 100644 cpp/src/qpid/broker/ConnectionToken.h create mode 100644 cpp/src/qpid/broker/Consumer.h create mode 100644 cpp/src/qpid/broker/Content.h create mode 100644 cpp/src/qpid/broker/Deliverable.h create mode 100644 cpp/src/qpid/broker/DeliverableMessage.cpp create mode 100644 cpp/src/qpid/broker/DeliverableMessage.h create mode 100644 cpp/src/qpid/broker/DeliveryRecord.cpp create mode 100644 cpp/src/qpid/broker/DeliveryRecord.h create mode 100644 cpp/src/qpid/broker/DirectExchange.cpp create mode 100644 cpp/src/qpid/broker/DirectExchange.h create mode 100644 cpp/src/qpid/broker/ExchangeRegistry.cpp create mode 100644 cpp/src/qpid/broker/ExchangeRegistry.h create mode 100644 cpp/src/qpid/broker/FanOutExchange.cpp create mode 100644 cpp/src/qpid/broker/FanOutExchange.h create mode 100644 cpp/src/qpid/broker/HandlerImpl.h create mode 100644 cpp/src/qpid/broker/HeadersExchange.cpp create mode 100644 cpp/src/qpid/broker/HeadersExchange.h create mode 100644 cpp/src/qpid/broker/InMemoryContent.cpp create mode 100644 cpp/src/qpid/broker/InMemoryContent.h create mode 100644 cpp/src/qpid/broker/LazyLoadedContent.cpp create mode 100644 cpp/src/qpid/broker/LazyLoadedContent.h create mode 100644 cpp/src/qpid/broker/MessageBuilder.cpp create mode 100644 cpp/src/qpid/broker/MessageBuilder.h create mode 100644 cpp/src/qpid/broker/MessageHandlerImpl.cpp create mode 100644 cpp/src/qpid/broker/MessageHandlerImpl.h create mode 100644 cpp/src/qpid/broker/MessageStore.h create mode 100644 cpp/src/qpid/broker/MessageStoreModule.cpp create mode 100644 cpp/src/qpid/broker/MessageStoreModule.h create mode 100644 cpp/src/qpid/broker/NameGenerator.cpp create mode 100644 cpp/src/qpid/broker/NameGenerator.h create mode 100644 cpp/src/qpid/broker/NullMessageStore.cpp create mode 100644 cpp/src/qpid/broker/NullMessageStore.h create mode 100644 cpp/src/qpid/broker/Persistable.h create mode 100644 cpp/src/qpid/broker/PersistableExchange.h create mode 100644 cpp/src/qpid/broker/PersistableMessage.h create mode 100644 cpp/src/qpid/broker/PersistableQueue.h create mode 100644 cpp/src/qpid/broker/Prefetch.h create mode 100644 cpp/src/qpid/broker/QueuePolicy.cpp create mode 100644 cpp/src/qpid/broker/QueuePolicy.h create mode 100644 cpp/src/qpid/broker/QueueRegistry.cpp create mode 100644 cpp/src/qpid/broker/QueueRegistry.h create mode 100644 cpp/src/qpid/broker/RecoverableMessage.h create mode 100644 cpp/src/qpid/broker/RecoverableQueue.h create mode 100644 cpp/src/qpid/broker/RecoveryManager.h create mode 100644 cpp/src/qpid/broker/RecoveryManagerImpl.cpp create mode 100644 cpp/src/qpid/broker/RecoveryManagerImpl.h create mode 100644 cpp/src/qpid/broker/Reference.cpp create mode 100644 cpp/src/qpid/broker/Reference.h create mode 100644 cpp/src/qpid/broker/TopicExchange.cpp create mode 100644 cpp/src/qpid/broker/TopicExchange.h create mode 100644 cpp/src/qpid/broker/TransactionalStore.h create mode 100644 cpp/src/qpid/broker/TxAck.cpp create mode 100644 cpp/src/qpid/broker/TxAck.h create mode 100644 cpp/src/qpid/broker/TxBuffer.cpp create mode 100644 cpp/src/qpid/broker/TxBuffer.h create mode 100644 cpp/src/qpid/broker/TxOp.h create mode 100644 cpp/src/qpid/broker/TxPublish.cpp create mode 100644 cpp/src/qpid/broker/TxPublish.h create mode 100644 cpp/src/qpid/client/AckMode.h create mode 100644 cpp/src/qpid/client/BasicMessageChannel.cpp create mode 100644 cpp/src/qpid/client/BasicMessageChannel.h create mode 100644 cpp/src/qpid/client/ClientAdapter.cpp create mode 100644 cpp/src/qpid/client/ClientAdapter.h create mode 100644 cpp/src/qpid/client/ClientChannel.cpp create mode 100644 cpp/src/qpid/client/ClientChannel.h create mode 100644 cpp/src/qpid/client/ClientConnection.cpp create mode 100644 cpp/src/qpid/client/ClientExchange.cpp create mode 100644 cpp/src/qpid/client/ClientExchange.h create mode 100644 cpp/src/qpid/client/ClientMessage.h create mode 100644 cpp/src/qpid/client/ClientQueue.cpp create mode 100644 cpp/src/qpid/client/ClientQueue.h create mode 100644 cpp/src/qpid/client/Connection.h create mode 100644 cpp/src/qpid/client/Connector.cpp create mode 100644 cpp/src/qpid/client/Connector.h create mode 100644 cpp/src/qpid/client/IncomingMessage.cpp create mode 100644 cpp/src/qpid/client/IncomingMessage.h create mode 100644 cpp/src/qpid/client/MessageChannel.h create mode 100644 cpp/src/qpid/client/MessageListener.cpp create mode 100644 cpp/src/qpid/client/MessageListener.h create mode 100644 cpp/src/qpid/client/MessageMessageChannel.cpp create mode 100644 cpp/src/qpid/client/MessageMessageChannel.h create mode 100644 cpp/src/qpid/client/MethodBodyInstances.h create mode 100644 cpp/src/qpid/client/ResponseHandler.cpp create mode 100644 cpp/src/qpid/client/ResponseHandler.h create mode 100644 cpp/src/qpid/client/ReturnedMessageHandler.cpp create mode 100644 cpp/src/qpid/client/ReturnedMessageHandler.h create mode 100644 cpp/src/qpid/doxygen_mainpage.h create mode 100644 cpp/src/qpid/framing/AMQBody.cpp create mode 100644 cpp/src/qpid/framing/AMQBody.h create mode 100644 cpp/src/qpid/framing/AMQContentBody.cpp create mode 100644 cpp/src/qpid/framing/AMQContentBody.h create mode 100644 cpp/src/qpid/framing/AMQDataBlock.h create mode 100644 cpp/src/qpid/framing/AMQFrame.cpp create mode 100644 cpp/src/qpid/framing/AMQFrame.h create mode 100644 cpp/src/qpid/framing/AMQHeaderBody.cpp create mode 100644 cpp/src/qpid/framing/AMQHeaderBody.h create mode 100644 cpp/src/qpid/framing/AMQHeartbeatBody.cpp create mode 100644 cpp/src/qpid/framing/AMQHeartbeatBody.h create mode 100644 cpp/src/qpid/framing/AMQMethodBody.cpp create mode 100644 cpp/src/qpid/framing/AMQMethodBody.h create mode 100644 cpp/src/qpid/framing/AMQRequestBody.cpp create mode 100644 cpp/src/qpid/framing/AMQRequestBody.h create mode 100644 cpp/src/qpid/framing/AMQResponseBody.cpp create mode 100644 cpp/src/qpid/framing/AMQResponseBody.h create mode 100644 cpp/src/qpid/framing/BasicHeaderProperties.cpp create mode 100644 cpp/src/qpid/framing/BasicHeaderProperties.h create mode 100644 cpp/src/qpid/framing/BodyHandler.cpp create mode 100644 cpp/src/qpid/framing/BodyHandler.h create mode 100644 cpp/src/qpid/framing/Buffer.cpp create mode 100644 cpp/src/qpid/framing/Buffer.h create mode 100644 cpp/src/qpid/framing/ChannelAdapter.cpp create mode 100644 cpp/src/qpid/framing/ChannelAdapter.h create mode 100644 cpp/src/qpid/framing/Correlator.cpp create mode 100644 cpp/src/qpid/framing/Correlator.h create mode 100644 cpp/src/qpid/framing/FieldTable.cpp create mode 100644 cpp/src/qpid/framing/FieldTable.h create mode 100644 cpp/src/qpid/framing/FramingContent.cpp create mode 100644 cpp/src/qpid/framing/FramingContent.h create mode 100644 cpp/src/qpid/framing/HeaderProperties.h create mode 100644 cpp/src/qpid/framing/InitiationHandler.cpp create mode 100644 cpp/src/qpid/framing/InitiationHandler.h create mode 100644 cpp/src/qpid/framing/InputHandler.h create mode 100644 cpp/src/qpid/framing/MethodContext.cpp create mode 100644 cpp/src/qpid/framing/MethodContext.h create mode 100644 cpp/src/qpid/framing/OutputHandler.h create mode 100644 cpp/src/qpid/framing/ProtocolInitiation.cpp create mode 100644 cpp/src/qpid/framing/ProtocolInitiation.h create mode 100644 cpp/src/qpid/framing/ProtocolVersion.cpp create mode 100644 cpp/src/qpid/framing/ProtocolVersion.h create mode 100644 cpp/src/qpid/framing/ProtocolVersionException.cpp create mode 100644 cpp/src/qpid/framing/ProtocolVersionException.h create mode 100644 cpp/src/qpid/framing/Proxy.cpp create mode 100644 cpp/src/qpid/framing/Proxy.h create mode 100644 cpp/src/qpid/framing/Requester.cpp create mode 100644 cpp/src/qpid/framing/Requester.h create mode 100644 cpp/src/qpid/framing/Responder.cpp create mode 100644 cpp/src/qpid/framing/Responder.h create mode 100644 cpp/src/qpid/framing/Value.cpp create mode 100644 cpp/src/qpid/framing/Value.h create mode 100644 cpp/src/qpid/framing/amqp_framing.h create mode 100644 cpp/src/qpid/framing/amqp_types.h create mode 100644 cpp/src/qpid/framing/amqp_types_full.h create mode 100644 cpp/src/qpid/shared_ptr.h create mode 100644 cpp/src/qpid/sys/Acceptor.h create mode 100644 cpp/src/qpid/sys/AtomicCount.h create mode 100644 cpp/src/qpid/sys/Condition.h create mode 100644 cpp/src/qpid/sys/ConnectionInputHandler.h create mode 100644 cpp/src/qpid/sys/ConnectionInputHandlerFactory.h create mode 100644 cpp/src/qpid/sys/ConnectionOutputHandler.h create mode 100644 cpp/src/qpid/sys/Module.h create mode 100644 cpp/src/qpid/sys/Monitor.h create mode 100644 cpp/src/qpid/sys/Mutex.h create mode 100644 cpp/src/qpid/sys/ProducerConsumer.cpp create mode 100644 cpp/src/qpid/sys/ProducerConsumer.h create mode 100644 cpp/src/qpid/sys/Runnable.cpp create mode 100644 cpp/src/qpid/sys/Runnable.h create mode 100644 cpp/src/qpid/sys/ScopedIncrement.h create mode 100644 cpp/src/qpid/sys/ShutdownHandler.h create mode 100644 cpp/src/qpid/sys/Socket.h create mode 100644 cpp/src/qpid/sys/Thread.h create mode 100644 cpp/src/qpid/sys/ThreadSafeQueue.h create mode 100644 cpp/src/qpid/sys/Time.cpp create mode 100644 cpp/src/qpid/sys/Time.h create mode 100644 cpp/src/qpid/sys/TimeoutHandler.h create mode 100644 cpp/src/qpid/sys/apr/APRAcceptor.cpp create mode 100644 cpp/src/qpid/sys/apr/APRBase.cpp create mode 100644 cpp/src/qpid/sys/apr/APRBase.h create mode 100644 cpp/src/qpid/sys/apr/APRPool.cpp create mode 100644 cpp/src/qpid/sys/apr/APRPool.h create mode 100644 cpp/src/qpid/sys/apr/APRSocket.cpp create mode 100644 cpp/src/qpid/sys/apr/APRSocket.h create mode 100644 cpp/src/qpid/sys/apr/LFProcessor.cpp create mode 100644 cpp/src/qpid/sys/apr/LFProcessor.h create mode 100644 cpp/src/qpid/sys/apr/LFSessionContext.cpp create mode 100644 cpp/src/qpid/sys/apr/LFSessionContext.h create mode 100644 cpp/src/qpid/sys/apr/Socket.cpp create mode 100644 cpp/src/qpid/sys/apr/Thread.cpp create mode 100644 cpp/src/qpid/sys/posix/EventChannel.cpp create mode 100644 cpp/src/qpid/sys/posix/EventChannel.h create mode 100644 cpp/src/qpid/sys/posix/EventChannelAcceptor.cpp create mode 100644 cpp/src/qpid/sys/posix/EventChannelConnection.cpp create mode 100644 cpp/src/qpid/sys/posix/EventChannelConnection.h create mode 100644 cpp/src/qpid/sys/posix/EventChannelThreads.cpp create mode 100644 cpp/src/qpid/sys/posix/EventChannelThreads.h create mode 100644 cpp/src/qpid/sys/posix/PosixAcceptor.cpp create mode 100644 cpp/src/qpid/sys/posix/Socket.cpp create mode 100644 cpp/src/qpid/sys/posix/Thread.cpp create mode 100644 cpp/src/qpid/sys/posix/check.cpp create mode 100644 cpp/src/qpid/sys/posix/check.h delete mode 100644 cpp/src/shared_ptr.h delete mode 100644 cpp/src/sys/Acceptor.h delete mode 100644 cpp/src/sys/AtomicCount.h delete mode 100644 cpp/src/sys/Condition.h delete mode 100644 cpp/src/sys/ConnectionInputHandler.h delete mode 100644 cpp/src/sys/ConnectionInputHandlerFactory.h delete mode 100644 cpp/src/sys/ConnectionOutputHandler.h delete mode 100644 cpp/src/sys/Module.h delete mode 100644 cpp/src/sys/Monitor.h delete mode 100644 cpp/src/sys/Mutex.h delete mode 100644 cpp/src/sys/ProducerConsumer.cpp delete mode 100644 cpp/src/sys/ProducerConsumer.h delete mode 100644 cpp/src/sys/Runnable.cpp delete mode 100644 cpp/src/sys/Runnable.h delete mode 100644 cpp/src/sys/ScopedIncrement.h delete mode 100644 cpp/src/sys/ShutdownHandler.h delete mode 100644 cpp/src/sys/Socket.h delete mode 100644 cpp/src/sys/Thread.h delete mode 100644 cpp/src/sys/ThreadSafeQueue.h delete mode 100644 cpp/src/sys/Time.cpp delete mode 100644 cpp/src/sys/Time.h delete mode 100644 cpp/src/sys/TimeoutHandler.h delete mode 100644 cpp/src/sys/apr/APRAcceptor.cpp delete mode 100644 cpp/src/sys/apr/APRBase.cpp delete mode 100644 cpp/src/sys/apr/APRBase.h delete mode 100644 cpp/src/sys/apr/APRPool.cpp delete mode 100644 cpp/src/sys/apr/APRPool.h delete mode 100644 cpp/src/sys/apr/APRSocket.cpp delete mode 100644 cpp/src/sys/apr/APRSocket.h delete mode 100644 cpp/src/sys/apr/LFProcessor.cpp delete mode 100644 cpp/src/sys/apr/LFProcessor.h delete mode 100644 cpp/src/sys/apr/LFSessionContext.cpp delete mode 100644 cpp/src/sys/apr/LFSessionContext.h delete mode 100644 cpp/src/sys/apr/Socket.cpp delete mode 100644 cpp/src/sys/apr/Thread.cpp delete mode 100644 cpp/src/sys/posix/EventChannel.cpp delete mode 100644 cpp/src/sys/posix/EventChannel.h delete mode 100644 cpp/src/sys/posix/EventChannelAcceptor.cpp delete mode 100644 cpp/src/sys/posix/EventChannelConnection.cpp delete mode 100644 cpp/src/sys/posix/EventChannelConnection.h delete mode 100644 cpp/src/sys/posix/EventChannelThreads.cpp delete mode 100644 cpp/src/sys/posix/EventChannelThreads.h delete mode 100644 cpp/src/sys/posix/PosixAcceptor.cpp delete mode 100644 cpp/src/sys/posix/Socket.cpp delete mode 100644 cpp/src/sys/posix/Thread.cpp delete mode 100644 cpp/src/sys/posix/check.cpp delete mode 100644 cpp/src/sys/posix/check.h (limited to 'cpp/src') diff --git a/cpp/src/Exception.cpp b/cpp/src/Exception.cpp deleted file mode 100644 index 1bbf89e99f..0000000000 --- a/cpp/src/Exception.cpp +++ /dev/null @@ -1,46 +0,0 @@ -/* - * - * 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 "Exception.h" - -namespace qpid { - -Exception::Exception() throw() {} - -Exception::Exception(const std::string& str) throw() : whatStr(str) {} - -Exception::Exception(const char* str) throw() : whatStr(str) {} - -Exception::~Exception() throw() {} - -const char* Exception::what() const throw() { return whatStr.c_str(); } - -std::string Exception::toString() const throw() { return whatStr; } - -Exception* Exception::clone() const throw() { return new Exception(*this); } - -void Exception::throwSelf() const { throw *this; } - -ShutdownException::ShutdownException() : Exception("Shut down.") {} - -EmptyException::EmptyException() : Exception("Empty.") {} - -} // namespace qpid diff --git a/cpp/src/Exception.h b/cpp/src/Exception.h deleted file mode 100644 index 4fbad7efaa..0000000000 --- a/cpp/src/Exception.h +++ /dev/null @@ -1,97 +0,0 @@ -#ifndef _Exception_ -#define _Exception_ - -/* - * - * 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 -#include -#include -#include -#include - -#include "framing/amqp_types.h" - -namespace qpid -{ -/** - * Exception base class for all Qpid exceptions. - */ -class Exception : public std::exception -{ - protected: - std::string whatStr; - - public: - Exception() throw(); - Exception(const std::string& str) throw(); - Exception(const char* str) throw(); - Exception(const std::exception&) throw(); - - /** Allow any type that has ostream operator<< to act as message */ - template - Exception(const T& message) - : whatStr(boost::lexical_cast(message)) {} - - virtual ~Exception() throw(); - - virtual const char* what() const throw(); - virtual std::string toString() const throw(); - - virtual Exception* clone() const throw(); - virtual void throwSelf() const; - - typedef boost::shared_ptr shared_ptr; -}; - -struct ChannelException : public Exception { - framing::ReplyCode code; - template - ChannelException(framing::ReplyCode code_, const T& message) - : Exception(message), code(code_) {} - void throwSelf() const { throw *this; } -}; - -struct ConnectionException : public Exception { - framing::ReplyCode code; - template - ConnectionException(framing::ReplyCode code_, const T& message) - : Exception(message), code(code_) {} - void throwSelf() const { throw *this; } -}; - -/** - * Exception used to indicate that a thread should shut down. - * Does not indicate an error that should be signalled to the user. - */ -struct ShutdownException : public Exception { - ShutdownException(); - void throwSelf() const { throw *this; } -}; - -/** Exception to indicate empty queue or other empty state */ -struct EmptyException : public Exception { - EmptyException(); - void throwSelf() const { throw *this; } -}; - -} - -#endif /*!_Exception_*/ diff --git a/cpp/src/ExceptionHolder.cpp b/cpp/src/ExceptionHolder.cpp deleted file mode 100644 index de8d7b2487..0000000000 --- a/cpp/src/ExceptionHolder.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * - * 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 "ExceptionHolder.h" - -namespace qpid { - -ExceptionHolder::ExceptionHolder(const std::exception& e) { - const Exception* ex = dynamic_cast(&e); - if (ex) { - reset(ex->clone()); - } else { - reset(new Exception(e.what())); - } -} - -} diff --git a/cpp/src/ExceptionHolder.h b/cpp/src/ExceptionHolder.h deleted file mode 100644 index 723dac4b2d..0000000000 --- a/cpp/src/ExceptionHolder.h +++ /dev/null @@ -1,67 +0,0 @@ -#ifndef _qpid_ExceptionHolder_h -#define _qpid_ExceptionHolder_h - -/* - * - * 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 -#include "Exception.h" -#include - -namespace qpid { - -// FIXME aconway 2007-02-20: Not necessary, a simple -// Exception::shared_ptr will do the job. Remove -// -/** - * Holder for a heap-allocated exc eption that can be stack allocated - * and thrown safely. - * - * Basically this is a shared_ptr with the Exception functions added - * so the catcher need not be aware that it is a pointer rather than a - * reference. - * - * shared_ptr is chosen over auto_ptr because it has normal - * copy semantics. - */ -class ExceptionHolder : public Exception, public boost::shared_ptr -{ - public: - typedef boost::shared_ptr shared_ptr; - - ExceptionHolder() throw() {} - ExceptionHolder(Exception* p) throw() : shared_ptr(p) {} - ExceptionHolder(shared_ptr p) throw() : shared_ptr(p) {} - - ExceptionHolder(const Exception& e) throw() : shared_ptr(e.clone()) {} - ExceptionHolder(const std::exception& e); - - ~ExceptionHolder() throw() {} - - const char* what() const throw() { return get()->what(); } - std::string toString() const throw() { return get()->toString(); } - Exception* clone() const throw() { return get()->clone(); } - void throwIf() const { if (get()) get()->throwSelf(); } - void throwSelf() const { assert(get()); get()->throwSelf(); } -}; - -} // namespace qpid - - - -#endif /*!_qpid_ExceptionHolder_h*/ diff --git a/cpp/src/Makefile.am b/cpp/src/Makefile.am index 6937db2e1b..0d81a01dcd 100644 --- a/cpp/src/Makefile.am +++ b/cpp/src/Makefile.am @@ -6,8 +6,16 @@ SUBDIRS = . tests AM_CXXFLAGS = $(WARNING_CFLAGS) $(APR_CXXFLAGS) +gen = gen/qpid/framing +framing= qpid/framing +broker = qpid/broker +client = qpid/client +apr = qpid/sys/apr +posix = qpid/sys/posix + + # -I top_builddir for config.h -INCLUDES = -I$(top_builddir) -I$(srcdir) +INCLUDES = -I$(top_builddir) -I$(srcdir)/qpid -Igen qpidd_LDADD = \ libqpidbroker.la \ @@ -16,7 +24,6 @@ qpidd_LDADD = \ sbin_PROGRAMS = qpidd qpidd_SOURCES = qpidd.cpp -apr = sys/apr apr_src = \ $(apr)/APRAcceptor.cpp \ $(apr)/APRBase.cpp \ @@ -34,7 +41,6 @@ apr_hdr = \ $(apr)/LFProcessor.h \ $(apr)/LFSessionContext.h -posix = sys/posix posix_src = \ $(posix)/PosixAcceptor.cpp \ $(posix)/Socket.cpp \ @@ -52,11 +58,6 @@ EXTRA_DIST=$(posix_src) $(posix_hdr) platform_src = $(apr_src) platform_hdr = $(apr_hdr) -framing = framing -gen = gen -broker = broker -client = client - lib_LTLIBRARIES = libqpidcommon.la libqpidbroker.la libqpidclient.la libqpidcommon_la_LIBADD = \ @@ -98,13 +99,14 @@ libqpidcommon_la_SOURCES = \ $(gen)/AMQP_HighestVersion.h \ $(gen)/AMQP_MethodVersionMap.cpp \ $(gen)/AMQP_ServerProxy.cpp \ - Exception.cpp \ - ExceptionHolder.cpp \ - QpidError.cpp \ - sys/Runnable.cpp \ - sys/Time.cpp \ - sys/ProducerConsumer.cpp + qpid/Exception.cpp \ + qpid/ExceptionHolder.cpp \ + qpid/QpidError.cpp \ + qpid/sys/Runnable.cpp \ + qpid/sys/Time.cpp \ + qpid/sys/ProducerConsumer.cpp +libqpidbroker_la_LIBADD = libqpidcommon.la libqpidbroker_la_LDFLAGS = -version-info $(LIBTOOL_VERSION_INFO_ARG) libqpidbroker_la_SOURCES = \ $(broker)/AccumulatedAck.cpp \ @@ -260,29 +262,29 @@ nobase_qpidinclude_HEADERS = \ $(framing)/amqp_framing.h \ $(framing)/amqp_types.h \ $(framing)/amqp_types_full.h \ - sys/Acceptor.h \ - sys/AtomicCount.h \ - sys/Condition.h \ - sys/ConnectionInputHandler.h \ - sys/ConnectionInputHandlerFactory.h \ - sys/ConnectionOutputHandler.h \ - sys/Module.h \ - sys/Monitor.h \ - sys/Mutex.h \ - sys/ProducerConsumer.h \ - sys/Runnable.h \ - sys/ScopedIncrement.h \ - sys/ShutdownHandler.h \ - sys/Socket.h \ - sys/Thread.h \ - sys/ThreadSafeQueue.h \ - sys/Time.h \ - sys/TimeoutHandler.h \ - Exception.h \ - ExceptionHolder.h \ - QpidError.h \ - SharedObject.h \ - shared_ptr.h + qpid/sys/Acceptor.h \ + qpid/sys/AtomicCount.h \ + qpid/sys/Condition.h \ + qpid/sys/ConnectionInputHandler.h \ + qpid/sys/ConnectionInputHandlerFactory.h \ + qpid/sys/ConnectionOutputHandler.h \ + qpid/sys/Module.h \ + qpid/sys/Monitor.h \ + qpid/sys/Mutex.h \ + qpid/sys/ProducerConsumer.h \ + qpid/sys/Runnable.h \ + qpid/sys/ScopedIncrement.h \ + qpid/sys/ShutdownHandler.h \ + qpid/sys/Socket.h \ + qpid/sys/Thread.h \ + qpid/sys/ThreadSafeQueue.h \ + qpid/sys/Time.h \ + qpid/sys/TimeoutHandler.h \ + qpid/Exception.h \ + qpid/ExceptionHolder.h \ + qpid/QpidError.h \ + qpid/SharedObject.h \ + qpid/shared_ptr.h # Distribute the generated sources, at least for now, since # the generator code is in java. @@ -299,22 +301,23 @@ spec_dir = $(srcdir)/../../specs spec = $(spec_dir)/amqp.0-9.xml $(spec_dir)/amqp-errata.0-9.xml gentools_srcdir = $(gentools_dir)/src/org/apache/qpid/gentools -$(BUILT_SOURCES) $(gen)/timestamp: $(spec) $(java_sources) $(cxx_templates) Makefile.am - rm -f $(BUILT_SOURCES) +$(BUILT_SOURCES) $(gen): $(spec) $(java_sources) $(cxx_templates) Makefile.am + rm -rf $(gen) + mkdir -p $(gen) cd $(gentools_srcdir) && rm -f *.class && $(JAVAC) *.java $(JAVA) -cp $(gentools_dir)/src org.apache.qpid.gentools.Main \ -c -o $(gen) -t $(gentools_dir)/templ.cpp $(spec) - touch $(gen)/timestamp + touch $(gen) -$(gen)/gen-src.mk: $(gen)/timestamp - mkdir -p $(gen) - ./make-gen-src-mk.sh $(gentools_dir) $(gentools_srcdir) > $@-t +gen/gen-src.mk: $(gen) + ./make-gen-src-mk.sh $(gentools_dir) $(gentools_srcdir) $(gen) > $@-t mv $@-t $@ + endif maintainer-clean: rm -f $(gentools_srcdir)/*.class - rm -f $(BUILT_SOURCES) + rm -f $(gen) # Force build during dist phase so help2man will work. dist-hook: $(lib_LTLIBRARIES) $(sbin_PROGRAMS) diff --git a/cpp/src/QpidError.cpp b/cpp/src/QpidError.cpp deleted file mode 100644 index 365c84ea1d..0000000000 --- a/cpp/src/QpidError.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * - * 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 - -#include "QpidError.h" -#include - -using namespace qpid; - -QpidError::QpidError() : code(0) {} - -QpidError::~QpidError() throw() {} - -Exception* QpidError::clone() const throw() { return new QpidError(*this); } - -void QpidError::throwSelf() const { throw *this; } - -void QpidError::init() { - whatStr = boost::str(boost::format("Error [%d] %s (%s:%d)") - % code % msg % loc.file % loc.line); -} - - diff --git a/cpp/src/QpidError.h b/cpp/src/QpidError.h deleted file mode 100644 index 12e80c9bd9..0000000000 --- a/cpp/src/QpidError.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef __QpidError__ -#define __QpidError__ -/* - * - * 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 -#include -#include - -#include "Exception.h" - -namespace qpid { - -struct SrcLine { - public: - SrcLine(const std::string& file_="", int line_=0) : - file(file_), line(line_) {} - - std::string file; - int line; -}; - -class QpidError : public Exception -{ - public: - const int code; - SrcLine loc; - std::string msg; - - QpidError(); - - template - QpidError(int code_, const T& msg_, const SrcLine& loc_) throw() - : code(code_), loc(loc_), msg(boost::lexical_cast(msg_)) - { init(); } - - ~QpidError() throw(); - Exception* clone() const throw(); - void throwSelf() const; - - private: - - void init(); -}; - - -} // namespace qpid - -#define SRCLINE ::qpid::SrcLine(__FILE__, __LINE__) - -#define QPID_ERROR(CODE, MESSAGE) ::qpid::QpidError((CODE), (MESSAGE), SRCLINE) - -#define THROW_QPID_ERROR(CODE, MESSAGE) throw QPID_ERROR(CODE,MESSAGE) - -const int PROTOCOL_ERROR = 10000; -const int APR_ERROR = 20000; -const int FRAMING_ERROR = 30000; -const int CLIENT_ERROR = 40000; -const int INTERNAL_ERROR = 50000; - -#endif diff --git a/cpp/src/SharedObject.h b/cpp/src/SharedObject.h deleted file mode 100644 index 852a036ab9..0000000000 --- a/cpp/src/SharedObject.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef _SharedObject_ -#define _SharedObject_ - -/* - * - * 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 -#include - -namespace qpid { - /** - * Template to enforce shared object conventions. - * Shared object classes should inherit : public qpid::SharedObject - * That ensures Foo: - * - has typedef boost::shared_ptr shared_ptr - * - has virtual destructor - * - is boost::noncopyable (no default copy or assign) - * - has a protected default constructor. - * - * Shared objects should not have public constructors. - * Make constructors protected and provide public statc create() - * functions that return a shared_ptr. - */ - template - class SharedObject : private boost::noncopyable - { - public: - typedef boost::shared_ptr shared_ptr; - - virtual ~SharedObject() {}; - - protected: - SharedObject() {} - }; -} - -#endif /*!_SharedObject_*/ diff --git a/cpp/src/broker/AccumulatedAck.cpp b/cpp/src/broker/AccumulatedAck.cpp deleted file mode 100644 index ff471b0287..0000000000 --- a/cpp/src/broker/AccumulatedAck.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/* - * - * 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 "AccumulatedAck.h" - -#include - -using std::less_equal; -using std::bind2nd; -using namespace qpid::broker; - -void AccumulatedAck::update(uint64_t firstTag, uint64_t lastTag){ - assert(firstTag<=lastTag); - if (firstTag <= range + 1) { - if (lastTag > range) range = lastTag; - } else { - for (uint64_t tag = firstTag; tag<=lastTag; tag++) - individual.push_back(tag); - } -} - -void AccumulatedAck::consolidate(){ - individual.sort(); - //remove any individual tags that are covered by range - individual.remove_if(bind2nd(less_equal(), range)); - //update range if possible (using <= allows for duplicates from overlapping ranges) - while (individual.front() <= range + 1) { - range = individual.front(); - individual.pop_front(); - } -} - -void AccumulatedAck::clear(){ - range = 0; - individual.clear(); -} - -bool AccumulatedAck::covers(uint64_t tag) const{ - return tag <= range || find(individual.begin(), individual.end(), tag) != individual.end(); -} diff --git a/cpp/src/broker/AccumulatedAck.h b/cpp/src/broker/AccumulatedAck.h deleted file mode 100644 index c4a6e3b79b..0000000000 --- a/cpp/src/broker/AccumulatedAck.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * - * 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 _AccumulatedAck_ -#define _AccumulatedAck_ - -#include -#include -#include - -namespace qpid { - namespace broker { - /** - * Keeps an accumulated record of acked messages (by delivery - * tag). - */ - class AccumulatedAck { - public: - /** - * If not zero, then everything up to this value has been - * acked. - */ - uint64_t range; - /** - * List of individually acked messages that are not - * included in the range marked by 'range'. - */ - std::list individual; - - AccumulatedAck(uint64_t r) : range(r) {} - void update(uint64_t firstTag, uint64_t lastTag); - void consolidate(); - void clear(); - bool covers(uint64_t tag) const; - }; - } -} - - -#endif diff --git a/cpp/src/broker/AutoDelete.cpp b/cpp/src/broker/AutoDelete.cpp deleted file mode 100644 index dee8a0ec78..0000000000 --- a/cpp/src/broker/AutoDelete.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - * - * 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 "AutoDelete.h" -#include "../sys/Time.h" - -using namespace qpid::broker; -using namespace qpid::sys; - -AutoDelete::AutoDelete(QueueRegistry* const _registry, uint32_t _period) - : registry(_registry), period(_period), stopped(true) { } - -void AutoDelete::add(Queue::shared_ptr const queue){ - Mutex::ScopedLock l(lock); - queues.push(queue); -} - -Queue::shared_ptr const AutoDelete::pop(){ - Queue::shared_ptr next; - Mutex::ScopedLock l(lock); - if(!queues.empty()){ - next = queues.front(); - queues.pop(); - } - return next; -} - -void AutoDelete::process(){ - Queue::shared_ptr seen; - for(Queue::shared_ptr q = pop(); q; q = pop()){ - if(seen == q){ - add(q); - break; - }else if(q->canAutoDelete()){ - std::string name(q->getName()); - registry->destroy(name); - std::cout << "INFO: Auto-deleted queue named " << name << std::endl; - }else{ - add(q); - if(!seen) seen = q; - } - } -} - -void AutoDelete::run(){ - Monitor::ScopedLock l(monitor); - while(!stopped){ - process(); - monitor.wait(period*TIME_MSEC); - } -} - -void AutoDelete::start(){ - Monitor::ScopedLock l(monitor); - if(stopped){ - stopped = false; - runner = Thread(this); - } -} - -void AutoDelete::stop(){ - { - Monitor::ScopedLock l(monitor); - if(stopped) return; - stopped = true; - } - monitor.notify(); - runner.join(); -} diff --git a/cpp/src/broker/AutoDelete.h b/cpp/src/broker/AutoDelete.h deleted file mode 100644 index 8cfd37325f..0000000000 --- a/cpp/src/broker/AutoDelete.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef _AutoDelete_ -#define _AutoDelete_ -/* - * - * 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 -#include -#include "../sys/Monitor.h" -#include "BrokerQueue.h" -#include "QueueRegistry.h" -#include "../sys/Thread.h" - -namespace qpid { - namespace broker{ - class AutoDelete : private qpid::sys::Runnable { - qpid::sys::Mutex lock; - qpid::sys::Monitor monitor; - std::queue queues; - QueueRegistry* const registry; - uint32_t period; - volatile bool stopped; - qpid::sys::Thread runner; - - Queue::shared_ptr const pop(); - void process(); - virtual void run(); - - public: - AutoDelete(QueueRegistry* const registry, uint32_t period); - void add(Queue::shared_ptr const); - void start(); - void stop(); - }; - } -} - - -#endif diff --git a/cpp/src/broker/Broker.cpp b/cpp/src/broker/Broker.cpp deleted file mode 100644 index e0ab5e2a37..0000000000 --- a/cpp/src/broker/Broker.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/* - * - * 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 -#include - -#include "../framing/AMQFrame.h" -#include "DirectExchange.h" -#include "TopicExchange.h" -#include "FanOutExchange.h" -#include "HeadersExchange.h" -#include "MessageStoreModule.h" -#include "NullMessageStore.h" -#include "../framing/ProtocolInitiation.h" -#include "RecoveryManagerImpl.h" -#include "Connection.h" -#include "../sys/ConnectionInputHandler.h" -#include "../sys/ConnectionInputHandlerFactory.h" -#include "../sys/TimeoutHandler.h" - -#include "Broker.h" - -namespace qpid { -namespace broker { - -const std::string empty; -const std::string amq_direct("amq.direct"); -const std::string amq_topic("amq.topic"); -const std::string amq_fanout("amq.fanout"); -const std::string amq_match("amq.match"); - -Broker::Broker(const Configuration& conf) : - config(conf), - store(createStore(conf)), - queues(store.get()), - timeout(30000), - stagingThreshold(0), - cleaner(&queues, timeout/10), - factory(*this) -{ - exchanges.declare(empty, DirectExchange::typeName); // Default exchange. - exchanges.declare(amq_direct, DirectExchange::typeName); - exchanges.declare(amq_topic, TopicExchange::typeName); - exchanges.declare(amq_fanout, FanOutExchange::typeName); - exchanges.declare(amq_match, HeadersExchange::typeName); - - if(store.get()) { - RecoveryManagerImpl recoverer(queues, exchanges, conf.getStagingThreshold()); - store->recover(recoverer); - } - - cleaner.start(); -} - - -Broker::shared_ptr Broker::create(int16_t port) -{ - Configuration config; - config.setPort(port); - return create(config); -} - -Broker::shared_ptr Broker::create(const Configuration& config) { - return Broker::shared_ptr(new Broker(config)); -} - -MessageStore* Broker::createStore(const Configuration& config) { - if (config.getStore().empty()) - return new NullMessageStore(config.isTrace()); - else - return new MessageStoreModule(config.getStore()); -} - -void Broker::run() { - getAcceptor().run(&factory); -} - -void Broker::shutdown() { - if (acceptor) - acceptor->shutdown(); - cleaner.stop(); -} - -Broker::~Broker() { - shutdown(); -} - -int16_t Broker::getPort() const { return getAcceptor().getPort(); } - -Acceptor& Broker::getAcceptor() const { - if (!acceptor) - const_cast(acceptor) = - Acceptor::create(config.getPort(), - config.getConnectionBacklog(), - config.getWorkerThreads(), - config.isTrace()); - return *acceptor; -} - - -const int16_t Broker::DEFAULT_PORT(5672); - - -}} // namespace qpid::broker - diff --git a/cpp/src/broker/Broker.h b/cpp/src/broker/Broker.h deleted file mode 100644 index f713f0955b..0000000000 --- a/cpp/src/broker/Broker.h +++ /dev/null @@ -1,108 +0,0 @@ -#ifndef _Broker_ -#define _Broker_ - -/* - * - * 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 "Configuration.h" -#include "ConnectionFactory.h" -#include "../sys/Runnable.h" -#include "../sys/Acceptor.h" -#include "../SharedObject.h" -#include "MessageStore.h" -#include "AutoDelete.h" -#include "ExchangeRegistry.h" -#include "ConnectionToken.h" -#include "DirectExchange.h" -#include "../framing/OutputHandler.h" -#include "../framing/ProtocolInitiation.h" -#include "QueueRegistry.h" - -namespace qpid { -namespace broker { -/** - * A broker instance. - */ -class Broker : public sys::Runnable, - public SharedObject -{ - public: - static const int16_t DEFAULT_PORT; - - virtual ~Broker(); - - /** - * Create a broker. - * @param port Port to listen on or 0 to pick a port dynamically. - */ - static shared_ptr create(int16_t port = DEFAULT_PORT); - - /** - * Create a broker using a Configuration. - */ - static shared_ptr create(const Configuration& config); - - /** - * Return listening port. If called before bind this is - * the configured port. If called after it is the actual - * port, which will be different if the configured port is - * 0. - */ - virtual int16_t getPort() const; - - /** - * Run the broker. Implements Runnable::run() so the broker - * can be run in a separate thread. - */ - virtual void run(); - - /** Shut down the broker */ - virtual void shutdown(); - - MessageStore& getStore() { return *store; } - QueueRegistry& getQueues() { return queues; } - ExchangeRegistry& getExchanges() { return exchanges; } - uint32_t getTimeout() { return timeout; } - uint64_t getStagingThreshold() { return stagingThreshold; } - AutoDelete& getCleaner() { return cleaner; } - - private: - Broker(const Configuration& config); - sys::Acceptor& getAcceptor() const; - - Configuration config; - sys::Acceptor::shared_ptr acceptor; - const std::auto_ptr store; - QueueRegistry queues; - ExchangeRegistry exchanges; - uint32_t timeout; - uint64_t stagingThreshold; - AutoDelete cleaner; - ConnectionFactory factory; - - static MessageStore* createStore(const Configuration& config); -}; - -}} - - - -#endif /*!_Broker_*/ diff --git a/cpp/src/broker/BrokerAdapter.cpp b/cpp/src/broker/BrokerAdapter.cpp deleted file mode 100644 index d04610c877..0000000000 --- a/cpp/src/broker/BrokerAdapter.cpp +++ /dev/null @@ -1,388 +0,0 @@ -/* - * - * 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 - -#include "BrokerAdapter.h" -#include "BrokerChannel.h" -#include "Connection.h" -#include "../framing/AMQMethodBody.h" -#include "../Exception.h" - -namespace qpid { -namespace broker { - -using boost::format; -using namespace qpid; -using namespace qpid::framing; - -typedef std::vector QueueVector; - - -BrokerAdapter::BrokerAdapter(Channel& ch, Connection& c, Broker& b) : - CoreRefs(ch, c, b), - connection(c), - basicHandler(*this), - channelHandler(*this), - connectionHandler(*this), - exchangeHandler(*this), - messageHandler(*this), - queueHandler(*this), - txHandler(*this) -{} - - -ProtocolVersion BrokerAdapter::getVersion() const { - return connection.getVersion(); -} - -void BrokerAdapter::ConnectionHandlerImpl::startOk( - const MethodContext&, const FieldTable& /*clientProperties*/, - const string& /*mechanism*/, - const string& /*response*/, const string& /*locale*/) -{ - client.tune( - 100, connection.getFrameMax(), connection.getHeartbeat()); -} - -void BrokerAdapter::ConnectionHandlerImpl::secureOk( - const MethodContext&, const string& /*response*/){} - -void BrokerAdapter::ConnectionHandlerImpl::tuneOk( - const MethodContext&, uint16_t /*channelmax*/, - uint32_t framemax, uint16_t heartbeat) -{ - connection.setFrameMax(framemax); - connection.setHeartbeat(heartbeat); -} - -void BrokerAdapter::ConnectionHandlerImpl::open( - const MethodContext& context, const string& /*virtualHost*/, - const string& /*capabilities*/, bool /*insist*/) -{ - string knownhosts; - client.openOk( - knownhosts, context.getRequestId()); -} - -void BrokerAdapter::ConnectionHandlerImpl::close( - const MethodContext& context, uint16_t /*replyCode*/, const string& /*replyText*/, - uint16_t /*classId*/, uint16_t /*methodId*/) -{ - client.closeOk(context.getRequestId()); - connection.getOutput().close(); -} - -void BrokerAdapter::ConnectionHandlerImpl::closeOk(const MethodContext&){ - connection.getOutput().close(); -} - -void BrokerAdapter::ChannelHandlerImpl::open( - const MethodContext& context, const string& /*outOfBand*/){ - channel.open(); - // FIXME aconway 2007-01-04: provide valid ID as per ampq 0-9 - client.openOk( - std::string()/* ID */, context.getRequestId()); -} - -void BrokerAdapter::ChannelHandlerImpl::flow(const MethodContext&, bool /*active*/){} -void BrokerAdapter::ChannelHandlerImpl::flowOk(const MethodContext&, bool /*active*/){} - -void BrokerAdapter::ChannelHandlerImpl::close( - const MethodContext& context, uint16_t /*replyCode*/, - const string& /*replyText*/, - uint16_t /*classId*/, uint16_t /*methodId*/) -{ - client.closeOk(context.getRequestId()); - // FIXME aconway 2007-01-18: Following line will "delete this". Ugly. - connection.closeChannel(channel.getId()); -} - -void BrokerAdapter::ChannelHandlerImpl::closeOk(const MethodContext&){} - - - -void BrokerAdapter::ExchangeHandlerImpl::declare(const MethodContext& context, uint16_t /*ticket*/, const string& exchange, const string& type, - bool passive, bool /*durable*/, bool /*autoDelete*/, bool /*internal*/, bool nowait, - const FieldTable& /*arguments*/){ - - if(passive){ - if(!broker.getExchanges().get(exchange)) { - throw ChannelException(404, "Exchange not found: " + exchange); - } - }else{ - try{ - std::pair response = broker.getExchanges().declare(exchange, type); - if(!response.second && response.first->getType() != type){ - throw ConnectionException( - 530, - "Exchange already declared to be of type " - + response.first->getType() + ", requested " + type); - } - }catch(UnknownExchangeTypeException& e){ - throw ConnectionException( - 503, "Exchange type not implemented: " + type); - } - } - if(!nowait){ - client.declareOk(context.getRequestId()); - } -} - -void BrokerAdapter::ExchangeHandlerImpl::delete_(const MethodContext& context, uint16_t /*ticket*/, - const string& exchange, bool /*ifUnused*/, bool nowait){ - - //TODO: implement unused - broker.getExchanges().destroy(exchange); - if(!nowait) client.deleteOk(context.getRequestId()); -} - -void BrokerAdapter::QueueHandlerImpl::declare(const MethodContext& context, uint16_t /*ticket*/, const string& name, - bool passive, bool durable, bool exclusive, - bool autoDelete, bool nowait, const qpid::framing::FieldTable& arguments){ - Queue::shared_ptr queue; - if (passive && !name.empty()) { - queue = connection.getQueue(name, channel.getId()); - } else { - std::pair queue_created = - broker.getQueues().declare( - name, durable, - autoDelete ? connection.getTimeout() : 0, - exclusive ? &connection : 0); - queue = queue_created.first; - assert(queue); - if (queue_created.second) { // This is a new queue - channel.setDefaultQueue(queue); - - //apply settings & create persistent record if required - queue_created.first->create(arguments); - - //add default binding: - broker.getExchanges().getDefault()->bind(queue, name, 0); - if (exclusive) { - connection.exclusiveQueues.push_back(queue); - } else if(autoDelete){ - broker.getCleaner().add(queue); - } - } - } - if (exclusive && !queue->isExclusiveOwner(&connection)) - throw ChannelException( - 405, - format("Cannot grant exclusive access to queue '%s'") - % queue->getName()); - if (!nowait) { - string queueName = queue->getName(); - client.declareOk( - queueName, queue->getMessageCount(), queue->getConsumerCount(), - context.getRequestId()); - } -} - -void BrokerAdapter::QueueHandlerImpl::bind(const MethodContext& context, uint16_t /*ticket*/, const string& queueName, - const string& exchangeName, const string& routingKey, bool nowait, - const FieldTable& arguments){ - - Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); - Exchange::shared_ptr exchange = broker.getExchanges().get(exchangeName); - if(exchange){ - string exchangeRoutingKey = routingKey.empty() && queueName.empty() ? queue->getName() : routingKey; - exchange->bind(queue, exchangeRoutingKey, &arguments); - if(!nowait) client.bindOk(context.getRequestId()); - }else{ - throw ChannelException( - 404, "Bind failed. No such exchange: " + exchangeName); - } -} - -void -BrokerAdapter::QueueHandlerImpl::unbind( - const MethodContext& context, - uint16_t /*ticket*/, - const string& queueName, - const string& exchangeName, - const string& routingKey, - const qpid::framing::FieldTable& arguments ) -{ - Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); - if (!queue.get()) throw ChannelException(404, "Unbind failed. No such exchange: " + exchangeName); - - Exchange::shared_ptr exchange = broker.getExchanges().get(exchangeName); - if (!exchange.get()) throw ChannelException(404, "Unbind failed. No such exchange: " + exchangeName); - - exchange->unbind(queue, routingKey, &arguments); - - client.unbindOk(context.getRequestId()); -} - -void BrokerAdapter::QueueHandlerImpl::purge(const MethodContext& context, uint16_t /*ticket*/, const string& queueName, bool nowait){ - - Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); - int count = queue->purge(); - if(!nowait) client.purgeOk( count, context.getRequestId()); -} - -void BrokerAdapter::QueueHandlerImpl::delete_(const MethodContext& context, uint16_t /*ticket*/, const string& queue, - bool ifUnused, bool ifEmpty, bool nowait){ - ChannelException error(0, ""); - int count(0); - Queue::shared_ptr q = connection.getQueue(queue, channel.getId()); - if(ifEmpty && q->getMessageCount() > 0){ - throw ChannelException(406, "Queue not empty."); - }else if(ifUnused && q->getConsumerCount() > 0){ - throw ChannelException(406, "Queue in use."); - }else{ - //remove the queue from the list of exclusive queues if necessary - if(q->isExclusiveOwner(&connection)){ - QueueVector::iterator i = find(connection.exclusiveQueues.begin(), connection.exclusiveQueues.end(), q); - if(i < connection.exclusiveQueues.end()) connection.exclusiveQueues.erase(i); - } - count = q->getMessageCount(); - q->destroy(); - broker.getQueues().destroy(queue); - } - - if(!nowait) - client.deleteOk(count, context.getRequestId()); -} - - - - -void BrokerAdapter::BasicHandlerImpl::qos(const MethodContext& context, uint32_t prefetchSize, uint16_t prefetchCount, bool /*global*/){ - //TODO: handle global - channel.setPrefetchSize(prefetchSize); - channel.setPrefetchCount(prefetchCount); - client.qosOk(context.getRequestId()); -} - -void BrokerAdapter::BasicHandlerImpl::consume( - const MethodContext& context, uint16_t /*ticket*/, - const string& queueName, const string& consumerTag, - bool noLocal, bool noAck, bool exclusive, - bool nowait, const FieldTable& fields) -{ - - Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); - if(!consumerTag.empty() && channel.exists(consumerTag)){ - throw ConnectionException(530, "Consumer tags must be unique"); - } - - string newTag = consumerTag; - channel.consume( - newTag, queue, !noAck, exclusive, noLocal ? &connection : 0, &fields); - - if(!nowait) client.consumeOk(newTag, context.getRequestId()); - - //allow messages to be dispatched if required as there is now a consumer: - queue->dispatch(); -} - -void BrokerAdapter::BasicHandlerImpl::cancel(const MethodContext& context, const string& consumerTag, bool nowait){ - channel.cancel(consumerTag); - - if(!nowait) client.cancelOk(consumerTag, context.getRequestId()); -} - -void BrokerAdapter::BasicHandlerImpl::publish( - const MethodContext& context, uint16_t /*ticket*/, - const string& exchangeName, const string& routingKey, - bool mandatory, bool immediate) -{ - - Exchange::shared_ptr exchange = exchangeName.empty() ? broker.getExchanges().getDefault() : broker.getExchanges().get(exchangeName); - if(exchange){ - BasicMessage* msg = new BasicMessage( - &connection, exchangeName, routingKey, mandatory, immediate, - context.methodBody); - channel.handlePublish(msg); - }else{ - throw ChannelException( - 404, "Exchange not found '" + exchangeName + "'"); - } -} - -void BrokerAdapter::BasicHandlerImpl::get(const MethodContext& context, uint16_t /*ticket*/, const string& queueName, bool noAck){ - Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); - if(!connection.getChannel(channel.getId()).get(queue, "", !noAck)){ - string clusterId;//not used, part of an imatix hack - - client.getEmpty(clusterId, context.getRequestId()); - } -} - -void BrokerAdapter::BasicHandlerImpl::ack(const MethodContext&, uint64_t deliveryTag, bool multiple){ - channel.ack(deliveryTag, multiple); -} - -void BrokerAdapter::BasicHandlerImpl::reject(const MethodContext&, uint64_t /*deliveryTag*/, bool /*requeue*/){} - -void BrokerAdapter::BasicHandlerImpl::recover(const MethodContext&, bool requeue){ - channel.recover(requeue); -} - -void BrokerAdapter::TxHandlerImpl::select(const MethodContext& context){ - channel.begin(); - client.selectOk(context.getRequestId()); -} - -void BrokerAdapter::TxHandlerImpl::commit(const MethodContext& context){ - channel.commit(); - client.commitOk(context.getRequestId()); -} - -void BrokerAdapter::TxHandlerImpl::rollback(const MethodContext& context){ - - channel.rollback(); - client.rollbackOk(context.getRequestId()); - channel.recover(false); -} - -void -BrokerAdapter::ChannelHandlerImpl::ok( const MethodContext& ) -{ - //no specific action required, generic response handling should be sufficient -} - - -// -// Message class method handlers -// -void -BrokerAdapter::ChannelHandlerImpl::ping( const MethodContext& context) -{ - client.ok(context.getRequestId()); - client.pong(); -} - - -void -BrokerAdapter::ChannelHandlerImpl::pong( const MethodContext& context) -{ - client.ok(context.getRequestId()); -} - -void -BrokerAdapter::ChannelHandlerImpl::resume( - const MethodContext&, - const string& /*channel*/ ) -{ - assert(0); // FIXME aconway 2007-01-04: 0-9 feature -} - -}} // namespace qpid::broker - diff --git a/cpp/src/broker/BrokerAdapter.h b/cpp/src/broker/BrokerAdapter.h deleted file mode 100644 index 0323cbad20..0000000000 --- a/cpp/src/broker/BrokerAdapter.h +++ /dev/null @@ -1,222 +0,0 @@ -#ifndef _broker_BrokerAdapter_h -#define _broker_BrokerAdapter_h - -/* - * - * 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 "../gen/AMQP_ServerOperations.h" -#include "HandlerImpl.h" -#include "MessageHandlerImpl.h" -#include "../Exception.h" - -namespace qpid { -namespace broker { - -class Channel; -class Connection; -class Broker; -class ChannelHandler; -class ConnectionHandler; -class BasicHandler; -class ExchangeHandler; -class QueueHandler; -class TxHandler; -class MessageHandler; -class AccessHandler; -class FileHandler; -class StreamHandler; -class DtxHandler; -class TunnelHandler; -class MessageHandlerImpl; - -/** - * Per-channel protocol adapter. - * - * A container for a collection of AMQP-class adapters that translate - * AMQP method bodies into calls on the core Channel, Connection and - * Broker objects. Each adapter class also provides a client proxy - * to send methods to the peer. - * - */ -class BrokerAdapter : public CoreRefs, public framing::AMQP_ServerOperations -{ - public: - BrokerAdapter(Channel& ch, Connection& c, Broker& b); - - framing::ProtocolVersion getVersion() const; - ChannelHandler* getChannelHandler() { return &channelHandler; } - ConnectionHandler* getConnectionHandler() { return &connectionHandler; } - BasicHandler* getBasicHandler() { return &basicHandler; } - ExchangeHandler* getExchangeHandler() { return &exchangeHandler; } - QueueHandler* getQueueHandler() { return &queueHandler; } - TxHandler* getTxHandler() { return &txHandler; } - MessageHandler* getMessageHandler() { return &messageHandler; } - AccessHandler* getAccessHandler() { - throw ConnectionException(540, "Access class not implemented"); } - FileHandler* getFileHandler() { - throw ConnectionException(540, "File class not implemented"); } - StreamHandler* getStreamHandler() { - throw ConnectionException(540, "Stream class not implemented"); } - DtxHandler* getDtxHandler() { - throw ConnectionException(540, "Dtx class not implemented"); } - TunnelHandler* getTunnelHandler() { - throw ConnectionException(540, "Tunnel class not implemented"); } - - framing::AMQP_ClientProxy& getProxy() { return proxy; } - - private: - - class ConnectionHandlerImpl : - public ConnectionHandler, - public HandlerImpl - { - public: - ConnectionHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} - - void startOk(const framing::MethodContext& context, - const qpid::framing::FieldTable& clientProperties, - const std::string& mechanism, const std::string& response, - const std::string& locale); - void secureOk(const framing::MethodContext& context, - const std::string& response); - void tuneOk(const framing::MethodContext& context, - uint16_t channelMax, - uint32_t frameMax, uint16_t heartbeat); - void open(const framing::MethodContext& context, - const std::string& virtualHost, - const std::string& capabilities, bool insist); - void close(const framing::MethodContext& context, uint16_t replyCode, - const std::string& replyText, - uint16_t classId, uint16_t methodId); - void closeOk(const framing::MethodContext& context); - }; - - class ChannelHandlerImpl : - public ChannelHandler, - public HandlerImpl - { - public: - ChannelHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} - - void open(const framing::MethodContext& context, const std::string& outOfBand); - void flow(const framing::MethodContext& context, bool active); - void flowOk(const framing::MethodContext& context, bool active); - void ok( const framing::MethodContext& context ); - void ping( const framing::MethodContext& context ); - void pong( const framing::MethodContext& context ); - void resume( const framing::MethodContext& context, const std::string& channelId ); - void close(const framing::MethodContext& context, uint16_t replyCode, const - std::string& replyText, uint16_t classId, uint16_t methodId); - void closeOk(const framing::MethodContext& context); - }; - - class ExchangeHandlerImpl : - public ExchangeHandler, - public HandlerImpl - { - public: - ExchangeHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} - - void declare(const framing::MethodContext& context, uint16_t ticket, - const std::string& exchange, const std::string& type, - bool passive, bool durable, bool autoDelete, - bool internal, bool nowait, - const qpid::framing::FieldTable& arguments); - void delete_(const framing::MethodContext& context, uint16_t ticket, - const std::string& exchange, bool ifUnused, bool nowait); - }; - - class QueueHandlerImpl : - public QueueHandler, - public HandlerImpl - { - public: - QueueHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} - - void declare(const framing::MethodContext& context, uint16_t ticket, const std::string& queue, - bool passive, bool durable, bool exclusive, - bool autoDelete, bool nowait, - const qpid::framing::FieldTable& arguments); - void bind(const framing::MethodContext& context, uint16_t ticket, const std::string& queue, - const std::string& exchange, const std::string& routingKey, - bool nowait, const qpid::framing::FieldTable& arguments); - void unbind(const framing::MethodContext& context, - uint16_t ticket, - const std::string& queue, - const std::string& exchange, - const std::string& routingKey, - const qpid::framing::FieldTable& arguments ); - void purge(const framing::MethodContext& context, uint16_t ticket, const std::string& queue, - bool nowait); - void delete_(const framing::MethodContext& context, uint16_t ticket, const std::string& queue, - bool ifUnused, bool ifEmpty, - bool nowait); - }; - - class BasicHandlerImpl : - public BasicHandler, - public HandlerImpl - { - public: - BasicHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} - - void qos(const framing::MethodContext& context, uint32_t prefetchSize, - uint16_t prefetchCount, bool global); - void consume( - const framing::MethodContext& context, uint16_t ticket, const std::string& queue, - const std::string& consumerTag, bool noLocal, bool noAck, - bool exclusive, bool nowait, - const qpid::framing::FieldTable& fields); - void cancel(const framing::MethodContext& context, const std::string& consumerTag, - bool nowait); - void publish(const framing::MethodContext& context, uint16_t ticket, - const std::string& exchange, const std::string& routingKey, - bool mandatory, bool immediate); - void get(const framing::MethodContext& context, uint16_t ticket, const std::string& queue, - bool noAck); - void ack(const framing::MethodContext& context, uint64_t deliveryTag, bool multiple); - void reject(const framing::MethodContext& context, uint64_t deliveryTag, bool requeue); - void recover(const framing::MethodContext& context, bool requeue); - }; - - class TxHandlerImpl : - public TxHandler, - public HandlerImpl - { - public: - TxHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} - - void select(const framing::MethodContext& context); - void commit(const framing::MethodContext& context); - void rollback(const framing::MethodContext& context); - }; - - Connection& connection; - BasicHandlerImpl basicHandler; - ChannelHandlerImpl channelHandler; - ConnectionHandlerImpl connectionHandler; - ExchangeHandlerImpl exchangeHandler; - MessageHandlerImpl messageHandler; - QueueHandlerImpl queueHandler; - TxHandlerImpl txHandler; - -}; -}} // namespace qpid::broker - - - -#endif /*!_broker_BrokerAdapter_h*/ diff --git a/cpp/src/broker/BrokerChannel.cpp b/cpp/src/broker/BrokerChannel.cpp deleted file mode 100644 index bd97c6f021..0000000000 --- a/cpp/src/broker/BrokerChannel.cpp +++ /dev/null @@ -1,347 +0,0 @@ -/* - * - * 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 - -#include -#include -#include -#include - -#include - -#include "BrokerChannel.h" -#include "../framing/ChannelAdapter.h" -#include "../QpidError.h" -#include "DeliverableMessage.h" -#include "BrokerQueue.h" -#include "BrokerMessage.h" -#include "MessageStore.h" -#include "TxAck.h" -#include "TxPublish.h" -#include "BrokerAdapter.h" -#include "Connection.h" - -using std::mem_fun_ref; -using std::bind2nd; -using namespace qpid::broker; -using namespace qpid::framing; -using namespace qpid::sys; - - -Channel::Channel( - Connection& con, ChannelId id, - uint32_t _framesize, MessageStore* const _store, - uint64_t _stagingThreshold -) : - ChannelAdapter(id, &con.getOutput(), con.getVersion()), - connection(con), - currentDeliveryTag(1), - transactional(false), - prefetchSize(0), - prefetchCount(0), - framesize(_framesize), - tagGenerator("sgen"), - accumulatedAck(0), - store(_store), - messageBuilder(this, _store, _stagingThreshold), - opened(id == 0),//channel 0 is automatically open, other must be explicitly opened - adapter(new BrokerAdapter(*this, con, con.broker)) -{ - outstanding.reset(); -} - -Channel::~Channel(){ - close(); -} - -bool Channel::exists(const string& consumerTag){ - return consumers.find(consumerTag) != consumers.end(); -} - -// TODO aconway 2007-02-12: Why is connection token passed in instead -// of using the channel's parent connection? -void Channel::consume(string& tagInOut, Queue::shared_ptr queue, bool acks, - bool exclusive, ConnectionToken* const connection, - const FieldTable*) -{ - if(tagInOut.empty()) - tagInOut = tagGenerator.generate(); - std::auto_ptr c( - new ConsumerImpl(this, tagInOut, queue, connection, acks)); - queue->consume(c.get(), exclusive);//may throw exception - consumers.insert(tagInOut, c.release()); -} - -void Channel::cancel(const string& tag){ - // consumers is a ptr_map so erase will delete the consumer - // which will call cancel. - ConsumerImplMap::iterator i = consumers.find(tag); - if (i != consumers.end()) - consumers.erase(i); -} - -void Channel::close(){ - opened = false; - consumers.clear(); - recover(true); -} - -void Channel::begin(){ - transactional = true; -} - -void Channel::commit(){ - TxOp::shared_ptr txAck(new TxAck(accumulatedAck, unacked)); - txBuffer.enlist(txAck); - if(txBuffer.prepare(store)){ - txBuffer.commit(); - } - accumulatedAck.clear(); -} - -void Channel::rollback(){ - txBuffer.rollback(); - accumulatedAck.clear(); -} - -void Channel::deliver( - Message::shared_ptr& msg, const string& consumerTag, - Queue::shared_ptr& queue, bool ackExpected) -{ - Mutex::ScopedLock locker(deliveryLock); - - // Key the delivered messages to the id of the request in which they're sent - uint64_t deliveryTag = getNextSendRequestId(); - - if(ackExpected){ - unacked.push_back(DeliveryRecord(msg, queue, consumerTag, deliveryTag)); - outstanding.size += msg->contentSize(); - outstanding.count++; - } - //send deliver method, header and content(s) - msg->deliver(*this, consumerTag, deliveryTag, framesize); -} - -bool Channel::checkPrefetch(Message::shared_ptr& msg){ - Mutex::ScopedLock locker(deliveryLock); - bool countOk = !prefetchCount || prefetchCount > unacked.size(); - bool sizeOk = !prefetchSize || prefetchSize > msg->contentSize() + outstanding.size || unacked.empty(); - return countOk && sizeOk; -} - -Channel::ConsumerImpl::ConsumerImpl(Channel* _parent, const string& _tag, - Queue::shared_ptr _queue, - ConnectionToken* const _connection, bool ack -) : parent(_parent), tag(_tag), queue(_queue), connection(_connection), - ackExpected(ack), blocked(false) {} - -bool Channel::ConsumerImpl::deliver(Message::shared_ptr& msg){ - if(!connection || connection != msg->getPublisher()){//check for no_local - if(ackExpected && !parent->checkPrefetch(msg)){ - blocked = true; - }else{ - blocked = false; - parent->deliver(msg, tag, queue, ackExpected); - return true; - } - } - return false; -} - -Channel::ConsumerImpl::~ConsumerImpl() { - cancel(); -} - -void Channel::ConsumerImpl::cancel(){ - if(queue) - queue->cancel(this); -} - -void Channel::ConsumerImpl::requestDispatch(){ - if(blocked) - queue->dispatch(); -} - -void Channel::handleInlineTransfer(Message::shared_ptr msg) -{ - Exchange::shared_ptr exchange = - connection.broker.getExchanges().get(msg->getExchange()); - if(transactional){ - TxPublish* deliverable(new TxPublish(msg)); - TxOp::shared_ptr op(deliverable); - exchange->route( - *deliverable, msg->getRoutingKey(), - &(msg->getApplicationHeaders())); - txBuffer.enlist(op); - }else{ - DeliverableMessage deliverable(msg); - exchange->route( - deliverable, msg->getRoutingKey(), - &(msg->getApplicationHeaders())); - } -} - -void Channel::handlePublish(Message* _message){ - Message::shared_ptr message(_message); - messageBuilder.initialise(message); -} - -void Channel::handleHeader(AMQHeaderBody::shared_ptr header){ - messageBuilder.setHeader(header); - //at this point, decide based on the size of the message whether we want - //to stage it by saving content directly to disk as it arrives -} - -void Channel::handleContent(AMQContentBody::shared_ptr content){ - messageBuilder.addContent(content); -} - -void Channel::handleHeartbeat(boost::shared_ptr) { - // TODO aconway 2007-01-17: Implement heartbeating. -} - -void Channel::complete(Message::shared_ptr msg) { - Exchange::shared_ptr exchange = - connection.broker.getExchanges().get(msg->getExchange()); - assert(exchange.get()); - if(transactional) { - TxPublish* deliverable(new TxPublish(msg)); - TxOp::shared_ptr op(deliverable); - exchange->route(*deliverable, msg->getRoutingKey(), - &(msg->getApplicationHeaders())); - txBuffer.enlist(op); - } else { - DeliverableMessage deliverable(msg); - exchange->route(deliverable, msg->getRoutingKey(), - &(msg->getApplicationHeaders())); - } -} - -void Channel::ack(){ - ack(getFirstAckRequest(), getLastAckRequest()); -} - -// Used by Basic -void Channel::ack(uint64_t deliveryTag, bool multiple){ - if (multiple) - ack(0, deliveryTag); - else - ack(deliveryTag, deliveryTag); -} - -void Channel::ack(uint64_t firstTag, uint64_t lastTag){ - if(transactional){ - accumulatedAck.update(firstTag, lastTag); - - //TODO: I think the outstanding prefetch size & count should be updated at this point... - //TODO: ...this may then necessitate dispatching to consumers - }else{ - Mutex::ScopedLock locker(deliveryLock);//need to synchronize with possible concurrent delivery - - ack_iterator i = find_if(unacked.begin(), unacked.end(), bind2nd(mem_fun_ref(&DeliveryRecord::matches), lastTag)); - ack_iterator j = (firstTag == 0) ? - unacked.begin() : - find_if(unacked.begin(), unacked.end(), bind2nd(mem_fun_ref(&DeliveryRecord::matches), firstTag)); - - if(i == unacked.end()){ - throw ConnectionException(530, "Received ack for unrecognised delivery tag"); - }else if(i!=j){ - ack_iterator end = ++i; - for_each(j, end, bind2nd(mem_fun_ref(&DeliveryRecord::discard), 0)); - unacked.erase(unacked.begin(), end); - - //recalculate the prefetch: - outstanding.reset(); - for_each(unacked.begin(), unacked.end(), bind2nd(mem_fun_ref(&DeliveryRecord::addTo), &outstanding)); - }else{ - i->discard(); - i->subtractFrom(&outstanding); - unacked.erase(i); - } - - //if the prefetch limit had previously been reached, there may - //be messages that can be now be delivered - std::for_each(consumers.begin(), consumers.end(), - boost::bind(&ConsumerImpl::requestDispatch, _1)); - } -} - -void Channel::recover(bool requeue){ - Mutex::ScopedLock locker(deliveryLock);//need to synchronize with possible concurrent delivery - - if(requeue){ - outstanding.reset(); - std::list copy = unacked; - unacked.clear(); - for_each(copy.begin(), copy.end(), mem_fun_ref(&DeliveryRecord::requeue)); - }else{ - for_each(unacked.begin(), unacked.end(), bind2nd(mem_fun_ref(&DeliveryRecord::redeliver), this)); - } -} - -bool Channel::get(Queue::shared_ptr queue, const string& destination, bool ackExpected){ - Message::shared_ptr msg = queue->dequeue(); - if(msg){ - Mutex::ScopedLock locker(deliveryLock); - uint64_t myDeliveryTag = getNextSendRequestId(); - msg->sendGetOk(MethodContext(this, msg->getRespondTo()), - destination, - queue->getMessageCount() + 1, myDeliveryTag, - framesize); - if(ackExpected){ - unacked.push_back(DeliveryRecord(msg, queue, myDeliveryTag)); - } - return true; - }else{ - return false; - } -} - -void Channel::deliver(Message::shared_ptr& msg, const string& consumerTag, - uint64_t deliveryTag) -{ - msg->deliver(*this, consumerTag, deliveryTag, framesize); -} - -void Channel::handleMethodInContext( - boost::shared_ptr method, - const MethodContext& context -) -{ - try{ - if(getId() != 0 && !method->isA() && !isOpen()) { - std::stringstream out; - out << "Attempt to use unopened channel: " << getId(); - throw ConnectionException(504, out.str()); - } else { - method->invoke(*adapter, context); - } - }catch(ChannelException& e){ - adapter->getProxy().getChannel().close( - e.code, e.toString(), - method->amqpClassId(), method->amqpMethodId()); - connection.closeChannel(getId()); - }catch(ConnectionException& e){ - connection.close(e.code, e.toString(), method->amqpClassId(), method->amqpMethodId()); - }catch(std::exception& e){ - connection.close(541/*internal error*/, e.what(), method->amqpClassId(), method->amqpMethodId()); - } -} diff --git a/cpp/src/broker/BrokerChannel.h b/cpp/src/broker/BrokerChannel.h deleted file mode 100644 index fb090196c2..0000000000 --- a/cpp/src/broker/BrokerChannel.h +++ /dev/null @@ -1,159 +0,0 @@ -#ifndef _broker_BrokerChannel_h -#define _broker_BrokerChannel_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 - -#include -#include -#include - -#include "AccumulatedAck.h" -#include "Consumer.h" -#include "DeliveryRecord.h" -#include "MessageBuilder.h" -#include "NameGenerator.h" -#include "Prefetch.h" -#include "TxBuffer.h" -#include "../framing/ChannelAdapter.h" -#include "../gen/ChannelOpenBody.h" -#include "CompletionHandler.h" - -namespace qpid { -namespace broker { - -class ConnectionToken; -class Connection; -class Queue; -class BrokerAdapter; - -using framing::string; - -/** - * Maintains state for an AMQP channel. Handles incoming and - * outgoing messages for that channel. - */ -class Channel : public framing::ChannelAdapter, - public CompletionHandler -{ - class ConsumerImpl : public Consumer - { - Channel* parent; - const string tag; - Queue::shared_ptr queue; - ConnectionToken* const connection; - const bool ackExpected; - bool blocked; - - public: - ConsumerImpl(Channel* parent, const string& tag, - Queue::shared_ptr queue, - ConnectionToken* const connection, bool ack); - ~ConsumerImpl(); - virtual bool deliver(Message::shared_ptr& msg); - void cancel(); - void requestDispatch(); - }; - - typedef boost::ptr_map ConsumerImplMap; - - Connection& connection; - uint64_t currentDeliveryTag; - Queue::shared_ptr defaultQueue; - bool transactional; - ConsumerImplMap consumers; - uint32_t prefetchSize; - uint16_t prefetchCount; - Prefetch outstanding; - uint32_t framesize; - NameGenerator tagGenerator; - std::list unacked; - sys::Mutex deliveryLock; - TxBuffer txBuffer; - AccumulatedAck accumulatedAck; - MessageStore* const store; - MessageBuilder messageBuilder;//builder for in-progress message - bool opened; - boost::scoped_ptr adapter; - - // completion handler for MessageBuilder - void complete(Message::shared_ptr msg); - - void deliver(Message::shared_ptr& msg, const string& tag, - Queue::shared_ptr& queue, bool ackExpected); - bool checkPrefetch(Message::shared_ptr& msg); - - public: - Channel(Connection& parent, - framing::ChannelId id, - uint32_t framesize, - MessageStore* const _store = 0, - uint64_t stagingThreshold = 0); - - ~Channel(); - - bool isOpen() const { return opened; } - BrokerAdapter& getAdatper() { return *adapter; } - - void open() { opened = true; } - void setDefaultQueue(Queue::shared_ptr queue){ defaultQueue = queue; } - Queue::shared_ptr getDefaultQueue() const { return defaultQueue; } - uint32_t setPrefetchSize(uint32_t size){ return prefetchSize = size; } - uint16_t setPrefetchCount(uint16_t n){ return prefetchCount = n; } - - bool exists(const string& consumerTag); - - /** - *@param tagInOut - if empty it is updated with the generated token. - */ - void consume(string& tagInOut, Queue::shared_ptr queue, bool acks, - bool exclusive, ConnectionToken* const connection = 0, - const framing::FieldTable* = 0); - void cancel(const string& tag); - bool get(Queue::shared_ptr queue, const std::string& destination, bool ackExpected); - void begin(); - void close(); - void commit(); - void rollback(); - void ack(); - void ack(uint64_t deliveryTag, bool multiple); - void ack(uint64_t deliveryTag, uint64_t endTag); - void recover(bool requeue); - void deliver(Message::shared_ptr& msg, const string& consumerTag, uint64_t deliveryTag); - void handlePublish(Message* msg); - void handleHeader(boost::shared_ptr); - void handleContent(boost::shared_ptr); - void handleHeartbeat(boost::shared_ptr); - - void handleInlineTransfer(Message::shared_ptr msg); - - // For ChannelAdapter - void handleMethodInContext( - boost::shared_ptr method, - const framing::MethodContext& context); -}; - -}} // namespace broker - - -#endif /*!_broker_BrokerChannel_h*/ diff --git a/cpp/src/broker/BrokerExchange.h b/cpp/src/broker/BrokerExchange.h deleted file mode 100644 index 07a411335a..0000000000 --- a/cpp/src/broker/BrokerExchange.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _broker_BrokerExchange_h -#define _broker_BrokerExchange_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 -#include "Deliverable.h" -#include "BrokerQueue.h" -#include "../framing/FieldTable.h" - -namespace qpid { - namespace broker { - using std::string; - - class Exchange{ - const string name; - public: - typedef boost::shared_ptr shared_ptr; - - explicit Exchange(const string& _name) : name(_name){} - virtual ~Exchange(){} - string getName() { return name; } - virtual string getType() = 0; - virtual void bind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args) = 0; - virtual void unbind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args) = 0; - virtual void route(Deliverable& msg, const string& routingKey, const qpid::framing::FieldTable* args) = 0; - }; - } -} - - -#endif /*!_broker_BrokerExchange_h*/ diff --git a/cpp/src/broker/BrokerMessage.cpp b/cpp/src/broker/BrokerMessage.cpp deleted file mode 100644 index 96a90ef608..0000000000 --- a/cpp/src/broker/BrokerMessage.cpp +++ /dev/null @@ -1,244 +0,0 @@ -/* - * - * 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 - -#include "BrokerMessage.h" -#include - -#include "InMemoryContent.h" -#include "LazyLoadedContent.h" -#include "MessageStore.h" -#include "../gen/BasicDeliverBody.h" -#include "../gen/BasicGetOkBody.h" -#include "../framing/AMQContentBody.h" -#include "../framing/AMQHeaderBody.h" -#include "../framing/AMQMethodBody.h" -#include "../framing/AMQFrame.h" -#include "../framing/ChannelAdapter.h" -#include "RecoveryManagerImpl.h" - -using namespace boost; -using namespace qpid::broker; -using namespace qpid::framing; -using namespace qpid::sys; - -BasicMessage::BasicMessage( - const ConnectionToken* const _publisher, - const string& _exchange, const string& _routingKey, - bool _mandatory, bool _immediate, framing::AMQMethodBody::shared_ptr respondTo -) : - Message(_publisher, _exchange, _routingKey, _mandatory, - _immediate, respondTo), - size(0) -{} - -// For tests only. -BasicMessage::BasicMessage() : size(0) -{} - -BasicMessage::~BasicMessage(){} - -void BasicMessage::setHeader(AMQHeaderBody::shared_ptr _header){ - this->header = _header; -} - -void BasicMessage::addContent(AMQContentBody::shared_ptr data){ - if (!content.get()) { - content = std::auto_ptr(new InMemoryContent()); - } - content->add(data); - size += data->size(); -} - -bool BasicMessage::isComplete(){ - return header.get() && (header->getContentSize() == contentSize()); -} - -void BasicMessage::deliver(ChannelAdapter& channel, - const string& consumerTag, uint64_t deliveryTag, - uint32_t framesize) -{ - // CCT -- TODO - Update code generator to take pointer/ not - // instance to avoid extra contruction - channel.send( - new BasicDeliverBody( - channel.getVersion(), consumerTag, deliveryTag, - getRedelivered(), getExchange(), getRoutingKey())); - sendContent(channel, framesize); -} - -void BasicMessage::sendGetOk(const MethodContext& context, - const std::string& /*destination*/, - uint32_t messageCount, - uint64_t deliveryTag, - uint32_t framesize) -{ - // CCT -- TODO - Update code generator to take pointer/ not - // instance to avoid extra contruction - context.channel->send( - new BasicGetOkBody( - context.channel->getVersion(), - context.methodBody->getRequestId(), - deliveryTag, getRedelivered(), getExchange(), - getRoutingKey(), messageCount)); - sendContent(*context.channel, framesize); -} - -void BasicMessage::sendContent( - ChannelAdapter& channel, uint32_t framesize) -{ - channel.send(header); - Mutex::ScopedLock locker(contentLock); - if (content.get()) - content->send(channel, framesize); -} - -BasicHeaderProperties* BasicMessage::getHeaderProperties(){ - return boost::polymorphic_downcast( - header->getProperties()); -} - -const FieldTable& BasicMessage::getApplicationHeaders(){ - return getHeaderProperties()->getHeaders(); -} - -bool BasicMessage::isPersistent() -{ - if(!header) return false; - BasicHeaderProperties* props = getHeaderProperties(); - return props && props->getDeliveryMode() == PERSISTENT; -} - -void BasicMessage::decode(Buffer& buffer, bool headersOnly, uint32_t contentChunkSize) -{ - decodeHeader(buffer); - if (!headersOnly) decodeContent(buffer, contentChunkSize); -} - -void BasicMessage::decodeHeader(Buffer& buffer) -{ - //don't care about the type here, but want encode/decode to be symmetric - RecoveryManagerImpl::decodeMessageType(buffer); - - string exchange; - string routingKey; - - buffer.getShortString(exchange); - buffer.getShortString(routingKey); - setRouting(exchange, routingKey); - - uint32_t headerSize = buffer.getLong(); - AMQHeaderBody::shared_ptr headerBody(new AMQHeaderBody()); - headerBody->decode(buffer, headerSize); - setHeader(headerBody); -} - -void BasicMessage::decodeContent(Buffer& buffer, uint32_t chunkSize) -{ - uint64_t expected = expectedContentSize(); - if (expected != buffer.available()) { - std::cout << "WARN: Expected " << expectedContentSize() << " bytes, got " << buffer.available() << std::endl; - throw Exception("Cannot decode content, buffer not large enough."); - } - - if (!chunkSize || chunkSize > expected) { - chunkSize = expected; - } - - uint64_t total = 0; - while (total < expectedContentSize()) { - uint64_t remaining = expected - total; - AMQContentBody::shared_ptr contentBody(new AMQContentBody()); - contentBody->decode(buffer, remaining < chunkSize ? remaining : chunkSize); - addContent(contentBody); - total += chunkSize; - } -} - -void BasicMessage::encode(Buffer& buffer) const -{ - encodeHeader(buffer); - encodeContent(buffer); -} - -void BasicMessage::encodeHeader(Buffer& buffer) const -{ - RecoveryManagerImpl::encodeMessageType(*this, buffer); - buffer.putShortString(getExchange()); - buffer.putShortString(getRoutingKey()); - buffer.putLong(header->size()); - header->encode(buffer); -} - -void BasicMessage::encodeContent(Buffer& buffer) const -{ - Mutex::ScopedLock locker(contentLock); - if (content.get()) content->encode(buffer); -} - -uint32_t BasicMessage::encodedSize() const -{ - return encodedHeaderSize() + encodedContentSize(); -} - -uint32_t BasicMessage::encodedContentSize() const -{ - Mutex::ScopedLock locker(contentLock); - return content.get() ? content->size() : 0; -} - -uint32_t BasicMessage::encodedHeaderSize() const -{ - return RecoveryManagerImpl::encodedMessageTypeSize() - +getExchange().size() + 1 - + getRoutingKey().size() + 1 - + header->size() + 4;//4 extra bytes for size -} - -uint64_t BasicMessage::expectedContentSize() -{ - return header.get() ? header->getContentSize() : 0; -} - -void BasicMessage::releaseContent(MessageStore* store) -{ - Mutex::ScopedLock locker(contentLock); - if (!isPersistent() && getPersistenceId() == 0) { - store->stage(*this); - } - if (!content.get() || content->size() > 0) { - //set content to lazy loading mode (but only if there is - //stored content): - - //Note: the LazyLoadedContent instance contains a raw pointer - //to the message, however it is then set as a member of that - //message so its lifetime is guaranteed to be no longer than - //that of the message itself - content = std::auto_ptr( - new LazyLoadedContent(store, this, expectedContentSize())); - } -} - -void BasicMessage::setContent(std::auto_ptr& _content) -{ - Mutex::ScopedLock locker(contentLock); - content = _content; -} diff --git a/cpp/src/broker/BrokerMessage.h b/cpp/src/broker/BrokerMessage.h deleted file mode 100644 index 1d13183f43..0000000000 --- a/cpp/src/broker/BrokerMessage.h +++ /dev/null @@ -1,137 +0,0 @@ -#ifndef _broker_BrokerMessage_h -#define _broker_BrokerMessage_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 -#include - -#include "BrokerMessageBase.h" -#include "../framing/BasicHeaderProperties.h" -#include "ConnectionToken.h" -#include "Content.h" -#include "../sys/Mutex.h" -#include "TxBuffer.h" - -namespace qpid { - -namespace framing { -class MethodContext; -class ChannelAdapter; -class AMQHeaderBody; -} - -namespace broker { - -class MessageStore; -using framing::string; - -/** - * Represents an AMQP message, i.e. a header body, a list of - * content bodies and some details about the publication - * request. - */ -class BasicMessage : public Message { - boost::shared_ptr header; - std::auto_ptr content; - mutable sys::Mutex contentLock; - uint64_t size; - - void sendContent(framing::ChannelAdapter&, uint32_t framesize); - - public: - typedef boost::shared_ptr shared_ptr; - - BasicMessage(const ConnectionToken* const publisher, - const string& exchange, const string& routingKey, - bool mandatory, bool immediate, - boost::shared_ptr respondTo); - BasicMessage(); - ~BasicMessage(); - void setHeader(boost::shared_ptr header); - void addContent(framing::AMQContentBody::shared_ptr data); - bool isComplete(); - - void deliver(framing::ChannelAdapter&, - const string& consumerTag, - uint64_t deliveryTag, - uint32_t framesize); - - void sendGetOk(const framing::MethodContext&, - const std::string& destination, - uint32_t messageCount, - uint64_t deliveryTag, - uint32_t framesize); - - framing::BasicHeaderProperties* getHeaderProperties(); - const framing::FieldTable& getApplicationHeaders(); - bool isPersistent(); - uint64_t contentSize() const { return size; } - - void decode(framing::Buffer& buffer, bool headersOnly = false, - uint32_t contentChunkSize = 0); - void decodeHeader(framing::Buffer& buffer); - void decodeContent(framing::Buffer& buffer, uint32_t contentChunkSize = 0); - - void encode(framing::Buffer& buffer) const; - void encodeHeader(framing::Buffer& buffer) const; - void encodeContent(framing::Buffer& buffer) const; - /** - * @returns the size of the buffer needed to encode this - * message in its entirety - */ - uint32_t encodedSize() const; - /** - * @returns the size of the buffer needed to encode the - * 'header' of this message (not just the header frame, - * but other meta data e.g.routing key and exchange) - */ - uint32_t encodedHeaderSize() const; - /** - * @returns the size of the buffer needed to encode the - * (possibly partial) content held by this message - */ - uint32_t encodedContentSize() const; - /** - * Releases the in-memory content data held by this - * message. Must pass in a store from which the data can - * be reloaded. - */ - void releaseContent(MessageStore* store); - /** - * If headers have been received, returns the expected - * content size else returns 0. - */ - uint64_t expectedContentSize(); - /** - * Sets the 'content' implementation of this message (the - * message controls the lifecycle of the content instance - * it uses). - */ - void setContent(std::auto_ptr& content); -}; - -} -} - - -#endif /*!_broker_BrokerMessage_h*/ diff --git a/cpp/src/broker/BrokerMessageBase.h b/cpp/src/broker/BrokerMessageBase.h deleted file mode 100644 index 41f54bc8bb..0000000000 --- a/cpp/src/broker/BrokerMessageBase.h +++ /dev/null @@ -1,182 +0,0 @@ -#ifndef _broker_BrokerMessageBase_h -#define _broker_BrokerMessageBase_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 -#include -#include "Content.h" -#include "PersistableMessage.h" -#include "../framing/amqp_types.h" - -namespace qpid { - -namespace framing { -class MethodContext; -class ChannelAdapter; -class BasicHeaderProperties; -class FieldTable; -class AMQMethodBody; -class AMQContentBody; -class AMQHeaderBody; -} - - -namespace broker { -class ConnectionToken; -class MessageStore; - -/** - * Base class for all types of internal broker messages - * abstracting away the operations - * TODO; AMS: for the moment this is mostly a placeholder - */ -class Message : public PersistableMessage{ - public: - typedef boost::shared_ptr shared_ptr; - typedef boost::shared_ptr AMQMethodBodyPtr; - - - Message(const ConnectionToken* publisher_, - const std::string& _exchange, - const std::string& _routingKey, - bool _mandatory, bool _immediate, - AMQMethodBodyPtr respondTo_) : - publisher(publisher_), - exchange(_exchange), - routingKey(_routingKey), - mandatory(_mandatory), - immediate(_immediate), - persistenceId(0), - redelivered(false), - respondTo(respondTo_) - {} - - Message() : - mandatory(false), - immediate(false), - persistenceId(0), - redelivered(false) - {} - - virtual ~Message() {}; - - // Accessors - const std::string& getRoutingKey() const { return routingKey; } - const std::string& getExchange() const { return exchange; } - uint64_t getPersistenceId() const { return persistenceId; } - bool getRedelivered() const { return redelivered; } - AMQMethodBodyPtr getRespondTo() const { return respondTo; } - - void setRouting(const std::string& _exchange, const std::string& _routingKey) - { exchange = _exchange; routingKey = _routingKey; } - void setPersistenceId(uint64_t _persistenceId) const { persistenceId = _persistenceId; } - void redeliver() { redelivered = true; } - - /** - * Used to deliver the message from the queue - */ - virtual void deliver(framing::ChannelAdapter& channel, - const std::string& consumerTag, - uint64_t deliveryTag, - uint32_t framesize) = 0; - /** - * Used to return a message in response to a get from a queue - */ - virtual void sendGetOk(const framing::MethodContext& context, - const std::string& destination, - uint32_t messageCount, - uint64_t deliveryTag, - uint32_t framesize) = 0; - - virtual bool isComplete() = 0; - - virtual uint64_t contentSize() const = 0; - virtual framing::BasicHeaderProperties* getHeaderProperties() = 0; - virtual const framing::FieldTable& getApplicationHeaders() = 0; - virtual bool isPersistent() = 0; - virtual const ConnectionToken* getPublisher() const { - return publisher; - } - - virtual void encode(framing::Buffer& buffer) const = 0; - virtual void encodeHeader(framing::Buffer& buffer) const = 0; - - /** - * @returns the size of the buffer needed to encode this - * message in its entirety - */ - virtual uint32_t encodedSize() const = 0; - /** - * @returns the size of the buffer needed to encode the - * 'header' of this message (not just the header frame, - * but other meta data e.g.routing key and exchange) - */ - virtual uint32_t encodedHeaderSize() const = 0; - /** - * @returns the size of the buffer needed to encode the - * (possibly partial) content held by this message - */ - virtual uint32_t encodedContentSize() const = 0; - /** - * If headers have been received, returns the expected - * content size else returns 0. - */ - virtual uint64_t expectedContentSize() = 0; - - virtual void decodeHeader(framing::Buffer& buffer) = 0; - virtual void decodeContent(framing::Buffer& buffer, uint32_t contentChunkSize = 0) = 0; - - static shared_ptr decode(framing::Buffer& buffer); - - // TODO: AMS 29/1/2007 Don't think these are really part of base class - - /** - * Sets the 'content' implementation of this message (the - * message controls the lifecycle of the content instance - * it uses). - */ - virtual void setContent(std::auto_ptr& /*content*/) {}; - virtual void setHeader(boost::shared_ptr) {}; - virtual void addContent(boost::shared_ptr) {}; - /** - * Releases the in-memory content data held by this - * message. Must pass in a store from which the data can - * be reloaded. - */ - virtual void releaseContent(MessageStore* /*store*/) {}; - - private: - const ConnectionToken* publisher; - std::string exchange; - std::string routingKey; - const bool mandatory; - const bool immediate; - mutable uint64_t persistenceId; - bool redelivered; - AMQMethodBodyPtr respondTo; -}; - -}} - - -#endif /*!_broker_BrokerMessage_h*/ diff --git a/cpp/src/broker/BrokerMessageMessage.cpp b/cpp/src/broker/BrokerMessageMessage.cpp deleted file mode 100644 index 7a2fa31343..0000000000 --- a/cpp/src/broker/BrokerMessageMessage.cpp +++ /dev/null @@ -1,320 +0,0 @@ -/* - * - * 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 "../QpidError.h" -#include "BrokerMessageMessage.h" -#include "../framing/ChannelAdapter.h" -#include "../gen/MessageTransferBody.h" -#include "../gen/MessageOpenBody.h" -#include "../gen/MessageCloseBody.h" -#include "../gen/MessageAppendBody.h" -#include "Reference.h" -#include "../framing/AMQFrame.h" -#include "../framing/FieldTable.h" -#include "../framing/BasicHeaderProperties.h" -#include "RecoveryManagerImpl.h" - -#include - -using namespace std; -using namespace qpid::framing; - -namespace qpid { -namespace broker { - -MessageMessage::MessageMessage( - ConnectionToken* publisher, RequestId requestId_, TransferPtr transfer_ -) : Message(publisher, transfer_->getDestination(), - transfer_->getRoutingKey(), - transfer_->getMandatory(), - transfer_->getImmediate(), - transfer_), - requestId(requestId_), - transfer(transfer_) -{ - assert(transfer->getBody().isInline()); -} - -MessageMessage::MessageMessage( - ConnectionToken* publisher, RequestId requestId_, TransferPtr transfer_, - ReferencePtr reference_ -) : Message(publisher, transfer_->getDestination(), - transfer_->getRoutingKey(), - transfer_->getMandatory(), - transfer_->getImmediate(), - transfer_), - requestId(requestId_), - transfer(transfer_), - reference(reference_) -{ - assert(!transfer->getBody().isInline()); - assert(reference_); -} - -/** - * Currently used by message store impls to recover messages - */ -MessageMessage::MessageMessage() : transfer(new MessageTransferBody(qpid::framing::highestProtocolVersion)) {} - -// TODO: astitcher 1-Mar-2007: This code desperately needs better factoring -void MessageMessage::transferMessage( - framing::ChannelAdapter& channel, - const std::string& consumerTag, - uint32_t framesize) -{ - const framing::Content& body = transfer->getBody(); - // Send any reference data - ReferencePtr ref= getReference(); - if (ref){ - - // Open - channel.send(new MessageOpenBody(channel.getVersion(), ref->getId())); - // Appends - for(Reference::Appends::const_iterator a = ref->getAppends().begin(); - a != ref->getAppends().end(); - ++a) { - uint32_t sizeleft = (*a)->size(); - const string& content = (*a)->getBytes(); - // Calculate overhead bytes - // Assume that the overhead is constant as the reference name doesn't change - uint32_t overhead = sizeleft - content.size(); - string::size_type contentStart = 0; - while (sizeleft) { - string::size_type contentSize = sizeleft <= framesize ? sizeleft : framesize-overhead; - channel.send(new MessageAppendBody(channel.getVersion(), ref->getId(), - string(content, contentStart, contentSize))); - sizeleft -= contentSize; - contentStart += contentSize; - } - } - } - - // The transfer - if ( transfer->size()<=framesize ) { - channel.send( - new MessageTransferBody(channel.getVersion(), - transfer->getTicket(), - consumerTag, - getRedelivered(), - transfer->getImmediate(), - transfer->getTtl(), - transfer->getPriority(), - transfer->getTimestamp(), - transfer->getDeliveryMode(), - transfer->getExpiration(), - getExchange(), - getRoutingKey(), - transfer->getMessageId(), - transfer->getCorrelationId(), - transfer->getReplyTo(), - transfer->getContentType(), - transfer->getContentEncoding(), - transfer->getUserId(), - transfer->getAppId(), - transfer->getTransactionId(), - transfer->getSecurityToken(), - transfer->getApplicationHeaders(), - body, - transfer->getMandatory())); - } else { - // Thing to do here is to construct a simple reference message then deliver that instead - // fragmentation will be taken care of in the delivery if necessary; - string content = body.getValue(); - string refname = "dummy"; - TransferPtr newTransfer( - new MessageTransferBody(channel.getVersion(), - transfer->getTicket(), - consumerTag, - getRedelivered(), - transfer->getImmediate(), - transfer->getTtl(), - transfer->getPriority(), - transfer->getTimestamp(), - transfer->getDeliveryMode(), - transfer->getExpiration(), - getExchange(), - getRoutingKey(), - transfer->getMessageId(), - transfer->getCorrelationId(), - transfer->getReplyTo(), - transfer->getContentType(), - transfer->getContentEncoding(), - transfer->getUserId(), - transfer->getAppId(), - transfer->getTransactionId(), - transfer->getSecurityToken(), - transfer->getApplicationHeaders(), - framing::Content(REFERENCE, refname), - transfer->getMandatory())); - ReferencePtr newRef(new Reference(refname)); - Reference::AppendPtr newAppend(new MessageAppendBody(channel.getVersion(), refname, content)); - newRef->append(newAppend); - MessageMessage newMsg(const_cast(getPublisher()), 0, newTransfer, newRef); - newMsg.transferMessage(channel, consumerTag, framesize); - return; - } - // Close any reference data - if (ref) - channel.send(new MessageCloseBody(channel.getVersion(), ref->getId())); -} - -void MessageMessage::deliver( - framing::ChannelAdapter& channel, - const std::string& consumerTag, - uint64_t /*deliveryTag*/, - uint32_t framesize) -{ - transferMessage(channel, consumerTag, framesize); -} - -void MessageMessage::sendGetOk( - const framing::MethodContext& context, - const std::string& destination, - uint32_t /*messageCount*/, - uint64_t /*deliveryTag*/, - uint32_t framesize) -{ - framing::ChannelAdapter* channel = context.channel; - transferMessage(*channel, destination, framesize); -} - -bool MessageMessage::isComplete() -{ - return true; -} - -uint64_t MessageMessage::contentSize() const -{ - if (transfer->getBody().isInline()) - return transfer->getBody().getValue().size(); - else { - assert(getReference()); - return getReference()->getSize(); - } -} - -qpid::framing::BasicHeaderProperties* MessageMessage::getHeaderProperties() -{ - return 0; // FIXME aconway 2007-02-05: -} - -const FieldTable& MessageMessage::getApplicationHeaders() -{ - return transfer->getApplicationHeaders(); -} -bool MessageMessage::isPersistent() -{ - return transfer->getDeliveryMode() == PERSISTENT; -} - -uint32_t MessageMessage::encodedSize() const -{ - return encodedHeaderSize() + encodedContentSize(); -} - -uint32_t MessageMessage::encodedHeaderSize() const -{ - return RecoveryManagerImpl::encodedMessageTypeSize() + transfer->size() - transfer->baseSize(); -} - -uint32_t MessageMessage::encodedContentSize() const -{ - return 0; -} - -uint64_t MessageMessage::expectedContentSize() -{ - return 0; -} - -void MessageMessage::encode(Buffer& buffer) const -{ - encodeHeader(buffer); -} - -void MessageMessage::encodeHeader(Buffer& buffer) const -{ - RecoveryManagerImpl::encodeMessageType(*this, buffer); - if (transfer->getBody().isInline()) { - transfer->encodeContent(buffer); - } else { - assert(getReference()); - string data; - const Reference::Appends& appends = getReference()->getAppends(); - for(Reference::Appends::const_iterator a = appends.begin(); a != appends.end(); ++a) { - data += (*a)->getBytes(); - } - framing::Content body(INLINE, data); - std::auto_ptr copy(copyTransfer(transfer->version, transfer->getDestination(), body)); - copy->encodeContent(buffer); - } -} - -void MessageMessage::decodeHeader(Buffer& buffer) -{ - //don't care about the type here, but want encode/decode to be symmetric - RecoveryManagerImpl::decodeMessageType(buffer); - - transfer->decodeContent(buffer); -} - -void MessageMessage::decodeContent(Buffer& /*buffer*/, uint32_t /*chunkSize*/) -{ -} - - -MessageTransferBody* MessageMessage::copyTransfer(const ProtocolVersion& version, - const string& destination, - const framing::Content& body) const -{ - return new MessageTransferBody(version, - transfer->getTicket(), - destination, - getRedelivered(), - transfer->getImmediate(), - transfer->getTtl(), - transfer->getPriority(), - transfer->getTimestamp(), - transfer->getDeliveryMode(), - transfer->getExpiration(), - getExchange(), - getRoutingKey(), - transfer->getMessageId(), - transfer->getCorrelationId(), - transfer->getReplyTo(), - transfer->getContentType(), - transfer->getContentEncoding(), - transfer->getUserId(), - transfer->getAppId(), - transfer->getTransactionId(), - transfer->getSecurityToken(), - transfer->getApplicationHeaders(), - body, - transfer->getMandatory()); - -} - -MessageMessage::ReferencePtr MessageMessage::getReference() const { - return reference; -} - - -}} // namespace qpid::broker - diff --git a/cpp/src/broker/BrokerMessageMessage.h b/cpp/src/broker/BrokerMessageMessage.h deleted file mode 100644 index ae9fc9c2c0..0000000000 --- a/cpp/src/broker/BrokerMessageMessage.h +++ /dev/null @@ -1,102 +0,0 @@ -#ifndef _broker_BrokerMessageMessage_h -#define _broker_BrokerMessageMessage_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 "BrokerMessageBase.h" -#include "../gen/MessageTransferBody.h" -#include "../framing/amqp_types.h" -#include -#include - -namespace qpid { - -namespace framing { -class MessageTransferBody; -} - -namespace broker { -class ConnectionToken; -class Reference; - -class MessageMessage: public Message{ - public: - typedef boost::shared_ptr shared_ptr; - typedef boost::shared_ptr TransferPtr; - typedef boost::shared_ptr ReferencePtr; - - MessageMessage(ConnectionToken* publisher, framing::RequestId, TransferPtr transfer); - MessageMessage(ConnectionToken* publisher, framing::RequestId, TransferPtr transfer, ReferencePtr reference); - MessageMessage(); - - // Default destructor okay - - framing::RequestId getRequestId() const {return requestId; } - TransferPtr getTransfer() const { return transfer; } - ReferencePtr getReference() const ; - - void deliver(framing::ChannelAdapter& channel, - const std::string& consumerTag, - uint64_t deliveryTag, - uint32_t framesize); - - void sendGetOk(const framing::MethodContext& context, - const std::string& destination, - uint32_t messageCount, - uint64_t deliveryTag, - uint32_t framesize); - - bool isComplete(); - - uint64_t contentSize() const; - framing::BasicHeaderProperties* getHeaderProperties(); - const framing::FieldTable& getApplicationHeaders(); - bool isPersistent(); - - void encode(framing::Buffer& buffer) const; - void encodeHeader(framing::Buffer& buffer) const; - uint32_t encodedSize() const; - uint32_t encodedHeaderSize() const; - uint32_t encodedContentSize() const; - uint64_t expectedContentSize(); - void decodeHeader(framing::Buffer& buffer); - void decodeContent(framing::Buffer& buffer, uint32_t contentChunkSize = 0); - - private: - void transferMessage( - framing::ChannelAdapter& channel, - const std::string& consumerTag, - uint32_t framesize); - - framing::MessageTransferBody* copyTransfer( - const framing::ProtocolVersion& version, - const std::string& destination, - const framing::Content& body) const; - - framing::RequestId requestId; - const TransferPtr transfer; - const boost::shared_ptr reference; -}; - -}} - - -#endif /*!_broker_BrokerMessage_h*/ diff --git a/cpp/src/broker/BrokerQueue.cpp b/cpp/src/broker/BrokerQueue.cpp deleted file mode 100644 index 5d34a52fcc..0000000000 --- a/cpp/src/broker/BrokerQueue.cpp +++ /dev/null @@ -1,284 +0,0 @@ -/* - * - * 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 - -#include "BrokerQueue.h" -#include "MessageStore.h" -#include "../sys/Monitor.h" -#include "../sys/Time.h" -#include -#include "QueueRegistry.h" - -using namespace qpid::broker; -using namespace qpid::sys; -using namespace qpid::framing; -using boost::format; - -Queue::Queue(const string& _name, uint32_t _autodelete, - MessageStore* const _store, - const ConnectionToken* const _owner) : - - name(_name), - autodelete(_autodelete), - store(_store), - owner(_owner), - queueing(false), - dispatching(false), - next(0), - lastUsed(0), - exclusive(0), - persistenceId(0) -{ - if(autodelete) lastUsed = now()/TIME_MSEC; -} - -Queue::~Queue(){} - -void Queue::deliver(Message::shared_ptr& msg){ - enqueue(0, msg); - process(msg); -} - -void Queue::recover(Message::shared_ptr& msg){ - push(msg); - if (store && msg->expectedContentSize() != msg->encodedContentSize()) { - //content has not been loaded, need to ensure that lazy loading mode is set: - //TODO: find a nicer way to do this - msg->releaseContent(store); - } -} - -void Queue::process(Message::shared_ptr& msg){ - Mutex::ScopedLock locker(lock); - if(queueing || !dispatch(msg)){ - push(msg); - } -} - -bool Queue::dispatch(Message::shared_ptr& msg){ - if(consumers.empty()){ - return false; - }else if(exclusive){ - if(!exclusive->deliver(msg)){ - std::cout << "WARNING: Dropping undeliverable message from queue with exclusive consumer." << std::endl; - } - return true; - }else{ - //deliver to next consumer - next = next % consumers.size(); - Consumer* c = consumers[next]; - int start = next; - while(c){ - next++; - if(c->deliver(msg)) return true; - - next = next % consumers.size(); - c = next == start ? 0 : consumers[next]; - } - return false; - } -} - -bool Queue::startDispatching(){ - Mutex::ScopedLock locker(lock); - if(queueing && !dispatching){ - dispatching = true; - return true; - }else{ - return false; - } -} - -void Queue::dispatch(){ - bool proceed = startDispatching(); - while(proceed){ - Mutex::ScopedLock locker(lock); - if(!messages.empty() && dispatch(messages.front())){ - pop(); - }else{ - dispatching = false; - proceed = false; - queueing = !messages.empty(); - } - } -} - -void Queue::consume(Consumer* c, bool requestExclusive){ - Mutex::ScopedLock locker(lock); - if(exclusive) - throw ChannelException( - 403, format("Queue '%s' has an exclusive consumer." - " No more consumers allowed.") % getName()); - if(requestExclusive) { - if(!consumers.empty()) - throw ChannelException( - 403, format("Queue '%s' already has conumers." - "Exclusive access denied.") %getName()); - exclusive = c; - } - if(autodelete && consumers.empty()) lastUsed = 0; - consumers.push_back(c); -} - -void Queue::cancel(Consumer* c){ - Mutex::ScopedLock locker(lock); - Consumers::iterator i = std::find(consumers.begin(), consumers.end(), c); - if (i != consumers.end()) - consumers.erase(i); - if(autodelete && consumers.empty()) lastUsed = now()*TIME_MSEC; - if(exclusive == c) exclusive = 0; -} - -Message::shared_ptr Queue::dequeue(){ - Mutex::ScopedLock locker(lock); - Message::shared_ptr msg; - if(!messages.empty()){ - msg = messages.front(); - pop(); - } - return msg; -} - -uint32_t Queue::purge(){ - Mutex::ScopedLock locker(lock); - int count = messages.size(); - while(!messages.empty()) pop(); - return count; -} - -void Queue::pop(){ - if (policy.get()) policy->dequeued(messages.front()->contentSize()); - messages.pop(); -} - -void Queue::push(Message::shared_ptr& msg){ - queueing = true; - messages.push(msg); - if (policy.get()) { - policy->enqueued(msg->contentSize()); - if (policy->limitExceeded()) { - msg->releaseContent(store); - } - } -} - -uint32_t Queue::getMessageCount() const{ - Mutex::ScopedLock locker(lock); - return messages.size(); -} - -uint32_t Queue::getConsumerCount() const{ - Mutex::ScopedLock locker(lock); - return consumers.size(); -} - -bool Queue::canAutoDelete() const{ - Mutex::ScopedLock locker(lock); - return lastUsed && (now()*TIME_MSEC - lastUsed > autodelete); -} - -void Queue::enqueue(TransactionContext* ctxt, Message::shared_ptr& msg) -{ - if (msg->isPersistent() && store) { - store->enqueue(ctxt, *msg.get(), *this); - } -} - -void Queue::dequeue(TransactionContext* ctxt, Message::shared_ptr& msg) -{ - if (msg->isPersistent() && store) { - store->dequeue(ctxt, *msg.get(), *this); - } -} - -namespace -{ - const std::string qpidMaxSize("qpid.max_size"); - const std::string qpidMaxCount("qpid.max_count"); -} - -void Queue::create(const FieldTable& _settings) -{ - settings = _settings; - //TODO: hold onto settings and persist them as part of encode - // in fact settings should be passed in on construction - if (store) { - store->create(*this); - } - configure(_settings); -} - -void Queue::configure(const FieldTable& _settings) -{ - std::auto_ptr _policy(new QueuePolicy(_settings)); - if (_policy->getMaxCount() || _policy->getMaxSize()) - setPolicy(_policy); -} - -void Queue::destroy() -{ - if (store) { - store->destroy(*this); - } -} - -void Queue::setPolicy(std::auto_ptr _policy) -{ - policy = _policy; -} - -const QueuePolicy* const Queue::getPolicy() -{ - return policy.get(); -} - -uint64_t Queue::getPersistenceId() const -{ - return persistenceId; -} - -void Queue::setPersistenceId(uint64_t _persistenceId) const -{ - persistenceId = _persistenceId; -} - -void Queue::encode(framing::Buffer& buffer) const -{ - buffer.putShortString(name); - buffer.putFieldTable(settings); -} - -uint32_t Queue::encodedSize() const -{ - return name.size() + 1/*short string size octet*/ + settings.size(); -} - -Queue::shared_ptr Queue::decode(QueueRegistry& queues, framing::Buffer& buffer) -{ - string name; - buffer.getShortString(name); - std::pair result = queues.declare(name, true); - buffer.getFieldTable(result.first->settings); - result.first->configure(result.first->settings); - return result.first; -} - diff --git a/cpp/src/broker/BrokerQueue.h b/cpp/src/broker/BrokerQueue.h deleted file mode 100644 index 808a523869..0000000000 --- a/cpp/src/broker/BrokerQueue.h +++ /dev/null @@ -1,152 +0,0 @@ -#ifndef _broker_BrokerQueue_h -#define _broker_BrokerQueue_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 -#include -#include -#include -#include "../framing/amqp_types.h" -#include "ConnectionToken.h" -#include "Consumer.h" -#include "BrokerMessage.h" -#include "../framing/FieldTable.h" -#include "../sys/Monitor.h" -#include "PersistableQueue.h" -#include "QueuePolicy.h" - -// TODO aconway 2007-02-06: Use auto_ptr and boost::ptr_vector to -// enforce ownership of Consumers. - -namespace qpid { - namespace broker { - class MessageStore; - class QueueRegistry; - - /** - * Thrown when exclusive access would be violated. - */ - using std::string; - - /** - * The brokers representation of an amqp queue. Messages are - * delivered to a queue from where they can be dispatched to - * registered consumers or be stored until dequeued or until one - * or more consumers registers. - */ - class Queue : public PersistableQueue{ - typedef std::vector Consumers; - typedef std::queue Messages; - - const string name; - const uint32_t autodelete; - MessageStore* const store; - const ConnectionToken* const owner; - Consumers consumers; - Messages messages; - bool queueing; - bool dispatching; - int next; - mutable qpid::sys::Mutex lock; - int64_t lastUsed; - Consumer* exclusive; - mutable uint64_t persistenceId; - framing::FieldTable settings; - std::auto_ptr policy; - - void pop(); - void push(Message::shared_ptr& msg); - bool startDispatching(); - bool dispatch(Message::shared_ptr& msg); - void setPolicy(std::auto_ptr policy); - - public: - - typedef boost::shared_ptr shared_ptr; - - typedef std::vector vector; - - Queue(const string& name, uint32_t autodelete = 0, - MessageStore* const store = 0, - const ConnectionToken* const owner = 0); - ~Queue(); - - void create(const qpid::framing::FieldTable& settings); - void configure(const qpid::framing::FieldTable& settings); - void destroy(); - /** - * Delivers a message to the queue. Will record it as - * enqueued if persistent then process it. - */ - void deliver(Message::shared_ptr& msg); - /** - * Dispatches the messages immediately to a consumer if - * one is available or stores it for later if not. - */ - void process(Message::shared_ptr& msg); - /** - * Used during recovery to add stored messages back to the queue - */ - void recover(Message::shared_ptr& msg); - /** - * Dispatch any queued messages providing there are - * consumers for them. Only one thread can be dispatching - * at any time, but this method (rather than the caller) - * is responsible for ensuring that. - */ - void dispatch(); - void consume(Consumer* c, bool exclusive = false); - void cancel(Consumer* c); - uint32_t purge(); - uint32_t getMessageCount() const; - uint32_t getConsumerCount() const; - inline const string& getName() const { return name; } - inline const bool isExclusiveOwner(const ConnectionToken* const o) const { return o == owner; } - inline bool hasExclusiveConsumer() const { return exclusive; } - - bool canAutoDelete() const; - - void enqueue(TransactionContext* ctxt, Message::shared_ptr& msg); - /** - * dequeue from store (only done once messages is acknowledged) - */ - void dequeue(TransactionContext* ctxt, Message::shared_ptr& msg); - /** - * dequeues from memory only - */ - Message::shared_ptr dequeue(); - - const QueuePolicy* const getPolicy(); - - //PersistableQueue support: - uint64_t getPersistenceId() const; - void setPersistenceId(uint64_t persistenceId) const; - void encode(framing::Buffer& buffer) const; - uint32_t encodedSize() const; - - static Queue::shared_ptr decode(QueueRegistry& queues, framing::Buffer& buffer); - }; - } -} - - -#endif /*!_broker_BrokerQueue_h*/ diff --git a/cpp/src/broker/BrokerSingleton.cpp b/cpp/src/broker/BrokerSingleton.cpp deleted file mode 100644 index 4571764850..0000000000 --- a/cpp/src/broker/BrokerSingleton.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* - * - * 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 "BrokerSingleton.h" - -namespace qpid { -namespace broker { - -BrokerSingleton::BrokerSingleton() { - if (broker.get() == 0) - broker = Broker::create(); - Broker::shared_ptr::operator=(broker); -} - -BrokerSingleton::~BrokerSingleton() { - broker->shutdown(); -} - -Broker::shared_ptr BrokerSingleton::broker; - -}} // namespace qpid::broker diff --git a/cpp/src/broker/BrokerSingleton.h b/cpp/src/broker/BrokerSingleton.h deleted file mode 100644 index 139e02a5fd..0000000000 --- a/cpp/src/broker/BrokerSingleton.h +++ /dev/null @@ -1,52 +0,0 @@ -#ifndef _broker_BrokerSingleton_h -#define _broker_BrokerSingleton_h - -/* - * - * 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 "Broker.h" - -namespace qpid { -namespace broker { - -/** - * BrokerSingleton is a smart pointer to a process-wide singleton broker - * started on an os-chosen port. The broker starts the first time - * an instance of BrokerSingleton is created and runs untill the process exits. - * - * Useful for unit tests that want to share a broker between multiple - * tests to reduce overhead of starting/stopping a broker for every test. - * - * Tests that need a new broker can call Broker::create directly. - * - * THREAD UNSAFE. - */ -class BrokerSingleton : public Broker::shared_ptr -{ - public: - BrokerSingleton(); - ~BrokerSingleton(); - private: - static Broker::shared_ptr broker; -}; - -}} // namespace qpid::broker - - - -#endif /*!_broker_BrokerSingleton_h*/ diff --git a/cpp/src/broker/CompletionHandler.h b/cpp/src/broker/CompletionHandler.h deleted file mode 100644 index 9d51656282..0000000000 --- a/cpp/src/broker/CompletionHandler.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef _broker_CompletionHandler_h -#define _broker_CompletionHandler_h - -/* - * - * 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. - * - */ - -namespace qpid { -namespace broker { - -/** - * Callback interface to handle completion of a message. - */ -class CompletionHandler -{ - public: - virtual ~CompletionHandler(){} - virtual void complete(Message::shared_ptr) = 0; -}; - -}} // namespace qpid::broker - - - -#endif /*!_broker_CompletionHandler_h*/ diff --git a/cpp/src/broker/Configuration.cpp b/cpp/src/broker/Configuration.cpp deleted file mode 100644 index 3449f6f179..0000000000 --- a/cpp/src/broker/Configuration.cpp +++ /dev/null @@ -1,252 +0,0 @@ -/* - * - * 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 "Configuration.h" -#include -#include "config.h" - -using namespace qpid::broker; -using namespace std; - -Configuration::Configuration() : - daemon('d', "daemon", "Run as system daemon, detached from terminal.", false), - trace('t', "trace", "Print incoming & outgoing frames to the console", false), - port('p', "port", "Set the port to listen on (default=5672)", 5672), - workerThreads("worker-threads", "Set the number of worker threads to use (default=5).", 5), - maxConnections("max-connections", "Set the maximum number of connections the broker can accept (default=500).", 500), - connectionBacklog("connection-backlog", "Set the connection backlog for the servers socket (default=10)", 10), - store('s', "store", "Set the message store module to use (default='' which implies no store)", ""), - stagingThreshold("staging-threshold", "Set the message size threshold above which messages will be written to disk as they arrive (default=5,000,000)", 5000000), - help("help", "Print usage information", false), - version("version", "Print version information", false) -{ - options.push_back(&daemon); - options.push_back(&trace); - options.push_back(&port); - options.push_back(&workerThreads); - options.push_back(&maxConnections); - options.push_back(&connectionBacklog); - options.push_back(&store); - options.push_back(&stagingThreshold); - options.push_back(&help); - options.push_back(&version); -} - -Configuration::~Configuration(){} - -void Configuration::parse(char const *progName, int argc, char** argv){ - programName = progName; - int position = 1; - while(position < argc){ - bool matched(false); - for(op_iterator i = options.begin(); i < options.end() && !matched; i++){ - matched = (*i)->parse(position, argv, argc); - } - if(!matched) { - throw BadOptionException( - std::string("Unrecognised option: ")+argv[position]); - } - } -} - -void Configuration::usage(){ - std::cout << "Usage: " << programName << " [OPTION]..." << std::endl - << "Start the Qpid AMQP broker daemon." << std::endl << std::endl - << "Options:" << std::endl; - for(op_iterator i = options.begin(); i < options.end(); i++){ - (*i)->print(std::cout); - } - - std::cout << std::endl << "Report bugs to <" << PACKAGE_BUGREPORT << ">." - << std::endl; -} - -bool Configuration::isHelp() const { - return help.getValue(); -} - -bool Configuration::isVersion() const { - return version.getValue(); -} - -bool Configuration::isDaemon() const { - return daemon.getValue(); -} - -bool Configuration::isTrace() const { - return trace.getValue(); -} - -int Configuration::getPort() const { - return port.getValue(); -} - -int Configuration::getWorkerThreads() const { - return workerThreads.getValue(); -} - -int Configuration::getMaxConnections() const { - return maxConnections.getValue(); -} - -int Configuration::getConnectionBacklog() const { - return connectionBacklog.getValue(); -} - -const std::string& Configuration::getStore() const { - return store.getValue(); -} - -long Configuration::getStagingThreshold() const { - return stagingThreshold.getValue(); -} - - -Configuration::Option::Option(const char _flag, const string& _name, const string& _desc) : - flag(string("-") + _flag), name("--" +_name), desc(_desc) {} - -Configuration::Option::Option(const string& _name, const string& _desc) : - flag(""), name("--" + _name), desc(_desc) {} - -Configuration::Option::~Option(){} - -bool Configuration::Option::match(const string& arg){ - return flag == arg || name == arg; -} - -bool Configuration::Option::parse(int& i, char** argv, int argc){ - const string arg(argv[i]); - if(match(arg)){ - if(needsValue()){ - if(++i < argc) setValue(argv[i]); - else throw ParseException("Argument " + arg + " requires a value!"); - }else{ - setValue(""); - } - i++; - return true; - }else{ - return false; - } -} - -void Configuration::Option::print(ostream& out) const { - out << " "; - if(flag.length() > 0){ - out << flag << ", "; - } else { - out << " "; - } - out << name; - if(needsValue()) out << " "; - out << std::endl; - out << " " << desc << std::endl; -} - - -// String Option: - -Configuration::StringOption::StringOption(const char _flag, const string& _name, const string& _desc, const string _value) : - Option(_flag,_name,_desc), defaultValue(_value), value(_value) {} - -Configuration::StringOption::StringOption(const string& _name, const string& _desc, const string _value) : - Option(_name,_desc), defaultValue(_value), value(_value) {} - -Configuration::StringOption::~StringOption(){} - -const string& Configuration::StringOption::getValue() const { - return value; -} - -bool Configuration::StringOption::needsValue() const { - return true; -} - -void Configuration::StringOption::setValue(const std::string& _value){ - value = _value; -} - -// Int Option: - -Configuration::IntOption::IntOption(const char _flag, const string& _name, const string& _desc, const int _value) : - Option(_flag,_name,_desc), defaultValue(_value), value(_value) {} - -Configuration::IntOption::IntOption(const string& _name, const string& _desc, const int _value) : - Option(_name,_desc), defaultValue(_value), value(_value) {} - -Configuration::IntOption::~IntOption(){} - -int Configuration::IntOption::getValue() const { - return value; -} - -bool Configuration::IntOption::needsValue() const { - return true; -} - -void Configuration::IntOption::setValue(const std::string& _value){ - value = atoi(_value.c_str()); -} - -// Long Option: - -Configuration::LongOption::LongOption(const char _flag, const string& _name, const string& _desc, const long _value) : - Option(_flag,_name,_desc), defaultValue(_value), value(_value) {} - -Configuration::LongOption::LongOption(const string& _name, const string& _desc, const long _value) : - Option(_name,_desc), defaultValue(_value), value(_value) {} - -Configuration::LongOption::~LongOption(){} - -long Configuration::LongOption::getValue() const { - return value; -} - -bool Configuration::LongOption::needsValue() const { - return true; -} - -void Configuration::LongOption::setValue(const std::string& _value){ - value = atol(_value.c_str()); -} - -// Bool Option: - -Configuration::BoolOption::BoolOption(const char _flag, const string& _name, const string& _desc, const bool _value) : - Option(_flag,_name,_desc), defaultValue(_value), value(_value) {} - -Configuration::BoolOption::BoolOption(const string& _name, const string& _desc, const bool _value) : - Option(_name,_desc), defaultValue(_value), value(_value) {} - -Configuration::BoolOption::~BoolOption(){} - -bool Configuration::BoolOption::getValue() const { - return value; -} - -bool Configuration::BoolOption::needsValue() const { - return false; -} - -void Configuration::BoolOption::setValue(const std::string& /*not required*/){ - //BoolOptions have no value. The fact that the option is specified - //implies the value is true. - value = true; -} diff --git a/cpp/src/broker/Configuration.h b/cpp/src/broker/Configuration.h deleted file mode 100644 index d93bf5c45b..0000000000 --- a/cpp/src/broker/Configuration.h +++ /dev/null @@ -1,171 +0,0 @@ -/* - * - * 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 _Configuration_ -#define _Configuration_ - -#include -#include -#include -#include "../Exception.h" - -namespace qpid { -namespace broker { -class Configuration{ - - class Option { - const std::string flag; - const std::string name; - const std::string desc; - - bool match(const std::string& arg); - - protected: - virtual bool needsValue() const = 0; - virtual void setValue(const std::string& value) = 0; - - public: - Option(const char flag, const std::string& name, const std::string& desc); - Option(const std::string& name, const std::string& desc); - virtual ~Option(); - - bool parse(int& i, char** argv, int argc); - void print(std::ostream& out) const; - }; - - class IntOption : public Option{ - const int defaultValue; - int value; - public: - IntOption(char flag, const std::string& name, const std::string& desc, const int value = 0); - IntOption(const std::string& name, const std::string& desc, const int value = 0); - virtual ~IntOption(); - - int getValue() const; - virtual bool needsValue() const; - virtual void setValue(const std::string& value); - virtual void setValue(int _value) { value = _value; } - }; - - class LongOption : public Option{ - const long defaultValue; - int value; - public: - LongOption(char flag, const std::string& name, const std::string& desc, const long value = 0); - LongOption(const std::string& name, const std::string& desc, const long value = 0); - virtual ~LongOption(); - - long getValue() const; - virtual bool needsValue() const; - virtual void setValue(const std::string& value); - virtual void setValue(int _value) { value = _value; } - }; - - class StringOption : public Option{ - const std::string defaultValue; - std::string value; - public: - StringOption(char flag, const std::string& name, const std::string& desc, const std::string value = ""); - StringOption(const std::string& name, const std::string& desc, const std::string value = ""); - virtual ~StringOption(); - - const std::string& getValue() const; - virtual bool needsValue() const; - virtual void setValue(const std::string& value); - }; - - class BoolOption : public Option{ - const bool defaultValue; - bool value; - public: - BoolOption(char flag, const std::string& name, const std::string& desc, const bool value = 0); - BoolOption(const std::string& name, const std::string& desc, const bool value = 0); - virtual ~BoolOption(); - - bool getValue() const; - virtual bool needsValue() const; - virtual void setValue(const std::string& value); - virtual void setValue(bool _value) { value = _value; } - }; - - BoolOption daemon; - BoolOption trace; - IntOption port; - IntOption workerThreads; - IntOption maxConnections; - IntOption connectionBacklog; - StringOption store; - LongOption stagingThreshold; - BoolOption help; - BoolOption version; - char const *programName; - - typedef std::vector::iterator op_iterator; - std::vector options; - - public: - - struct BadOptionException : public Exception { - template - BadOptionException(const T& msg) : Exception(msg) {} - }; - - - class ParseException : public Exception { - public: - template - ParseException(const T& msg) : Exception(msg) {} - }; - - - Configuration(); - ~Configuration(); - - void parse(char const*, int argc, char** argv); - - bool isHelp() const; - bool isVersion() const; - bool isDaemon() const; - bool isTrace() const; - int getPort() const; - int getWorkerThreads() const; - int getMaxConnections() const; - int getConnectionBacklog() const; - const std::string& getStore() const; - long getStagingThreshold() const; - - void setHelp(bool b) { help.setValue(b); } - void setVersion(bool b) { version.setValue(b); } - void setDaemon(bool b) { daemon.setValue(b); } - void setTrace(bool b) { trace.setValue(b); } - void setPort(int i) { port.setValue(i); } - void setWorkerThreads(int i) { workerThreads.setValue(i); } - void setMaxConnections(int i) { maxConnections.setValue(i); } - void setConnectionBacklog(int i) { connectionBacklog.setValue(i); } - void setStore(const std::string& s) { store.setValue(s); } - void setStagingThreshold(long l) { stagingThreshold.setValue(l); } - - void usage(); -}; -} -} - - -#endif diff --git a/cpp/src/broker/Connection.cpp b/cpp/src/broker/Connection.cpp deleted file mode 100644 index ab1c2ac128..0000000000 --- a/cpp/src/broker/Connection.cpp +++ /dev/null @@ -1,128 +0,0 @@ -/* - * - * 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 -#include - -#include "Connection.h" -#include "BrokerChannel.h" -#include "../gen/AMQP_ClientProxy.h" -#include "BrokerAdapter.h" - -using namespace boost; -using namespace qpid::sys; -using namespace qpid::framing; -using namespace qpid::sys; - -namespace qpid { -namespace broker { - -Connection::Connection(ConnectionOutputHandler* out_, Broker& broker_) : - broker(broker_), - out(out_), - framemax(65536), - heartbeat(0), - client(0), - timeout(broker.getTimeout()), - stagingThreshold(broker.getStagingThreshold()) -{} - - -Queue::shared_ptr Connection::getQueue(const string& name, uint16_t channel){ - Queue::shared_ptr queue; - if (name.empty()) { - queue = getChannel(channel).getDefaultQueue(); - if (!queue) throw ConnectionException( 530, "Queue must be specified or previously declared" ); - } else { - queue = broker.getQueues().find(name); - if (queue == 0) { - throw ChannelException( 404, "Queue not found: " + name); - } - } - return queue; -} - - -Exchange::shared_ptr Connection::findExchange(const string& name){ - return broker.getExchanges().get(name); -} - - -void Connection::received(framing::AMQFrame* frame){ - getChannel(frame->getChannel()).handleBody(frame->getBody()); -} - -void Connection::close( - ReplyCode code, const string& text, ClassId classId, MethodId methodId) -{ - client->close(code, text, classId, methodId); - getOutput().close(); -} - -void Connection::initiated(const framing::ProtocolInitiation& header) { - version = ProtocolVersion(header.getMajor(), header.getMinor()); - FieldTable properties; - string mechanisms("PLAIN"); - string locales("en_US"); - getChannel(0).init(0, *out, getVersion()); - client = &getChannel(0).getAdatper().getProxy().getConnection(); - client->start( - header.getMajor(), header.getMinor(), - properties, mechanisms, locales); -} - -void Connection::idleOut(){} - -void Connection::idleIn(){} - -void Connection::closed(){ - try { - while (!exclusiveQueues.empty()) { - broker.getQueues().destroy(exclusiveQueues.front()->getName()); - exclusiveQueues.erase(exclusiveQueues.begin()); - } - } catch(std::exception& e) { - std::cout << "Caught unhandled exception while closing session: " << - e.what() << std::endl; - assert(0); - } -} - -void Connection::closeChannel(uint16_t id) { - ChannelMap::iterator i = channels.find(id); - if (i != channels.end()) - i->close(); -} - - -Channel& Connection::getChannel(ChannelId id) { - ChannelMap::iterator i = channels.find(id); - if (i == channels.end()) { - i = channels.insert( - id, new Channel( - *this, id, framemax, broker.getQueues().getStore(), - broker.getStagingThreshold())).first; - } - return *i; -} - - -}} - diff --git a/cpp/src/broker/Connection.h b/cpp/src/broker/Connection.h deleted file mode 100644 index ffda0425cc..0000000000 --- a/cpp/src/broker/Connection.h +++ /dev/null @@ -1,108 +0,0 @@ -/* - * - * 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 _Connection_ -#define _Connection_ - -#include -#include - -#include - -#include "../framing/AMQFrame.h" -#include "../gen/AMQP_ServerOperations.h" -#include "../gen/AMQP_ClientProxy.h" -#include "../sys/ConnectionOutputHandler.h" -#include "../sys/ConnectionInputHandler.h" -#include "../sys/TimeoutHandler.h" -#include "../framing/ProtocolVersion.h" -#include "Broker.h" -#include "../Exception.h" -#include "BrokerChannel.h" - -namespace qpid { -namespace broker { - -class Channel; - -class Connection : public sys::ConnectionInputHandler, - public ConnectionToken -{ - public: - Connection(sys::ConnectionOutputHandler* out, Broker& broker); - - /** Get a channel. Create if it does not already exist */ - Channel& getChannel(framing::ChannelId channel); - - /** Close a channel */ - void closeChannel(framing::ChannelId channel); - - /** Close the connection */ - void close(framing::ReplyCode code, const string& text, framing::ClassId classId, framing::MethodId methodId); - - sys::ConnectionOutputHandler& getOutput() const { return *out; } - framing::ProtocolVersion getVersion() const { return version; } - - uint32_t getFrameMax() const { return framemax; } - uint16_t getHeartbeat() const { return heartbeat; } - uint32_t getTimeout() const { return timeout; } - uint64_t getStagingThreshold() const { return stagingThreshold; } - - void setFrameMax(uint32_t fm) { framemax = fm; } - void setHeartbeat(uint16_t hb) { heartbeat = hb; } - - /** - * Get named queue, never returns 0. - * @return: named queue or default queue for channel if name="" - * @exception: ChannelException if no queue of that name is found. - * @exception: ConnectionException if name="" and channel has no default. - */ - Queue::shared_ptr getQueue(const string& name, uint16_t channel); - - Broker& broker; - std::vector exclusiveQueues; - - // ConnectionInputHandler methods - void received(framing::AMQFrame* frame); - void initiated(const framing::ProtocolInitiation& header); - void idleOut(); - void idleIn(); - void closed(); - - private: - typedef boost::ptr_map ChannelMap; - - typedef std::vector::iterator queue_iterator; - Exchange::shared_ptr findExchange(const string& name); - - framing::ProtocolVersion version; - ChannelMap channels; - sys::ConnectionOutputHandler* out; - uint32_t framemax; - uint16_t heartbeat; - framing::AMQP_ClientProxy::Connection* client; - const uint32_t timeout; //timeout for auto-deleted queues (in ms) - const uint64_t stagingThreshold; - -}; - -}} - -#endif diff --git a/cpp/src/broker/ConnectionFactory.cpp b/cpp/src/broker/ConnectionFactory.cpp deleted file mode 100644 index 5372bfe377..0000000000 --- a/cpp/src/broker/ConnectionFactory.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * - * 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 "ConnectionFactory.h" -#include "Connection.h" - -namespace qpid { -namespace broker { - - -ConnectionFactory::ConnectionFactory(Broker& b) : broker(b) -{} - - -ConnectionFactory::~ConnectionFactory() -{ - broker.getCleaner().stop(); -} - -qpid::sys::ConnectionInputHandler* -ConnectionFactory::create(qpid::sys::ConnectionOutputHandler* out) -{ - return new Connection(out, broker); -} - -}} // namespace qpid::broker diff --git a/cpp/src/broker/ConnectionFactory.h b/cpp/src/broker/ConnectionFactory.h deleted file mode 100644 index 54fabb8089..0000000000 --- a/cpp/src/broker/ConnectionFactory.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * - * 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 _ConnectionFactory_ -#define _ConnectionFactory_ - -#include "../sys/ConnectionInputHandlerFactory.h" - -namespace qpid { -namespace broker { -class Broker; - -class ConnectionFactory : public qpid::sys::ConnectionInputHandlerFactory -{ - public: - ConnectionFactory(Broker& b); - - virtual qpid::sys::ConnectionInputHandler* create( - qpid::sys::ConnectionOutputHandler* ctxt); - - virtual ~ConnectionFactory(); - - private: - Broker& broker; -}; - -}} - - -#endif diff --git a/cpp/src/broker/ConnectionToken.h b/cpp/src/broker/ConnectionToken.h deleted file mode 100644 index 7e7f813d0e..0000000000 --- a/cpp/src/broker/ConnectionToken.h +++ /dev/null @@ -1,38 +0,0 @@ -/* - * - * 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 _ConnectionToken_ -#define _ConnectionToken_ - -namespace qpid { - namespace broker { - /** - * An empty interface allowing opaque implementations of some - * form of token to identify a connection. - */ - class ConnectionToken{ - public: - virtual ~ConnectionToken(){} - }; - } -} - - -#endif diff --git a/cpp/src/broker/Consumer.h b/cpp/src/broker/Consumer.h deleted file mode 100644 index d0c397d184..0000000000 --- a/cpp/src/broker/Consumer.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * - * 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 _Consumer_ -#define _Consumer_ - -#include "BrokerMessage.h" - -namespace qpid { - namespace broker { - class Consumer{ - public: - virtual bool deliver(Message::shared_ptr& msg) = 0; - virtual ~Consumer(){} - }; - } -} - - -#endif diff --git a/cpp/src/broker/Content.h b/cpp/src/broker/Content.h deleted file mode 100644 index dd895e2eb3..0000000000 --- a/cpp/src/broker/Content.h +++ /dev/null @@ -1,64 +0,0 @@ -/* - * - * 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 _Content_ -#define _Content_ - -#include - -#include "../framing/AMQContentBody.h" -#include "../framing/Buffer.h" -#include "../framing/OutputHandler.h" - -namespace qpid { - -namespace framing { -class ChannelAdapter; -} - -namespace broker { -class Content{ - public: - typedef std::string DataBlock; - typedef boost::function1 SendFn; - - virtual ~Content(){} - - /** Add a block of data to the content */ - virtual void add(framing::AMQContentBody::shared_ptr data) = 0; - - /** Total size of content in bytes */ - virtual uint32_t size() = 0; - - /** - * Iterate over the content calling SendFn for each block. - * Subdivide blocks if necessary to ensure each block is - * <= framesize bytes long. - */ - virtual void send(framing::ChannelAdapter& channel, uint32_t framesize) = 0; - - //FIXME aconway 2007-02-07: This is inconsistently implemented - //find out what is needed. - virtual void encode(qpid::framing::Buffer& buffer) = 0; -}; -}} - - -#endif diff --git a/cpp/src/broker/Deliverable.h b/cpp/src/broker/Deliverable.h deleted file mode 100644 index 1570917849..0000000000 --- a/cpp/src/broker/Deliverable.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * - * 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 _Deliverable_ -#define _Deliverable_ - -#include "BrokerQueue.h" - -namespace qpid { - namespace broker { - class Deliverable{ - public: - virtual void deliverTo(Queue::shared_ptr& queue) = 0; - virtual ~Deliverable(){} - }; - } -} - - -#endif diff --git a/cpp/src/broker/DeliverableMessage.cpp b/cpp/src/broker/DeliverableMessage.cpp deleted file mode 100644 index 0c2e46ccce..0000000000 --- a/cpp/src/broker/DeliverableMessage.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * - * 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 "DeliverableMessage.h" - -using namespace qpid::broker; - -DeliverableMessage::DeliverableMessage(Message::shared_ptr& _msg) : msg(_msg) -{ -} - -void DeliverableMessage::deliverTo(Queue::shared_ptr& queue) -{ - queue->deliver(msg); -} - diff --git a/cpp/src/broker/DeliverableMessage.h b/cpp/src/broker/DeliverableMessage.h deleted file mode 100644 index 43e738a96a..0000000000 --- a/cpp/src/broker/DeliverableMessage.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * - * 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 _DeliverableMessage_ -#define _DeliverableMessage_ - -#include "Deliverable.h" -#include "BrokerMessage.h" -#include "BrokerQueue.h" - -namespace qpid { - namespace broker { - class DeliverableMessage : public Deliverable{ - Message::shared_ptr msg; - public: - DeliverableMessage(Message::shared_ptr& msg); - virtual void deliverTo(Queue::shared_ptr& queue); - virtual ~DeliverableMessage(){} - }; - } -} - - -#endif diff --git a/cpp/src/broker/DeliveryRecord.cpp b/cpp/src/broker/DeliveryRecord.cpp deleted file mode 100644 index 95650b7b23..0000000000 --- a/cpp/src/broker/DeliveryRecord.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/* - * - * 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 "DeliveryRecord.h" -#include "BrokerChannel.h" - -using namespace qpid::broker; -using std::string; - -DeliveryRecord::DeliveryRecord(Message::shared_ptr _msg, - Queue::shared_ptr _queue, - const string _consumerTag, - const uint64_t _deliveryTag) : msg(_msg), - queue(_queue), - consumerTag(_consumerTag), - deliveryTag(_deliveryTag), - pull(false){} - -DeliveryRecord::DeliveryRecord(Message::shared_ptr _msg, - Queue::shared_ptr _queue, - const uint64_t _deliveryTag) : msg(_msg), - queue(_queue), - consumerTag(""), - deliveryTag(_deliveryTag), - pull(true){} - - -void DeliveryRecord::discard(TransactionContext* ctxt) const{ - queue->dequeue(ctxt, msg); -} - -bool DeliveryRecord::matches(uint64_t tag) const{ - return deliveryTag == tag; -} - -bool DeliveryRecord::coveredBy(const AccumulatedAck* const range) const{ - return range->covers(deliveryTag); -} - -void DeliveryRecord::redeliver(Channel* const channel) const{ - if(pull){ - //if message was originally sent as response to get, we must requeue it - requeue(); - }else{ - channel->deliver(msg, consumerTag, deliveryTag); - } -} - -void DeliveryRecord::requeue() const{ - msg->redeliver(); - queue->process(msg); -} - -void DeliveryRecord::addTo(Prefetch* const prefetch) const{ - if(!pull){ - //ignore 'pulled' messages (i.e. those that were sent in - //response to get) when calculating prefetch - prefetch->size += msg->contentSize(); - prefetch->count++; - } -} - -void DeliveryRecord::subtractFrom(Prefetch* const prefetch) const{ - if(!pull){ - //ignore 'pulled' messages (i.e. those that were sent in - //response to get) when calculating prefetch - prefetch->size -= msg->contentSize(); - prefetch->count--; - } -} diff --git a/cpp/src/broker/DeliveryRecord.h b/cpp/src/broker/DeliveryRecord.h deleted file mode 100644 index 15c207ce44..0000000000 --- a/cpp/src/broker/DeliveryRecord.h +++ /dev/null @@ -1,63 +0,0 @@ -/* - * - * 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 _DeliveryRecord_ -#define _DeliveryRecord_ - -#include -#include -#include "AccumulatedAck.h" -#include "BrokerMessage.h" -#include "Prefetch.h" -#include "BrokerQueue.h" - -namespace qpid { - namespace broker { - class Channel; - - /** - * Record of a delivery for which an ack is outstanding. - */ - class DeliveryRecord{ - mutable Message::shared_ptr msg; - mutable Queue::shared_ptr queue; - std::string consumerTag; - uint64_t deliveryTag; - bool pull; - - public: - DeliveryRecord(Message::shared_ptr msg, Queue::shared_ptr queue, const std::string consumerTag, const uint64_t deliveryTag); - DeliveryRecord(Message::shared_ptr msg, Queue::shared_ptr queue, const uint64_t deliveryTag); - - void discard(TransactionContext* ctxt = 0) const; - bool matches(uint64_t tag) const; - bool coveredBy(const AccumulatedAck* const range) const; - void requeue() const; - void redeliver(Channel* const) const; - void addTo(Prefetch* const prefetch) const; - void subtractFrom(Prefetch* const prefetch) const; - }; - - typedef std::list::iterator ack_iterator; - } -} - - -#endif diff --git a/cpp/src/broker/DirectExchange.cpp b/cpp/src/broker/DirectExchange.cpp deleted file mode 100644 index 7d15410374..0000000000 --- a/cpp/src/broker/DirectExchange.cpp +++ /dev/null @@ -1,71 +0,0 @@ -/* - * - * 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 "DirectExchange.h" -#include - -using namespace qpid::broker; -using namespace qpid::framing; -using namespace qpid::sys; - -DirectExchange::DirectExchange(const string& _name) : Exchange(_name) { - -} - -void DirectExchange::bind(Queue::shared_ptr queue, const string& routingKey, const FieldTable*){ - Mutex::ScopedLock l(lock); - std::vector& queues(bindings[routingKey]); - std::vector::iterator i = find(queues.begin(), queues.end(), queue); - if(i == queues.end()){ - bindings[routingKey].push_back(queue); - } -} - -void DirectExchange::unbind(Queue::shared_ptr queue, const string& routingKey, const FieldTable* /*args*/){ - Mutex::ScopedLock l(lock); - std::vector& queues(bindings[routingKey]); - - std::vector::iterator i = find(queues.begin(), queues.end(), queue); - if(i < queues.end()){ - queues.erase(i); - if(queues.empty()){ - bindings.erase(routingKey); - } - } -} - -void DirectExchange::route(Deliverable& msg, const string& routingKey, const FieldTable* /*args*/){ - Mutex::ScopedLock l(lock); - std::vector& queues(bindings[routingKey]); - int count(0); - for(std::vector::iterator i = queues.begin(); i != queues.end(); i++, count++){ - msg.deliverTo(*i); - } - if(!count){ - std::cout << "WARNING: DirectExchange " << getName() << " could not route message with key " << routingKey << std::endl; - } -} - -DirectExchange::~DirectExchange(){ - -} - - -const std::string DirectExchange::typeName("direct"); diff --git a/cpp/src/broker/DirectExchange.h b/cpp/src/broker/DirectExchange.h deleted file mode 100644 index 94b064327d..0000000000 --- a/cpp/src/broker/DirectExchange.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * - * 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 _DirectExchange_ -#define _DirectExchange_ - -#include -#include -#include "BrokerExchange.h" -#include "../framing/FieldTable.h" -#include "BrokerMessage.h" -#include "../sys/Monitor.h" -#include "BrokerQueue.h" - -namespace qpid { -namespace broker { - class DirectExchange : public virtual Exchange{ - std::map > bindings; - qpid::sys::Mutex lock; - - public: - static const std::string typeName; - - DirectExchange(const std::string& name); - - virtual std::string getType(){ return typeName; } - - virtual void bind(Queue::shared_ptr queue, const std::string& routingKey, const qpid::framing::FieldTable* args); - - virtual void unbind(Queue::shared_ptr queue, const std::string& routingKey, const qpid::framing::FieldTable* args); - - virtual void route(Deliverable& msg, const std::string& routingKey, const qpid::framing::FieldTable* args); - - virtual ~DirectExchange(); - }; -} -} - - -#endif diff --git a/cpp/src/broker/ExchangeRegistry.cpp b/cpp/src/broker/ExchangeRegistry.cpp deleted file mode 100644 index 03863673df..0000000000 --- a/cpp/src/broker/ExchangeRegistry.cpp +++ /dev/null @@ -1,76 +0,0 @@ -/* - * - * 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 "ExchangeRegistry.h" -#include "DirectExchange.h" -#include "FanOutExchange.h" -#include "HeadersExchange.h" -#include "TopicExchange.h" - -using namespace qpid::broker; -using namespace qpid::sys; -using std::pair; - -pair ExchangeRegistry::declare(const string& name, const string& type) throw(UnknownExchangeTypeException){ - Mutex::ScopedLock locker(lock); - ExchangeMap::iterator i = exchanges.find(name); - if (i == exchanges.end()) { - Exchange::shared_ptr exchange; - - if(type == TopicExchange::typeName){ - exchange = Exchange::shared_ptr(new TopicExchange(name)); - }else if(type == DirectExchange::typeName){ - exchange = Exchange::shared_ptr(new DirectExchange(name)); - }else if(type == FanOutExchange::typeName){ - exchange = Exchange::shared_ptr(new FanOutExchange(name)); - }else if (type == HeadersExchange::typeName) { - exchange = Exchange::shared_ptr(new HeadersExchange(name)); - }else{ - throw UnknownExchangeTypeException(); - } - exchanges[name] = exchange; - return std::pair(exchange, true); - } else { - return std::pair(i->second, false); - } -} - -void ExchangeRegistry::destroy(const string& name){ - Mutex::ScopedLock locker(lock); - exchanges.erase(name); -} - -Exchange::shared_ptr ExchangeRegistry::get(const string& name){ - Mutex::ScopedLock locker(lock); - Exchange::shared_ptr exchange =exchanges[name]; - if (!exchange) - throw ChannelException(404, "Exchange not found:" + name); - return exchange; -} - -namespace -{ -const std::string empty; -} - -Exchange::shared_ptr ExchangeRegistry::getDefault() -{ - return get(empty); -} diff --git a/cpp/src/broker/ExchangeRegistry.h b/cpp/src/broker/ExchangeRegistry.h deleted file mode 100644 index 1c81cb6021..0000000000 --- a/cpp/src/broker/ExchangeRegistry.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef _broker_ExchangeRegistry_h -#define _broker_ExchangeRegistry_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 -#include "BrokerExchange.h" -#include "../sys/Monitor.h" - -namespace qpid { -namespace broker { - struct UnknownExchangeTypeException{}; - - class ExchangeRegistry{ - typedef std::map ExchangeMap; - ExchangeMap exchanges; - qpid::sys::Mutex lock; - public: - std::pair declare(const std::string& name, const std::string& type) throw(UnknownExchangeTypeException); - void destroy(const std::string& name); - Exchange::shared_ptr get(const std::string& name); - Exchange::shared_ptr getDefault(); - }; -} -} - - -#endif /*!_broker_ExchangeRegistry_h*/ diff --git a/cpp/src/broker/FanOutExchange.cpp b/cpp/src/broker/FanOutExchange.cpp deleted file mode 100644 index 1ac92c89e2..0000000000 --- a/cpp/src/broker/FanOutExchange.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * - * 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 "FanOutExchange.h" -#include - -using namespace qpid::broker; -using namespace qpid::framing; -using namespace qpid::sys; - -FanOutExchange::FanOutExchange(const std::string& _name) : Exchange(_name) {} - -void FanOutExchange::bind(Queue::shared_ptr queue, const string& /*routingKey*/, const FieldTable* /*args*/){ - Mutex::ScopedLock locker(lock); - // Add if not already present. - Queue::vector::iterator i = std::find(bindings.begin(), bindings.end(), queue); - if (i == bindings.end()) { - bindings.push_back(queue); - } -} - -void FanOutExchange::unbind(Queue::shared_ptr queue, const string& /*routingKey*/, const FieldTable* /*args*/){ - Mutex::ScopedLock locker(lock); - Queue::vector::iterator i = std::find(bindings.begin(), bindings.end(), queue); - if (i != bindings.end()) { - bindings.erase(i); - } -} - -void FanOutExchange::route(Deliverable& msg, const string& /*routingKey*/, const FieldTable* /*args*/){ - Mutex::ScopedLock locker(lock); - for(Queue::vector::iterator i = bindings.begin(); i != bindings.end(); ++i){ - msg.deliverTo(*i); - } -} - -FanOutExchange::~FanOutExchange() {} - -const std::string FanOutExchange::typeName("fanout"); diff --git a/cpp/src/broker/FanOutExchange.h b/cpp/src/broker/FanOutExchange.h deleted file mode 100644 index 147383ba1a..0000000000 --- a/cpp/src/broker/FanOutExchange.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * - * 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 _FanOutExchange_ -#define _FanOutExchange_ - -#include -#include -#include "BrokerExchange.h" -#include "../framing/FieldTable.h" -#include "BrokerMessage.h" -#include "../sys/Monitor.h" -#include "BrokerQueue.h" - -namespace qpid { -namespace broker { - -class FanOutExchange : public virtual Exchange { - std::vector bindings; - qpid::sys::Mutex lock; - - public: - static const std::string typeName; - - FanOutExchange(const std::string& name); - - virtual std::string getType(){ return typeName; } - - virtual void bind(Queue::shared_ptr queue, const std::string& routingKey, const qpid::framing::FieldTable* args); - - virtual void unbind(Queue::shared_ptr queue, const std::string& routingKey, const qpid::framing::FieldTable* args); - - virtual void route(Deliverable& msg, const std::string& routingKey, const qpid::framing::FieldTable* args); - - virtual ~FanOutExchange(); -}; - -} -} - - - -#endif diff --git a/cpp/src/broker/HandlerImpl.h b/cpp/src/broker/HandlerImpl.h deleted file mode 100644 index 51175f868a..0000000000 --- a/cpp/src/broker/HandlerImpl.h +++ /dev/null @@ -1,71 +0,0 @@ -#ifndef _broker_HandlerImpl_h -#define _broker_HandlerImpl_h - -/* - * - * 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 "BrokerChannel.h" -#include "../gen/AMQP_ClientProxy.h" - -namespace qpid { - -namespace framing { -class AMQP_ClientProxy; -} - -namespace broker { - -class Broker; -class Channel; -class Connection; - -/** - * A collection of references to the core objects required by an adapter, - * and a client proxy. - */ -struct CoreRefs -{ - CoreRefs(Channel& ch, Connection& c, Broker& b) - : channel(ch), connection(c), broker(b), proxy(ch) {} - - Channel& channel; - Connection& connection; - Broker& broker; - framing::AMQP_ClientProxy proxy; -}; - - -/** - * Base template for protocol handler implementations. - * Provides the core references and appropriate AMQP class proxy. - */ -template -struct HandlerImpl : public CoreRefs { - typedef HandlerImpl HandlerImplType; - HandlerImpl(CoreRefs& parent) - : CoreRefs(parent), client(ProxyType::get(proxy)) {} - ProxyType client; -}; - - - -}} // namespace qpid::broker - - - -#endif /*!_broker_HandlerImpl_h*/ diff --git a/cpp/src/broker/HeadersExchange.cpp b/cpp/src/broker/HeadersExchange.cpp deleted file mode 100644 index 21f23af00d..0000000000 --- a/cpp/src/broker/HeadersExchange.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/* - * - * 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 "HeadersExchange.h" -#include "../framing/Value.h" -#include "../QpidError.h" -#include - - -using namespace qpid::broker; -using namespace qpid::framing; -using namespace qpid::sys; - -// TODO aconway 2006-09-20: More efficient matching algorithm. -// The current search algorithm really sucks. -// Fieldtables are heavy, maybe use shared_ptr to do handle-body. - -using namespace qpid::broker; - -namespace { - const std::string all("all"); - const std::string any("any"); - const std::string x_match("x-match"); -} - -HeadersExchange::HeadersExchange(const string& _name) : Exchange(_name) { } - -void HeadersExchange::bind(Queue::shared_ptr queue, const string& /*routingKey*/, const FieldTable* args){ - Mutex::ScopedLock locker(lock); - std::string what = args->getString("x-match"); - if (what != all && what != any) { - THROW_QPID_ERROR(PROTOCOL_ERROR, "Invalid x-match value binding to headers exchange."); - } - bindings.push_back(Binding(*args, queue)); -} - -void HeadersExchange::unbind(Queue::shared_ptr queue, const string& /*routingKey*/, const FieldTable* args){ - Mutex::ScopedLock locker(lock); - Bindings::iterator i = - std::find(bindings.begin(),bindings.end(), Binding(*args, queue)); - if (i != bindings.end()) bindings.erase(i); -} - - -void HeadersExchange::route(Deliverable& msg, const string& /*routingKey*/, const FieldTable* args){ - Mutex::ScopedLock locker(lock);; - for (Bindings::iterator i = bindings.begin(); i != bindings.end(); ++i) { - if (match(i->first, *args)) msg.deliverTo(i->second); - } -} - -HeadersExchange::~HeadersExchange() {} - -const std::string HeadersExchange::typeName("headers"); - -namespace -{ - - bool match_values(const Value& bind, const Value& msg) { - return dynamic_cast(&bind) || bind == msg; - } - -} - - -bool HeadersExchange::match(const FieldTable& bind, const FieldTable& msg) { - typedef FieldTable::ValueMap Map; - std::string what = bind.getString(x_match); - if (what == all) { - for (Map::const_iterator i = bind.getMap().begin(); - i != bind.getMap().end(); - ++i) - { - if (i->first != x_match) - { - Map::const_iterator j = msg.getMap().find(i->first); - if (j == msg.getMap().end()) return false; - if (!match_values(*(i->second), *(j->second))) return false; - } - } - return true; - } else if (what == any) { - for (Map::const_iterator i = bind.getMap().begin(); - i != bind.getMap().end(); - ++i) - { - if (i->first != x_match) - { - Map::const_iterator j = msg.getMap().find(i->first); - if (j != msg.getMap().end()) { - if (match_values(*(i->second), *(j->second))) return true; - } - } - } - return false; - } else { - return false; - } -} - - - diff --git a/cpp/src/broker/HeadersExchange.h b/cpp/src/broker/HeadersExchange.h deleted file mode 100644 index 4d26b95f50..0000000000 --- a/cpp/src/broker/HeadersExchange.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * - * 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 _HeadersExchange_ -#define _HeadersExchange_ - -#include -#include "BrokerExchange.h" -#include "../framing/FieldTable.h" -#include "BrokerMessage.h" -#include "../sys/Monitor.h" -#include "BrokerQueue.h" - -namespace qpid { -namespace broker { - - -class HeadersExchange : public virtual Exchange { - typedef std::pair Binding; - typedef std::vector Bindings; - - Bindings bindings; - qpid::sys::Mutex lock; - - public: - static const std::string typeName; - - HeadersExchange(const string& name); - - virtual std::string getType(){ return typeName; } - - virtual void bind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args); - - virtual void unbind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args); - - virtual void route(Deliverable& msg, const string& routingKey, const qpid::framing::FieldTable* args); - - virtual ~HeadersExchange(); - - static bool match(const qpid::framing::FieldTable& bindArgs, const qpid::framing::FieldTable& msgArgs); -}; - - - -} -} - -#endif diff --git a/cpp/src/broker/InMemoryContent.cpp b/cpp/src/broker/InMemoryContent.cpp deleted file mode 100644 index d5f3479894..0000000000 --- a/cpp/src/broker/InMemoryContent.cpp +++ /dev/null @@ -1,72 +0,0 @@ -/* - * - * 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 "InMemoryContent.h" -#include "../framing/AMQFrame.h" -#include "../framing/ChannelAdapter.h" - -using namespace qpid::broker; -using namespace qpid::framing; -using boost::static_pointer_cast; - -void InMemoryContent::add(AMQContentBody::shared_ptr data) -{ - content.push_back(data); -} - -uint32_t InMemoryContent::size() -{ - int sum(0); - for (content_iterator i = content.begin(); i != content.end(); i++) { - sum += (*i)->size(); - } - return sum; -} - -void InMemoryContent::send(ChannelAdapter& channel, uint32_t framesize) -{ - for (content_iterator i = content.begin(); i != content.end(); i++) { - if ((*i)->size() > framesize) { - uint32_t offset = 0; - for (int chunk = (*i)->size() / framesize; chunk > 0; chunk--) { - string data = (*i)->getData().substr(offset, framesize); - channel.send(new AMQContentBody(data)); - offset += framesize; - } - uint32_t remainder = (*i)->size() % framesize; - if (remainder) { - string data = (*i)->getData().substr(offset, remainder); - channel.send(new AMQContentBody(data)); - } - } else { - AMQBody::shared_ptr contentBody = - static_pointer_cast(*i); - channel.send(contentBody); - } - } -} - -void InMemoryContent::encode(Buffer& buffer) -{ - for (content_iterator i = content.begin(); i != content.end(); i++) { - (*i)->encode(buffer); - } -} - diff --git a/cpp/src/broker/InMemoryContent.h b/cpp/src/broker/InMemoryContent.h deleted file mode 100644 index 425f0e4e26..0000000000 --- a/cpp/src/broker/InMemoryContent.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * - * 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 _InMemoryContent_ -#define _InMemoryContent_ - -#include "Content.h" -#include - - -namespace qpid { - namespace broker { - class InMemoryContent : public Content{ - typedef std::vector content_list; - typedef content_list::iterator content_iterator; - - content_list content; - public: - void add(qpid::framing::AMQContentBody::shared_ptr data); - uint32_t size(); - void send(framing::ChannelAdapter&, uint32_t framesize); - void encode(qpid::framing::Buffer& buffer); - }; - } -} - - -#endif diff --git a/cpp/src/broker/LazyLoadedContent.cpp b/cpp/src/broker/LazyLoadedContent.cpp deleted file mode 100644 index b5bead11a5..0000000000 --- a/cpp/src/broker/LazyLoadedContent.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - * - * 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 "LazyLoadedContent.h" -#include "../framing/AMQFrame.h" -#include "../framing/ChannelAdapter.h" - -using namespace qpid::broker; -using namespace qpid::framing; - -LazyLoadedContent::~LazyLoadedContent() -{ - store->destroy(*msg); -} - -LazyLoadedContent::LazyLoadedContent(MessageStore* const _store, Message* const _msg, uint64_t _expectedSize) : - store(_store), msg(_msg), expectedSize(_expectedSize) {} - -void LazyLoadedContent::add(AMQContentBody::shared_ptr data) -{ - store->appendContent(*msg, data->getData()); -} - -uint32_t LazyLoadedContent::size() -{ - return 0;//all content is written as soon as it is added -} - -void LazyLoadedContent::send(ChannelAdapter& channel, uint32_t framesize) -{ - if (expectedSize > framesize) { - for (uint64_t offset = 0; offset < expectedSize; offset += framesize) - { - uint64_t remaining = expectedSize - offset; - string data; - store->loadContent(*msg, data, offset, - remaining > framesize ? framesize : remaining); - channel.send(new AMQContentBody(data)); - } - } else { - string data; - store->loadContent(*msg, data, 0, expectedSize); - channel.send(new AMQContentBody(data)); - } -} - -void LazyLoadedContent::encode(Buffer&) -{ - //do nothing as all content is written as soon as it is added -} - diff --git a/cpp/src/broker/LazyLoadedContent.h b/cpp/src/broker/LazyLoadedContent.h deleted file mode 100644 index 9dff6158a5..0000000000 --- a/cpp/src/broker/LazyLoadedContent.h +++ /dev/null @@ -1,50 +0,0 @@ -/* - * - * 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 _LazyLoadedContent_ -#define _LazyLoadedContent_ - -#include "Content.h" -#include "MessageStore.h" -#include "BrokerMessageBase.h" - -namespace qpid { - namespace broker { - class LazyLoadedContent : public Content{ - MessageStore* const store; - Message* const msg; - const uint64_t expectedSize; - public: - LazyLoadedContent( - MessageStore* const store, Message* const msg, - uint64_t expectedSize); - ~LazyLoadedContent(); - void add(qpid::framing::AMQContentBody::shared_ptr data); - uint32_t size(); - void send( - framing::ChannelAdapter&, - uint32_t framesize); - void encode(qpid::framing::Buffer& buffer); - }; - } -} - - -#endif diff --git a/cpp/src/broker/MessageBuilder.cpp b/cpp/src/broker/MessageBuilder.cpp deleted file mode 100644 index 6c33b38e72..0000000000 --- a/cpp/src/broker/MessageBuilder.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/* - * - * 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 "MessageBuilder.h" - -#include "InMemoryContent.h" -#include "LazyLoadedContent.h" - -using namespace qpid::broker; -using namespace qpid::framing; -using std::auto_ptr; - -MessageBuilder::MessageBuilder(CompletionHandler* _handler, - MessageStore* const _store, - uint64_t _stagingThreshold -) : - handler(_handler), - store(_store), - stagingThreshold(_stagingThreshold) -{} - -void MessageBuilder::route(){ - if (message->isComplete()) { - if (handler) handler->complete(message); - message.reset(); - } -} - -void MessageBuilder::initialise(Message::shared_ptr& msg){ - if(message.get()){ - THROW_QPID_ERROR(PROTOCOL_ERROR + 504, "Invalid message sequence: got publish before previous content was completed."); - } - message = msg; -} - -void MessageBuilder::setHeader(AMQHeaderBody::shared_ptr& header){ - if(!message.get()){ - THROW_QPID_ERROR(PROTOCOL_ERROR + 504, "Invalid message sequence: got header before publish."); - } - message->setHeader(header); - if (stagingThreshold && header->getContentSize() >= stagingThreshold) { - store->stage(*message); - message->releaseContent(store); - } else { - auto_ptr content(new InMemoryContent()); - message->setContent(content); - } - route(); -} - -void MessageBuilder::addContent(AMQContentBody::shared_ptr& content){ - if(!message.get()){ - THROW_QPID_ERROR(PROTOCOL_ERROR + 504, "Invalid message sequence: got content before publish."); - } - message->addContent(content); - route(); -} diff --git a/cpp/src/broker/MessageBuilder.h b/cpp/src/broker/MessageBuilder.h deleted file mode 100644 index 85de2e47bc..0000000000 --- a/cpp/src/broker/MessageBuilder.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * - * 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 _MessageBuilder_ -#define _MessageBuilder_ - -#include -#include "../QpidError.h" -#include "BrokerExchange.h" -#include "BrokerMessage.h" -#include "MessageStore.h" -#include "../framing/AMQContentBody.h" -#include "../framing/AMQHeaderBody.h" -#include "../gen/BasicPublishBody.h" -#include "CompletionHandler.h" - -namespace qpid { - namespace broker { - class MessageBuilder{ - public: - MessageBuilder(CompletionHandler* _handler, - MessageStore* const store = 0, - uint64_t stagingThreshold = 0); - void initialise(Message::shared_ptr& msg); - void setHeader(framing::AMQHeaderBody::shared_ptr& header); - void addContent(framing::AMQContentBody::shared_ptr& content); - Message::shared_ptr getMessage() { return message; } - private: - Message::shared_ptr message; - CompletionHandler* handler; - MessageStore* const store; - const uint64_t stagingThreshold; - - void route(); - }; - } -} - - -#endif diff --git a/cpp/src/broker/MessageHandlerImpl.cpp b/cpp/src/broker/MessageHandlerImpl.cpp deleted file mode 100644 index e5e5581ac4..0000000000 --- a/cpp/src/broker/MessageHandlerImpl.cpp +++ /dev/null @@ -1,243 +0,0 @@ -/* - * - * 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 "../QpidError.h" -#include "MessageHandlerImpl.h" -#include "BrokerChannel.h" -#include "../framing/FramingContent.h" -#include "Connection.h" -#include "Broker.h" -#include "BrokerMessageMessage.h" -#include "../gen/MessageAppendBody.h" -#include "../gen/MessageTransferBody.h" -#include "BrokerAdapter.h" - -namespace qpid { -namespace broker { - -using namespace framing; - -MessageHandlerImpl::MessageHandlerImpl(CoreRefs& parent) - : HandlerImplType(parent) {} - -// -// Message class method handlers -// - -void -MessageHandlerImpl::cancel(const MethodContext& context, - const string& destination ) -{ - channel.cancel(destination); - client.ok(context.getRequestId()); -} - -void -MessageHandlerImpl::open(const MethodContext& context, - const string& reference) -{ - references.open(reference); - client.ok(context.getRequestId()); -} - -void -MessageHandlerImpl::append(const MethodContext& context, - const string& reference, - const string& /*bytes*/ ) -{ - references.get(reference)->append( - boost::shared_polymorphic_downcast( - context.methodBody)); - client.ok(context.getRequestId()); -} - -void -MessageHandlerImpl::close(const MethodContext& context, - const string& reference) -{ - Reference::shared_ptr ref = references.get(reference); - client.ok(context.getRequestId()); - - // Send any transfer messages to their correct exchanges and okay them - const Reference::Messages& msgs = ref->getMessages(); - for (Reference::Messages::const_iterator m = msgs.begin(); m != msgs.end(); ++m) { - channel.handleInlineTransfer(*m); - client.ok((*m)->getRequestId()); - } - ref->close(); -} - -void -MessageHandlerImpl::checkpoint(const MethodContext& context, - const string& /*reference*/, - const string& /*identifier*/ ) -{ - // Initial implementation (which is conforming) is to do nothing here - // and return offset zero for the resume - client.ok(context.getRequestId()); -} - -void -MessageHandlerImpl::resume(const MethodContext& context, - const string& reference, - const string& /*identifier*/ ) -{ - // Initial (null) implementation - // open reference and return 0 offset - references.open(reference); - client.offset(0, context.getRequestId()); -} - -void -MessageHandlerImpl::offset(const MethodContext&, - uint64_t /*value*/ ) -{ - // Shouldn't ever receive this as it is reponse to resume - // which is never sent - // TODO astitcher 2007-02-16 What is the correct exception to throw here? - THROW_QPID_ERROR(INTERNAL_ERROR, "impossible"); -} - -void -MessageHandlerImpl::consume(const MethodContext& context, - uint16_t /*ticket*/, - const string& queueName, - const string& destination, - bool noLocal, - bool noAck, - bool exclusive, - const framing::FieldTable& filter ) -{ - Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); - if(!destination.empty() && channel.exists(destination)) - throw ConnectionException(530, "Consumer tags must be unique"); - string tag = destination; - channel.consume( - tag, queue, !noAck, exclusive, - noLocal ? &connection : 0, &filter); - client.ok(context.getRequestId()); - // Dispatch messages as there is now a consumer. - queue->dispatch(); -} - -void -MessageHandlerImpl::get( const MethodContext& context, - uint16_t /*ticket*/, - const string& queueName, - const string& destination, - bool noAck ) -{ - Queue::shared_ptr queue = - connection.getQueue(queueName, context.channel->getId()); - - if(channel.get(queue, destination, !noAck)) - client.ok(context.getRequestId()); - else - client.empty(context.getRequestId()); -} - -void -MessageHandlerImpl::empty( const MethodContext& ) -{ - // Shouldn't ever receive this as it is a response to get - // which is never sent - // TODO astitcher 2007-02-09 What is the correct exception to throw here? - THROW_QPID_ERROR(INTERNAL_ERROR, "Impossible"); -} - -void -MessageHandlerImpl::ok(const MethodContext& /*context*/) -{ - channel.ack(); -} - -void -MessageHandlerImpl::qos(const MethodContext& context, - uint32_t prefetchSize, - uint16_t prefetchCount, - bool /*global*/ ) -{ - //TODO: handle global - channel.setPrefetchSize(prefetchSize); - channel.setPrefetchCount(prefetchCount); - client.ok(context.getRequestId()); -} - -void -MessageHandlerImpl::recover(const MethodContext& context, - bool requeue) -{ - channel.recover(requeue); - client.ok(context.getRequestId()); -} - -void -MessageHandlerImpl::reject(const MethodContext& /*context*/, - uint16_t /*code*/, - const string& /*text*/ ) -{ - channel.ack(); - // channel.requeue(); -} - -void -MessageHandlerImpl::transfer(const MethodContext& context, - uint16_t /*ticket*/, - const string& /* destination */, - bool /*redelivered*/, - bool /*immediate*/, - uint64_t /*ttl*/, - uint8_t /*priority*/, - uint64_t /*timestamp*/, - uint8_t /*deliveryMode*/, - uint64_t /*expiration*/, - const string& /*exchangeName*/, - const string& /*routingKey*/, - const string& /*messageId*/, - const string& /*correlationId*/, - const string& /*replyTo*/, - const string& /*contentType*/, - const string& /*contentEncoding*/, - const string& /*userId*/, - const string& /*appId*/, - const string& /*transactionId*/, - const string& /*securityToken*/, - const framing::FieldTable& /*applicationHeaders*/, - const framing::Content& body, - bool /*mandatory*/) -{ - MessageTransferBody::shared_ptr transfer( - boost::shared_polymorphic_downcast( - context.methodBody)); - RequestId requestId = context.getRequestId(); - - if (body.isInline()) { - MessageMessage::shared_ptr message( - new MessageMessage(&connection, requestId, transfer)); - channel.handleInlineTransfer(message); - client.ok(requestId); - } else { - Reference::shared_ptr ref(references.get(body.getValue())); - MessageMessage::shared_ptr message( - new MessageMessage(&connection, requestId, transfer, ref)); - ref->addMessage(message); - } -} - - -}} // namespace qpid::broker diff --git a/cpp/src/broker/MessageHandlerImpl.h b/cpp/src/broker/MessageHandlerImpl.h deleted file mode 100644 index a5647200d3..0000000000 --- a/cpp/src/broker/MessageHandlerImpl.h +++ /dev/null @@ -1,130 +0,0 @@ -#ifndef _broker_MessageHandlerImpl_h -#define _broker_MessageHandlerImpl_h - -/* - * - * 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 - -#include "../gen/AMQP_ServerOperations.h" -#include "../gen/AMQP_ClientProxy.h" -#include "Reference.h" -#include "HandlerImpl.h" - -namespace qpid { -namespace broker { - -class Connection; -class Broker; -class MessageMessage; - -class MessageHandlerImpl : - public framing::AMQP_ServerOperations::MessageHandler, - public HandlerImpl -{ - public: - MessageHandlerImpl(CoreRefs& parent); - - void append(const framing::MethodContext&, - const std::string& reference, - const std::string& bytes ); - - void cancel(const framing::MethodContext&, - const std::string& destination ); - - void checkpoint(const framing::MethodContext&, - const std::string& reference, - const std::string& identifier ); - - void close(const framing::MethodContext&, - const std::string& reference ); - - void consume(const framing::MethodContext&, - uint16_t ticket, - const std::string& queue, - const std::string& destination, - bool noLocal, - bool noAck, - bool exclusive, - const framing::FieldTable& filter ); - - void empty( const framing::MethodContext& ); - - void get(const framing::MethodContext&, - uint16_t ticket, - const std::string& queue, - const std::string& destination, - bool noAck ); - - void offset(const framing::MethodContext&, - uint64_t value ); - - void ok( const framing::MethodContext& ); - - void open(const framing::MethodContext&, - const std::string& reference ); - - void qos(const framing::MethodContext&, - uint32_t prefetchSize, - uint16_t prefetchCount, - bool global ); - - void recover(const framing::MethodContext&, - bool requeue ); - - void reject(const framing::MethodContext&, - uint16_t code, - const std::string& text ); - - void resume(const framing::MethodContext&, - const std::string& reference, - const std::string& identifier ); - - void transfer(const framing::MethodContext&, - uint16_t ticket, - const std::string& destination, - bool redelivered, - bool immediate, - uint64_t ttl, - uint8_t priority, - uint64_t timestamp, - uint8_t deliveryMode, - uint64_t expiration, - const std::string& exchange, - const std::string& routingKey, - const std::string& messageId, - const std::string& correlationId, - const std::string& replyTo, - const std::string& contentType, - const std::string& contentEncoding, - const std::string& userId, - const std::string& appId, - const std::string& transactionId, - const std::string& securityToken, - const framing::FieldTable& applicationHeaders, - const framing::Content& body, - bool mandatory ); - private: - ReferenceRegistry references; -}; - -}} // namespace qpid::broker - - - -#endif /*!_broker_MessageHandlerImpl_h*/ diff --git a/cpp/src/broker/MessageStore.h b/cpp/src/broker/MessageStore.h deleted file mode 100644 index 1d9ee86e48..0000000000 --- a/cpp/src/broker/MessageStore.h +++ /dev/null @@ -1,129 +0,0 @@ -/* - * - * 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 _MessageStore_ -#define _MessageStore_ - -#include "PersistableExchange.h" -#include "PersistableMessage.h" -#include "PersistableQueue.h" -#include "RecoveryManager.h" -#include "TransactionalStore.h" - -namespace qpid { -namespace broker { - -/** - * An abstraction of the persistent storage for messages. (In - * all methods, any pointers/references to queues or messages - * are valid only for the duration of the call). - */ -class MessageStore : public TransactionalStore{ -public: - /** - * Record the existence of a durable queue - */ - virtual void create(const PersistableQueue& queue) = 0; - /** - * Destroy a durable queue - */ - virtual void destroy(const PersistableQueue& queue) = 0; - - /** - * Record the existence of a durable exchange - */ - virtual void create(const PersistableExchange& exchange) = 0; - /** - * Destroy a durable exchange - */ - virtual void destroy(const PersistableExchange& exchange) = 0; - - /** - * Request recovery of queue and message state from store - */ - virtual void recover(RecoveryManager& queues) = 0; - - /** - * Stores a messages before it has been enqueued - * (enqueueing automatically stores the message so this is - * only required if storage is required prior to that - * point). If the message has not yet been stored it will - * store the headers as well as any content passed in. A - * persistence id will be set on the message which can be - * used to load the content or to append to it. - */ - virtual void stage(PersistableMessage& msg) = 0; - - /** - * Destroys a previously staged message. This only needs - * to be called if the message is never enqueued. (Once - * enqueued, deletion will be automatic when the message - * is dequeued from all queues it was enqueued onto). - */ - virtual void destroy(PersistableMessage& msg) = 0; - - /** - * Appends content to a previously staged message - */ - virtual void appendContent(PersistableMessage& msg, const std::string& data) = 0; - - /** - * Loads (a section) of content data for the specified - * message (previously stored through a call to stage or - * enqueue) into data. The offset refers to the content - * only (i.e. an offset of 0 implies that the start of the - * content should be loaded, not the headers or related - * meta-data). - */ - virtual void loadContent(PersistableMessage& msg, std::string& data, uint64_t offset, uint32_t length) = 0; - - /** - * Enqueues a message, storing the message if it has not - * been previously stored and recording that the given - * message is on the given queue. - * - * @param msg the message to enqueue - * @param queue the name of the queue onto which it is to be enqueued - * @param xid (a pointer to) an identifier of the - * distributed transaction in which the operation takes - * place or null for 'local' transactions - */ - virtual void enqueue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue) = 0; - /** - * Dequeues a message, recording that the given message is - * no longer on the given queue and deleting the message - * if it is no longer on any other queue. - * - * @param msg the message to dequeue - * @param queue the name of th queue from which it is to be dequeued - * @param xid (a pointer to) an identifier of the - * distributed transaction in which the operation takes - * place or null for 'local' transactions - */ - virtual void dequeue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue) = 0; - - virtual ~MessageStore(){} -}; - -} -} - - -#endif diff --git a/cpp/src/broker/MessageStoreModule.cpp b/cpp/src/broker/MessageStoreModule.cpp deleted file mode 100644 index 17e5d3cca8..0000000000 --- a/cpp/src/broker/MessageStoreModule.cpp +++ /dev/null @@ -1,114 +0,0 @@ -/* - * - * 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 "MessageStoreModule.h" -#include - -using namespace qpid::broker; - -MessageStoreModule::MessageStoreModule(const std::string& name) : store(name) -{ -} - -void MessageStoreModule::create(const PersistableQueue& queue) -{ - store->create(queue); -} - -void MessageStoreModule::destroy(const PersistableQueue& queue) -{ - store->destroy(queue); -} - -void MessageStoreModule::create(const PersistableExchange& exchange) -{ - store->create(exchange); -} - -void MessageStoreModule::destroy(const PersistableExchange& exchange) -{ - store->destroy(exchange); -} - -void MessageStoreModule::recover(RecoveryManager& registry) -{ - store->recover(registry); -} - -void MessageStoreModule::stage(PersistableMessage& msg) -{ - store->stage(msg); -} - -void MessageStoreModule::destroy(PersistableMessage& msg) -{ - store->destroy(msg); -} - -void MessageStoreModule::appendContent(PersistableMessage& msg, const std::string& data) -{ - store->appendContent(msg, data); -} - -void MessageStoreModule::loadContent(PersistableMessage& msg, string& data, uint64_t offset, uint32_t length) -{ - store->loadContent(msg, data, offset, length); -} - -void MessageStoreModule::enqueue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue) -{ - store->enqueue(ctxt, msg, queue); -} - -void MessageStoreModule::dequeue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue) -{ - store->dequeue(ctxt, msg, queue); -} - -std::auto_ptr MessageStoreModule::begin() -{ - return store->begin(); -} - -std::auto_ptr MessageStoreModule::begin(const std::string& xid) -{ - return store->begin(xid); -} - -void MessageStoreModule::prepare(TPCTransactionContext& txn) -{ - store->prepare(txn); -} - -void MessageStoreModule::commit(TransactionContext& ctxt) -{ - store->commit(ctxt); -} - -void MessageStoreModule::abort(TransactionContext& ctxt) -{ - store->abort(ctxt); -} - -void MessageStoreModule::collectPreparedXids(std::set& xids) -{ - store->collectPreparedXids(xids); -} diff --git a/cpp/src/broker/MessageStoreModule.h b/cpp/src/broker/MessageStoreModule.h deleted file mode 100644 index 02c87a1bf6..0000000000 --- a/cpp/src/broker/MessageStoreModule.h +++ /dev/null @@ -1,68 +0,0 @@ -/* - * - * 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 _MessageStoreModule_ -#define _MessageStoreModule_ - -#include "BrokerMessage.h" -#include "MessageStore.h" -#include "BrokerQueue.h" -#include "RecoveryManager.h" -#include "../sys/Module.h" - -namespace qpid { -namespace broker { - -/** - * A null implementation of the MessageStore interface - */ -class MessageStoreModule : public MessageStore -{ - qpid::sys::Module store; -public: - MessageStoreModule(const std::string& name); - - std::auto_ptr begin(); - std::auto_ptr begin(const std::string& xid); - void prepare(TPCTransactionContext& txn); - void commit(TransactionContext& txn); - void abort(TransactionContext& txn); - void collectPreparedXids(std::set& xids); - - void create(const PersistableQueue& queue); - void destroy(const PersistableQueue& queue); - void create(const PersistableExchange& exchange); - void destroy(const PersistableExchange& exchange); - void recover(RecoveryManager& queues); - void stage(PersistableMessage& msg); - void destroy(PersistableMessage& msg); - void appendContent(PersistableMessage& msg, const std::string& data); - void loadContent(PersistableMessage& msg, std::string& data, uint64_t offset, uint32_t length); - void enqueue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue); - void dequeue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue); - - ~MessageStoreModule(){} -}; - -} -} - - -#endif diff --git a/cpp/src/broker/NameGenerator.cpp b/cpp/src/broker/NameGenerator.cpp deleted file mode 100644 index 8484f921e9..0000000000 --- a/cpp/src/broker/NameGenerator.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * - * 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 "NameGenerator.h" -#include - -using namespace qpid::broker; - -NameGenerator::NameGenerator(const std::string& _base) : base(_base), counter(1) {} - -std::string NameGenerator::generate(){ - std::stringstream ss; - ss << base << counter++; - return ss.str(); -} diff --git a/cpp/src/broker/NameGenerator.h b/cpp/src/broker/NameGenerator.h deleted file mode 100644 index affcedba41..0000000000 --- a/cpp/src/broker/NameGenerator.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * - * 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 _NameGenerator_ -#define _NameGenerator_ - -#include "BrokerMessage.h" - -namespace qpid { - namespace broker { - class NameGenerator{ - const std::string base; - unsigned int counter; - public: - NameGenerator(const std::string& base); - std::string generate(); - }; - } -} - - -#endif diff --git a/cpp/src/broker/NullMessageStore.cpp b/cpp/src/broker/NullMessageStore.cpp deleted file mode 100644 index 393bbb8f02..0000000000 --- a/cpp/src/broker/NullMessageStore.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* - * - * 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 "NullMessageStore.h" - -#include "RecoveryManager.h" - -#include - -using namespace qpid::broker; - -NullMessageStore::NullMessageStore(bool _warn) : warn(_warn){} - -void NullMessageStore::create(const PersistableQueue& queue) -{ - if (warn) std::cout << "WARNING: Can't create durable queue '" << queue.getName() << "'. Persistence not enabled." << std::endl; -} - -void NullMessageStore::destroy(const PersistableQueue& queue) -{ - if (warn) std::cout << "WARNING: Can't destroy durable queue '" << queue.getName() << "'. Persistence not enabled." << std::endl; -} - -void NullMessageStore::create(const PersistableExchange&) -{ -} - -void NullMessageStore::destroy(const PersistableExchange&) -{ -} - -void NullMessageStore::recover(RecoveryManager&) -{ - if (warn) std::cout << "WARNING: Persistence not enabled, no recovery of queues or messages." << std::endl; -} - -void NullMessageStore::stage(PersistableMessage&) -{ - if (warn) std::cout << "WARNING: Can't stage message. Persistence not enabled." << std::endl; -} - -void NullMessageStore::destroy(PersistableMessage&) -{ - if (warn) std::cout << "WARNING: No need to destroy staged message. Persistence not enabled." << std::endl; -} - -void NullMessageStore::appendContent(PersistableMessage&, const string&) -{ - if (warn) std::cout << "WARNING: Can't append content. Persistence not enabled." << std::endl; -} - -void NullMessageStore::loadContent(PersistableMessage&, string&, uint64_t, uint32_t) -{ - if (warn) std::cout << "WARNING: Can't load content. Persistence not enabled." << std::endl; -} - -void NullMessageStore::enqueue(TransactionContext*, PersistableMessage&, const PersistableQueue& queue) -{ - if (warn) std::cout << "WARNING: Can't enqueue message onto '" << queue.getName() << "'. Persistence not enabled." << std::endl; -} - -void NullMessageStore::dequeue(TransactionContext*, PersistableMessage&, const PersistableQueue& queue) -{ - if (warn) std::cout << "WARNING: Can't dequeue message from '" << queue.getName() << "'. Persistence not enabled." << std::endl; -} - -std::auto_ptr NullMessageStore::begin() -{ - return std::auto_ptr(); -} - -std::auto_ptr NullMessageStore::begin(const std::string&) -{ - return std::auto_ptr(); -} - -void NullMessageStore::prepare(TPCTransactionContext&) -{ -} - -void NullMessageStore::commit(TransactionContext&) -{ -} - -void NullMessageStore::abort(TransactionContext&) -{ -} - -void NullMessageStore::collectPreparedXids(std::set&) -{ - -} diff --git a/cpp/src/broker/NullMessageStore.h b/cpp/src/broker/NullMessageStore.h deleted file mode 100644 index e0b215bb39..0000000000 --- a/cpp/src/broker/NullMessageStore.h +++ /dev/null @@ -1,65 +0,0 @@ -/* - * - * 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 _NullMessageStore_ -#define _NullMessageStore_ - -#include "BrokerMessage.h" -#include "MessageStore.h" -#include "BrokerQueue.h" - -namespace qpid { -namespace broker { - -/** - * A null implementation of the MessageStore interface - */ -class NullMessageStore : public MessageStore -{ - const bool warn; -public: - NullMessageStore(bool warn = false); - - virtual std::auto_ptr begin(); - virtual std::auto_ptr begin(const std::string& xid); - virtual void prepare(TPCTransactionContext& txn); - virtual void commit(TransactionContext& txn); - virtual void abort(TransactionContext& txn); - virtual void collectPreparedXids(std::set& xids); - - virtual void create(const PersistableQueue& queue); - virtual void destroy(const PersistableQueue& queue); - virtual void create(const PersistableExchange& exchange); - virtual void destroy(const PersistableExchange& exchange); - virtual void recover(RecoveryManager& queues); - virtual void stage(PersistableMessage& msg); - virtual void destroy(PersistableMessage& msg); - virtual void appendContent(PersistableMessage& msg, const std::string& data); - virtual void loadContent(PersistableMessage& msg, std::string& data, uint64_t offset, uint32_t length); - virtual void enqueue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue); - virtual void dequeue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue); - ~NullMessageStore(){} -}; - -} -} - - -#endif diff --git a/cpp/src/broker/Persistable.h b/cpp/src/broker/Persistable.h deleted file mode 100644 index 58ea1a7322..0000000000 --- a/cpp/src/broker/Persistable.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef _broker_Persistable_h -#define _broker_Persistable_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 "../framing/amqp_types.h" -#include "../framing/Buffer.h" - -namespace qpid { -namespace broker { - -/** - * Base class for all persistable objects - */ -class Persistable -{ -public: - /** - * Allows the store to attach its own identifier to this object - */ - virtual void setPersistenceId(uint64_t id) const = 0; - /** - * Returns any identifier the store may have attached to this - * object - */ - virtual uint64_t getPersistenceId() const = 0; - /** - * Encodes the persistable state of this object into the supplied - * buffer - */ - virtual void encode(framing::Buffer& buffer) const = 0; - /** - * @returns the size of the buffer needed to encode this object - */ - virtual uint32_t encodedSize() const = 0; - - virtual ~Persistable() {}; -}; - -}} - - -#endif diff --git a/cpp/src/broker/PersistableExchange.h b/cpp/src/broker/PersistableExchange.h deleted file mode 100644 index 9badf5f609..0000000000 --- a/cpp/src/broker/PersistableExchange.h +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef _broker_PersistableExchange_h -#define _broker_PersistableExchange_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 -#include "Persistable.h" - -namespace qpid { -namespace broker { - -/** - * The interface exchanges must expose to the MessageStore in order to be - * persistable. - */ -class PersistableExchange : public Persistable -{ -public: - virtual ~PersistableExchange() {}; -}; - -}} - - -#endif diff --git a/cpp/src/broker/PersistableMessage.h b/cpp/src/broker/PersistableMessage.h deleted file mode 100644 index c10d16bc65..0000000000 --- a/cpp/src/broker/PersistableMessage.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef _broker_PersistableMessage_h -#define _broker_PersistableMessage_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 -#include -#include "Persistable.h" -#include "../framing/amqp_types.h" - -namespace qpid { -namespace broker { - -/** - * The interface messages must expose to the MessageStore in order to - * be persistable. - */ - class PersistableMessage : public Persistable -{ -public: - typedef boost::shared_ptr shared_ptr; - - /** - * @returns the size of the headers when encoded - */ - virtual uint32_t encodedHeaderSize() const = 0; - - virtual ~PersistableMessage() {}; -}; - -}} - - -#endif diff --git a/cpp/src/broker/PersistableQueue.h b/cpp/src/broker/PersistableQueue.h deleted file mode 100644 index 5dd91dde9b..0000000000 --- a/cpp/src/broker/PersistableQueue.h +++ /dev/null @@ -1,45 +0,0 @@ -#ifndef _broker_PersistableQueue_h -#define _broker_PersistableQueue_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 -#include "Persistable.h" - -namespace qpid { -namespace broker { - -/** - * The interface queues must expose to the MessageStore in order to be - * persistable. - */ -class PersistableQueue : public Persistable -{ -public: - virtual const std::string& getName() const = 0; - virtual ~PersistableQueue() {}; -}; - -}} - - -#endif diff --git a/cpp/src/broker/Prefetch.h b/cpp/src/broker/Prefetch.h deleted file mode 100644 index 448156c187..0000000000 --- a/cpp/src/broker/Prefetch.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * - * 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 _Prefetch_ -#define _Prefetch_ - -#include "../framing/amqp_types.h" - -namespace qpid { - namespace broker { - /** - * Count and total size of asynchronously delivered - * (i.e. pushed) messages that have acks outstanding. - */ - struct Prefetch{ - uint32_t size; - uint16_t count; - - void reset() { size = 0; count = 0; } - }; - } -} - - -#endif diff --git a/cpp/src/broker/QueuePolicy.cpp b/cpp/src/broker/QueuePolicy.cpp deleted file mode 100644 index 620822cddc..0000000000 --- a/cpp/src/broker/QueuePolicy.cpp +++ /dev/null @@ -1,69 +0,0 @@ -/* - * - * 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 "QueuePolicy.h" - -using namespace qpid::broker; -using namespace qpid::framing; - -QueuePolicy::QueuePolicy(uint32_t _maxCount, uint64_t _maxSize) : - maxCount(_maxCount), maxSize(_maxSize), count(0), size(0) {} - -QueuePolicy::QueuePolicy(const FieldTable& settings) : - maxCount(getInt(settings, maxCountKey, 0)), - maxSize(getInt(settings, maxSizeKey, 0)), count(0), size(0) {} - -void QueuePolicy::enqueued(uint64_t _size) -{ - if (maxCount) count++; - if (maxSize) size += _size; -} - -void QueuePolicy::dequeued(uint64_t _size) -{ - if (maxCount) count--; - if (maxSize) size -= _size; -} - -bool QueuePolicy::limitExceeded() -{ - return (maxSize && size > maxSize) || (maxCount && count > maxCount); -} - -void QueuePolicy::update(FieldTable& settings) -{ - if (maxCount) settings.setInt(maxCountKey, maxCount); - if (maxSize) settings.setInt(maxSizeKey, maxSize); -} - - -int QueuePolicy::getInt(const FieldTable& settings, const std::string& key, int defaultValue) -{ - //Note: currently field table only contain signed 32 bit ints, which - // restricts the values that can be set on the queue policy. - try { - return settings.getInt(key); - } catch (FieldNotFoundException& ignore) { - return defaultValue; - } -} - -const std::string QueuePolicy::maxCountKey("qpid.max_count"); -const std::string QueuePolicy::maxSizeKey("qpid.max_size"); diff --git a/cpp/src/broker/QueuePolicy.h b/cpp/src/broker/QueuePolicy.h deleted file mode 100644 index 28c3f81516..0000000000 --- a/cpp/src/broker/QueuePolicy.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * - * 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 _QueuePolicy_ -#define _QueuePolicy_ - -#include "../framing/FieldTable.h" - -namespace qpid { - namespace broker { - class QueuePolicy - { - static const std::string maxCountKey; - static const std::string maxSizeKey; - - const uint32_t maxCount; - const uint64_t maxSize; - uint32_t count; - uint64_t size; - - static int getInt(const qpid::framing::FieldTable& settings, const std::string& key, int defaultValue); - - public: - QueuePolicy(uint32_t maxCount, uint64_t maxSize); - QueuePolicy(const qpid::framing::FieldTable& settings); - void enqueued(uint64_t size); - void dequeued(uint64_t size); - void update(qpid::framing::FieldTable& settings); - bool limitExceeded(); - uint32_t getMaxCount() const { return maxCount; } - uint64_t getMaxSize() const { return maxSize; } - }; - } -} - - -#endif diff --git a/cpp/src/broker/QueueRegistry.cpp b/cpp/src/broker/QueueRegistry.cpp deleted file mode 100644 index dcaf7ec0f6..0000000000 --- a/cpp/src/broker/QueueRegistry.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - * - * 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 "QueueRegistry.h" -#include -#include - -using namespace qpid::broker; -using namespace qpid::sys; - -QueueRegistry::QueueRegistry(MessageStore* const _store) : counter(1), store(_store){} - -QueueRegistry::~QueueRegistry(){} - -std::pair -QueueRegistry::declare(const string& declareName, bool durable, - uint32_t autoDelete, const ConnectionToken* owner) -{ - Mutex::ScopedLock locker(lock); - string name = declareName.empty() ? generateName() : declareName; - assert(!name.empty()); - QueueMap::iterator i = queues.find(name); - if (i == queues.end()) { - Queue::shared_ptr queue(new Queue(name, autoDelete, durable ? store : 0, owner)); - queues[name] = queue; - return std::pair(queue, true); - } else { - return std::pair(i->second, false); - } -} - -void QueueRegistry::destroy(const string& name){ - Mutex::ScopedLock locker(lock); - queues.erase(name); -} - -Queue::shared_ptr QueueRegistry::find(const string& name){ - Mutex::ScopedLock locker(lock); - QueueMap::iterator i = queues.find(name); - if (i == queues.end()) { - return Queue::shared_ptr(); - } else { - return i->second; - } -} - -string QueueRegistry::generateName(){ - string name; - do { - std::stringstream ss; - ss << "tmp_" << counter++; - name = ss.str(); - // Thread safety: Private function, only called with lock held - // so this is OK. - } while(queues.find(name) != queues.end()); - return name; -} - -MessageStore* const QueueRegistry::getStore() const { - return store; -} diff --git a/cpp/src/broker/QueueRegistry.h b/cpp/src/broker/QueueRegistry.h deleted file mode 100644 index 5798226c48..0000000000 --- a/cpp/src/broker/QueueRegistry.h +++ /dev/null @@ -1,96 +0,0 @@ -/* - * - * 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 _QueueRegistry_ -#define _QueueRegistry_ - -#include -#include "../sys/Monitor.h" -#include "BrokerQueue.h" - -namespace qpid { -namespace broker { - -/** - * A registry of queues indexed by queue name. - * - * Queues are reference counted using shared_ptr to ensure that they - * are deleted when and only when they are no longer in use. - * - */ -class QueueRegistry{ - - public: - QueueRegistry(MessageStore* const store = 0); - ~QueueRegistry(); - - /** - * Declare a queue. - * - * @return The queue and a boolean flag which is true if the queue - * was created by this declare call false if it already existed. - */ - std::pair declare(const string& name, bool durable = false, uint32_t autodelete = 0, - const ConnectionToken* const owner = 0); - - /** - * Destroy the named queue. - * - * Note: if the queue is in use it is not actually destroyed until - * all shared_ptrs to it are destroyed. During that time it is - * possible that a new queue with the same name may be - * created. This should not create any problems as the new and - * old queues exist independently. The registry has - * forgotten the old queue so there can be no confusion for - * subsequent calls to find or declare with the same name. - * - */ - void destroy(const string& name); - - /** - * Find the named queue. Return 0 if not found. - */ - Queue::shared_ptr find(const string& name); - - /** - * Generate unique queue name. - */ - string generateName(); - - /** - * Return the message store used. - */ - MessageStore* const getStore() const; - - - private: - typedef std::map QueueMap; - QueueMap queues; - qpid::sys::Mutex lock; - int counter; - MessageStore* const store; -}; - - -} -} - - -#endif diff --git a/cpp/src/broker/RecoverableMessage.h b/cpp/src/broker/RecoverableMessage.h deleted file mode 100644 index 3dd892d577..0000000000 --- a/cpp/src/broker/RecoverableMessage.h +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef _broker_RecoverableMessage_h -#define _broker_RecoverableMessage_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 -#include "../framing/amqp_types.h" -#include "../framing/Buffer.h" - -namespace qpid { -namespace broker { - -/** - * The interface through which messages are reloaded on recovery. - */ -class RecoverableMessage -{ -public: - typedef boost::shared_ptr shared_ptr; - virtual void setPersistenceId(uint64_t id) = 0; - /** - * Used by store to determine whether to load content on recovery - * or let message load its own content as and when it requires it. - * - * @returns true if the content of the message should be loaded - */ - virtual bool loadContent(uint64_t available) = 0; - /** - * Loads the content held in the supplied buffer (may do checking - * of length as necessary) - */ - virtual void decodeContent(framing::Buffer& buffer) = 0; - virtual ~RecoverableMessage() {}; -}; - -}} - - -#endif diff --git a/cpp/src/broker/RecoverableQueue.h b/cpp/src/broker/RecoverableQueue.h deleted file mode 100644 index 656a5f0149..0000000000 --- a/cpp/src/broker/RecoverableQueue.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _broker_RecoverableQueue_h -#define _broker_RecoverableQueue_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 "RecoverableMessage.h" -#include - -namespace qpid { -namespace broker { - -/** - * The interface through which messages are added back to queues on - * recovery. - */ -class RecoverableQueue -{ -public: - typedef boost::shared_ptr shared_ptr; - - virtual void setPersistenceId(uint64_t id) = 0; - /** - * Used during recovery to add stored messages back to the queue - */ - virtual void recover(RecoverableMessage::shared_ptr msg) = 0; - virtual ~RecoverableQueue() {}; -}; - -}} - - -#endif diff --git a/cpp/src/broker/RecoveryManager.h b/cpp/src/broker/RecoveryManager.h deleted file mode 100644 index 3f351f6a2e..0000000000 --- a/cpp/src/broker/RecoveryManager.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * - * 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 _RecoveryManager_ -#define _RecoveryManager_ - -#include "RecoverableQueue.h" -#include "RecoverableMessage.h" -#include "../framing/Buffer.h" - -namespace qpid { -namespace broker { - - class RecoveryManager{ - public: - virtual ~RecoveryManager(){} - virtual void recoverExchange(framing::Buffer& buffer) = 0; - virtual RecoverableQueue::shared_ptr recoverQueue(framing::Buffer& buffer) = 0; - virtual RecoverableMessage::shared_ptr recoverMessage(framing::Buffer& buffer) = 0; - virtual void recoveryComplete() = 0; - }; - - -} -} - - -#endif diff --git a/cpp/src/broker/RecoveryManagerImpl.cpp b/cpp/src/broker/RecoveryManagerImpl.cpp deleted file mode 100644 index f82399f95c..0000000000 --- a/cpp/src/broker/RecoveryManagerImpl.cpp +++ /dev/null @@ -1,143 +0,0 @@ -/* - * - * 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 "RecoveryManagerImpl.h" - -#include "BrokerMessage.h" -#include "BrokerMessageMessage.h" -#include "BrokerQueue.h" - -using namespace qpid; -using namespace qpid::broker; -using boost::dynamic_pointer_cast; - - -static const uint8_t BASIC = 1; -static const uint8_t MESSAGE = 2; - -RecoveryManagerImpl::RecoveryManagerImpl(QueueRegistry& _queues, ExchangeRegistry& _exchanges, uint64_t _stagingThreshold) - : queues(_queues), exchanges(_exchanges), stagingThreshold(_stagingThreshold) {} - -RecoveryManagerImpl::~RecoveryManagerImpl() {} - -class RecoverableMessageImpl : public RecoverableMessage -{ - Message::shared_ptr msg; - const uint64_t stagingThreshold; -public: - RecoverableMessageImpl(Message::shared_ptr& _msg, uint64_t _stagingThreshold) - : msg(_msg), stagingThreshold(_stagingThreshold) {} - ~RecoverableMessageImpl() {}; - void setPersistenceId(uint64_t id); - bool loadContent(uint64_t available); - void decodeContent(framing::Buffer& buffer); - void recover(Queue::shared_ptr queue); -}; - -class RecoverableQueueImpl : public RecoverableQueue -{ - Queue::shared_ptr queue; -public: - RecoverableQueueImpl(Queue::shared_ptr& _queue) : queue(_queue) {} - ~RecoverableQueueImpl() {}; - void setPersistenceId(uint64_t id); - void recover(RecoverableMessage::shared_ptr msg); -}; - -void RecoveryManagerImpl::recoverExchange(framing::Buffer&) -{ - //TODO -} - -RecoverableQueue::shared_ptr RecoveryManagerImpl::recoverQueue(framing::Buffer& buffer) -{ - Queue::shared_ptr queue = Queue::decode(queues, buffer); - try { - Exchange::shared_ptr exchange = exchanges.getDefault(); - if (exchange) { - exchange->bind(queue, queue->getName(), 0); - } - } catch (ChannelException& e) { - //assume no default exchange has been declared - } - return RecoverableQueue::shared_ptr(new RecoverableQueueImpl(queue)); -} - -RecoverableMessage::shared_ptr RecoveryManagerImpl::recoverMessage(framing::Buffer& buffer) -{ - buffer.record(); - //peek at type: - Message::shared_ptr message(decodeMessageType(buffer) == MESSAGE ? - ((Message*) new MessageMessage()) : - ((Message*) new BasicMessage())); - buffer.restore(); - message->decodeHeader(buffer); - return RecoverableMessage::shared_ptr(new RecoverableMessageImpl(message, stagingThreshold)); -} - -void RecoveryManagerImpl::recoveryComplete() -{ - //TODO (finalise binding setup etc) -} - -uint8_t RecoveryManagerImpl::decodeMessageType(framing::Buffer& buffer) -{ - return buffer.getOctet(); -} - -void RecoveryManagerImpl::encodeMessageType(const Message& msg, framing::Buffer& buffer) -{ - buffer.putOctet(dynamic_cast(&msg) ? MESSAGE : BASIC); -} - -uint32_t RecoveryManagerImpl::encodedMessageTypeSize() -{ - return 1; -} - -bool RecoverableMessageImpl::loadContent(uint64_t available) -{ - return !stagingThreshold || available < stagingThreshold; -} - -void RecoverableMessageImpl::decodeContent(framing::Buffer& buffer) -{ - msg->decodeContent(buffer); -} - -void RecoverableMessageImpl::recover(Queue::shared_ptr queue) -{ - queue->recover(msg); -} - -void RecoverableMessageImpl::setPersistenceId(uint64_t id) -{ - msg->setPersistenceId(id); -} - -void RecoverableQueueImpl::recover(RecoverableMessage::shared_ptr msg) -{ - dynamic_pointer_cast(msg)->recover(queue); -} - -void RecoverableQueueImpl::setPersistenceId(uint64_t id) -{ - queue->setPersistenceId(id); -} diff --git a/cpp/src/broker/RecoveryManagerImpl.h b/cpp/src/broker/RecoveryManagerImpl.h deleted file mode 100644 index c40de7895f..0000000000 --- a/cpp/src/broker/RecoveryManagerImpl.h +++ /dev/null @@ -1,55 +0,0 @@ -/* - * - * 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 _RecoveryManagerImpl_ -#define _RecoveryManagerImpl_ - -#include -#include "ExchangeRegistry.h" -#include "QueueRegistry.h" -#include "RecoveryManager.h" - -namespace qpid { -namespace broker { - - class RecoveryManagerImpl : public RecoveryManager{ - QueueRegistry& queues; - ExchangeRegistry& exchanges; - const uint64_t stagingThreshold; - public: - RecoveryManagerImpl(QueueRegistry& queues, ExchangeRegistry& exchanges, uint64_t stagingThreshold); - ~RecoveryManagerImpl(); - - void recoverExchange(framing::Buffer& buffer); - RecoverableQueue::shared_ptr recoverQueue(framing::Buffer& buffer); - RecoverableMessage::shared_ptr recoverMessage(framing::Buffer& buffer); - void recoveryComplete(); - - static uint8_t decodeMessageType(framing::Buffer& buffer); - static void encodeMessageType(const Message& msg, framing::Buffer& buffer); - static uint32_t encodedMessageTypeSize(); - }; - - -} -} - - -#endif diff --git a/cpp/src/broker/Reference.cpp b/cpp/src/broker/Reference.cpp deleted file mode 100644 index b0b2d8a3c1..0000000000 --- a/cpp/src/broker/Reference.cpp +++ /dev/null @@ -1,53 +0,0 @@ -/* - * - * 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 -#include "Reference.h" -#include "BrokerMessageMessage.h" -#include "../QpidError.h" -#include "../gen/MessageAppendBody.h" -#include "CompletionHandler.h" - -namespace qpid { -namespace broker { - -Reference::shared_ptr ReferenceRegistry::open(const Reference::Id& id) { - ReferenceMap::iterator i = references.find(id); - if (i != references.end()) - throw ConnectionException(503, "Attempt to re-open reference " +id); - return references[id] = Reference::shared_ptr(new Reference(id, this)); -} - -Reference::shared_ptr ReferenceRegistry::get(const Reference::Id& id) { - ReferenceMap::iterator i = references.find(id); - if (i == references.end()) - throw ConnectionException(503, "Attempt to use non-existent reference "+id); - return i->second; -} - -void Reference::append(AppendPtr ptr) { - appends.push_back(ptr); - size += ptr->getBytes().length(); -} - -void Reference::close() { - messages.clear(); - registry->references.erase(getId()); -} - -}} // namespace qpid::broker diff --git a/cpp/src/broker/Reference.h b/cpp/src/broker/Reference.h deleted file mode 100644 index 277eb7b917..0000000000 --- a/cpp/src/broker/Reference.h +++ /dev/null @@ -1,114 +0,0 @@ -#ifndef _broker_Reference_h -#define _broker_Reference_h - -/* - * - * 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 -#include -#include -#include -#include - -namespace qpid { - -namespace framing { -class MessageAppendBody; -} - -namespace broker { - -class MessageMessage; -class ReferenceRegistry; - -// FIXME aconway 2007-03-27: Merge with client::IncomingMessage -// to common reference handling code. - -/** - * A reference is an accumulation point for data in a multi-frame - * message. A reference can be used by multiple transfer commands to - * create multiple messages, so the reference tracks which commands - * are using it. When the reference is closed, all the associated - * transfers are completed. - * - * THREAD UNSAFE: per-channel resource, access to channels is - * serialized. - */ -class Reference -{ - public: - typedef std::string Id; - typedef boost::shared_ptr shared_ptr; - typedef boost::shared_ptr MessagePtr; - typedef std::vector Messages; - typedef boost::shared_ptr AppendPtr; - typedef std::vector Appends; - - Reference(const Id& id_=Id(), ReferenceRegistry* reg=0) - : id(id_), size(0), registry(reg) {} - - const std::string& getId() const { return id; } - uint64_t getSize() const { return size; } - - /** Add a message to be completed with this reference */ - void addMessage(MessagePtr message) { messages.push_back(message); } - - /** Append more data to the reference */ - void append(AppendPtr ptr); - - /** Close the reference, complete each associated message */ - void close(); - - const Appends& getAppends() const { return appends; } - const Messages& getMessages() const { return messages; } - - private: - Id id; - uint64_t size; - ReferenceRegistry* registry; - Messages messages; - Appends appends; -}; - - -/** - * A registry/factory for references. - * - * THREAD UNSAFE: per-channel resource, access to channels is - * serialized. - */ -class ReferenceRegistry { - public: - ReferenceRegistry() {}; - Reference::shared_ptr open(const Reference::Id& id); - Reference::shared_ptr get(const Reference::Id& id); - - private: - typedef std::map ReferenceMap; - ReferenceMap references; - - // Reference calls references.erase(). - friend class Reference; -}; - - -}} // namespace qpid::broker - - - -#endif /*!_broker_Reference_h*/ diff --git a/cpp/src/broker/TopicExchange.cpp b/cpp/src/broker/TopicExchange.cpp deleted file mode 100644 index f29dfc38ba..0000000000 --- a/cpp/src/broker/TopicExchange.cpp +++ /dev/null @@ -1,154 +0,0 @@ -/* - * - * 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 "TopicExchange.h" -#include - -using namespace qpid::broker; -using namespace qpid::framing; -using namespace qpid::sys; - -// TODO aconway 2006-09-20: More efficient matching algorithm. -// Areas for improvement: -// - excessive string copying: should be 0 copy, match from original buffer. -// - match/lookup: use descision tree or other more efficient structure. - -Tokens& Tokens::operator=(const std::string& s) { - clear(); - if (s.empty()) return *this; - std::string::const_iterator i = s.begin(); - while (true) { - // Invariant: i is at the beginning of the next untokenized word. - std::string::const_iterator j = find(i, s.end(), '.'); - push_back(std::string(i, j)); - if (j == s.end()) return *this; - i = j + 1; - } - return *this; -} - -TopicPattern& TopicPattern::operator=(const Tokens& tokens) { - Tokens::operator=(tokens); - normalize(); - return *this; -} - -namespace { -const std::string hashmark("#"); -const std::string star("*"); -} - -void TopicPattern::normalize() { - std::string word; - Tokens::iterator i = begin(); - while (i != end()) { - if (*i == hashmark) { - ++i; - while (i != end()) { - // Invariant: *(i-1)==#, [begin()..i-1] is normalized. - if (*i == star) { // Move * before #. - std::swap(*i, *(i-1)); - ++i; - } else if (*i == hashmark) { - erase(i); // Remove extra # - } else { - break; - } - } - } else { - i ++; - } - } -} - - -namespace { -// TODO aconway 2006-09-20: Ineficient to convert every routingKey to a string. -// Need StringRef class that operates on a string in place witout copy. -// Should be applied everywhere strings are extracted from frames. -// -bool do_match(Tokens::const_iterator pattern_begin, Tokens::const_iterator pattern_end, Tokens::const_iterator target_begin, Tokens::const_iterator target_end) -{ - // Invariant: [pattern_begin..p) matches [target_begin..t) - Tokens::const_iterator p = pattern_begin; - Tokens::const_iterator t = target_begin; - while (p != pattern_end && t != target_end) - { - if (*p == star || *p == *t) { - ++p, ++t; - } else if (*p == hashmark) { - ++p; - if (do_match(p, pattern_end, t, target_end)) return true; - while (t != target_end) { - ++t; - if (do_match(p, pattern_end, t, target_end)) return true; - } - return false; - } else { - return false; - } - } - while (p != pattern_end && *p == hashmark) ++p; // Ignore trailing # - return t == target_end && p == pattern_end; -} -} - -bool TopicPattern::match(const Tokens& target) const -{ - return do_match(begin(), end(), target.begin(), target.end()); -} - -TopicExchange::TopicExchange(const string& _name) : Exchange(_name) { } - -void TopicExchange::bind(Queue::shared_ptr queue, const string& routingKey, const FieldTable* /*args*/){ - Monitor::ScopedLock l(lock); - TopicPattern routingPattern(routingKey); - bindings[routingPattern].push_back(queue); -} - -void TopicExchange::unbind(Queue::shared_ptr queue, const string& routingKey, const FieldTable* /*args*/){ - Monitor::ScopedLock l(lock); - BindingMap::iterator bi = bindings.find(TopicPattern(routingKey)); - Queue::vector& qv(bi->second); - if (bi == bindings.end()) return; - Queue::vector::iterator q = find(qv.begin(), qv.end(), queue); - if(q == qv.end()) return; - qv.erase(q); - if(qv.empty()) bindings.erase(bi); -} - - -void TopicExchange::route(Deliverable& msg, const string& routingKey, const FieldTable* /*args*/){ - Monitor::ScopedLock l(lock); - for (BindingMap::iterator i = bindings.begin(); i != bindings.end(); ++i) { - if (i->first.match(routingKey)) { - Queue::vector& qv(i->second); - for(Queue::vector::iterator j = qv.begin(); j != qv.end(); j++){ - msg.deliverTo(*j); - } - } - } -} - -TopicExchange::~TopicExchange() {} - -const std::string TopicExchange::typeName("topic"); - - diff --git a/cpp/src/broker/TopicExchange.h b/cpp/src/broker/TopicExchange.h deleted file mode 100644 index 8aba0911c0..0000000000 --- a/cpp/src/broker/TopicExchange.h +++ /dev/null @@ -1,100 +0,0 @@ -/* - * - * 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 _TopicExchange_ -#define _TopicExchange_ - -#include -#include -#include "BrokerExchange.h" -#include "../framing/FieldTable.h" -#include "BrokerMessage.h" -#include "../sys/Monitor.h" -#include "BrokerQueue.h" - -namespace qpid { -namespace broker { - -/** A vector of string tokens */ -class Tokens : public std::vector { - public: - Tokens() {}; - // Default copy, assign, dtor are sufficient. - - /** Tokenize s, provides automatic conversion of string to Tokens */ - Tokens(const std::string& s) { operator=(s); } - /** Tokenizing assignment operator s */ - Tokens & operator=(const std::string& s); - - private: - size_t hash; -}; - - -/** - * Tokens that have been normalized as a pattern and can be matched - * with topic Tokens. Normalized meands all sequences of mixed * and - * # are reduced to a series of * followed by at most one #. - */ -class TopicPattern : public Tokens -{ - public: - TopicPattern() {} - // Default copy, assign, dtor are sufficient. - TopicPattern(const Tokens& tokens) { operator=(tokens); } - TopicPattern(const std::string& str) { operator=(str); } - TopicPattern& operator=(const Tokens&); - TopicPattern& operator=(const std::string& str) { return operator=(Tokens(str)); } - - /** Match a topic */ - bool match(const std::string& topic) { return match(Tokens(topic)); } - bool match(const Tokens& topic) const; - - private: - void normalize(); -}; - -class TopicExchange : public virtual Exchange{ - typedef std::map BindingMap; - BindingMap bindings; - qpid::sys::Mutex lock; - - public: - static const std::string typeName; - - TopicExchange(const string& name); - - virtual std::string getType(){ return typeName; } - - virtual void bind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args); - - virtual void unbind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args); - - virtual void route(Deliverable& msg, const string& routingKey, const qpid::framing::FieldTable* args); - - virtual ~TopicExchange(); -}; - - - -} -} - -#endif diff --git a/cpp/src/broker/TransactionalStore.h b/cpp/src/broker/TransactionalStore.h deleted file mode 100644 index 2a2bac0c51..0000000000 --- a/cpp/src/broker/TransactionalStore.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * - * 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 _TransactionalStore_ -#define _TransactionalStore_ - -#include -#include -#include - -namespace qpid { -namespace broker { - -struct InvalidTransactionContextException : public std::exception {}; - -class TransactionContext { -public: - virtual ~TransactionContext(){} -}; - -class TPCTransactionContext : public TransactionContext { -public: - virtual ~TPCTransactionContext(){} -}; - -class TransactionalStore { -public: - virtual std::auto_ptr begin() = 0; - virtual std::auto_ptr begin(const std::string& xid) = 0; - virtual void prepare(TPCTransactionContext& txn) = 0; - virtual void commit(TransactionContext& txn) = 0; - virtual void abort(TransactionContext& txn) = 0; - - virtual void collectPreparedXids(std::set& xids) = 0; - - virtual ~TransactionalStore(){} -}; - -} -} - - -#endif diff --git a/cpp/src/broker/TxAck.cpp b/cpp/src/broker/TxAck.cpp deleted file mode 100644 index 60d8049102..0000000000 --- a/cpp/src/broker/TxAck.cpp +++ /dev/null @@ -1,54 +0,0 @@ -/* - * - * 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 "TxAck.h" - -using std::bind1st; -using std::bind2nd; -using std::mem_fun_ref; -using namespace qpid::broker; - -TxAck::TxAck(AccumulatedAck& _acked, std::list& _unacked) : - acked(_acked), unacked(_unacked){ - -} - -bool TxAck::prepare(TransactionContext* ctxt) throw(){ - try{ - //dequeue all acked messages from their queues - for (ack_iterator i = unacked.begin(); i != unacked.end(); i++) { - if (i->coveredBy(&acked)) { - i->discard(ctxt); - } - } - return true; - }catch(...){ - std::cout << "TxAck::prepare() - Failed to prepare" << std::endl; - return false; - } -} - -void TxAck::commit() throw(){ - //remove all acked records from the list - unacked.remove_if(bind2nd(mem_fun_ref(&DeliveryRecord::coveredBy), &acked)); -} - -void TxAck::rollback() throw(){ -} diff --git a/cpp/src/broker/TxAck.h b/cpp/src/broker/TxAck.h deleted file mode 100644 index 5e6d0a370c..0000000000 --- a/cpp/src/broker/TxAck.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * - * 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 _TxAck_ -#define _TxAck_ - -#include -#include -#include -#include "AccumulatedAck.h" -#include "DeliveryRecord.h" -#include "TxOp.h" - -namespace qpid { - namespace broker { - /** - * Defines the transactional behaviour for acks received by a - * transactional channel. - */ - class TxAck : public TxOp{ - AccumulatedAck& acked; - std::list& unacked; - - public: - /** - * @param acked a representation of the accumulation of - * acks received - * @param unacked the record of delivered messages - */ - TxAck(AccumulatedAck& acked, std::list& unacked); - virtual bool prepare(TransactionContext* ctxt) throw(); - virtual void commit() throw(); - virtual void rollback() throw(); - virtual ~TxAck(){} - }; - } -} - - -#endif diff --git a/cpp/src/broker/TxBuffer.cpp b/cpp/src/broker/TxBuffer.cpp deleted file mode 100644 index c526c88c25..0000000000 --- a/cpp/src/broker/TxBuffer.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * - * 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 "TxBuffer.h" - -#include -using boost::mem_fn; -using namespace qpid::broker; - -bool TxBuffer::prepare(TransactionalStore* const store) -{ - std::auto_ptr ctxt; - if(store) ctxt = store->begin(); - for(op_iterator i = ops.begin(); i < ops.end(); i++){ - if(!(*i)->prepare(ctxt.get())){ - if(store) store->abort(*ctxt); - return false; - } - } - if(store) store->commit(*ctxt); - return true; -} - -void TxBuffer::commit() -{ - for_each(ops.begin(), ops.end(), mem_fn(&TxOp::commit)); - ops.clear(); -} - -void TxBuffer::rollback() -{ - for_each(ops.begin(), ops.end(), mem_fn(&TxOp::rollback)); - ops.clear(); -} - -void TxBuffer::enlist(TxOp::shared_ptr op) -{ - ops.push_back(op); -} diff --git a/cpp/src/broker/TxBuffer.h b/cpp/src/broker/TxBuffer.h deleted file mode 100644 index 86ee8945b5..0000000000 --- a/cpp/src/broker/TxBuffer.h +++ /dev/null @@ -1,107 +0,0 @@ -/* - * - * 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 _TxBuffer_ -#define _TxBuffer_ - -#include -#include -#include -#include "TransactionalStore.h" -#include "TxOp.h" - -/** - * Represents a single transaction. As such, an instance of this class - * will hold a list of operations representing the workload of the - * transaction. This work can be committed or rolled back. Committing - * is a two-stage process: first all the operations should be - * prepared, then if that succeeds they can be committed. - * - * In the 2pc case, a successful prepare may be followed by either a - * commit or a rollback. - * - * Atomicity of prepare is ensured by using a lower level - * transactional facility. This saves explicitly rolling back all the - * successfully prepared ops when one of them fails. i.e. we do not - * use 2pc internally, we instead ensure that prepare is atomic at a - * lower level. This makes individual prepare operations easier to - * code. - * - * Transactions on a messaging broker effect three types of 'action': - * (1) updates to persistent storage (2) updates to transient storage - * or cached data (3) network writes. - * - * Of these, (1) should always occur atomically during prepare to - * ensure that if the broker crashes while a transaction is being - * completed the persistent state (which is all that then remains) is - * consistent. (3) can only be done on commit, after a successful - * prepare. There is a little more flexibility with (2) but any - * changes made during prepare should be subject to the control of the - * TransactionalStore in use. - */ -namespace qpid { - namespace broker { - class TxBuffer{ - typedef std::vector::iterator op_iterator; - std::vector ops; - public: - /** - * Requests that all ops are prepared. This should - * primarily involve making sure that a persistent record - * of the operations is stored where necessary. - * - * All ops will be prepared under a transaction on the - * specified store. If any operation fails on prepare, - * this transaction will be rolled back. - * - * Once prepared, a transaction can be committed (or in - * the 2pc case, rolled back). - * - * @returns true if all the operations prepared - * successfully, false if not. - */ - bool prepare(TransactionalStore* const store); - /** - * Signals that the ops all prepared all completed - * successfully and can now commit, i.e. the operation can - * now be fully carried out. - * - * Should only be called after a call to prepare() returns - * true. - */ - void commit(); - /** - * Rolls back all the operations. - * - * Should only be called either after a call to prepare() - * returns true (2pc) or instead of a prepare call - * ('server-local') - */ - void rollback(); - /** - * Adds an operation to the transaction. - */ - void enlist(TxOp::shared_ptr op); - }; - } -} - - -#endif diff --git a/cpp/src/broker/TxOp.h b/cpp/src/broker/TxOp.h deleted file mode 100644 index e687c437cc..0000000000 --- a/cpp/src/broker/TxOp.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * - * 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 _TxOp_ -#define _TxOp_ - -#include "TransactionalStore.h" -#include - -namespace qpid { - namespace broker { - class TxOp{ - public: - typedef boost::shared_ptr shared_ptr; - - virtual bool prepare(TransactionContext*) throw() = 0; - virtual void commit() throw() = 0; - virtual void rollback() throw() = 0; - virtual ~TxOp(){} - }; - } -} - - -#endif diff --git a/cpp/src/broker/TxPublish.cpp b/cpp/src/broker/TxPublish.cpp deleted file mode 100644 index 687505909c..0000000000 --- a/cpp/src/broker/TxPublish.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * - * 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 "TxPublish.h" - -using namespace qpid::broker; - -TxPublish::TxPublish(Message::shared_ptr _msg) : msg(_msg) {} - -bool TxPublish::prepare(TransactionContext* ctxt) throw(){ - try{ - for_each(queues.begin(), queues.end(), Prepare(ctxt, msg)); - return true; - }catch(...){ - std::cout << "TxPublish::prepare() - Failed to prepare" << std::endl; - return false; - } -} - -void TxPublish::commit() throw(){ - for_each(queues.begin(), queues.end(), Commit(msg)); -} - -void TxPublish::rollback() throw(){ -} - -void TxPublish::deliverTo(Queue::shared_ptr& queue){ - queues.push_back(queue); -} - -TxPublish::Prepare::Prepare(TransactionContext* _ctxt, Message::shared_ptr& _msg) - : ctxt(_ctxt), msg(_msg){} - -void TxPublish::Prepare::operator()(Queue::shared_ptr& queue){ - queue->enqueue(ctxt, msg); -} - -TxPublish::Commit::Commit(Message::shared_ptr& _msg) : msg(_msg){} - -void TxPublish::Commit::operator()(Queue::shared_ptr& queue){ - queue->process(msg); -} - diff --git a/cpp/src/broker/TxPublish.h b/cpp/src/broker/TxPublish.h deleted file mode 100644 index 29b1dc38af..0000000000 --- a/cpp/src/broker/TxPublish.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * - * 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 _TxPublish_ -#define _TxPublish_ - -#include -#include -#include -#include "Deliverable.h" -#include "BrokerMessage.h" -#include "MessageStore.h" -#include "BrokerQueue.h" -#include "TxOp.h" - -namespace qpid { - namespace broker { - /** - * Defines the behaviour for publish operations on a - * transactional channel. Messages are routed through - * exchanges when received but are not at that stage delivered - * to the matching queues, rather the queues are held in an - * instance of this class. On prepare() the message is marked - * enqueued to the relevant queues in the MessagesStore. On - * commit() the messages will be passed to the queue for - * dispatch or to be added to the in-memory queue. - */ - class TxPublish : public TxOp, public Deliverable{ - class Prepare{ - TransactionContext* ctxt; - Message::shared_ptr& msg; - public: - Prepare(TransactionContext* ctxt, Message::shared_ptr& msg); - void operator()(Queue::shared_ptr& queue); - }; - - class Commit{ - Message::shared_ptr& msg; - public: - Commit(Message::shared_ptr& msg); - void operator()(Queue::shared_ptr& queue); - }; - - Message::shared_ptr msg; - std::list queues; - - public: - TxPublish(Message::shared_ptr msg); - virtual bool prepare(TransactionContext* ctxt) throw(); - virtual void commit() throw(); - virtual void rollback() throw(); - - virtual void deliverTo(Queue::shared_ptr& queue); - - virtual ~TxPublish(){} - }; - } -} - - -#endif diff --git a/cpp/src/client/AckMode.h b/cpp/src/client/AckMode.h deleted file mode 100644 index 9ad5ef925c..0000000000 --- a/cpp/src/client/AckMode.h +++ /dev/null @@ -1,102 +0,0 @@ -#ifndef _client_AckMode_h -#define _client_AckMode_h - -/* - * - * 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. - * - */ - -namespace qpid { -namespace client { - -/** - * The available acknowledgements modes. - * - * \ingroup clientapi - */ -enum AckMode { - /** No acknowledgement will be sent, broker can - discard messages as soon as they are delivered - to a consumer using this mode. **/ - NO_ACK = 0, - /** Each message will be automatically - acknowledged as soon as it is delivered to the - application **/ - AUTO_ACK = 1, - /** Acknowledgements will be sent automatically, - but not for each message. **/ - LAZY_ACK = 2, - /** The application is responsible for explicitly - acknowledging messages. **/ - CLIENT_ACK = 3 -}; - -}} // namespace qpid::client - - - -#endif /*!_client_AckMode_h*/ -#ifndef _client_AckMode_h -#define _client_AckMode_h - -/* - * - * 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. - * - */ - -namespace qpid { -namespace client { - -/** - * The available acknowledgements modes. - * - * \ingroup clientapi - */ -enum AckMode { - /** No acknowledgement will be sent, broker can - discard messages as soon as they are delivered - to a consumer using this mode. **/ - NO_ACK = 0, - /** Each message will be automatically - acknowledged as soon as it is delivered to the - application **/ - AUTO_ACK = 1, - /** Acknowledgements will be sent automatically, - but not for each message. **/ - LAZY_ACK = 2, - /** The application is responsible for explicitly - acknowledging messages. **/ - CLIENT_ACK = 3 -}; - -}} // namespace qpid::client - - - -#endif /*!_client_AckMode_h*/ diff --git a/cpp/src/client/BasicMessageChannel.cpp b/cpp/src/client/BasicMessageChannel.cpp deleted file mode 100644 index c577c0a305..0000000000 --- a/cpp/src/client/BasicMessageChannel.cpp +++ /dev/null @@ -1,345 +0,0 @@ -/* - * - * 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 "BasicMessageChannel.h" -#include "../framing/AMQMethodBody.h" -#include "ClientChannel.h" -#include "ReturnedMessageHandler.h" -#include "MessageListener.h" -#include "../framing/FieldTable.h" -#include "Connection.h" -#include -#include -#include -#include - -namespace qpid { -namespace client { - -using namespace std; -using namespace sys; -using namespace framing; -using boost::format; - -namespace { - -// Destination name constants -const std::string BASIC_GET("__basic_get__"); -const std::string BASIC_RETURN("__basic_return__"); - -// Reference name constant -const std::string BASIC_REF("__basic_reference__"); -} - -BasicMessageChannel::BasicMessageChannel(Channel& ch) - : channel(ch), returnsHandler(0) -{ - incoming.addDestination(BASIC_RETURN, destDispatch); -} - -void BasicMessageChannel::consume( - Queue& queue, std::string& tag, MessageListener* listener, - AckMode ackMode, bool noLocal, bool synch, const FieldTable* fields) -{ - { - // Note we create a consumer even if tag="". In that case - // It will be renamed when we handle BasicConsumeOkBody. - // - Mutex::ScopedLock l(lock); - ConsumerMap::iterator i = consumers.find(tag); - if (i != consumers.end()) - THROW_QPID_ERROR(CLIENT_ERROR, - "Consumer already exists with tag="+tag); - Consumer& c = consumers[tag]; - c.listener = listener; - c.ackMode = ackMode; - c.lastDeliveryTag = 0; - } - - // FIXME aconway 2007-03-23: get processed in both. - - // BasicConsumeOkBody is really processed in handle(), here - // we just pick up the tag to return to the user. - // - // We can't process it here because messages for the consumer may - // already be arriving. - // - BasicConsumeOkBody::shared_ptr ok = - channel.sendAndReceiveSync( - synch, - make_shared_ptr(new BasicConsumeBody( - channel.version, 0, queue.getName(), tag, noLocal, - ackMode == NO_ACK, false, !synch, - fields ? *fields : FieldTable()))); - tag = ok->getConsumerTag(); -} - - -void BasicMessageChannel::cancel(const std::string& tag, bool synch) { - Consumer c; - { - Mutex::ScopedLock l(lock); - ConsumerMap::iterator i = consumers.find(tag); - if (i == consumers.end()) - return; - c = i->second; - consumers.erase(i); - } - if(c.ackMode == LAZY_ACK && c.lastDeliveryTag > 0) - channel.send(new BasicAckBody(channel.version, c.lastDeliveryTag, true)); - channel.sendAndReceiveSync( - synch, make_shared_ptr(new BasicCancelBody(channel.version, tag, !synch))); -} - -void BasicMessageChannel::close(){ - ConsumerMap consumersCopy; - { - Mutex::ScopedLock l(lock); - consumersCopy = consumers; - consumers.clear(); - } - destGet.shutdown(); - destDispatch.shutdown(); - for (ConsumerMap::iterator i=consumersCopy.begin(); - i != consumersCopy.end(); ++i) - { - Consumer& c = i->second; - if ((c.ackMode == LAZY_ACK || c.ackMode == AUTO_ACK) - && c.lastDeliveryTag > 0) - { - channel.send(new BasicAckBody(channel.version, c.lastDeliveryTag, true)); - } - } -} - - -bool BasicMessageChannel::get( - Message& msg, const Queue& queue, AckMode ackMode) -{ - // Prepare for incoming response - incoming.addDestination(BASIC_GET, destGet); - channel.send( - new BasicGetBody(channel.version, 0, queue.getName(), ackMode)); - bool got = destGet.wait(msg); - return got; -} - -void BasicMessageChannel::publish( - const Message& msg, const Exchange& exchange, - const std::string& routingKey, bool mandatory, bool immediate) -{ - const string e = exchange.getName(); - string key = routingKey; - - // Make a header for the message - AMQHeaderBody::shared_ptr header(new AMQHeaderBody(BASIC)); - BasicHeaderProperties::copy( - *static_cast(header->getProperties()), msg); - header->setContentSize(msg.getData().size()); - - channel.send( - new BasicPublishBody( - channel.version, 0, e, key, mandatory, immediate)); - channel.send(header); - string data = msg.getData(); - u_int64_t data_length = data.length(); - if(data_length > 0){ - //frame itself uses 8 bytes - u_int32_t frag_size = channel.connection->getMaxFrameSize() - 8; - if(data_length < frag_size){ - channel.send(new AMQContentBody(data)); - }else{ - u_int32_t offset = 0; - u_int32_t remaining = data_length - offset; - while (remaining > 0) { - u_int32_t length = remaining > frag_size ? frag_size : remaining; - string frag(data.substr(offset, length)); - channel.send(new AMQContentBody(frag)); - - offset += length; - remaining = data_length - offset; - } - } - } -} - -void BasicMessageChannel::handle(boost::shared_ptr method) { - assert(method->amqpClassId() ==BasicGetBody::CLASS_ID); - switch(method->amqpMethodId()) { - case BasicGetOkBody::METHOD_ID: { - incoming.openReference(BASIC_REF); - incoming.createMessage(BASIC_GET, BASIC_REF); - return; - } - case BasicGetEmptyBody::METHOD_ID: { - incoming.getDestination(BASIC_GET).empty(); - incoming.removeDestination(BASIC_GET); - return; - } - case BasicDeliverBody::METHOD_ID: { - BasicDeliverBody::shared_ptr deliver= - boost::shared_polymorphic_downcast(method); - incoming.openReference(BASIC_REF); - Message& msg = incoming.createMessage( - deliver->getConsumerTag(), BASIC_REF); - msg.setDestination(deliver->getConsumerTag()); - msg.setDeliveryTag(deliver->getDeliveryTag()); - msg.setRedelivered(deliver->getRedelivered()); - return; - } - case BasicReturnBody::METHOD_ID: { - incoming.openReference(BASIC_REF); - incoming.createMessage(BASIC_RETURN, BASIC_REF); - return; - } - case BasicConsumeOkBody::METHOD_ID: { - Mutex::ScopedLock l(lock); - BasicConsumeOkBody::shared_ptr consumeOk = - boost::shared_polymorphic_downcast(method); - std::string tag = consumeOk->getConsumerTag(); - ConsumerMap::iterator i = consumers.find(std::string()); - if (i != consumers.end()) { - // Need to rename the un-named consumer. - if (consumers.find(tag) == consumers.end()) { - consumers[tag] = i->second; - consumers.erase(i); - } - else // Tag already exists. - throw ChannelException(404, "Tag already exists: "+tag); - } - // FIXME aconway 2007-03-23: Integrate consumer & destination - // maps. - incoming.addDestination(tag, destDispatch); - return; - } - } - throw Channel::UnknownMethod(); -} - -void BasicMessageChannel::handle(AMQHeaderBody::shared_ptr header) { - BasicHeaderProperties* props = - boost::polymorphic_downcast( - header->getProperties()); - IncomingMessage::Reference& ref = incoming.getReference(BASIC_REF); - assert (ref.messages.size() == 1); - ref.messages.front().BasicHeaderProperties::operator=(*props); - incoming_size = header->getContentSize(); - if (incoming_size==0) - incoming.closeReference(BASIC_REF); -} - -void BasicMessageChannel::handle(AMQContentBody::shared_ptr content){ - incoming.appendReference(BASIC_REF, content->getData()); - size_t size = incoming.getReference(BASIC_REF).data.size(); - if (size >= incoming_size) { - incoming.closeReference(BASIC_REF); - if (size > incoming_size) - throw ChannelException(502, "Content exceeded declared size"); - } -} - -void BasicMessageChannel::deliver(Consumer& consumer, Message& msg){ - //record delivery tag: - consumer.lastDeliveryTag = msg.getDeliveryTag(); - - //allow registered listener to handle the message - consumer.listener->received(msg); - - if(channel.isOpen()){ - bool multiple(false); - switch(consumer.ackMode){ - case LAZY_ACK: - multiple = true; - if(++(consumer.count) < channel.getPrefetch()) - break; - //else drop-through - case AUTO_ACK: - consumer.lastDeliveryTag = 0; - channel.send( - new BasicAckBody( - channel.version, - msg.getDeliveryTag(), - multiple)); - case NO_ACK: // Nothing to do - case CLIENT_ACK: // User code must ack. - break; - // TODO aconway 2007-02-22: Provide a way for user - // to ack! - } - } - - //as it stands, transactionality is entirely orthogonal to ack - //mode, though the acks will not be processed by the broker under - //a transaction until it commits. -} - - -void BasicMessageChannel::run() { - while(channel.isOpen()) { - try { - Message msg; - bool gotMessge = destDispatch.wait(msg); - if (gotMessge) { - if(msg.getDestination() == BASIC_RETURN) { - ReturnedMessageHandler* handler=0; - { - Mutex::ScopedLock l(lock); - handler=returnsHandler; - } - if(handler != 0) - handler->returned(msg); - } - else { - Consumer consumer; - { - Mutex::ScopedLock l(lock); - ConsumerMap::iterator i = consumers.find( - msg.getDestination()); - if(i == consumers.end()) - THROW_QPID_ERROR(PROTOCOL_ERROR+504, - "Unknown consumer tag=" + - msg.getDestination()); - consumer = i->second; - } - deliver(consumer, msg); - } - } - } - catch (const ShutdownException&) { - /* Orderly shutdown */ - } - catch (const Exception& e) { - // FIXME aconway 2007-02-20: Report exception to user. - cout << "client::BasicMessageChannel::run() terminated by: " - << e.toString() << endl; - } - } -} - -void BasicMessageChannel::setReturnedMessageHandler(ReturnedMessageHandler* handler){ - Mutex::ScopedLock l(lock); - returnsHandler = handler; -} - -void BasicMessageChannel::setQos(){ - channel.sendAndReceive( - make_shared_ptr(new BasicQosBody(channel.version, 0, channel.getPrefetch(), false))); - if(channel.isTransactional()) - channel.sendAndReceive(make_shared_ptr(new TxSelectBody(channel.version))); -} - -}} // namespace qpid::client diff --git a/cpp/src/client/BasicMessageChannel.h b/cpp/src/client/BasicMessageChannel.h deleted file mode 100644 index 13e1cf1e00..0000000000 --- a/cpp/src/client/BasicMessageChannel.h +++ /dev/null @@ -1,90 +0,0 @@ -#ifndef _client_BasicMessageChannel_h -#define _client_BasicMessageChannel_h - -/* - * - * 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 "MessageChannel.h" -#include "IncomingMessage.h" -#include - -namespace qpid { -namespace client { -/** - * Messaging implementation using AMQP 0-8 BasicMessageChannel class - * to send and receiving messages. - */ -class BasicMessageChannel : public MessageChannel -{ - public: - BasicMessageChannel(Channel& parent); - - void consume( - Queue& queue, std::string& tag, MessageListener* listener, - AckMode ackMode = NO_ACK, bool noLocal = false, bool synch = true, - const framing::FieldTable* fields = 0); - - void cancel(const std::string& tag, bool synch = true); - - bool get(Message& msg, const Queue& queue, AckMode ackMode = NO_ACK); - - void publish(const Message& msg, const Exchange& exchange, - const std::string& routingKey, - bool mandatory = false, bool immediate = false); - - void setReturnedMessageHandler(ReturnedMessageHandler* handler); - - void run(); - - void handle(boost::shared_ptr); - - void handle(shared_ptr); - - void handle(shared_ptr); - - void setQos(); - - void close(); - - private: - - struct Consumer{ - MessageListener* listener; - AckMode ackMode; - int count; - u_int64_t lastDeliveryTag; - }; - typedef std::map ConsumerMap; - - void deliver(Consumer& consumer, Message& msg); - - sys::Mutex lock; - Channel& channel; - IncomingMessage incoming; - uint64_t incoming_size; - ConsumerMap consumers ; - ReturnedMessageHandler* returnsHandler; - IncomingMessage::WaitableDestination destGet; - IncomingMessage::WaitableDestination destDispatch; -}; - -}} // namespace qpid::client - - - -#endif /*!_client_BasicMessageChannel_h*/ diff --git a/cpp/src/client/ClientAdapter.cpp b/cpp/src/client/ClientAdapter.cpp deleted file mode 100644 index f5eb2f4536..0000000000 --- a/cpp/src/client/ClientAdapter.cpp +++ /dev/null @@ -1,70 +0,0 @@ -/* - * - * 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 "../gen/AMQP_ClientOperations.h" -#include "ClientAdapter.h" -#include "Connection.h" -#include "../Exception.h" -#include "../framing/AMQMethodBody.h" - -namespace qpid { -namespace client { - -using namespace qpid; -using namespace qpid::framing; - -typedef std::vector::iterator queue_iterator; - -void ClientAdapter::handleMethodInContext( - boost::shared_ptr method, - const MethodContext& context -) -{ - try{ - method->invoke(*clientOps, context); - }catch(ChannelException& e){ - connection.client->getChannel().close( - context, e.code, e.toString(), - method->amqpClassId(), method->amqpMethodId()); - connection.closeChannel(getId()); - }catch(ConnectionException& e){ - connection.client->getConnection().close( - context, e.code, e.toString(), - method->amqpClassId(), method->amqpMethodId()); - }catch(std::exception& e){ - connection.client->getConnection().close( - context, 541/*internal error*/, e.what(), - method->amqpClassId(), method->amqpMethodId()); - } -} - -void ClientAdapter::handleHeader(AMQHeaderBody::shared_ptr body) { - channel->handleHeader(body); -} - -void ClientAdapter::handleContent(AMQContentBody::shared_ptr body) { - channel->handleContent(body); -} - -void ClientAdapter::handleHeartbeat(AMQHeartbeatBody::shared_ptr) { - // TODO aconway 2007-01-17: Implement heartbeats. -} - - - -}} // namespace qpid::client - diff --git a/cpp/src/client/ClientAdapter.h b/cpp/src/client/ClientAdapter.h deleted file mode 100644 index ca029a793f..0000000000 --- a/cpp/src/client/ClientAdapter.h +++ /dev/null @@ -1,66 +0,0 @@ -#ifndef _client_ClientAdapter_h -#define _client_ClientAdapter_h - -/* - * - * 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 "../framing/ChannelAdapter.h" -#include "ClientChannel.h" - -namespace qpid { -namespace client { - -class AMQMethodBody; -class Connection; - -/** - * Per-channel protocol adapter. - * - * Translates protocol bodies into calls on the core Channel, - * Connection and Client objects. - * - * Owns a channel, has references to Connection and Client. - */ -class ClientAdapter : public framing::ChannelAdapter -{ - public: - ClientAdapter(std::auto_ptr ch, Connection&, Client&); - Channel& getChannel() { return *channel; } - - void handleHeader(boost::shared_ptr); - void handleContent(boost::shared_ptr); - void handleHeartbeat(boost::shared_ptr); - - private: - void handleMethodInContext( - boost::shared_ptr method, - const framing::MethodContext& context); - - class ClientOps; - - std::auto_ptr channel; - Connection& connection; - Client& client; - boost::shared_ptr clientOps; -}; - -}} // namespace qpid::client - - - -#endif /*!_client_ClientAdapter_h*/ diff --git a/cpp/src/client/ClientChannel.cpp b/cpp/src/client/ClientChannel.cpp deleted file mode 100644 index 533b590010..0000000000 --- a/cpp/src/client/ClientChannel.cpp +++ /dev/null @@ -1,341 +0,0 @@ -/* - * - * 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 -#include "ClientChannel.h" -#include "../sys/Monitor.h" -#include "ClientMessage.h" -#include "../QpidError.h" -#include "MethodBodyInstances.h" -#include "Connection.h" -#include "BasicMessageChannel.h" -#include "MessageMessageChannel.h" - -// FIXME aconway 2007-01-26: Evaluate all throws, ensure consistent -// handling of errors that should close the connection or the channel. -// Make sure the user thread receives a connection in each case. -// -using namespace std; -using namespace boost; -using namespace qpid::client; -using namespace qpid::framing; -using namespace qpid::sys; - -Channel::Channel(bool _transactional, u_int16_t _prefetch, InteropMode mode) : - connection(0), prefetch(_prefetch), transactional(_transactional) -{ - switch (mode) { - case AMQP_08: messaging.reset(new BasicMessageChannel(*this)); break; - case AMQP_09: messaging.reset(new MessageMessageChannel(*this)); break; - default: assert(0); QPID_ERROR(INTERNAL_ERROR, "Invalid interop-mode."); - } -} - -Channel::~Channel(){ - close(); -} - -void Channel::open(ChannelId id, Connection& con) -{ - if (isOpen()) - THROW_QPID_ERROR(INTERNAL_ERROR, "Attempt to re-open channel "+id); - connection = &con; - init(id, con, con.getVersion()); // ChannelAdapter initialization. - string oob; - if (id != 0) - sendAndReceive(make_shared_ptr(new ChannelOpenBody(version, oob))); -} - -void Channel::protocolInit( - const std::string& uid, const std::string& pwd, const std::string& vhost) { - assert(connection); - responses.expect(); - connection->connector->init(); // Send ProtocolInit block. - ConnectionStartBody::shared_ptr connectionStart = - responses.receive(); - - FieldTable props; - string mechanism("PLAIN"); - string response = ((char)0) + uid + ((char)0) + pwd; - string locale("en_US"); - ConnectionTuneBody::shared_ptr proposal = - sendAndReceive( - make_shared_ptr(new ConnectionStartOkBody( - version, connectionStart->getRequestId(), - props, mechanism, - response, locale))); - - /** - * Assume for now that further challenges will not be required - //receive connection.secure - responses.receive(connection_secure)); - //send connection.secure-ok - connection->send(new AMQFrame(0, new ConnectionSecureOkBody(response))); - **/ - - send(new ConnectionTuneOkBody( - version, proposal->getRequestId(), - proposal->getChannelMax(), connection->getMaxFrameSize(), - proposal->getHeartbeat())); - - uint16_t heartbeat = proposal->getHeartbeat(); - connection->connector->setReadTimeout(heartbeat * 2); - connection->connector->setWriteTimeout(heartbeat); - - // Send connection open. - std::string capabilities; - responses.expect(); - send(new ConnectionOpenBody(version, vhost, capabilities, true)); - //receive connection.open-ok (or redirect, but ignore that for now - //esp. as using force=true). - AMQMethodBody::shared_ptr openResponse = responses.receive(); - if(openResponse->isA()) { - //ok - }else if(openResponse->isA()){ - //ignore for now - ConnectionRedirectBody::shared_ptr redirect( - shared_polymorphic_downcast(openResponse)); - cout << "Received redirection to " << redirect->getHost() - << endl; - } else { - THROW_QPID_ERROR(PROTOCOL_ERROR, "Bad response to Connection.open"); - } -} - -bool Channel::isOpen() const { return connection; } - -void Channel::setQos() { - messaging->setQos(); -} - -void Channel::setPrefetch(uint16_t _prefetch){ - prefetch = _prefetch; - setQos(); -} - -void Channel::declareExchange(Exchange& exchange, bool synch){ - string name = exchange.getName(); - string type = exchange.getType(); - FieldTable args; - sendAndReceiveSync( - synch, - make_shared_ptr(new ExchangeDeclareBody( - version, 0, name, type, false, false, false, false, !synch, args))); -} - -void Channel::deleteExchange(Exchange& exchange, bool synch){ - string name = exchange.getName(); - sendAndReceiveSync( - synch, - make_shared_ptr(new ExchangeDeleteBody(version, 0, name, false, !synch))); -} - -void Channel::declareQueue(Queue& queue, bool synch){ - string name = queue.getName(); - FieldTable args; - QueueDeclareOkBody::shared_ptr response = - sendAndReceiveSync( - synch, - make_shared_ptr(new QueueDeclareBody( - version, 0, name, false/*passive*/, queue.isDurable(), - queue.isExclusive(), queue.isAutoDelete(), !synch, args))); - if(synch) { - if(queue.getName().length() == 0) - queue.setName(response->getQueue()); - } -} - -void Channel::deleteQueue(Queue& queue, bool ifunused, bool ifempty, bool synch){ - //ticket, queue, ifunused, ifempty, nowait - string name = queue.getName(); - sendAndReceiveSync( - synch, - make_shared_ptr(new QueueDeleteBody(version, 0, name, ifunused, ifempty, !synch))); -} - -void Channel::bind(const Exchange& exchange, const Queue& queue, const std::string& key, const FieldTable& args, bool synch){ - string e = exchange.getName(); - string q = queue.getName(); - sendAndReceiveSync( - synch, - make_shared_ptr(new QueueBindBody(version, 0, q, e, key,!synch, args))); -} - -void Channel::commit(){ - sendAndReceive(make_shared_ptr(new TxCommitBody(version))); -} - -void Channel::rollback(){ - sendAndReceive(make_shared_ptr(new TxRollbackBody(version))); -} - -void Channel::handleMethodInContext( - AMQMethodBody::shared_ptr method, const MethodContext&) -{ - // TODO aconway 2007-03-23: Special case for consume OK as it - // is both an expected response and needs handling in this thread. - // Need to review & reationalize the client-side processing model. - if (method->isA()) { - messaging->handle(method); - responses.signalResponse(method); - return; - } - if(responses.isWaiting()) { - responses.signalResponse(method); - return; - } - try { - switch (method->amqpClassId()) { - case MessageOkBody::CLASS_ID: - case BasicGetOkBody::CLASS_ID: messaging->handle(method); break; - case ChannelCloseBody::CLASS_ID: handleChannel(method); break; - case ConnectionCloseBody::CLASS_ID: handleConnection(method); break; - default: throw UnknownMethod(); - } - } - catch (const UnknownMethod&) { - connection->close( - 504, "Unknown method", - method->amqpClassId(), method->amqpMethodId()); - } - } - -void Channel::handleChannel(AMQMethodBody::shared_ptr method) { - switch (method->amqpMethodId()) { - case ChannelCloseBody::METHOD_ID: - peerClose(shared_polymorphic_downcast(method)); - return; - case ChannelFlowBody::METHOD_ID: - // FIXME aconway 2007-02-22: Not yet implemented. - return; - } - throw UnknownMethod(); -} - -void Channel::handleConnection(AMQMethodBody::shared_ptr method) { - if (method->amqpMethodId() == ConnectionCloseBody::METHOD_ID) { - connection->close(); - return; - } - throw UnknownMethod(); -} - -void Channel::handleHeader(AMQHeaderBody::shared_ptr body){ - messaging->handle(body); -} - -void Channel::handleContent(AMQContentBody::shared_ptr body){ - messaging->handle(body); -} - -void Channel::handleHeartbeat(AMQHeartbeatBody::shared_ptr /*body*/){ - THROW_QPID_ERROR(PROTOCOL_ERROR + 504, "Channel received heartbeat"); -} - -void Channel::start(){ - dispatcher = Thread(*messaging); -} - -// Close called by local application. -void Channel::close( - uint16_t code, const std::string& text, - ClassId classId, MethodId methodId) -{ - if (isOpen()) { - try { - if (getId() != 0) { - sendAndReceive( - make_shared_ptr(new ChannelCloseBody( - version, code, text, classId, methodId))); - } - static_cast(connection)->erase(getId()); - closeInternal(); - } catch (...) { - static_cast(connection)->erase(getId()); - closeInternal(); - throw; - } - } -} - -// Channel closed by peer. -void Channel::peerClose(ChannelCloseBody::shared_ptr) { - assert(isOpen()); - closeInternal(); -} - -void Channel::closeInternal() { - if (isOpen()); - { - messaging->close(); - connection = 0; - // A 0 response means we are closed. - responses.signalResponse(AMQMethodBody::shared_ptr()); - } - dispatcher.join(); -} - -AMQMethodBody::shared_ptr Channel::sendAndReceive( - AMQMethodBody::shared_ptr toSend, ClassId c, MethodId m) -{ - responses.expect(); - send(toSend); - return responses.receive(c, m); -} - -AMQMethodBody::shared_ptr Channel::sendAndReceiveSync( - bool sync, AMQMethodBody::shared_ptr body, ClassId c, MethodId m) -{ - if(sync) - return sendAndReceive(body, c, m); - else { - send(body); - return AMQMethodBody::shared_ptr(); - } -} - -void Channel::consume( - Queue& queue, std::string& tag, MessageListener* listener, - AckMode ackMode, bool noLocal, bool synch, const FieldTable* fields) { - messaging->consume(queue, tag, listener, ackMode, noLocal, synch, fields); -} - -void Channel::cancel(const std::string& tag, bool synch) { - messaging->cancel(tag, synch); -} - -bool Channel::get(Message& msg, const Queue& queue, AckMode ackMode) { - return messaging->get(msg, queue, ackMode); -} - -void Channel::publish(const Message& msg, const Exchange& exchange, - const std::string& routingKey, - bool mandatory, bool immediate) { - messaging->publish(msg, exchange, routingKey, mandatory, immediate); -} - -void Channel::setReturnedMessageHandler(ReturnedMessageHandler* handler) { - messaging->setReturnedMessageHandler(handler); -} - -void Channel::run() { - messaging->run(); -} - diff --git a/cpp/src/client/ClientChannel.h b/cpp/src/client/ClientChannel.h deleted file mode 100644 index 328fc23f68..0000000000 --- a/cpp/src/client/ClientChannel.h +++ /dev/null @@ -1,356 +0,0 @@ -#ifndef _client_ClientChannel_h -#define _client_ClientChannel_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 -#include "../framing/amqp_framing.h" -#include "ClientExchange.h" -#include "ClientMessage.h" -#include "ClientQueue.h" -#include "ResponseHandler.h" -#include "../framing/ChannelAdapter.h" -#include "../sys/Thread.h" -#include "AckMode.h" - -namespace qpid { - -namespace framing { -class ChannelCloseBody; -class AMQMethodBody; -} - -namespace client { - -class Connection; -class MessageChannel; -class MessageListener; -class ReturnedMessageHandler; - -/** - * Represents an AMQP channel, i.e. loosely a session of work. It - * is through a channel that most of the AMQP 'methods' are - * exposed. - * - * \ingroup clientapi - */ -class Channel : public framing::ChannelAdapter -{ - private: - struct UnknownMethod {}; - typedef shared_ptr MethodPtr; - - sys::Mutex lock; - boost::scoped_ptr messaging; - Connection* connection; - sys::Thread dispatcher; - ResponseHandler responses; - - uint16_t prefetch; - const bool transactional; - framing::ProtocolVersion version; - - void handleHeader(framing::AMQHeaderBody::shared_ptr body); - void handleContent(framing::AMQContentBody::shared_ptr body); - void handleHeartbeat(framing::AMQHeartbeatBody::shared_ptr body); - void handleMethodInContext( - framing::AMQMethodBody::shared_ptr, const framing::MethodContext&); - void handleChannel(framing::AMQMethodBody::shared_ptr method); - void handleConnection(framing::AMQMethodBody::shared_ptr method); - - void setQos(); - - void protocolInit( - const std::string& uid, const std::string& pwd, - const std::string& vhost); - - framing::AMQMethodBody::shared_ptr sendAndReceive( - framing::AMQMethodBody::shared_ptr, - framing::ClassId, framing::MethodId); - - framing::AMQMethodBody::shared_ptr sendAndReceiveSync( - bool sync, - framing::AMQMethodBody::shared_ptr, - framing::ClassId, framing::MethodId); - - template - boost::shared_ptr sendAndReceive(framing::AMQMethodBody::shared_ptr body) { - return boost::shared_polymorphic_downcast( - sendAndReceive(body, BodyType::CLASS_ID, BodyType::METHOD_ID)); - } - - template - boost::shared_ptr sendAndReceiveSync( - bool sync, framing::AMQMethodBody::shared_ptr body) { - return boost::shared_polymorphic_downcast( - sendAndReceiveSync( - sync, body, BodyType::CLASS_ID, BodyType::METHOD_ID)); - } - - void open(framing::ChannelId, Connection&); - void closeInternal(); - void peerClose(boost::shared_ptr); - - // FIXME aconway 2007-02-23: Get rid of friendships. - friend class Connection; - friend class BasicMessageChannel; // for sendAndReceive. - friend class MessageMessageChannel; // for sendAndReceive. - - public: - enum InteropMode { AMQP_08, AMQP_09 }; - - /** - * Creates a channel object. - * - * @param transactional if true, the publishing and acknowledgement - * of messages will be transactional and can be committed or - * aborted in atomic units (@see commit(), @see rollback()) - * - * @param prefetch specifies the number of unacknowledged - * messages the channel is willing to have sent to it - * asynchronously - * - * @param messageImpl Alternate messaging implementation class to - * allow alternate protocol implementations of messaging - * operations. Takes ownership. - */ - Channel( - bool transactional = false, u_int16_t prefetch = 500, - InteropMode=AMQP_08); - - ~Channel(); - - /** - * Declares an exchange. - * - * In AMQP Exchanges are the destinations to which messages - * are published. They have Queues bound to them and route - * messages they receive to those queues. The routing rules - * depend on the type of the exchange. - * - * @param exchange an Exchange object representing the - * exchange to declare - * - * @param synch if true this call will block until a response - * is received from the broker - */ - void declareExchange(Exchange& exchange, bool synch = true); - /** - * Deletes an exchange - * - * @param exchange an Exchange object representing the exchange to delete - * - * @param synch if true this call will block until a response - * is received from the broker - */ - void deleteExchange(Exchange& exchange, bool synch = true); - /** - * Declares a Queue - * - * @param queue a Queue object representing the queue to declare - * - * @param synch if true this call will block until a response - * is received from the broker - */ - void declareQueue(Queue& queue, bool synch = true); - /** - * Deletes a Queue - * - * @param queue a Queue object representing the queue to delete - * - * @param synch if true this call will block until a response - * is received from the broker - */ - void deleteQueue(Queue& queue, bool ifunused = false, bool ifempty = false, bool synch = true); - /** - * Binds a queue to an exchange. The exact semantics of this - * (in particular how 'routing keys' and 'binding arguments' - * are used) depends on the type of the exchange. - * - * @param exchange an Exchange object representing the - * exchange to bind to - * - * @param queue a Queue object representing the queue to be - * bound - * - * @param key the 'routing key' for the binding - * - * @param args the 'binding arguments' for the binding - * - * @param synch if true this call will block until a response - * is received from the broker - */ - void bind(const Exchange& exchange, const Queue& queue, - const std::string& key, const framing::FieldTable& args, - bool synch = true); - - /** - * For a transactional channel this will commit all - * publications and acknowledgements since the last commit (or - * the channel was opened if there has been no previous - * commit). This will cause published messages to become - * available to consumers and acknowledged messages to be - * consumed and removed from the queues they were dispatched - * from. - * - * Transactionailty of a channel is specified when the channel - * object is created (@see Channel()). - */ - void commit(); - - /** - * For a transactional channel, this will rollback any - * publications or acknowledgements. It will be as if the - * ppblished messages were never sent and the acknowledged - * messages were never consumed. - */ - void rollback(); - - /** - * Change the prefetch in use. - */ - void setPrefetch(uint16_t prefetch); - - uint16_t getPrefetch() { return prefetch; } - - /** - * Start message dispatching on a new thread - */ - void start(); - - /** - * Close the channel with optional error information. - * Closing a channel that is not open has no effect. - */ - void close( - framing::ReplyCode = 200, const std::string& ="OK", - framing::ClassId = 0, framing::MethodId = 0); - - /** True if the channel is transactional */ - bool isTransactional() { return transactional; } - - /** True if the channel is open */ - bool isOpen() const; - - /** Get the connection associated with this channel */ - Connection& getConnection() { return *connection; } - - /** Return the protocol version */ - framing::ProtocolVersion getVersion() const { return version ; } - - /** - * Creates a 'consumer' for a queue. Messages in (or arriving - * at) that queue will be delivered to consumers - * asynchronously. - * - * @param queue a Queue instance representing the queue to - * consume from - * - * @param tag an identifier to associate with the consumer - * that can be used to cancel its subscription (if empty, this - * will be assigned by the broker) - * - * @param listener a pointer to an instance of an - * implementation of the MessageListener interface. Messages - * received from this queue for this consumer will result in - * invocation of the received() method on the listener, with - * the message itself passed in. - * - * @param ackMode the mode of acknowledgement that the broker - * should assume for this consumer. @see AckMode - * - * @param noLocal if true, this consumer will not be sent any - * message published by this connection - * - * @param synch if true this call will block until a response - * is received from the broker - */ - void consume( - Queue& queue, std::string& tag, MessageListener* listener, - AckMode ackMode = NO_ACK, bool noLocal = false, bool synch = true, - const framing::FieldTable* fields = 0); - - /** - * Cancels a subscription previously set up through a call to consume(). - * - * @param tag the identifier used (or assigned) in the consume - * request that set up the subscription to be cancelled. - * - * @param synch if true this call will block until a response - * is received from the broker - */ - void cancel(const std::string& tag, bool synch = true); - /** - * Synchronous pull of a message from a queue. - * - * @param msg a message object that will contain the message - * headers and content if the call completes. - * - * @param queue the queue to consume from - * - * @param ackMode the acknowledgement mode to use (@see - * AckMode) - * - * @return true if a message was succcessfully dequeued from - * the queue, false if the queue was empty. - */ - bool get(Message& msg, const Queue& queue, AckMode ackMode = NO_ACK); - - /** - * Publishes (i.e. sends a message to the broker). - * - * @param msg the message to publish - * - * @param exchange the exchange to publish the message to - * - * @param routingKey the routing key to publish with - * - * @param mandatory if true and the exchange to which this - * publish is directed has no matching bindings, the message - * will be returned (see setReturnedMessageHandler()). - * - * @param immediate if true and there is no consumer to - * receive this message on publication, the message will be - * returned (see setReturnedMessageHandler()). - */ - void publish(const Message& msg, const Exchange& exchange, - const std::string& routingKey, - bool mandatory = false, bool immediate = false); - - /** - * Set a handler for this channel that will process any - * returned messages - * - * @see publish() - */ - void setReturnedMessageHandler(ReturnedMessageHandler* handler); - - /** - * Deliver messages from the broker to the appropriate MessageListener. - */ - void run(); - - -}; - -}} - -#endif /*!_client_ClientChannel_h*/ diff --git a/cpp/src/client/ClientConnection.cpp b/cpp/src/client/ClientConnection.cpp deleted file mode 100644 index b053a45b0f..0000000000 --- a/cpp/src/client/ClientConnection.cpp +++ /dev/null @@ -1,156 +0,0 @@ -/* - * - * 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 -#include -#include - -#include "Connection.h" -#include "ClientChannel.h" -#include "ClientMessage.h" -#include "../QpidError.h" -#include -#include -#include "MethodBodyInstances.h" -#include - -using namespace qpid::framing; -using namespace qpid::sys; - - -namespace qpid { -namespace client { - -const std::string Connection::OK("OK"); - -Connection::Connection( - bool _debug, uint32_t _max_frame_size, - framing::ProtocolVersion _version -) : channelIdCounter(0), version(_version), max_frame_size(_max_frame_size), - defaultConnector(version, _debug, _max_frame_size), - isOpen(false), debug(_debug) -{ - setConnector(defaultConnector); -} - -Connection::~Connection(){} - -void Connection::setConnector(Connector& con) -{ - connector = &con; - connector->setInputHandler(this); - connector->setTimeoutHandler(this); - connector->setShutdownHandler(this); - out = connector->getOutputHandler(); -} - -void Connection::open( - const std::string& host, int port, - const std::string& uid, const std::string& pwd, const std::string& vhost) -{ - if (isOpen) - THROW_QPID_ERROR(INTERNAL_ERROR, "Channel object is already open"); - connector->connect(host, port); - channels[0] = &channel0; - channel0.open(0, *this); - channel0.protocolInit(uid, pwd, vhost); - isOpen = true; -} - -void Connection::shutdown() { - close(); -} - -void Connection::close( - ReplyCode code, const string& msg, ClassId classId, MethodId methodId -) -{ - if(isOpen) { - // TODO aconway 2007-01-29: Exception handling - could end up - // partly closed with threads left unjoined. - isOpen = false; - channel0.sendAndReceive( - make_shared_ptr(new ConnectionCloseBody( - getVersion(), code, msg, classId, methodId))); - - using boost::bind; - for_each(channels.begin(), channels.end(), - bind(&Channel::closeInternal, - bind(&ChannelMap::value_type::second, _1))); - channels.clear(); - connector->close(); - } -} - -void Connection::openChannel(Channel& channel) { - ChannelId id = ++channelIdCounter; - assert (channels.find(id) == channels.end()); - assert(out); - channels[id] = &channel; - channel.open(id, *this); -} - -void Connection::erase(ChannelId id) { - channels.erase(id); -} - -void Connection::received(AMQFrame* frame){ - // FIXME aconway 2007-01-25: Mutex - ChannelId id = frame->getChannel(); - Channel* channel = channels[id]; - // FIXME aconway 2007-01-26: Exception thrown here is hanging the - // client. Need to review use of exceptions. - if (channel == 0) - THROW_QPID_ERROR( - PROTOCOL_ERROR+504, - (boost::format("Invalid channel number %g") % id).str()); - try{ - channel->handleBody(frame->getBody()); - }catch(const qpid::QpidError& e){ - channelException( - *channel, dynamic_cast(frame->getBody().get()), e); - } -} - -void Connection::send(AMQFrame* frame) { - out->send(frame); -} - -void Connection::channelException( - Channel& channel, AMQMethodBody* method, const QpidError& e) -{ - int code = (e.code >= PROTOCOL_ERROR) ? e.code - PROTOCOL_ERROR : 500; - string msg = e.msg; - if(method == 0) - channel.close(code, msg); - else - channel.close( - code, msg, method->amqpClassId(), method->amqpMethodId()); -} - -void Connection::idleIn(){ - connector->close(); -} - -void Connection::idleOut(){ - out->send(new AMQFrame(version, 0, new AMQHeartbeatBody())); -} - -}} // namespace qpid::client diff --git a/cpp/src/client/ClientExchange.cpp b/cpp/src/client/ClientExchange.cpp deleted file mode 100644 index d5914beea2..0000000000 --- a/cpp/src/client/ClientExchange.cpp +++ /dev/null @@ -1,34 +0,0 @@ -/* - * - * 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 "ClientExchange.h" - -qpid::client::Exchange::Exchange(std::string _name, std::string _type) : name(_name), type(_type){} -const std::string& qpid::client::Exchange::getName() const { return name; } -const std::string& qpid::client::Exchange::getType() const { return type; } - -const std::string qpid::client::Exchange::DIRECT_EXCHANGE = "direct"; -const std::string qpid::client::Exchange::TOPIC_EXCHANGE = "topic"; -const std::string qpid::client::Exchange::HEADERS_EXCHANGE = "headers"; - -const qpid::client::Exchange qpid::client::Exchange::DEFAULT_EXCHANGE("", DIRECT_EXCHANGE); -const qpid::client::Exchange qpid::client::Exchange::STANDARD_DIRECT_EXCHANGE("amq.direct", DIRECT_EXCHANGE); -const qpid::client::Exchange qpid::client::Exchange::STANDARD_TOPIC_EXCHANGE("amq.topic", TOPIC_EXCHANGE); -const qpid::client::Exchange qpid::client::Exchange::STANDARD_HEADERS_EXCHANGE("amq.headers", HEADERS_EXCHANGE); diff --git a/cpp/src/client/ClientExchange.h b/cpp/src/client/ClientExchange.h deleted file mode 100644 index a8ac21fa9b..0000000000 --- a/cpp/src/client/ClientExchange.h +++ /dev/null @@ -1,106 +0,0 @@ -/* - * - * 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 - -#ifndef _Exchange_ -#define _Exchange_ - -namespace qpid { -namespace client { - - /** - * A 'handle' used to represent an AMQP exchange in the Channel - * methods. Exchanges are the destinations to which messages are - * published. - * - * There are different types of exchange (the standard types are - * available as static constants, see DIRECT_EXCHANGE, - * TOPIC_EXCHANGE and HEADERS_EXCHANGE). A Queue can be bound to - * an exchange using Channel::bind() and messages published to - * that exchange are then routed to the queue based on the details - * of the binding and the type of exchange. - * - * There are some standard exchange instances that are predeclared - * on all AMQP brokers. These are defined as static members - * STANDARD_DIRECT_EXCHANGE, STANDARD_TOPIC_EXCHANGE and - * STANDARD_HEADERS_EXCHANGE. There is also the 'default' exchange - * (member DEFAULT_EXCHANGE) which is nameless and of type - * 'direct' and has every declared queue bound to it by queue - * name. - * - * \ingroup clientapi - */ - class Exchange{ - const std::string name; - const std::string type; - - public: - /** - * A direct exchange routes messages published with routing - * key X to any queue bound with key X (i.e. an exact match is - * used). - */ - static const std::string DIRECT_EXCHANGE; - /** - * A topic exchange treat the key with which a queue is bound - * as a pattern and routes all messages whose routing keys - * match that pattern to the bound queue. The routing key for - * a message must consist of zero or more alpha-numeric words - * delimited by dots. The pattern is of a similar form but * - * can be used to match excatly one word and # can be used to - * match zero or more words. - */ - static const std::string TOPIC_EXCHANGE; - /** - * The headers exchange routes messages based on whether their - * headers match the binding arguments specified when - * binding. (see the AMQP spec for more details). - */ - static const std::string HEADERS_EXCHANGE; - - /** - * The 'default' exchange, nameless and of type 'direct'. Has - * every declared queue bound to it by name. - */ - static const Exchange DEFAULT_EXCHANGE; - /** - * The standard direct exchange, named amq.direct. - */ - static const Exchange STANDARD_DIRECT_EXCHANGE; - /** - * The standard topic exchange, named amq.topic. - */ - static const Exchange STANDARD_TOPIC_EXCHANGE; - /** - * The standard headers exchange, named amq.header. - */ - static const Exchange STANDARD_HEADERS_EXCHANGE; - - Exchange(std::string name, std::string type = DIRECT_EXCHANGE); - const std::string& getName() const; - const std::string& getType() const; - }; - -} -} - - -#endif diff --git a/cpp/src/client/ClientMessage.h b/cpp/src/client/ClientMessage.h deleted file mode 100644 index 35aed6c734..0000000000 --- a/cpp/src/client/ClientMessage.h +++ /dev/null @@ -1,64 +0,0 @@ -#ifndef _client_ClientMessage_h -#define _client_ClientMessage_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 -#include "../framing/BasicHeaderProperties.h" - -namespace qpid { -namespace client { - -/** - * A representation of messages for sent or recived through the - * client api. - * - * \ingroup clientapi - */ -// FIXME aconway 2007-04-05: Should be based on MessageTransfer properties not -// basic header properties. -class Message : public framing::BasicHeaderProperties { - public: - Message(const std::string& data_=std::string()) : data(data_) {} - - std::string getData() const { return data; } - void setData(const std::string& _data) { data = _data; } - - std::string getDestination() const { return destination; } - void setDestination(const std::string& dest) { destination = dest; } - - // TODO aconway 2007-03-22: only needed for Basic.deliver support. - uint64_t getDeliveryTag() const { return deliveryTag; } - void setDeliveryTag(uint64_t dt) { deliveryTag = dt; } - - bool isRedelivered() const { return redelivered; } - void setRedelivered(bool _redelivered){ redelivered = _redelivered; } - - private: - std::string data; - std::string destination; - bool redelivered; - uint64_t deliveryTag; -}; - -}} - -#endif /*!_client_ClientMessage_h*/ diff --git a/cpp/src/client/ClientQueue.cpp b/cpp/src/client/ClientQueue.cpp deleted file mode 100644 index 613cf8d288..0000000000 --- a/cpp/src/client/ClientQueue.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - * - * 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 "ClientQueue.h" - -qpid::client::Queue::Queue() : name(""), autodelete(true), exclusive(true), durable(false){} - -qpid::client::Queue::Queue(std::string _name) : name(_name), autodelete(false), exclusive(false), durable(false){} - -qpid::client::Queue::Queue(std::string _name, bool temp) : name(_name), autodelete(temp), exclusive(temp), durable(false){} - -qpid::client::Queue::Queue(std::string _name, bool _autodelete, bool _exclusive, bool _durable) - : name(_name), autodelete(_autodelete), exclusive(_exclusive), durable(_durable){} - -const std::string& qpid::client::Queue::getName() const{ - return name; -} - -void qpid::client::Queue::setName(const std::string& _name){ - name = _name; -} - -bool qpid::client::Queue::isAutoDelete() const{ - return autodelete; -} - -bool qpid::client::Queue::isExclusive() const{ - return exclusive; -} - -bool qpid::client::Queue::isDurable() const{ - return durable; -} - -void qpid::client::Queue::setDurable(bool _durable){ - durable = _durable; -} - - - - diff --git a/cpp/src/client/ClientQueue.h b/cpp/src/client/ClientQueue.h deleted file mode 100644 index b37a44b004..0000000000 --- a/cpp/src/client/ClientQueue.h +++ /dev/null @@ -1,103 +0,0 @@ -#ifndef _client_ClientQueue_h -#define _client_ClientQueue_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 - -namespace qpid { -namespace client { - - /** - * A 'handle' used to represent an AMQP queue in the Channel - * methods. Creating an instance of this class does not cause the - * queue to be created on the broker. Rather, an instance of this - * class should be passed to Channel::declareQueue() to ensure - * that the queue exists or is created. - * - * Queues hold messages and allow clients to consume - * (see Channel::consume()) or get (see Channel::get()) those messags. A - * queue receives messages by being bound to one or more Exchange; - * messages published to that exchange may then be routed to the - * queue based on the details of the binding and the type of the - * exchange (see Channel::bind()). - * - * Queues are identified by a name. They can be exclusive (in which - * case they can only be used in the context of the connection - * over which they were declared, and are deleted when then - * connection closes), or they can be shared. Shared queues can be - * auto deleted when they have no consumers. - * - * We use the term 'temporary queue' to refer to an exclusive - * queue. - * - * \ingroup clientapi - */ - class Queue{ - std::string name; - const bool autodelete; - const bool exclusive; - bool durable; - - public: - - /** - * Creates an unnamed, non-durable, temporary queue. A name - * will be assigned to this queue instance by a call to - * Channel::declareQueue(). - */ - Queue(); - /** - * Creates a shared, non-durable, queue with a given name, - * that will not be autodeleted. - * - * @param name the name of the queue - */ - Queue(std::string name); - /** - * Creates a non-durable queue with a given name. - * - * @param name the name of the queue - * - * @param temp if true the queue will be a temporary queue, if - * false it will be shared and not autodeleted. - */ - Queue(std::string name, bool temp); - /** - * This constructor allows the autodelete, exclusive and - * durable propeties to be explictly set. Note however that if - * exclusive is true, autodelete has no meaning as exclusive - * queues are always destroyed when the connection that - * created them is closed. - */ - Queue(std::string name, bool autodelete, bool exclusive, bool durable); - const std::string& getName() const; - void setName(const std::string&); - bool isAutoDelete() const; - bool isExclusive() const; - bool isDurable() const; - void setDurable(bool durable); - }; - -} -} - -#endif /*!_client_ClientQueue_h*/ diff --git a/cpp/src/client/Connection.h b/cpp/src/client/Connection.h deleted file mode 100644 index 5e0b413dac..0000000000 --- a/cpp/src/client/Connection.h +++ /dev/null @@ -1,179 +0,0 @@ -#ifndef _client_Connection_ -#define _client_Connection_ - -/* - * - * 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 -#include -#include "../QpidError.h" -#include "ClientChannel.h" -#include "Connector.h" -#include "../sys/ShutdownHandler.h" -#include "../sys/TimeoutHandler.h" - - -namespace qpid { - -/** - * The client namespace contains all classes that make up a client - * implementation of the AMQP protocol. The key classes that form - * the basis of the client API to be used by applications are - * Connection and Channel. - */ -namespace client { - -/** - * \internal provide access to selected private channel functions - * for the Connection without making it a friend of the entire channel. - */ -class ConnectionForChannel : - public framing::InputHandler, - public framing::OutputHandler, - public sys::TimeoutHandler, - public sys::ShutdownHandler - -{ - private: - friend class Channel; - virtual void erase(framing::ChannelId) = 0; -}; - - -/** - * \defgroup clientapi Application API for an AMQP client - */ - -/** - * Represents a connection to an AMQP broker. All communication is - * initiated by establishing a connection, then opening one or - * more Channels over that connection. - * - * \ingroup clientapi - */ -class Connection : public ConnectionForChannel -{ - typedef std::map ChannelMap; - - framing::ChannelId channelIdCounter; - static const std::string OK; - - framing::ProtocolVersion version; - const uint32_t max_frame_size; - ChannelMap channels; - Connector defaultConnector; - Connector* connector; - framing::OutputHandler* out; - volatile bool isOpen; - Channel channel0; - bool debug; - - void erase(framing::ChannelId); - void channelException( - Channel&, framing::AMQMethodBody*, const QpidError&); - - // TODO aconway 2007-01-26: too many friendships, untagle these classes. - friend class Channel; - - public: - /** - * Creates a connection object, but does not open the - * connection. - * - * @param _version the version of the protocol to connect with - * - * @param debug turns on tracing for the connection - * (i.e. prints details of the frames sent and received to std - * out). Optional and defaults to false. - * - * @param max_frame_size the maximum frame size that the - * client will accept. Optional and defaults to 65536. - */ - Connection(bool debug = false, uint32_t max_frame_size = 65536, - framing::ProtocolVersion=framing::highestProtocolVersion); - ~Connection(); - - /** - * Opens a connection to a broker. - * - * @param host the host on which the broker is running - * - * @param port the port on the which the broker is listening - * - * @param uid the userid to connect with - * - * @param pwd the password to connect with (currently SASL - * PLAIN is the only authentication method supported so this - * is sent in clear text) - * - * @param virtualhost the AMQP virtual host to use (virtual - * hosts, where implemented(!), provide namespace partitioning - * within a single broker). - */ - void open(const std::string& host, int port = 5672, - const std::string& uid = "guest", - const std::string& pwd = "guest", - const std::string& virtualhost = "/"); - - /** - * Close the connection with optional error information for the peer. - * - * Any further use of this connection (without reopening it) will - * not succeed. - */ - void close(framing::ReplyCode=200, const std::string& msg=OK, - framing::ClassId = 0, framing::MethodId = 0); - - /** - * Associate a Channel with this connection and open it for use. - * - * In AMQP channels are like multi-plexed 'sessions' of work over - * a connection. Almost all the interaction with AMQP is done over - * a channel. - * - * @param connection the connection object to be associated with - * the channel. Call Channel::close() to close the channel. - */ - void openChannel(Channel&); - - - // TODO aconway 2007-01-26: can these be private? - void send(framing::AMQFrame*); - void received(framing::AMQFrame*); - void idleOut(); - void idleIn(); - void shutdown(); - - /**\internal used for testing */ - void setConnector(Connector& connector); - - /** - * @return the maximum frame size in use on this connection - */ - inline uint32_t getMaxFrameSize(){ return max_frame_size; } - - /** @return protocol version in use on this connection. */ - framing::ProtocolVersion getVersion() const { return version; } -}; - -}} // namespace qpid::client - - -#endif diff --git a/cpp/src/client/Connector.cpp b/cpp/src/client/Connector.cpp deleted file mode 100644 index 566e58ec13..0000000000 --- a/cpp/src/client/Connector.cpp +++ /dev/null @@ -1,188 +0,0 @@ -/* - * - * 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 -#include "../QpidError.h" -#include "../sys/Time.h" -#include "Connector.h" - -namespace qpid { -namespace client { - -using namespace qpid::sys; -using namespace qpid::framing; -using qpid::QpidError; - -Connector::Connector( - ProtocolVersion ver, bool _debug, uint32_t buffer_size -) : debug(_debug), - receive_buffer_size(buffer_size), - send_buffer_size(buffer_size), - version(ver), - closed(true), - lastIn(0), lastOut(0), - timeout(0), - idleIn(0), idleOut(0), - timeoutHandler(0), - shutdownHandler(0), - inbuf(receive_buffer_size), - outbuf(send_buffer_size) -{ } - -Connector::~Connector(){ } - -void Connector::connect(const std::string& host, int port){ - socket = Socket::createTcp(); - socket.connect(host, port); - closed = false; - receiver = Thread(this); -} - -void Connector::init(){ - ProtocolInitiation init(version); - writeBlock(&init); -} - -void Connector::close(){ - closed = true; - socket.close(); - receiver.join(); -} - -void Connector::setInputHandler(InputHandler* handler){ - input = handler; -} - -void Connector::setShutdownHandler(ShutdownHandler* handler){ - shutdownHandler = handler; -} - -OutputHandler* Connector::getOutputHandler(){ - return this; -} - -void Connector::send(AMQFrame* f){ - std::auto_ptr frame(f); - AMQBody::shared_ptr body = frame->getBody(); - writeBlock(frame.get()); - if(debug) std::cout << "SENT: " << *frame << std::endl; -} - -void Connector::writeBlock(AMQDataBlock* data){ - Mutex::ScopedLock l(writeLock); - data->encode(outbuf); - //transfer data to wire - outbuf.flip(); - writeToSocket(outbuf.start(), outbuf.available()); - outbuf.clear(); -} - -void Connector::writeToSocket(char* data, size_t available){ - size_t written = 0; - while(written < available && !closed){ - ssize_t sent = socket.send(data + written, available-written); - if(sent > 0) { - lastOut = now() * TIME_MSEC; - written += sent; - } - } -} - -void Connector::handleClosed(){ - closed = true; - socket.close(); - if(shutdownHandler) shutdownHandler->shutdown(); -} - -void Connector::checkIdle(ssize_t status){ - if(timeoutHandler){ - Time t = now() * TIME_MSEC; - if(status == Socket::SOCKET_TIMEOUT) { - if(idleIn && (t - lastIn > idleIn)){ - timeoutHandler->idleIn(); - } - } - else if(status == 0 || status == Socket::SOCKET_EOF) { - handleClosed(); - } - else { - lastIn = t; - } - if(idleOut && (t - lastOut > idleOut)){ - timeoutHandler->idleOut(); - } - } -} - -void Connector::setReadTimeout(uint16_t t){ - idleIn = t * 1000;//t is in secs - if(idleIn && (!timeout || idleIn < timeout)){ - timeout = idleIn; - setSocketTimeout(); - } - -} - -void Connector::setWriteTimeout(uint16_t t){ - idleOut = t * 1000;//t is in secs - if(idleOut && (!timeout || idleOut < timeout)){ - timeout = idleOut; - setSocketTimeout(); - } -} - -void Connector::setSocketTimeout(){ - socket.setTimeout(timeout*TIME_MSEC); -} - -void Connector::setTimeoutHandler(TimeoutHandler* handler){ - timeoutHandler = handler; -} - -void Connector::run(){ - try{ - while(!closed){ - ssize_t available = inbuf.available(); - if(available < 1){ - THROW_QPID_ERROR(INTERNAL_ERROR, "Frame exceeds buffer size."); - } - ssize_t received = socket.recv(inbuf.start(), available); - checkIdle(received); - - if(!closed && received > 0){ - inbuf.move(received); - inbuf.flip();//position = 0, limit = total data read - - AMQFrame frame(version); - while(frame.decode(inbuf)){ - if(debug) std::cout << "RECV: " << frame << std::endl; - input->received(&frame); - } - //need to compact buffer to preserve any 'extra' data - inbuf.compact(); - } - } - } catch (const std::exception& e) { - std::cout << e.what() << std::endl; - handleClosed(); - } -} - -}} // namespace qpid::client diff --git a/cpp/src/client/Connector.h b/cpp/src/client/Connector.h deleted file mode 100644 index 928bfa2d14..0000000000 --- a/cpp/src/client/Connector.h +++ /dev/null @@ -1,98 +0,0 @@ -/* - * - * 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 _Connector_ -#define _Connector_ - - -#include "../framing/InputHandler.h" -#include "../framing/OutputHandler.h" -#include "../framing/InitiationHandler.h" -#include "../framing/ProtocolInitiation.h" -#include "../framing/ProtocolVersion.h" -#include "../sys/ShutdownHandler.h" -#include "../sys/TimeoutHandler.h" -#include "../sys/Thread.h" -#include "../sys/Monitor.h" -#include "../sys/Socket.h" - -namespace qpid { - -namespace client { - -class Connector : public framing::OutputHandler, - private sys::Runnable -{ - const bool debug; - const int receive_buffer_size; - const int send_buffer_size; - framing::ProtocolVersion version; - - bool closed; - - int64_t lastIn; - int64_t lastOut; - int64_t timeout; - uint32_t idleIn; - uint32_t idleOut; - - sys::TimeoutHandler* timeoutHandler; - sys::ShutdownHandler* shutdownHandler; - framing::InputHandler* input; - framing::InitiationHandler* initialiser; - framing::OutputHandler* output; - - framing::Buffer inbuf; - framing::Buffer outbuf; - - sys::Mutex writeLock; - sys::Thread receiver; - - sys::Socket socket; - - void checkIdle(ssize_t status); - void writeBlock(framing::AMQDataBlock* data); - void writeToSocket(char* data, size_t available); - void setSocketTimeout(); - - void run(); - void handleClosed(); - - friend class Channel; - public: - Connector(framing::ProtocolVersion pVersion, - bool debug = false, uint32_t buffer_size = 1024); - virtual ~Connector(); - virtual void connect(const std::string& host, int port); - virtual void init(); - virtual void close(); - virtual void setInputHandler(framing::InputHandler* handler); - virtual void setTimeoutHandler(sys::TimeoutHandler* handler); - virtual void setShutdownHandler(sys::ShutdownHandler* handler); - virtual framing::OutputHandler* getOutputHandler(); - virtual void send(framing::AMQFrame* frame); - virtual void setReadTimeout(uint16_t timeout); - virtual void setWriteTimeout(uint16_t timeout); -}; - -}} - - -#endif diff --git a/cpp/src/client/IncomingMessage.cpp b/cpp/src/client/IncomingMessage.cpp deleted file mode 100644 index eb5f2b6fae..0000000000 --- a/cpp/src/client/IncomingMessage.cpp +++ /dev/null @@ -1,168 +0,0 @@ -/* - * - * 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 "IncomingMessage.h" -#include "../Exception.h" -#include "ClientMessage.h" -#include - -namespace qpid { -namespace client { - -using boost::format; -using sys::Mutex; - -IncomingMessage::Destination::~Destination() {} - - -IncomingMessage::WaitableDestination::WaitableDestination() - : shutdownFlag(false) {} - -void IncomingMessage::WaitableDestination::message(const Message& msg) { - Mutex::ScopedLock l(monitor); - queue.push(msg); - monitor.notify(); -} - -void IncomingMessage::WaitableDestination::empty() { - Mutex::ScopedLock l(monitor); - queue.push(Empty()); - monitor.notify(); -} - -bool IncomingMessage::WaitableDestination::wait(Message& msgOut) { - Mutex::ScopedLock l(monitor); - while (queue.empty() && !shutdownFlag) - monitor.wait(); - if (shutdownFlag) - return false; - Message* msg = boost::get(&queue.front()); - bool success = msg; - if (success) - msgOut=*msg; - queue.pop(); - if (!queue.empty()) - monitor.notify(); // Wake another waiter. - return success; -} - -void IncomingMessage::WaitableDestination::shutdown() { - Mutex::ScopedLock l(monitor); - shutdownFlag = true; - monitor.notifyAll(); -} - -void IncomingMessage::openReference(const std::string& name) { - Mutex::ScopedLock l(lock); - if (references.find(name) != references.end()) - throw ConnectionException( - 503, format("Attempt to open existing reference %s.") % name); - references[name]; - return; -} - -void IncomingMessage::appendReference( - const std::string& name, const std::string& data) -{ - Mutex::ScopedLock l(lock); - getRefUnlocked(name).data += data; -} - -Message& IncomingMessage::createMessage( - const std::string& destination, const std::string& reference) -{ - Mutex::ScopedLock l(lock); - getDestUnlocked(destination); // Verify destination. - Reference& ref = getRefUnlocked(reference); - ref.messages.resize(ref.messages.size() +1); - ref.messages.back().setDestination(destination); - return ref.messages.back(); -} - -void IncomingMessage::closeReference(const std::string& name) { - Reference refCopy; - { - Mutex::ScopedLock l(lock); - refCopy = getRefUnlocked(name); - references.erase(name); - } - for (std::vector::iterator i = refCopy.messages.begin(); - i != refCopy.messages.end(); - ++i) - { - i->setData(refCopy.data); - // TODO aconway 2007-03-23: Thread safety, - // can a destination be removed while we're doing this? - getDestination(i->getDestination()).message(*i); - } -} - - -void IncomingMessage::addDestination(std::string name, Destination& dest) { - Mutex::ScopedLock l(lock); - DestinationMap::iterator i = destinations.find(name); - if (i == destinations.end()) - destinations[name]=&dest; - else if (i->second != &dest) - throw ConnectionException( - 503, format("Destination already exists: %s.") % name); -} - -void IncomingMessage::removeDestination(std::string name) { - Mutex::ScopedLock l(lock); - DestinationMap::iterator i = destinations.find(name); - if (i == destinations.end()) - throw ConnectionException( - 503, format("No such destination: %s.") % name); - destinations.erase(i); -} - -IncomingMessage::Destination& IncomingMessage::getDestination( - const std::string& name) { - return getDestUnlocked(name); -} - -IncomingMessage::Reference& IncomingMessage::getReference( - const std::string& name) { - return getRefUnlocked(name); -} - -IncomingMessage::Reference& IncomingMessage::getRefUnlocked( - const std::string& name) { - Mutex::ScopedLock l(lock); - ReferenceMap::iterator i = references.find(name); - if (i == references.end()) - throw ConnectionException( - 503, format("No such reference: %s.") % name); - return i->second; -} - -IncomingMessage::Destination& IncomingMessage::getDestUnlocked( - const std::string& name) { - Mutex::ScopedLock l(lock); - DestinationMap::iterator i = destinations.find(name); - if (i == destinations.end()) - throw ConnectionException( - 503, format("No such destination: %s.") % name); - return *i->second; -} - -}} // namespace qpid::client diff --git a/cpp/src/client/IncomingMessage.h b/cpp/src/client/IncomingMessage.h deleted file mode 100644 index bc650dfbe1..0000000000 --- a/cpp/src/client/IncomingMessage.h +++ /dev/null @@ -1,136 +0,0 @@ -#ifndef _IncomingMessage_ -#define _IncomingMessage_ - -/* - * - * 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 "../sys/Monitor.h" -#include -#include -#include -#include - -namespace qpid { -namespace client { - -class Message; - -/** - * Manage incoming messages. - * - * Uses reference and destination concepts from 0-9 Messsage class. - * - * Basic messages use special destination and reference names to indicate - * get-ok, return etc. messages. - * - */ -class IncomingMessage { - public: - /** Accumulate data associated with a set of messages. */ - struct Reference { - std::string data; - std::vector messages; - }; - - /** Interface to a destination for messages. */ - class Destination { - public: - virtual ~Destination(); - - /** Pass a message to the destination */ - virtual void message(const Message&) = 0; - - /** Notify destination of queue-empty contition */ - virtual void empty() = 0; - }; - - - /** A destination that a thread can wait on till a message arrives. */ - class WaitableDestination : public Destination - { - public: - WaitableDestination(); - void message(const Message& msg); - void empty(); - /** Wait till message() or empty() is called. True for message() */ - bool wait(Message& msgOut); - void shutdown(); - - private: - struct Empty {}; - typedef boost::variant Item; - sys::Monitor monitor; - std::queue queue; - bool shutdownFlag; - }; - - - - /** Add a reference. Throws if already open. */ - void openReference(const std::string& name); - - /** Get a reference. Throws if not already open. */ - void appendReference(const std::string& name, - const std::string& data); - - /** Create a message to destination associated with reference - *@exception if destination or reference non-existent. - */ - Message& createMessage(const std::string& destination, - const std::string& reference); - - /** Get a reference. - *@exception if non-existent. - */ - Reference& getReference(const std::string& name); - - /** Close a reference and deliver all its messages. - * Throws if not open or a message has an invalid destination. - */ - void closeReference(const std::string& name); - - /** Add a destination. - *@exception if a different Destination is already registered - * under name. - */ - void addDestination(std::string name, Destination&); - - /** Remove a destination. Throws if does not exist */ - void removeDestination(std::string name); - - /** Get a destination. Throws if does not exist */ - Destination& getDestination(const std::string& name); - private: - - typedef std::map ReferenceMap; - typedef std::map DestinationMap; - - Reference& getRefUnlocked(const std::string& name); - Destination& getDestUnlocked(const std::string& name); - - mutable sys::Mutex lock; - ReferenceMap references; - DestinationMap destinations; -}; - -}} - - -#endif diff --git a/cpp/src/client/MessageChannel.h b/cpp/src/client/MessageChannel.h deleted file mode 100644 index 2fa387b7f7..0000000000 --- a/cpp/src/client/MessageChannel.h +++ /dev/null @@ -1,94 +0,0 @@ -#ifndef _client_MessageChannel_h -#define _client_MessageChannel_h - -/* - * - * 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 "../shared_ptr.h" -#include "../sys/Runnable.h" -#include "AckMode.h" - -namespace qpid { - -namespace framing { -class AMQMethodBody; -class AMQHeaderBody; -class AMQContentBody; -class FieldTable; -} - -namespace client { - -class Channel; -class Message; -class Queue; -class Exchange; -class MessageListener; -class ReturnedMessageHandler; - -/** - * Abstract interface for messaging implementation for a channel. - * - *@see Channel for documentation. - */ -class MessageChannel : public sys::Runnable -{ - public: - /**@see Channel::consume */ - virtual void consume( - Queue& queue, std::string& tag, MessageListener* listener, - AckMode ackMode = NO_ACK, bool noLocal = false, bool synch = true, - const framing::FieldTable* fields = 0) = 0; - - /**@see Channel::cancel */ - virtual void cancel(const std::string& tag, bool synch = true) = 0; - - /**@see Channel::get */ - virtual bool get( - Message& msg, const Queue& queue, AckMode ackMode = NO_ACK) = 0; - - /**@see Channel::get */ - virtual void publish(const Message& msg, const Exchange& exchange, - const std::string& routingKey, - bool mandatory = false, bool immediate = false) = 0; - - /**@see Channel::setReturnedMessageHandler */ - virtual void setReturnedMessageHandler( - ReturnedMessageHandler* handler) = 0; - - /** Handle an incoming method. */ - virtual void handle(shared_ptr) = 0; - - /** Handle an incoming header */ - virtual void handle(shared_ptr) = 0; - - /** Handle an incoming content */ - virtual void handle(shared_ptr) = 0; - - /** Send channel's QOS settings */ - virtual void setQos() = 0; - - /** Channel is closing */ - virtual void close() = 0; -}; - -}} // namespace qpid::client - - - -#endif /*!_client_MessageChannel_h*/ diff --git a/cpp/src/client/MessageListener.cpp b/cpp/src/client/MessageListener.cpp deleted file mode 100644 index 68ebedeb0d..0000000000 --- a/cpp/src/client/MessageListener.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* - * - * 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 "MessageListener.h" - -qpid::client::MessageListener::~MessageListener() {} diff --git a/cpp/src/client/MessageListener.h b/cpp/src/client/MessageListener.h deleted file mode 100644 index 501862a3ef..0000000000 --- a/cpp/src/client/MessageListener.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * - * 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 - -#ifndef _MessageListener_ -#define _MessageListener_ - -#include "ClientMessage.h" - -namespace qpid { -namespace client { - - /** - * An interface through which asynchronously delivered messages - * can be received by an application. - * - * @see Channel::consume() - * - * \ingroup clientapi - */ - class MessageListener{ - public: - virtual ~MessageListener(); - virtual void received(Message& msg) = 0; - }; - -} -} - - -#endif diff --git a/cpp/src/client/MessageMessageChannel.cpp b/cpp/src/client/MessageMessageChannel.cpp deleted file mode 100644 index 8d0fdc3189..0000000000 --- a/cpp/src/client/MessageMessageChannel.cpp +++ /dev/null @@ -1,431 +0,0 @@ -/* - * - * 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 -#include -#include "MessageMessageChannel.h" -#include "../framing/AMQMethodBody.h" -#include "ClientChannel.h" -#include "ReturnedMessageHandler.h" -#include "MessageListener.h" -#include "../framing/FieldTable.h" -#include "Connection.h" -#include "../shared_ptr.h" -#include - -namespace qpid { -namespace client { - -using namespace std; -using namespace sys; -using namespace framing; - -MessageMessageChannel::MessageMessageChannel(Channel& ch) - : channel(ch), tagCount(0) {} - -string MessageMessageChannel::newTag() { - Mutex::ScopedLock l(lock); - return (boost::format("__tag%d")%++tagCount).str(); -} - -void MessageMessageChannel::consume( - Queue& queue, std::string& tag, MessageListener* /*listener*/, - AckMode ackMode, bool noLocal, bool /*synch*/, const FieldTable* fields) -{ - if (tag.empty()) - tag = newTag(); - channel.sendAndReceive( - make_shared_ptr(new MessageConsumeBody( - channel.getVersion(), 0, queue.getName(), tag, noLocal, - ackMode == NO_ACK, false, fields ? *fields : FieldTable()))); - -// // FIXME aconway 2007-02-20: Race condition! -// // We could receive the first message for the consumer -// // before we create the consumer below. -// // Move consumer creation to handler for MessageConsumeOkBody -// { -// Mutex::ScopedLock l(lock); -// ConsumerMap::iterator i = consumers.find(tag); -// if (i != consumers.end()) -// THROW_QPID_ERROR(CLIENT_ERROR, -// "Consumer already exists with tag="+tag); -// Consumer& c = consumers[tag]; -// c.listener = listener; -// c.ackMode = ackMode; -// c.lastDeliveryTag = 0; -// } -} - - -void MessageMessageChannel::cancel(const std::string& /*tag*/, bool /*synch*/) { - // FIXME aconway 2007-02-23: -// Consumer c; -// { -// Mutex::ScopedLock l(lock); -// ConsumerMap::iterator i = consumers.find(tag); -// if (i == consumers.end()) -// return; -// c = i->second; -// consumers.erase(i); -// } -// if(c.ackMode == LAZY_ACK && c.lastDeliveryTag > 0) -// channel.send(new MessageAckBody(channel.version, c.lastDeliveryTag, true)); -// channel.sendAndReceiveSync( -// synch, new MessageCancelBody(channel.version, tag, !synch)); -} - -void MessageMessageChannel::close(){ - // FIXME aconway 2007-02-23: -// ConsumerMap consumersCopy; -// { -// Mutex::ScopedLock l(lock); -// consumersCopy = consumers; -// consumers.clear(); -// } -// for (ConsumerMap::iterator i=consumersCopy.begin(); -// i != consumersCopy.end(); ++i) -// { -// Consumer& c = i->second; -// if ((c.ackMode == LAZY_ACK || c.ackMode == AUTO_ACK) -// && c.lastDeliveryTag > 0) -// { -// channel.send(new MessageAckBody(channel.version, c.lastDeliveryTag, true)); -// } -// } -// incoming.shutdown(); -} - - -/** Destination ID for the current get. - * Must not clash with a generated consumer ID. - * TODO aconway 2007-03-06: support multiple outstanding gets? - */ -const string getDestinationId("__get__"); - -/** - * A destination that provides a Correlator::Action to handle - * MessageEmpty responses. - */ -struct MessageGetDestination : public IncomingMessage::WaitableDestination -{ - void response(shared_ptr response) { - if (response->amqpClassId() == MessageOkBody::CLASS_ID) { - switch (response->amqpMethodId()) { - case MessageOkBody::METHOD_ID: - // Nothing to do, wait for transfer. - return; - case MessageEmptyBody::METHOD_ID: - empty(); // Wake up waiter with empty queue. - return; - } - } - throw QPID_ERROR(PROTOCOL_ERROR, "Invalid response"); - } - - Correlator::Action action() { - return boost::bind(&MessageGetDestination::response, this, _1); - } -}; - -bool MessageMessageChannel::get( - Message& msg, const Queue& queue, AckMode ackMode) -{ - Mutex::ScopedLock l(lock); - std::string destName=newTag(); - MessageGetDestination dest; - incoming.addDestination(destName, dest); - channel.send( - make_shared_ptr( - new MessageGetBody( - channel.version, 0, queue.getName(), destName, ackMode)), - dest.action()); - return dest.wait(msg); -} - - -/** Convert a message to a transfer command. */ -MessageTransferBody::shared_ptr makeTransfer( - ProtocolVersion version, - const Message& msg, const string& destination, - const std::string& routingKey, bool mandatory, bool immediate) -{ - return MessageTransferBody::shared_ptr( - new MessageTransferBody( - version, - 0, // FIXME aconway 2007-04-03: ticket. - destination, - msg.isRedelivered(), - immediate, - 0, // FIXME aconway 2007-02-23: ttl - msg.getPriority(), - msg.getTimestamp(), - static_cast(msg.getDeliveryMode()), - 0, // FIXME aconway 2007-04-03: Expiration - string(), // Exchange: for broker use only. - routingKey, - msg.getMessageId(), - msg.getCorrelationId(), - msg.getReplyTo(), - msg.getContentType(), - msg.getContentEncoding(), - msg.getUserId(), - msg.getAppId(), - string(), // FIXME aconway 2007-04-03: TransactionId - string(), //FIXME aconway 2007-04-03: SecurityToken - msg.getHeaders(), - Content(INLINE, msg.getData()), - mandatory - )); -} - -// FIXME aconway 2007-04-05: Generated code should provide this. -/** - * Calculate the size of a frame containing the given body type - * if all variable-lengths parts are empty. - */ -template size_t overhead() { - static AMQFrame frame( - ProtocolVersion(), 0, make_shared_ptr(new T(ProtocolVersion()))); - return frame.size(); -} - -void MessageMessageChannel::publish( - const Message& msg, const Exchange& exchange, - const std::string& routingKey, bool mandatory, bool immediate) -{ - MessageTransferBody::shared_ptr transfer = makeTransfer( - channel.getVersion(), - msg, exchange.getName(), routingKey, mandatory, immediate); - // Frame itself uses 8 bytes. - u_int32_t frameMax = channel.connection->getMaxFrameSize() - 8; - if (transfer->size() <= frameMax) { - channel.sendAndReceive(transfer); - } - else { - std::string ref = newTag(); - std::string data = transfer->getBody().getValue(); - size_t chunk = - channel.connection->getMaxFrameSize() - - (overhead() + ref.size()); - // TODO aconway 2007-04-05: cast around lack of generated setters - const_cast(transfer->getBody()) = Content(REFERENCE,ref); - channel.send( - make_shared_ptr(new MessageOpenBody(channel.version, ref))); - channel.send(transfer); - const char* p = data.data(); - const char* end = data.data()+data.size(); - while (p+chunk <= end) { - channel.send( - make_shared_ptr( - new MessageAppendBody(channel.version, ref, std::string(p, chunk)))); - p += chunk; - } - if (p < end) { - channel.send( - make_shared_ptr( - new MessageAppendBody(channel.version, ref, std::string(p, end-p)))); - } - channel.send(make_shared_ptr(new MessageCloseBody(channel.version, ref))); - } -} - -void copy(Message& msg, MessageTransferBody& transfer) { - // FIXME aconway 2007-04-05: Verify all required fields - // are copied. - msg.setContentType(transfer.getContentType()); - msg.setContentEncoding(transfer.getContentEncoding()); - msg.setHeaders(transfer.getApplicationHeaders()); - msg.setDeliveryMode(DeliveryMode(transfer.getDeliveryMode())); - msg.setPriority(transfer.getPriority()); - msg.setCorrelationId(transfer.getCorrelationId()); - msg.setReplyTo(transfer.getReplyTo()); - // FIXME aconway 2007-04-05: TTL/Expiration - msg.setMessageId(transfer.getMessageId()); - msg.setTimestamp(transfer.getTimestamp()); - msg.setUserId(transfer.getUserId()); - msg.setAppId(transfer.getAppId()); - msg.setDestination(transfer.getDestination()); - msg.setRedelivered(transfer.getRedelivered()); - msg.setDeliveryTag(0); // No meaning in 0-9 - if (transfer.getBody().isInline()) - msg.setData(transfer.getBody().getValue()); -} - -void MessageMessageChannel::handle(boost::shared_ptr method) { - assert(method->amqpClassId() ==MessageTransferBody::CLASS_ID); - switch(method->amqpMethodId()) { - case MessageAppendBody::METHOD_ID: { - MessageAppendBody::shared_ptr append = - shared_polymorphic_downcast(method); - incoming.appendReference(append->getReference(), append->getBytes()); - break; - } - case MessageOpenBody::METHOD_ID: { - MessageOpenBody::shared_ptr open = - shared_polymorphic_downcast(method); - incoming.openReference(open->getReference()); - break; - } - - case MessageCloseBody::METHOD_ID: { - MessageCloseBody::shared_ptr close = - shared_polymorphic_downcast(method); - incoming.closeReference(close->getReference()); - break; - } - - case MessageTransferBody::METHOD_ID: { - MessageTransferBody::shared_ptr transfer= - shared_polymorphic_downcast(method); - if (transfer->getBody().isInline()) { - Message msg; - copy(msg, *transfer); - // Deliver it. - incoming.getDestination(transfer->getDestination()).message(msg); - } - else { - Message& msg=incoming.createMessage( - transfer->getDestination(), transfer->getBody().getValue()); - copy(msg, *transfer); - // Will be delivered when reference closes. - } - break; - } - - case MessageEmptyBody::METHOD_ID: - case MessageOkBody::METHOD_ID: - // Nothing to do - break; - - // FIXME aconway 2007-04-03: TODO - case MessageCancelBody::METHOD_ID: - case MessageCheckpointBody::METHOD_ID: - case MessageOffsetBody::METHOD_ID: - case MessageQosBody::METHOD_ID: - case MessageRecoverBody::METHOD_ID: - case MessageRejectBody::METHOD_ID: - case MessageResumeBody::METHOD_ID: - break; - default: - throw Channel::UnknownMethod(); - } -} - -void MessageMessageChannel::handle(AMQHeaderBody::shared_ptr ){ - throw QPID_ERROR(INTERNAL_ERROR, "Basic protocol not supported"); -} - -void MessageMessageChannel::handle(AMQContentBody::shared_ptr ){ - throw QPID_ERROR(INTERNAL_ERROR, "Basic protocol not supported"); -} - -// FIXME aconway 2007-02-23: -// void MessageMessageChannel::deliver(IncomingMessage::Destination& consumer, Message& msg){ -// //record delivery tag: -// consumer.lastDeliveryTag = msg.getDeliveryTag(); - -// //allow registered listener to handle the message -// consumer.listener->received(msg); - -// if(channel.isOpen()){ -// bool multiple(false); -// switch(consumer.ackMode){ -// case LAZY_ACK: -// multiple = true; -// if(++(consumer.count) < channel.getPrefetch()) -// break; -// //else drop-through -// case AUTO_ACK: -// consumer.lastDeliveryTag = 0; -// channel.send( -// new MessageAckBody( -// channel.version, msg.getDeliveryTag(), multiple)); -// case NO_ACK: // Nothing to do -// case CLIENT_ACK: // User code must ack. -// break; -// // TODO aconway 2007-02-22: Provide a way for user -// // to ack! -// } -// } - -// //as it stands, transactionality is entirely orthogonal to ack -// //mode, though the acks will not be processed by the broker under -// //a transaction until it commits. -// } - - -void MessageMessageChannel::run() { - // FIXME aconway 2007-02-23: -// while(channel.isOpen()) { -// try { -// Message msg = incoming.waitDispatch(); -// if(msg.getMethod()->isA()) { -// ReturnedMessageHandler* handler=0; -// { -// Mutex::ScopedLock l(lock); -// handler=returnsHandler; -// } -// if(handler == 0) { -// // TODO aconway 2007-02-20: proper logging. -// cout << "Message returned: " << msg.getData() << endl; -// } -// else -// handler->returned(msg); -// } -// else { -// MessageDeliverBody::shared_ptr deliverBody = -// boost::shared_polymorphic_downcast( -// msg.getMethod()); -// std::string tag = deliverBody->getConsumerTag(); -// Consumer consumer; -// { -// Mutex::ScopedLock l(lock); -// ConsumerMap::iterator i = consumers.find(tag); -// if(i == consumers.end()) -// THROW_QPID_ERROR(PROTOCOL_ERROR+504, -// "Unknown consumer tag=" + tag); -// consumer = i->second; -// } -// deliver(consumer, msg); -// } -// } -// catch (const ShutdownException&) { -// /* Orderly shutdown */ -// } -// catch (const Exception& e) { -// // FIXME aconway 2007-02-20: Report exception to user. -// cout << "client::Message::run() terminated by: " << e.toString() -// << "(" << typeid(e).name() << ")" << endl; -// } -// } -} - -void MessageMessageChannel::setReturnedMessageHandler( - ReturnedMessageHandler* ) -{ - throw QPID_ERROR(INTERNAL_ERROR, "Message class does not support returns"); -} - -void MessageMessageChannel::setQos(){ - channel.sendAndReceive( - make_shared_ptr(new MessageQosBody(channel.version, 0, channel.getPrefetch(), false))); - if(channel.isTransactional()) - channel.sendAndReceive( - make_shared_ptr(new TxSelectBody(channel.version))); -} - -}} // namespace qpid::client diff --git a/cpp/src/client/MessageMessageChannel.h b/cpp/src/client/MessageMessageChannel.h deleted file mode 100644 index 4c4721ce90..0000000000 --- a/cpp/src/client/MessageMessageChannel.h +++ /dev/null @@ -1,82 +0,0 @@ -#ifndef _client_MessageMessageChannel_h -#define _client_MessageMessageChannel_h - -/* - * - * 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 "MessageChannel.h" -#include "IncomingMessage.h" -#include "../sys/Monitor.h" -#include - -namespace qpid { -namespace client { -/** - * Messaging implementation using AMQP 0-9 MessageMessageChannel class - * to send and receiving messages. - */ -class MessageMessageChannel : public MessageChannel -{ - public: - MessageMessageChannel(Channel& parent); - - void consume( - Queue& queue, std::string& tag, MessageListener* listener, - AckMode ackMode = NO_ACK, bool noLocal = false, bool synch = true, - const framing::FieldTable* fields = 0); - - void cancel(const std::string& tag, bool synch = true); - - bool get(Message& msg, const Queue& queue, AckMode ackMode = NO_ACK); - - void publish(const Message& msg, const Exchange& exchange, - const std::string& routingKey, - bool mandatory = false, bool immediate = false); - - void setReturnedMessageHandler(ReturnedMessageHandler* handler); - - void run(); - - void handle(boost::shared_ptr); - - void handle(shared_ptr); - - void handle(shared_ptr); - - void setQos(); - - void close(); - - private: - typedef boost::ptr_map - Destinations; - - std::string newTag(); - - sys::Mutex lock; - Channel& channel; - IncomingMessage incoming; - long tagCount; -}; - -}} // namespace qpid::client - - - -#endif /*!_client_MessageMessageChannel_h*/ - diff --git a/cpp/src/client/MethodBodyInstances.h b/cpp/src/client/MethodBodyInstances.h deleted file mode 100644 index 57b9bf73ce..0000000000 --- a/cpp/src/client/MethodBodyInstances.h +++ /dev/null @@ -1,100 +0,0 @@ -/* - * - * 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 "../framing/amqp_framing.h" - -#ifndef _MethodBodyInstances_h_ -#define _MethodBodyInstances_h_ - -namespace qpid { -namespace client { - -/** - * A list of method body instances that can be used to compare against - * incoming bodies. - */ -class MethodBodyInstances -{ -private: - qpid::framing::ProtocolVersion version; -public: - const qpid::framing::BasicCancelOkBody basic_cancel_ok; - const qpid::framing::BasicConsumeOkBody basic_consume_ok; - const qpid::framing::BasicDeliverBody basic_deliver; - const qpid::framing::BasicGetEmptyBody basic_get_empty; - const qpid::framing::BasicGetOkBody basic_get_ok; - const qpid::framing::BasicQosOkBody basic_qos_ok; - const qpid::framing::BasicReturnBody basic_return; - const qpid::framing::ChannelCloseBody channel_close; - const qpid::framing::ChannelCloseOkBody channel_close_ok; - const qpid::framing::ChannelFlowBody channel_flow; - const qpid::framing::ChannelOpenOkBody channel_open_ok; - const qpid::framing::ConnectionCloseBody connection_close; - const qpid::framing::ConnectionCloseOkBody connection_close_ok; - const qpid::framing::ConnectionOpenOkBody connection_open_ok; - const qpid::framing::ConnectionRedirectBody connection_redirect; - const qpid::framing::ConnectionStartBody connection_start; - const qpid::framing::ConnectionTuneBody connection_tune; - const qpid::framing::ExchangeDeclareOkBody exchange_declare_ok; - const qpid::framing::ExchangeDeleteOkBody exchange_delete_ok; - const qpid::framing::QueueDeclareOkBody queue_declare_ok; - const qpid::framing::QueueDeleteOkBody queue_delete_ok; - const qpid::framing::QueueBindOkBody queue_bind_ok; - const qpid::framing::TxCommitOkBody tx_commit_ok; - const qpid::framing::TxRollbackOkBody tx_rollback_ok; - const qpid::framing::TxSelectOkBody tx_select_ok; - - MethodBodyInstances(uint8_t major, uint8_t minor) : - version(major, minor), - basic_cancel_ok(version), - basic_consume_ok(version), - basic_deliver(version), - basic_get_empty(version), - basic_get_ok(version), - basic_qos_ok(version), - basic_return(version), - channel_close(version), - channel_close_ok(version), - channel_flow(version), - channel_open_ok(version), - connection_close(version), - connection_close_ok(version), - connection_open_ok(version), - connection_redirect(version), - connection_start(version), - connection_tune(version), - exchange_declare_ok(version), - exchange_delete_ok(version), - queue_declare_ok(version), - queue_delete_ok(version), - queue_bind_ok(version), - tx_commit_ok(version), - tx_rollback_ok(version), - tx_select_ok(version) - {} - -}; - -static MethodBodyInstances method_bodies(8, 0); - -} // namespace client -} // namespace qpid - -#endif diff --git a/cpp/src/client/ResponseHandler.cpp b/cpp/src/client/ResponseHandler.cpp deleted file mode 100644 index ca0129d587..0000000000 --- a/cpp/src/client/ResponseHandler.cpp +++ /dev/null @@ -1,79 +0,0 @@ -/* - * - * 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 "../QpidError.h" -#include -#include "ResponseHandler.h" -#include "../framing/AMQMethodBody.h" - -using namespace qpid::sys; -using namespace qpid::framing; - -namespace qpid { -namespace client { - -ResponseHandler::ResponseHandler() : waiting(false), shutdownFlag(false) {} - -ResponseHandler::~ResponseHandler(){} - -bool ResponseHandler::isWaiting() { - Monitor::ScopedLock l(monitor); - return waiting; -} - -void ResponseHandler::expect(){ - Monitor::ScopedLock l(monitor); - waiting = true; -} - -void ResponseHandler::signalResponse(MethodPtr _response) -{ - Monitor::ScopedLock l(monitor); - response = _response; - if (!response) - shutdownFlag=true; - waiting = false; - monitor.notify(); -} - -ResponseHandler::MethodPtr ResponseHandler::receive() { - Monitor::ScopedLock l(monitor); - while (!response && !shutdownFlag) - monitor.wait(); - if (shutdownFlag) - THROW_QPID_ERROR( - PROTOCOL_ERROR, "Channel closed unexpectedly."); - MethodPtr result = response; - response.reset(); - return result; -} - -ResponseHandler::MethodPtr ResponseHandler::receive(ClassId c, MethodId m) { - MethodPtr response = receive(); - if(c != response->amqpClassId() || m != response->amqpMethodId()) { - THROW_QPID_ERROR( - PROTOCOL_ERROR, - boost::format("Expected class:method %d:%d, got %d:%d") - % c % m % response->amqpClassId() % response->amqpMethodId()); - } - return response; -} - -}} // namespace qpid::client diff --git a/cpp/src/client/ResponseHandler.h b/cpp/src/client/ResponseHandler.h deleted file mode 100644 index 289a5dd994..0000000000 --- a/cpp/src/client/ResponseHandler.h +++ /dev/null @@ -1,75 +0,0 @@ -/* - * - * 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 "../shared_ptr.h" -#include "../sys/Monitor.h" - -#ifndef _ResponseHandler_ -#define _ResponseHandler_ - -namespace qpid { - -namespace framing { -class AMQMethodBody; -} - -namespace client { - -/** - * Holds a response from the broker peer for the client. - */ -class ResponseHandler{ - typedef shared_ptr MethodPtr; - bool waiting; - bool shutdownFlag; - MethodPtr response; - sys::Monitor monitor; - - public: - ResponseHandler(); - ~ResponseHandler(); - - /** Is a response expected? */ - bool isWaiting(); - - /** Provide a response to the waiting thread */ - void signalResponse(MethodPtr response); - - /** Indicate a message is expected. */ - void expect(); - - /** Wait for a response. */ - MethodPtr receive(); - - /** Wait for a specific response. */ - MethodPtr receive(framing::ClassId, framing::MethodId); - - /** Template version of receive returns typed pointer. */ - template - shared_ptr receive() { - return shared_polymorphic_downcast( - receive(BodyType::CLASS_ID, BodyType::METHOD_ID)); - } -}; - -}} - - -#endif diff --git a/cpp/src/client/ReturnedMessageHandler.cpp b/cpp/src/client/ReturnedMessageHandler.cpp deleted file mode 100644 index 35d0b5c0a8..0000000000 --- a/cpp/src/client/ReturnedMessageHandler.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* - * - * 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 "ReturnedMessageHandler.h" - -qpid::client::ReturnedMessageHandler::~ReturnedMessageHandler() {} diff --git a/cpp/src/client/ReturnedMessageHandler.h b/cpp/src/client/ReturnedMessageHandler.h deleted file mode 100644 index 8b42fc0764..0000000000 --- a/cpp/src/client/ReturnedMessageHandler.h +++ /dev/null @@ -1,49 +0,0 @@ -/* - * - * 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 - -#ifndef _ReturnedMessageHandler_ -#define _ReturnedMessageHandler_ - -#include "ClientMessage.h" - -namespace qpid { -namespace client { - - /** - * An interface through which returned messages can be received by - * an application. - * - * @see Channel::setReturnedMessageHandler() - * - * \ingroup clientapi - */ - class ReturnedMessageHandler{ - public: - virtual ~ReturnedMessageHandler(); - virtual void returned(Message& msg) = 0; - }; - -} -} - - -#endif diff --git a/cpp/src/doxygen_mainpage.h b/cpp/src/doxygen_mainpage.h deleted file mode 100644 index b354238cd0..0000000000 --- a/cpp/src/doxygen_mainpage.h +++ /dev/null @@ -1,45 +0,0 @@ -// This header file is just for doxygen documentation purposes. - -/*!\mainpage Qpid C++ Developer Kit. - * - *\section intro_sec Introduction - * - * The Qpid project provides implementations of the AMQP messaging specification in several programming language. - * - * Qpidc provides APIs and libraries to implement AMQP - * clients in C++. Qpidc clients can interact with any compliant AMQP - * message broker. The Qpid project also provides an AMQP broker - * daemon called qpidd that you can use with your qpidc clients. - * - *\section install_sec Installation - * - * If you are installing from the source distribution -
-   > ./configure && make
-   > make install  
- * This will build and install the client development kit and the broker - * in standard places. Use - * ./configure --help for more options. - * - * You can also install from RPMs with the rpm -i command. - * You will need - * - qpidc for core libraries. - * - qpidc-devel for header files and developer documentation. - * - qpidd for the broker daemon. - * - *\section getstart_sec Getting Started - * - * If you have installed in the standard places you should use - * these compile flags: - * - * -I/usr/include/qpidc -I/usr/include/qpidc/framing -I/usr/include/qpidc/sys - * - * and these link flags: - * - * -lqpidcommon -lqpidclient - * - * If you have installed somewhere else you should modify the flags - * appropriately. - * - * See the \ref clientapi "client API module" for more on the client API. - */ diff --git a/cpp/src/framing/AMQBody.cpp b/cpp/src/framing/AMQBody.cpp deleted file mode 100644 index a64d224a86..0000000000 --- a/cpp/src/framing/AMQBody.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * - * 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 "AMQBody.h" -#include - -std::ostream& qpid::framing::operator<<(std::ostream& out, const qpid::framing::AMQBody& body) -{ - body.print(out); - return out; -} - -qpid::framing::AMQBody::~AMQBody() {} - - diff --git a/cpp/src/framing/AMQBody.h b/cpp/src/framing/AMQBody.h deleted file mode 100644 index eaa568c06a..0000000000 --- a/cpp/src/framing/AMQBody.h +++ /dev/null @@ -1,59 +0,0 @@ -/* - * - * 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 -#include "amqp_types.h" -#include "Buffer.h" - -#ifndef _AMQBody_ -#define _AMQBody_ - -namespace qpid { - namespace framing { - - class AMQBody - { - public: - typedef boost::shared_ptr shared_ptr; - - virtual ~AMQBody(); - virtual uint32_t size() const = 0; - virtual uint8_t type() const = 0; - virtual void encode(Buffer& buffer) const = 0; - virtual void decode(Buffer& buffer, uint32_t size) = 0; - - virtual void print(std::ostream& out) const = 0; - }; - - std::ostream& operator<<(std::ostream& out, const AMQBody& body) ; - - enum BodyTypes { - METHOD_BODY = 1, - HEADER_BODY = 2, - CONTENT_BODY = 3, - HEARTBEAT_BODY = 8, - REQUEST_BODY = 9, - RESPONSE_BODY = 10 - }; - } -} - - -#endif diff --git a/cpp/src/framing/AMQContentBody.cpp b/cpp/src/framing/AMQContentBody.cpp deleted file mode 100644 index 19d2e5714a..0000000000 --- a/cpp/src/framing/AMQContentBody.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * - * 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 "AMQContentBody.h" -#include - -qpid::framing::AMQContentBody::AMQContentBody(){ -} - -qpid::framing::AMQContentBody::AMQContentBody(const string& _data) : data(_data){ -} - -uint32_t qpid::framing::AMQContentBody::size() const{ - return data.size(); -} -void qpid::framing::AMQContentBody::encode(Buffer& buffer) const{ - buffer.putRawData(data); -} -void qpid::framing::AMQContentBody::decode(Buffer& buffer, uint32_t _size){ - buffer.getRawData(data, _size); -} - -void qpid::framing::AMQContentBody::print(std::ostream& out) const -{ - out << "content (" << size() << " bytes)"; -} diff --git a/cpp/src/framing/AMQContentBody.h b/cpp/src/framing/AMQContentBody.h deleted file mode 100644 index 6d4c1ef4b6..0000000000 --- a/cpp/src/framing/AMQContentBody.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * - * 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 "amqp_types.h" -#include "AMQBody.h" -#include "Buffer.h" - -#ifndef _AMQContentBody_ -#define _AMQContentBody_ - -namespace qpid { -namespace framing { - -class AMQContentBody : public AMQBody -{ - string data; - -public: - typedef boost::shared_ptr shared_ptr; - - AMQContentBody(); - AMQContentBody(const string& data); - inline virtual ~AMQContentBody(){} - inline uint8_t type() const { return CONTENT_BODY; }; - inline string& getData(){ return data; } - uint32_t size() const; - void encode(Buffer& buffer) const; - void decode(Buffer& buffer, uint32_t size); - void print(std::ostream& out) const; -}; - -} -} - - -#endif diff --git a/cpp/src/framing/AMQDataBlock.h b/cpp/src/framing/AMQDataBlock.h deleted file mode 100644 index 6ff61b185e..0000000000 --- a/cpp/src/framing/AMQDataBlock.h +++ /dev/null @@ -1,42 +0,0 @@ -/* - * - * 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 "Buffer.h" - -#ifndef _AMQDataBlock_ -#define _AMQDataBlock_ - -namespace qpid { -namespace framing { - -class AMQDataBlock -{ -public: - virtual ~AMQDataBlock() {} - virtual void encode(Buffer& buffer) = 0; - virtual bool decode(Buffer& buffer) = 0; - virtual uint32_t size() const = 0; -}; - -} -} - - -#endif diff --git a/cpp/src/framing/AMQFrame.cpp b/cpp/src/framing/AMQFrame.cpp deleted file mode 100644 index 8b79286107..0000000000 --- a/cpp/src/framing/AMQFrame.cpp +++ /dev/null @@ -1,139 +0,0 @@ - -/* - * - * 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 - -#include "AMQFrame.h" -#include "../QpidError.h" -#include "AMQRequestBody.h" -#include "AMQResponseBody.h" - - -namespace qpid { -namespace framing { - - -AMQP_MethodVersionMap AMQFrame::versionMap; - -AMQFrame::AMQFrame(ProtocolVersion _version): -version(_version) - { - assert(version != ProtocolVersion(0,0)); - } - -AMQFrame::AMQFrame(ProtocolVersion _version, uint16_t _channel, AMQBody* _body) : -version(_version), channel(_channel), body(_body) -{} - -AMQFrame::AMQFrame(ProtocolVersion _version, uint16_t _channel, const AMQBody::shared_ptr& _body) : -version(_version), channel(_channel), body(_body) -{} - -AMQFrame::~AMQFrame() {} - -uint16_t AMQFrame::getChannel(){ - return channel; -} - -AMQBody::shared_ptr AMQFrame::getBody(){ - return body; -} - -void AMQFrame::encode(Buffer& buffer) -{ - buffer.putOctet(body->type()); - buffer.putShort(channel); - buffer.putLong(body->size()); - body->encode(buffer); - buffer.putOctet(0xCE); -} - -uint32_t AMQFrame::size() const{ - assert(body.get()); - return 1/*type*/ + 2/*channel*/ + 4/*body size*/ + body->size() - + 1/*0xCE*/; -} - -bool AMQFrame::decode(Buffer& buffer) -{ - if(buffer.available() < 7) - return false; - buffer.record(); - uint32_t frameSize = decodeHead(buffer); - if(buffer.available() < frameSize + 1){ - buffer.restore(); - return false; - } - decodeBody(buffer, frameSize); - uint8_t end = buffer.getOctet(); - if(end != 0xCE) THROW_QPID_ERROR(FRAMING_ERROR, "Frame end not found"); - return true; -} - -uint32_t AMQFrame::decodeHead(Buffer& buffer){ - type = buffer.getOctet(); - channel = buffer.getShort(); - return buffer.getLong(); -} - -void AMQFrame::decodeBody(Buffer& buffer, uint32_t size) -{ - switch(type) - { - case METHOD_BODY: - body = AMQMethodBody::create(versionMap, version, buffer); - break; - case REQUEST_BODY: - body = AMQRequestBody::create(versionMap, version, buffer); - break; - case RESPONSE_BODY: - body = AMQResponseBody::create(versionMap, version, buffer); - break; - case HEADER_BODY: - body = AMQBody::shared_ptr(new AMQHeaderBody()); - break; - case CONTENT_BODY: - body = AMQBody::shared_ptr(new AMQContentBody()); - break; - case HEARTBEAT_BODY: - body = AMQBody::shared_ptr(new AMQHeartbeatBody()); - break; - default: - THROW_QPID_ERROR( - FRAMING_ERROR, - boost::format("Unknown frame type %d") % type); - } - body->decode(buffer, size); -} - -std::ostream& operator<<(std::ostream& out, const AMQFrame& t) -{ - out << "Frame[channel=" << t.channel << "; "; - if (t.body.get() == 0) - out << "empty"; - else - out << *t.body; - out << "]"; - return out; -} - - -}} // namespace qpid::framing diff --git a/cpp/src/framing/AMQFrame.h b/cpp/src/framing/AMQFrame.h deleted file mode 100644 index d31df4cc41..0000000000 --- a/cpp/src/framing/AMQFrame.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef _AMQFrame_ -#define _AMQFrame_ - -/* - * - * 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 - -#include "amqp_types.h" -#include "AMQBody.h" -#include "AMQDataBlock.h" -#include "AMQMethodBody.h" -#include "AMQHeaderBody.h" -#include "AMQContentBody.h" -#include "AMQHeartbeatBody.h" -#include "../gen/AMQP_MethodVersionMap.h" -#include "../gen/AMQP_HighestVersion.h" -#include "Buffer.h" - -namespace qpid { -namespace framing { - - -class AMQFrame : public AMQDataBlock -{ - public: - AMQFrame(ProtocolVersion _version = highestProtocolVersion); - AMQFrame(ProtocolVersion _version, uint16_t channel, AMQBody* body); - AMQFrame(ProtocolVersion _version, uint16_t channel, const AMQBody::shared_ptr& body); - virtual ~AMQFrame(); - virtual void encode(Buffer& buffer); - virtual bool decode(Buffer& buffer); - virtual uint32_t size() const; - uint16_t getChannel(); - AMQBody::shared_ptr getBody(); - - /** Convenience template to cast the body to an expected type */ - template boost::shared_ptr castBody() { - assert(dynamic_cast(getBody().get())); - boost::static_pointer_cast(getBody()); - } - - uint32_t decodeHead(Buffer& buffer); - void decodeBody(Buffer& buffer, uint32_t size); - - private: - static AMQP_MethodVersionMap versionMap; - ProtocolVersion version; - - uint16_t channel; - uint8_t type; - AMQBody::shared_ptr body; - - - friend std::ostream& operator<<(std::ostream& out, const AMQFrame& body); -}; - -}} // namespace qpid::framing - - -#endif diff --git a/cpp/src/framing/AMQHeaderBody.cpp b/cpp/src/framing/AMQHeaderBody.cpp deleted file mode 100644 index fccffd006c..0000000000 --- a/cpp/src/framing/AMQHeaderBody.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - * - * 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 "AMQHeaderBody.h" -#include "../QpidError.h" -#include "BasicHeaderProperties.h" - -qpid::framing::AMQHeaderBody::AMQHeaderBody(int classId) : weight(0), contentSize(0){ - createProperties(classId); -} - -qpid::framing::AMQHeaderBody::AMQHeaderBody() : properties(0), weight(0), contentSize(0){ -} - -qpid::framing::AMQHeaderBody::~AMQHeaderBody(){ - delete properties; -} - -uint32_t qpid::framing::AMQHeaderBody::size() const{ - return 12 + properties->size(); -} - -void qpid::framing::AMQHeaderBody::encode(Buffer& buffer) const { - buffer.putShort(properties->classId()); - buffer.putShort(weight); - buffer.putLongLong(contentSize); - properties->encode(buffer); -} - -void qpid::framing::AMQHeaderBody::decode(Buffer& buffer, uint32_t bufSize){ - uint16_t classId = buffer.getShort(); - weight = buffer.getShort(); - contentSize = buffer.getLongLong(); - createProperties(classId); - properties->decode(buffer, bufSize - 12); -} - -void qpid::framing::AMQHeaderBody::createProperties(int classId){ - switch(classId){ - case BASIC: - properties = new qpid::framing::BasicHeaderProperties(); - break; - default: - THROW_QPID_ERROR(FRAMING_ERROR, "Unknown header class"); - } -} - -void qpid::framing::AMQHeaderBody::print(std::ostream& out) const -{ - out << "header (" << size() << " bytes)" << " content_size=" << getContentSize(); - const BasicHeaderProperties* props = - dynamic_cast(getProperties()); - if (props) { - out << ", message_id=" << props->getMessageId(); - out << ", delivery_mode=" << (int) props->getDeliveryMode(); - out << ", headers=" << const_cast(props)->getHeaders(); - } -} diff --git a/cpp/src/framing/AMQHeaderBody.h b/cpp/src/framing/AMQHeaderBody.h deleted file mode 100644 index 691ceeff73..0000000000 --- a/cpp/src/framing/AMQHeaderBody.h +++ /dev/null @@ -1,60 +0,0 @@ -/* - * - * 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 "amqp_types.h" -#include "AMQBody.h" -#include "Buffer.h" -#include "HeaderProperties.h" - -#ifndef _AMQHeaderBody_ -#define _AMQHeaderBody_ - -namespace qpid { -namespace framing { - -class AMQHeaderBody : public AMQBody -{ - HeaderProperties* properties; - uint16_t weight; - uint64_t contentSize; - - void createProperties(int classId); -public: - typedef boost::shared_ptr shared_ptr; - - AMQHeaderBody(int classId); - AMQHeaderBody(); - inline uint8_t type() const { return HEADER_BODY; } - HeaderProperties* getProperties(){ return properties; } - const HeaderProperties* getProperties() const { return properties; } - inline uint64_t getContentSize() const { return contentSize; } - inline void setContentSize(uint64_t _size) { contentSize = _size; } - virtual ~AMQHeaderBody(); - virtual uint32_t size() const; - virtual void encode(Buffer& buffer) const; - virtual void decode(Buffer& buffer, uint32_t size); - virtual void print(std::ostream& out) const; -}; - -} -} - - -#endif diff --git a/cpp/src/framing/AMQHeartbeatBody.cpp b/cpp/src/framing/AMQHeartbeatBody.cpp deleted file mode 100644 index 140ce2e794..0000000000 --- a/cpp/src/framing/AMQHeartbeatBody.cpp +++ /dev/null @@ -1,29 +0,0 @@ -/* - * - * 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 "AMQHeartbeatBody.h" -#include - -qpid::framing::AMQHeartbeatBody::~AMQHeartbeatBody() {} - -void qpid::framing::AMQHeartbeatBody::print(std::ostream& out) const { - out << "heartbeat"; -} diff --git a/cpp/src/framing/AMQHeartbeatBody.h b/cpp/src/framing/AMQHeartbeatBody.h deleted file mode 100644 index 4c046b81a7..0000000000 --- a/cpp/src/framing/AMQHeartbeatBody.h +++ /dev/null @@ -1,47 +0,0 @@ -/* - * - * 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 "amqp_types.h" -#include "AMQBody.h" -#include "Buffer.h" - -#ifndef _AMQHeartbeatBody_ -#define _AMQHeartbeatBody_ - -namespace qpid { -namespace framing { - -class AMQHeartbeatBody : public AMQBody -{ -public: - typedef boost::shared_ptr shared_ptr; - - virtual ~AMQHeartbeatBody(); - inline uint32_t size() const { return 0; } - inline uint8_t type() const { return HEARTBEAT_BODY; } - inline void encode(Buffer& ) const {} - inline void decode(Buffer& , uint32_t /*size*/) {} - virtual void print(std::ostream& out) const; -}; - -} -} - -#endif diff --git a/cpp/src/framing/AMQMethodBody.cpp b/cpp/src/framing/AMQMethodBody.cpp deleted file mode 100644 index 0180d6c372..0000000000 --- a/cpp/src/framing/AMQMethodBody.cpp +++ /dev/null @@ -1,59 +0,0 @@ -/* - * - * 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 "AMQFrame.h" -#include "AMQMethodBody.h" -#include "../QpidError.h" -#include "../gen/AMQP_MethodVersionMap.h" - -namespace qpid { -namespace framing { - -void AMQMethodBody::encodeId(Buffer& buffer) const{ - buffer.putShort(amqpClassId()); - buffer.putShort(amqpMethodId()); -} - -void AMQMethodBody::invoke(AMQP_ServerOperations&, const MethodContext&){ - assert(0); - THROW_QPID_ERROR(PROTOCOL_ERROR, "Method not supported by AMQP Server."); -} - -AMQMethodBody::shared_ptr AMQMethodBody::create( - AMQP_MethodVersionMap& versionMap, ProtocolVersion version, - Buffer& buffer) -{ - ClassMethodId id; - id.decode(buffer); - return AMQMethodBody::shared_ptr( - versionMap.createMethodBody( - id.classId, id.methodId, version.getMajor(), version.getMinor())); -} - -void AMQMethodBody::ClassMethodId::decode(Buffer& buffer) { - classId = buffer.getShort(); - methodId = buffer.getShort(); -} - -void AMQMethodBody::decode(Buffer& buffer, uint32_t /*size*/) { - decodeContent(buffer); -} - -}} // namespace qpid::framing diff --git a/cpp/src/framing/AMQMethodBody.h b/cpp/src/framing/AMQMethodBody.h deleted file mode 100644 index 2fb952ddbb..0000000000 --- a/cpp/src/framing/AMQMethodBody.h +++ /dev/null @@ -1,84 +0,0 @@ -#ifndef _AMQMethodBody_ -#define _AMQMethodBody_ - -/* - * - * 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 -#include "amqp_types.h" -#include "AMQBody.h" -#include "Buffer.h" -#include "../gen/AMQP_ServerOperations.h" -#include "MethodContext.h" - -namespace qpid { -namespace framing { - -class AMQP_MethodVersionMap; - -class AMQMethodBody : public AMQBody -{ - public: - typedef boost::shared_ptr shared_ptr; - - static shared_ptr create( - AMQP_MethodVersionMap& map, ProtocolVersion version, Buffer& buf); - - ProtocolVersion version; - uint8_t type() const { return METHOD_BODY; } - AMQMethodBody(uint8_t major, uint8_t minor) : version(major, minor) {} - AMQMethodBody(ProtocolVersion ver) : version(ver) {} - virtual ~AMQMethodBody() {} - void decode(Buffer&, uint32_t); - - virtual MethodId amqpMethodId() const = 0; - virtual ClassId amqpClassId() const = 0; - - virtual void invoke(AMQP_ServerOperations&, const MethodContext&); - - template bool isA() { - return amqpClassId()==T::CLASS_ID && amqpMethodId()==T::METHOD_ID; - } - - /** Return request ID or response correlationID */ - virtual RequestId getRequestId() const { return 0; } - - virtual bool isRequest() const { return false; } - virtual bool isResponse() const { return false; } - - protected: - static uint32_t baseSize() { return 4; } - - struct ClassMethodId { - uint16_t classId; - uint16_t methodId; - void decode(Buffer& b); - }; - - void encodeId(Buffer& buffer) const; - virtual void encodeContent(Buffer& buffer) const = 0; - virtual void decodeContent(Buffer& buffer) = 0; -}; - - -}} // namespace qpid::framing - - -#endif diff --git a/cpp/src/framing/AMQRequestBody.cpp b/cpp/src/framing/AMQRequestBody.cpp deleted file mode 100644 index bb5a448dc3..0000000000 --- a/cpp/src/framing/AMQRequestBody.cpp +++ /dev/null @@ -1,66 +0,0 @@ -/* - * - * 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 "AMQRequestBody.h" -#include "../gen/AMQP_MethodVersionMap.h" - -namespace qpid { -namespace framing { - -void AMQRequestBody::Data::encode(Buffer& buffer) const { - buffer.putLongLong(requestId); - buffer.putLongLong(responseMark); - buffer.putLong(0); // Reserved long in spec. -} - -void AMQRequestBody::Data::decode(Buffer& buffer) { - requestId = buffer.getLongLong(); - responseMark = buffer.getLongLong(); - buffer.getLong(); // Ignore reserved long. -} - -void AMQRequestBody::encode(Buffer& buffer) const { - data.encode(buffer); - encodeId(buffer); - encodeContent(buffer); -} - -AMQRequestBody::shared_ptr -AMQRequestBody::create( - AMQP_MethodVersionMap& versionMap, ProtocolVersion version, - Buffer& buffer) -{ - ClassMethodId id; - Data data; - data.decode(buffer); - id.decode(buffer); - AMQRequestBody* body = dynamic_cast( - versionMap.createMethodBody( - id.classId, id.methodId, version.getMajor(), version.getMinor())); - assert(body); - body->data = data; - return AMQRequestBody::shared_ptr(body); -} - -void AMQRequestBody::printPrefix(std::ostream& out) const { - out << "request(id=" << data.requestId << ",mark=" - << data.responseMark << "): "; -} - -}} // namespace qpid::framing - diff --git a/cpp/src/framing/AMQRequestBody.h b/cpp/src/framing/AMQRequestBody.h deleted file mode 100644 index f21659a57a..0000000000 --- a/cpp/src/framing/AMQRequestBody.h +++ /dev/null @@ -1,78 +0,0 @@ -#ifndef _framing_AMQRequestBody_h -#define _framing_AMQRequestBody_h - -/* - * - * 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 "AMQMethodBody.h" - -namespace qpid { -namespace framing { - -/** - * Body of a request method frame. - */ -class AMQRequestBody : public AMQMethodBody -{ - public: - typedef boost::shared_ptr shared_ptr; - - struct Data { - Data(RequestId id=0, ResponseId mark=0) - : requestId(id), responseMark(mark) {} - void encode(Buffer&) const; - void decode(Buffer&); - - RequestId requestId; - ResponseId responseMark; - }; - - static Data& getData(const AMQBody::shared_ptr& body) { - return boost::dynamic_pointer_cast(body)->getData(); - } - - static shared_ptr create( - AMQP_MethodVersionMap& versionMap, ProtocolVersion version, - Buffer& buffer); - - AMQRequestBody(ProtocolVersion v, RequestId id=0, ResponseId mark=0) - : AMQMethodBody(v), data(id, mark) {} - - uint8_t type() const { return REQUEST_BODY; } - void encode(Buffer& buffer) const; - - Data& getData() { return data; } - RequestId getRequestId() const { return data.requestId; } - ResponseId getResponseMark() const { return data.responseMark; } - void setRequestId(RequestId id) { data.requestId=id; } - void setResponseMark(ResponseId mark) { data.responseMark=mark; } - - bool isRequest()const { return true; } - static const uint32_t baseSize() { return AMQMethodBody::baseSize()+20; } - protected: - void printPrefix(std::ostream& out) const; - - private: - Data data; -}; - -}} // namespace qpid::framing - - - -#endif /*!_framing_AMQRequestBody_h*/ diff --git a/cpp/src/framing/AMQResponseBody.cpp b/cpp/src/framing/AMQResponseBody.cpp deleted file mode 100644 index ad99ab32e0..0000000000 --- a/cpp/src/framing/AMQResponseBody.cpp +++ /dev/null @@ -1,65 +0,0 @@ -/* - * - * 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 "AMQFrame.h" -#include "AMQResponseBody.h" -#include "../gen/AMQP_MethodVersionMap.h" - -namespace qpid { -namespace framing { - -void AMQResponseBody::Data::encode(Buffer& buffer) const { - buffer.putLongLong(responseId); - buffer.putLongLong(requestId); - buffer.putLong(batchOffset); -} - -void AMQResponseBody::Data::decode(Buffer& buffer) { - responseId = buffer.getLongLong(); - requestId = buffer.getLongLong(); - batchOffset = buffer.getLong(); -} - -void AMQResponseBody::encode(Buffer& buffer) const { - data.encode(buffer); - encodeId(buffer); - encodeContent(buffer); -} - -AMQResponseBody::shared_ptr AMQResponseBody::create( - AMQP_MethodVersionMap& versionMap, ProtocolVersion version, - Buffer& buffer) -{ - ClassMethodId id; - Data data; - data.decode(buffer); - id.decode(buffer); - AMQResponseBody* body = dynamic_cast( - versionMap.createMethodBody( - id.classId, id.methodId, version.getMajor(), version.getMinor())); - assert(body); - body->data = data; - return AMQResponseBody::shared_ptr(body); -} - -void AMQResponseBody::printPrefix(std::ostream& out) const { - out << "response(id=" << data.responseId << ",request=" << data.requestId - << ",batch=" << data.batchOffset << "): "; -} - -}} // namespace qpid::framing diff --git a/cpp/src/framing/AMQResponseBody.h b/cpp/src/framing/AMQResponseBody.h deleted file mode 100644 index fa381baddd..0000000000 --- a/cpp/src/framing/AMQResponseBody.h +++ /dev/null @@ -1,85 +0,0 @@ -#ifndef _framing_AMQResponseBody_h -#define _framing_AMQResponseBody_h - -/* - * - * 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 "AMQMethodBody.h" - -namespace qpid { -namespace framing { - -class AMQP_MethodVersionMap; - -/** - * Body of a response method frame. - */ -class AMQResponseBody : public AMQMethodBody -{ - - public: - typedef boost::shared_ptr shared_ptr; - - struct Data { - Data(ResponseId id=0, RequestId req=0, BatchOffset off=0) - : responseId(id), requestId(req), batchOffset(off) {} - void encode(Buffer&) const; - void decode(Buffer&); - - uint64_t responseId; - uint64_t requestId; - uint32_t batchOffset; - }; - - static Data& getData(const AMQBody::shared_ptr& body) { - return boost::dynamic_pointer_cast(body)->getData(); - } - - static shared_ptr create( - AMQP_MethodVersionMap& versionMap, ProtocolVersion version, - Buffer& buffer); - - AMQResponseBody( - ProtocolVersion v, ResponseId id=0, RequestId req=0, BatchOffset off=0) - : AMQMethodBody(v), data(id, req, off) {} - - uint8_t type() const { return RESPONSE_BODY; } - void encode(Buffer& buffer) const; - - Data& getData() { return data; } - ResponseId getResponseId() const { return data.responseId; } - RequestId getRequestId() const { return data.requestId; } - BatchOffset getBatchOffset() const { return data.batchOffset; } - void setResponseId(ResponseId id) { data.responseId = id; } - void setRequestId(RequestId id) { data.requestId = id; } - void setBatchOffset(BatchOffset id) { data.batchOffset = id; } - - bool isResponse() const { return true; } - protected: - static const uint32_t baseSize() { return AMQMethodBody::baseSize()+20; } - void printPrefix(std::ostream& out) const; - - private: - Data data; -}; - -}} // namespace qpid::framing - - - -#endif /*!_framing_AMQResponseBody_h*/ diff --git a/cpp/src/framing/BasicHeaderProperties.cpp b/cpp/src/framing/BasicHeaderProperties.cpp deleted file mode 100644 index dfa5e1bc3f..0000000000 --- a/cpp/src/framing/BasicHeaderProperties.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - * - * 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 "BasicHeaderProperties.h" - -//TODO: This could be easily generated from the spec - -qpid::framing::BasicHeaderProperties::BasicHeaderProperties() : deliveryMode(DeliveryMode(0)), priority(0), timestamp(0){} -qpid::framing::BasicHeaderProperties::~BasicHeaderProperties(){} - -uint32_t qpid::framing::BasicHeaderProperties::size() const{ - uint32_t bytes = 2;//flags - if(contentType.length() > 0) bytes += contentType.length() + 1; - if(contentEncoding.length() > 0) bytes += contentEncoding.length() + 1; - if(headers.count() > 0) bytes += headers.size(); - if(deliveryMode != 0) bytes += 1; - if(priority != 0) bytes += 1; - if(correlationId.length() > 0) bytes += correlationId.length() + 1; - if(replyTo.length() > 0) bytes += replyTo.length() + 1; - if(expiration.length() > 0) bytes += expiration.length() + 1; - if(messageId.length() > 0) bytes += messageId.length() + 1; - if(timestamp != 0) bytes += 8; - if(type.length() > 0) bytes += type.length() + 1; - if(userId.length() > 0) bytes += userId.length() + 1; - if(appId.length() > 0) bytes += appId.length() + 1; - if(clusterId.length() > 0) bytes += clusterId.length() + 1; - - return bytes; -} - -void qpid::framing::BasicHeaderProperties::encode(qpid::framing::Buffer& buffer) const{ - uint16_t flags = getFlags(); - buffer.putShort(flags); - - if(contentType.length() > 0) buffer.putShortString(contentType); - if(contentEncoding.length() > 0) buffer.putShortString(contentEncoding); - if(headers.count() > 0) buffer.putFieldTable(headers); - if(deliveryMode != 0) buffer.putOctet(deliveryMode); - if(priority != 0) buffer.putOctet(priority); - if(correlationId.length() > 0) buffer.putShortString(correlationId); - if(replyTo.length() > 0) buffer.putShortString(replyTo); - if(expiration.length() > 0) buffer.putShortString(expiration); - if(messageId.length() > 0) buffer.putShortString(messageId); - if(timestamp != 0) buffer.putLongLong(timestamp);; - if(type.length() > 0) buffer.putShortString(type); - if(userId.length() > 0) buffer.putShortString(userId); - if(appId.length() > 0) buffer.putShortString(appId); - if(clusterId.length() > 0) buffer.putShortString(clusterId); -} - -void qpid::framing::BasicHeaderProperties::decode(qpid::framing::Buffer& buffer, uint32_t /*size*/){ - uint16_t flags = buffer.getShort(); - if(flags & (1 << 15)) buffer.getShortString(contentType); - if(flags & (1 << 14)) buffer.getShortString(contentEncoding); - if(flags & (1 << 13)) buffer.getFieldTable(headers); - if(flags & (1 << 12)) deliveryMode = DeliveryMode(buffer.getOctet()); - if(flags & (1 << 11)) priority = buffer.getOctet(); - if(flags & (1 << 10)) buffer.getShortString(correlationId); - if(flags & (1 << 9)) buffer.getShortString(replyTo); - if(flags & (1 << 8)) buffer.getShortString(expiration); - if(flags & (1 << 7)) buffer.getShortString(messageId); - if(flags & (1 << 6)) timestamp = buffer.getLongLong(); - if(flags & (1 << 5)) buffer.getShortString(type); - if(flags & (1 << 4)) buffer.getShortString(userId); - if(flags & (1 << 3)) buffer.getShortString(appId); - if(flags & (1 << 2)) buffer.getShortString(clusterId); -} - -uint16_t qpid::framing::BasicHeaderProperties::getFlags() const{ - uint16_t flags(0); - if(contentType.length() > 0) flags |= (1 << 15); - if(contentEncoding.length() > 0) flags |= (1 << 14); - if(headers.count() > 0) flags |= (1 << 13); - if(deliveryMode != 0) flags |= (1 << 12); - if(priority != 0) flags |= (1 << 11); - if(correlationId.length() > 0) flags |= (1 << 10); - if(replyTo.length() > 0) flags |= (1 << 9); - if(expiration.length() > 0) flags |= (1 << 8); - if(messageId.length() > 0) flags |= (1 << 7); - if(timestamp != 0) flags |= (1 << 6); - if(type.length() > 0) flags |= (1 << 5); - if(userId.length() > 0) flags |= (1 << 4); - if(appId.length() > 0) flags |= (1 << 3); - if(clusterId.length() > 0) flags |= (1 << 2); - return flags; -} diff --git a/cpp/src/framing/BasicHeaderProperties.h b/cpp/src/framing/BasicHeaderProperties.h deleted file mode 100644 index a6347b37fd..0000000000 --- a/cpp/src/framing/BasicHeaderProperties.h +++ /dev/null @@ -1,115 +0,0 @@ -/* - * - * 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 "amqp_types.h" -#include "Buffer.h" -#include "FieldTable.h" -#include "HeaderProperties.h" - -#ifndef _BasicHeaderProperties_ -#define _BasicHeaderProperties_ - -namespace qpid { -namespace framing { - -enum DeliveryMode { TRANSIENT = 1, PERSISTENT = 2}; - -class BasicHeaderProperties : public HeaderProperties -{ - string contentType; - string contentEncoding; - FieldTable headers; - DeliveryMode deliveryMode; - uint8_t priority; - string correlationId; - string replyTo; - string expiration; - string messageId; - uint64_t timestamp; - string type; - string userId; - string appId; - string clusterId; - - uint16_t getFlags() const; - - public: - BasicHeaderProperties(); - virtual ~BasicHeaderProperties(); - virtual uint32_t size() const; - virtual void encode(Buffer& buffer) const; - virtual void decode(Buffer& buffer, uint32_t size); - - virtual uint8_t classId() { return BASIC; } - - string getContentType() const { return contentType; } - string getContentEncoding() const { return contentEncoding; } - FieldTable& getHeaders() { return headers; } - const FieldTable& getHeaders() const { return headers; } - DeliveryMode getDeliveryMode() const { return deliveryMode; } - uint8_t getPriority() const { return priority; } - string getCorrelationId() const {return correlationId; } - string getReplyTo() const { return replyTo; } - string getExpiration() const { return expiration; } - string getMessageId() const {return messageId; } - uint64_t getTimestamp() const { return timestamp; } - string getType() const { return type; } - string getUserId() const { return userId; } - string getAppId() const { return appId; } - string getClusterId() const { return clusterId; } - - void setContentType(const string& _type){ contentType = _type; } - void setContentEncoding(const string& encoding){ contentEncoding = encoding; } - void setHeaders(const FieldTable& _headers){ headers = _headers; } - void setDeliveryMode(DeliveryMode mode){ deliveryMode = mode; } - void setPriority(uint8_t _priority){ priority = _priority; } - void setCorrelationId(const string& _correlationId){ correlationId = _correlationId; } - void setReplyTo(const string& _replyTo){ replyTo = _replyTo;} - void setExpiration(const string& _expiration){ expiration = _expiration; } - void setMessageId(const string& _messageId){ messageId = _messageId; } - void setTimestamp(uint64_t _timestamp){ timestamp = _timestamp; } - void setType(const string& _type){ type = _type; } - void setUserId(const string& _userId){ userId = _userId; } - void setAppId(const string& _appId){appId = _appId; } - void setClusterId(const string& _clusterId){ clusterId = _clusterId; } - - /** \internal - * Template to copy between types like BasicHeaderProperties. - */ - template - static void copy(T& to, const U& from) { - to.setContentType(from.getContentType()); - to.setContentEncoding(from.getContentEncoding()); - to.setHeaders(from.getHeaders()); - to.setDeliveryMode(from.getDeliveryMode()); - to.setPriority(from.getPriority()); - to.setCorrelationId(from.getCorrelationId()); - to.setReplyTo(from.getReplyTo()); - to.setExpiration(from.getExpiration()); - to.setMessageId(from.getMessageId()); - to.setTimestamp(from.getTimestamp()); - to.setType(from.getType()); - to.setUserId(from.getUserId()); - to.setAppId(from.getAppId()); - to.setClusterId(from.getClusterId()); - } -}; -}} -#endif diff --git a/cpp/src/framing/BodyHandler.cpp b/cpp/src/framing/BodyHandler.cpp deleted file mode 100644 index 3b389fb5ec..0000000000 --- a/cpp/src/framing/BodyHandler.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * - * 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 "../QpidError.h" -#include "BodyHandler.h" -#include "AMQRequestBody.h" -#include "AMQResponseBody.h" -#include "AMQMethodBody.h" -#include "AMQHeaderBody.h" -#include "AMQContentBody.h" -#include "AMQHeartbeatBody.h" - -using namespace qpid::framing; -using namespace boost; - -BodyHandler::~BodyHandler() {} - -void BodyHandler::handleBody(shared_ptr body) { - switch(body->type()) - { - case REQUEST_BODY: - handleRequest(shared_polymorphic_cast(body)); - break; - case RESPONSE_BODY: - handleResponse(shared_polymorphic_cast(body)); - break; - case METHOD_BODY: - handleMethod(shared_polymorphic_cast(body)); - break; - case HEADER_BODY: - handleHeader(shared_polymorphic_cast(body)); - break; - case CONTENT_BODY: - handleContent(shared_polymorphic_cast(body)); - break; - case HEARTBEAT_BODY: - handleHeartbeat(shared_polymorphic_cast(body)); - break; - default: - QPID_ERROR(PROTOCOL_ERROR, "Unknown frame type "+body->type()); - } -} - diff --git a/cpp/src/framing/BodyHandler.h b/cpp/src/framing/BodyHandler.h deleted file mode 100644 index cb3f0997b0..0000000000 --- a/cpp/src/framing/BodyHandler.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef _BodyHandler_ -#define _BodyHandler_ - -/* - * - * 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 - -#include "Requester.h" -#include "Responder.h" - -namespace qpid { -namespace framing { - -class AMQRequestBody; -class AMQResponseBody; -class AMQMethodBody; -class AMQHeaderBody; -class AMQContentBody; -class AMQHeartbeatBody; - -/** - * Interface to handle incoming frame bodies. - * Derived classes provide logic for each frame type. - */ -class BodyHandler { - public: - virtual ~BodyHandler(); - virtual void handleBody(boost::shared_ptr body); - - protected: - virtual void handleRequest(boost::shared_ptr) = 0; - virtual void handleResponse(boost::shared_ptr) = 0; - virtual void handleMethod(boost::shared_ptr) = 0; - virtual void handleHeader(boost::shared_ptr) = 0; - virtual void handleContent(boost::shared_ptr) = 0; - virtual void handleHeartbeat(boost::shared_ptr) = 0; -}; - -}} - - -#endif diff --git a/cpp/src/framing/Buffer.cpp b/cpp/src/framing/Buffer.cpp deleted file mode 100644 index 2a14e854d0..0000000000 --- a/cpp/src/framing/Buffer.cpp +++ /dev/null @@ -1,183 +0,0 @@ -/* - * - * 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 "Buffer.h" -#include "FramingContent.h" -#include "FieldTable.h" - -qpid::framing::Buffer::Buffer(uint32_t _size) : size(_size), owner(true), position(0), limit(_size){ - data = new char[size]; -} - -qpid::framing::Buffer::Buffer(char* _data, uint32_t _size) : size(_size), owner(false), data(_data), position(0), limit(_size){ -} - -qpid::framing::Buffer::~Buffer(){ - if(owner) delete[] data; -} - -void qpid::framing::Buffer::flip(){ - limit = position; - position = 0; -} - -void qpid::framing::Buffer::clear(){ - limit = size; - position = 0; -} - -void qpid::framing::Buffer::compact(){ - uint32_t p = limit - position; - //copy p chars from position to 0 - memmove(data, data + position, p); - limit = size; - position = p; -} - -void qpid::framing::Buffer::record(){ - r_position = position; - r_limit = limit; -} - -void qpid::framing::Buffer::restore(){ - position = r_position; - limit = r_limit; -} - -uint32_t qpid::framing::Buffer::available(){ - return limit - position; -} - -char* qpid::framing::Buffer::start(){ - return data + position; -} - -void qpid::framing::Buffer::move(uint32_t bytes){ - position += bytes; -} - -void qpid::framing::Buffer::putOctet(uint8_t i){ - data[position++] = i; -} - -void qpid::framing::Buffer::putShort(uint16_t i){ - uint16_t b = i; - data[position++] = (uint8_t) (0xFF & (b >> 8)); - data[position++] = (uint8_t) (0xFF & b); -} - -void qpid::framing::Buffer::putLong(uint32_t i){ - uint32_t b = i; - data[position++] = (uint8_t) (0xFF & (b >> 24)); - data[position++] = (uint8_t) (0xFF & (b >> 16)); - data[position++] = (uint8_t) (0xFF & (b >> 8)); - data[position++] = (uint8_t) (0xFF & b); -} - -void qpid::framing::Buffer::putLongLong(uint64_t i){ - uint32_t hi = i >> 32; - uint32_t lo = i; - putLong(hi); - putLong(lo); -} - -uint8_t qpid::framing::Buffer::getOctet(){ - return (uint8_t) data[position++]; -} - -uint16_t qpid::framing::Buffer::getShort(){ - uint16_t hi = (unsigned char) data[position++]; - hi = hi << 8; - hi |= (unsigned char) data[position++]; - return hi; -} - -uint32_t qpid::framing::Buffer::getLong(){ - uint32_t a = (unsigned char) data[position++]; - uint32_t b = (unsigned char) data[position++]; - uint32_t c = (unsigned char) data[position++]; - uint32_t d = (unsigned char) data[position++]; - a = a << 24; - a |= b << 16; - a |= c << 8; - a |= d; - return a; -} - -uint64_t qpid::framing::Buffer::getLongLong(){ - uint64_t hi = getLong(); - uint64_t lo = getLong(); - hi = hi << 32; - return hi | lo; -} - - -void qpid::framing::Buffer::putShortString(const string& s){ - uint8_t len = s.length(); - putOctet(len); - s.copy(data + position, len); - position += len; -} - -void qpid::framing::Buffer::putLongString(const string& s){ - uint32_t len = s.length(); - putLong(len); - s.copy(data + position, len); - position += len; -} - -void qpid::framing::Buffer::getShortString(string& s){ - uint8_t len = getOctet(); - s.assign(data + position, len); - position += len; -} - -void qpid::framing::Buffer::getLongString(string& s){ - uint32_t len = getLong(); - s.assign(data + position, len); - position += len; -} - -void qpid::framing::Buffer::putFieldTable(const FieldTable& t){ - t.encode(*this); -} - -void qpid::framing::Buffer::getFieldTable(FieldTable& t){ - t.decode(*this); -} - -void qpid::framing::Buffer::putContent(const Content& c){ - c.encode(*this); -} - -void qpid::framing::Buffer::getContent(Content& c){ - c.decode(*this); -} - -void qpid::framing::Buffer::putRawData(const string& s){ - uint32_t len = s.length(); - s.copy(data + position, len); - position += len; -} - -void qpid::framing::Buffer::getRawData(string& s, uint32_t len){ - s.assign(data + position, len); - position += len; -} diff --git a/cpp/src/framing/Buffer.h b/cpp/src/framing/Buffer.h deleted file mode 100644 index e1a3fb065a..0000000000 --- a/cpp/src/framing/Buffer.h +++ /dev/null @@ -1,86 +0,0 @@ -/* - * - * 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 "amqp_types.h" - -#ifndef _Buffer_ -#define _Buffer_ - -namespace qpid { -namespace framing { - -class Content; -class FieldTable; - -class Buffer -{ - const uint32_t size; - const bool owner;//indicates whether the data is owned by this instance - char* data; - uint32_t position; - uint32_t limit; - uint32_t r_position; - uint32_t r_limit; - -public: - - Buffer(uint32_t size); - Buffer(char* data, uint32_t size); - ~Buffer(); - - void flip(); - void clear(); - void compact(); - void record(); - void restore(); - uint32_t available(); - char* start(); - void move(uint32_t bytes); - - void putOctet(uint8_t i); - void putShort(uint16_t i); - void putLong(uint32_t i); - void putLongLong(uint64_t i); - - uint8_t getOctet(); - uint16_t getShort(); - uint32_t getLong(); - uint64_t getLongLong(); - - void putShortString(const string& s); - void putLongString(const string& s); - void getShortString(string& s); - void getLongString(string& s); - - void putFieldTable(const FieldTable& t); - void getFieldTable(FieldTable& t); - - void putContent(const Content& c); - void getContent(Content& c); - - void putRawData(const string& s); - void getRawData(string& s, uint32_t size); - -}; - -}} // namespace qpid::framing - - -#endif diff --git a/cpp/src/framing/ChannelAdapter.cpp b/cpp/src/framing/ChannelAdapter.cpp deleted file mode 100644 index d16934a857..0000000000 --- a/cpp/src/framing/ChannelAdapter.cpp +++ /dev/null @@ -1,109 +0,0 @@ -/* - * - * 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 - -#include "ChannelAdapter.h" -#include "AMQFrame.h" -#include "../Exception.h" - -using boost::format; - -namespace qpid { -namespace framing { - -void ChannelAdapter::init( - ChannelId i, OutputHandler& o, ProtocolVersion v) -{ - assertChannelNotOpen(); - id = i; - out = &o; - version = v; -} - -RequestId ChannelAdapter::send( - shared_ptr body, Correlator::Action action) -{ - RequestId requestId = 0; - assertChannelOpen(); - switch (body->type()) { - case REQUEST_BODY: { - AMQRequestBody::shared_ptr request = - boost::shared_polymorphic_downcast(body); - requester.sending(request->getData()); - requestId = request->getData().requestId; - if (!action.empty()) - correlator.request(requestId, action); - break; - } - case RESPONSE_BODY: { - AMQResponseBody::shared_ptr response = - boost::shared_polymorphic_downcast(body); - responder.sending(response->getData()); - break; - } - // No action required for other body types. - } - out->send(new AMQFrame(getVersion(), getId(), body)); - return requestId; -} - -void ChannelAdapter::handleRequest(AMQRequestBody::shared_ptr request) { - assertMethodOk(*request); - AMQRequestBody::Data& requestData = request->getData(); - responder.received(requestData); - handleMethodInContext(request, MethodContext(this, request)); -} - -void ChannelAdapter::handleResponse(AMQResponseBody::shared_ptr response) { - assertMethodOk(*response); - AMQResponseBody::Data& responseData = response->getData(); - - // FIXME aconway 2007-04-05: processed should be last - // but causes problems with InProcessBroker tests because - // we execute client code in handleMethod. - // Need to introduce a queue & 2 threads for inprocess. - requester.processed(responseData); - // FIXME aconway 2007-04-04: exception handling. - correlator.response(response); - handleMethod(response); -} - -void ChannelAdapter::handleMethod(AMQMethodBody::shared_ptr method) { - assertMethodOk(*method); - handleMethodInContext(method, MethodContext(this, method)); -} - -void ChannelAdapter::assertMethodOk(AMQMethodBody& method) const { - if (getId() != 0 && method.amqpClassId() == ConnectionOpenBody::CLASS_ID) - throw ConnectionException( - 504, format("Connection method on non-0 channel %d.")%getId()); -} - -void ChannelAdapter::assertChannelOpen() const { - if (getId() != 0 && !isOpen()) - throw ConnectionException( - 504, format("Channel %d is not open.")%getId()); -} - -void ChannelAdapter::assertChannelNotOpen() const { - if (getId() != 0 && isOpen()) - throw ConnectionException( - 504, format("Channel %d is already open.") % getId()); -} - -}} // namespace qpid::framing diff --git a/cpp/src/framing/ChannelAdapter.h b/cpp/src/framing/ChannelAdapter.h deleted file mode 100644 index 1b325495ff..0000000000 --- a/cpp/src/framing/ChannelAdapter.h +++ /dev/null @@ -1,113 +0,0 @@ -#ifndef _ChannelAdapter_ -#define _ChannelAdapter_ - -/* - * - * 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 "../shared_ptr.h" -#include "BodyHandler.h" -#include "Requester.h" -#include "Responder.h" -#include "Correlator.h" -#include "amqp_types.h" - -namespace qpid { -namespace framing { - -class MethodContext; - -// FIXME aconway 2007-02-20: Rename as ChannelBase or just Channel. - -/** - * Base class for client and broker channels. - * - * - receives frame bodies from the network. - * - Updates request/response data. - * - Dispatches requests with a MethodContext for responses. - * - * send() - * - Updates request/resposne ID data. - * - Forwards frame to the peer. - * - * Thread safety: OBJECT UNSAFE. Instances must not be called - * concurrently. AMQP defines channels to be serialized. - */ -class ChannelAdapter : public BodyHandler { - public: - /** - *@param output Processed frames are forwarded to this handler. - */ - ChannelAdapter(ChannelId id_=0, OutputHandler* out_=0, - ProtocolVersion ver=ProtocolVersion()) - : id(id_), out(out_), version(ver) {} - - /** Initialize the channel adapter. */ - void init(ChannelId, OutputHandler&, ProtocolVersion); - - ChannelId getId() const { return id; } - ProtocolVersion getVersion() const { return version; } - - /** - * Send a frame. - *@param body Body of the frame. - *@param action optional action to execute when we receive a - *response to this frame. Ignored if body is not a Request. - *@return If body is a request, the ID assigned else 0. - */ - RequestId send(shared_ptr body, - Correlator::Action action=Correlator::Action()); - - // TODO aconway 2007-04-05: remove and use make_shared_ptr at call sites. - /**@deprecated Use make_shared_ptr with the other send() override */ - RequestId send(AMQBody* body) { return send(AMQBody::shared_ptr(body)); } - - void handleMethod(shared_ptr); - void handleRequest(shared_ptr); - void handleResponse(shared_ptr); - - virtual bool isOpen() const = 0; - - protected: - void assertMethodOk(AMQMethodBody& method) const; - void assertChannelOpen() const; - void assertChannelNotOpen() const; - - virtual void handleMethodInContext( - shared_ptr method, - const MethodContext& context) = 0; - - RequestId getFirstAckRequest() { return requester.getFirstAckRequest(); } - RequestId getLastAckRequest() { return requester.getLastAckRequest(); } - RequestId getNextSendRequestId() { return requester.getNextId(); } - - private: - ChannelId id; - OutputHandler* out; - ProtocolVersion version; - Requester requester; - Responder responder; - Correlator correlator; -}; - -}} - - -#endif diff --git a/cpp/src/framing/Correlator.cpp b/cpp/src/framing/Correlator.cpp deleted file mode 100644 index 1c18f6b414..0000000000 --- a/cpp/src/framing/Correlator.cpp +++ /dev/null @@ -1,42 +0,0 @@ -/* - * - * 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 "Correlator.h" - -namespace qpid { -namespace framing { - -void Correlator::request(RequestId id, Action action) { - actions[id] = action; -} - -bool Correlator::response(shared_ptr r) { - Actions::iterator begin = actions.lower_bound(r->getRequestId()); - Actions::iterator end = - actions.upper_bound(r->getRequestId()+r->getBatchOffset()); - bool didAction = false; - for(Actions::iterator i=begin; i != end; ++i) { - // FIXME aconway 2007-04-04: Exception handling. - didAction = true; - i->second(r); - actions.erase(i); - } - return didAction; -} - -}} // namespace qpid::framing diff --git a/cpp/src/framing/Correlator.h b/cpp/src/framing/Correlator.h deleted file mode 100644 index b3eb998149..0000000000 --- a/cpp/src/framing/Correlator.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef _framing_Correlator_h -#define _framing_Correlator_h - -/* - * - * 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 "../shared_ptr.h" -#include "../framing/AMQResponseBody.h" -#include -#include - -namespace qpid { -namespace framing { - -/** - * Correlate responses with actions established when sending the request. - * - * THREAD UNSAFE. - */ -class Correlator -{ - public: - typedef shared_ptr ResponsePtr; - typedef boost::function Action; - - /** - * Note that request with id was sent, record an action to call - * when a response arrives. - */ - void request(RequestId id, Action doOnResponse); - - /** - * Note response received, call action for associated request if any. - * Return true of some action(s) were executed. - */ - bool response(shared_ptr); - - /** - * Note the given execution mark was received, call actions - * for any requests that are impicitly responded to. - */ - void mark(RequestId mark); - - private: - typedef std::map Actions; - Actions actions; -}; - -}} // namespace qpid::framing - - - -#endif /*!_framing_Correlator_h*/ diff --git a/cpp/src/framing/FieldTable.cpp b/cpp/src/framing/FieldTable.cpp deleted file mode 100644 index c4881a7c94..0000000000 --- a/cpp/src/framing/FieldTable.cpp +++ /dev/null @@ -1,150 +0,0 @@ -/* - * - * 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 "FieldTable.h" -#include "../QpidError.h" -#include "Buffer.h" -#include "Value.h" -#include - -namespace qpid { -namespace framing { - -FieldTable::~FieldTable() {} - -uint32_t FieldTable::size() const { - uint32_t len(4); - for(ValueMap::const_iterator i = values.begin(); i != values.end(); ++i) { - // 2 = shortstr_len_byyte + type_char_byte - len += 2 + (i->first).size() + (i->second)->size(); - } - return len; -} - -int FieldTable::count() const { - return values.size(); -} - -namespace -{ -std::ostream& operator<<(std::ostream& out, const FieldTable::ValueMap::value_type& i) { - return out << i.first << ":" << *i.second; -} -} - -std::ostream& operator<<(std::ostream& out, const FieldTable& t) { - out << "{"; - FieldTable::ValueMap::const_iterator i = t.getMap().begin(); - if (i != t.getMap().end()) out << *i++; - while (i != t.getMap().end()) - { - out << "," << *i++; - } - return out << "}"; -} - -void FieldTable::setString(const std::string& name, const std::string& value){ - values[name] = ValuePtr(new StringValue(value)); -} - -void FieldTable::setInt(const std::string& name, int value){ - values[name] = ValuePtr(new IntegerValue(value)); -} - -void FieldTable::setTimestamp(const std::string& name, uint64_t value){ - values[name] = ValuePtr(new TimeValue(value)); -} - -void FieldTable::setTable(const std::string& name, const FieldTable& value){ - values[name] = ValuePtr(new FieldTableValue(value)); -} - -namespace { -template T default_value() { return T(); } -template <> int default_value() { return 0; } -template <> uint64_t default_value() { return 0; } -} - -template -T FieldTable::getValue(const std::string& name) const -{ - ValueMap::const_iterator i = values.find(name); - if (i == values.end()) return default_value(); - const ValueOps *vt = dynamic_cast*>(i->second.get()); - return vt->getValue(); -} - -std::string FieldTable::getString(const std::string& name) const { - return getValue(name); -} - -int FieldTable::getInt(const std::string& name) const { - return getValue(name); -} - -uint64_t FieldTable::getTimestamp(const std::string& name) const { - return getValue(name); -} - -void FieldTable::getTable(const std::string& name, FieldTable& value) const { - value = getValue(name); -} - -void FieldTable::encode(Buffer& buffer) const{ - buffer.putLong(size() - 4); - for (ValueMap::const_iterator i = values.begin(); i!=values.end(); ++i) { - buffer.putShortString(i->first); - buffer.putOctet(i->second->getType()); - i->second->encode(buffer); - } -} - -void FieldTable::decode(Buffer& buffer){ - uint32_t len = buffer.getLong(); - uint32_t available = buffer.available(); - if (available < len) - THROW_QPID_ERROR(FRAMING_ERROR, "Not enough data for field table."); - uint32_t leftover = available - len; - while(buffer.available() > leftover){ - std::string name; - buffer.getShortString(name); - std::auto_ptr value(Value::decode_value(buffer)); - values[name] = ValuePtr(value.release()); - } -} - - -bool FieldTable::operator==(const FieldTable& x) const { - if (values.size() != x.values.size()) return false; - for (ValueMap::const_iterator i = values.begin(); i != values.end(); ++i) { - ValueMap::const_iterator j = x.values.find(i->first); - if (j == x.values.end()) return false; - if (*(i->second) != *(j->second)) return false; - } - return true; -} - -void FieldTable::erase(const std::string& name) -{ - values.erase(values.find(name)); -} - -} -} diff --git a/cpp/src/framing/FieldTable.h b/cpp/src/framing/FieldTable.h deleted file mode 100644 index 2be3e6d3ff..0000000000 --- a/cpp/src/framing/FieldTable.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * - * 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 -#include -#include -#include -#include "amqp_types.h" - -#ifndef _FieldTable_ -#define _FieldTable_ - -namespace qpid { - /** - * The framing namespace contains classes that are used to create, - * send and receive the basic packets from which AMQP is built. - */ -namespace framing { - -class Value; -class Buffer; - -/** - * A set of name-value pairs. (See the AMQP spec for more details on - * AMQP field tables). - * - * \ingroup clientapi - */ -class FieldTable -{ - public: - typedef boost::shared_ptr ValuePtr; - typedef std::map ValueMap; - - ~FieldTable(); - uint32_t size() const; - int count() const; - void setString(const std::string& name, const std::string& value); - void setInt(const std::string& name, int value); - void setTimestamp(const std::string& name, uint64_t value); - void setTable(const std::string& name, const FieldTable& value); - //void setDecimal(string& name, xxx& value); - std::string getString(const std::string& name) const; - int getInt(const std::string& name) const; - uint64_t getTimestamp(const std::string& name) const; - void getTable(const std::string& name, FieldTable& value) const; - //void getDecimal(string& name, xxx& value); - void erase(const std::string& name); - - void encode(Buffer& buffer) const; - void decode(Buffer& buffer); - - bool operator==(const FieldTable& other) const; - - // TODO aconway 2006-09-26: Yeuch! Rework FieldTable to have - // a map-like interface. - const ValueMap& getMap() const { return values; } - ValueMap& getMap() { return values; } - - private: - friend std::ostream& operator<<(std::ostream& out, const FieldTable& body); - ValueMap values; - template T getValue(const std::string& name) const; -}; - -class FieldNotFoundException{}; -class UnknownFieldName : public FieldNotFoundException{}; -class IncorrectFieldType : public FieldNotFoundException{}; -} -} - - -#endif diff --git a/cpp/src/framing/FramingContent.cpp b/cpp/src/framing/FramingContent.cpp deleted file mode 100644 index 1f8f77d129..0000000000 --- a/cpp/src/framing/FramingContent.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - * - * 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 - -#include "Buffer.h" -#include "FramingContent.h" -#include "../QpidError.h" -#include - -namespace qpid { -namespace framing { - -Content::Content() : discriminator(0) {} - -Content::Content(uint8_t _discriminator, const string& _value): discriminator(_discriminator), value(_value) { - validate(); -} - -void Content::validate() { - if (discriminator == REFERENCE) { - if(value.empty()) { - THROW_QPID_ERROR(FRAMING_ERROR, "Reference cannot be empty"); - } - }else if (discriminator != INLINE) { - std::stringstream out; - out << "Invalid discriminator: " << (int) discriminator; - THROW_QPID_ERROR(FRAMING_ERROR, out.str()); - } -} - -Content::~Content() {} - -void Content::encode(Buffer& buffer) const { - buffer.putOctet(discriminator); - buffer.putLongString(value); -} - -void Content::decode(Buffer& buffer) { - discriminator = buffer.getOctet(); - buffer.getLongString(value); - validate(); -} - -size_t Content::size() const { - return 1/*discriminator*/ + 4/*for recording size of long string*/ + value.size(); -} - -std::ostream& operator<<(std::ostream& out, const Content& content) { - if (content.discriminator == REFERENCE) { - out << "{REF:" << content.value << "}"; - } else if (content.discriminator == INLINE) { - out << "{INLINE:" << content.value.size() << " bytes}"; - } - return out; -} - -}} // namespace framing::qpid diff --git a/cpp/src/framing/FramingContent.h b/cpp/src/framing/FramingContent.h deleted file mode 100644 index 876e90c905..0000000000 --- a/cpp/src/framing/FramingContent.h +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef _framing_FramingContent_h -#define _framing_FramingContent_h - -#include - -namespace qpid { -namespace framing { - -enum discriminator_types { INLINE = 0, REFERENCE = 1 }; - -/** - * A representation of the AMQP 'content' data type (used for message - * bodies) which can hold inline data or a reference. - */ -class Content -{ - uint8_t discriminator; - string value; - - void validate(); - - public: - Content(); - Content(uint8_t _discriminator, const string& _value); - ~Content(); - - void encode(Buffer& buffer) const; - void decode(Buffer& buffer); - size_t size() const; - bool isInline() const { return discriminator == INLINE; } - bool isReference() const { return discriminator == REFERENCE; } - const string& getValue() const { return value; } - void setValue(const string& newValue) { value = newValue; } - - friend std::ostream& operator<<(std::ostream&, const Content&); -}; - -}} // namespace qpid::framing - - -#endif /*!_framing_FramingContent_h*/ diff --git a/cpp/src/framing/HeaderProperties.h b/cpp/src/framing/HeaderProperties.h deleted file mode 100644 index ae8b796aa9..0000000000 --- a/cpp/src/framing/HeaderProperties.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * - * 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 "amqp_types.h" -#include "Buffer.h" - -#ifndef _HeaderProperties_ -#define _HeaderProperties_ - -namespace qpid { -namespace framing { - - enum header_classes{BASIC = 60}; - - class HeaderProperties - { - - public: - inline virtual ~HeaderProperties(){} - virtual uint8_t classId() = 0; - virtual uint32_t size() const = 0; - virtual void encode(Buffer& buffer) const = 0; - virtual void decode(Buffer& buffer, uint32_t size) = 0; - }; -} -} - - -#endif diff --git a/cpp/src/framing/InitiationHandler.cpp b/cpp/src/framing/InitiationHandler.cpp deleted file mode 100644 index eceeaf4abc..0000000000 --- a/cpp/src/framing/InitiationHandler.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/* - * - * 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 "InitiationHandler.h" - -qpid::framing::InitiationHandler::~InitiationHandler() {} diff --git a/cpp/src/framing/InitiationHandler.h b/cpp/src/framing/InitiationHandler.h deleted file mode 100644 index 16a6b502e8..0000000000 --- a/cpp/src/framing/InitiationHandler.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * - * 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 - -#ifndef _InitiationHandler_ -#define _InitiationHandler_ - -#include "ProtocolInitiation.h" - -namespace qpid { -namespace framing { - - class InitiationHandler{ - public: - virtual ~InitiationHandler(); - virtual void initiated(const ProtocolInitiation&) = 0; - }; - -} -} - - -#endif diff --git a/cpp/src/framing/InputHandler.h b/cpp/src/framing/InputHandler.h deleted file mode 100644 index dc6814b849..0000000000 --- a/cpp/src/framing/InputHandler.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef _InputHandler_ -#define _InputHandler_ -/* - * - * 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 "AMQFrame.h" -#include - -namespace qpid { -namespace framing { - -class InputHandler : private boost::noncopyable { - public: - virtual ~InputHandler() {} - virtual void received(AMQFrame* frame) = 0; -}; - -}} - - -#endif diff --git a/cpp/src/framing/MethodContext.cpp b/cpp/src/framing/MethodContext.cpp deleted file mode 100644 index 73af73f8e5..0000000000 --- a/cpp/src/framing/MethodContext.cpp +++ /dev/null @@ -1,31 +0,0 @@ -/* - * - * 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 "MethodContext.h" -#include "amqp_types.h" -#include "AMQRequestBody.h" - -namespace qpid { -namespace framing { - -RequestId MethodContext::getRequestId() const { - return boost::shared_polymorphic_downcast(methodBody) - ->getRequestId(); -} - -}} // namespace qpid::framing diff --git a/cpp/src/framing/MethodContext.h b/cpp/src/framing/MethodContext.h deleted file mode 100644 index 80e4c55d7e..0000000000 --- a/cpp/src/framing/MethodContext.h +++ /dev/null @@ -1,75 +0,0 @@ -#ifndef _framing_MethodContext_h -#define _framing_MethodContext_h - -/* - * - * 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 - -#include "OutputHandler.h" -#include "ProtocolVersion.h" - -namespace qpid { -namespace framing { - -class BodyHandler; -class AMQMethodBody; -class ChannelAdapter; - -/** - * Invocation context for an AMQP method. - * - * It provides the method being processed and the channel on which - * it arrived. - * - * All Handler functions take a MethodContext as the last parameter. - */ -struct MethodContext -{ - typedef boost::shared_ptr BodyPtr; - - MethodContext(ChannelAdapter* ch=0, BodyPtr method=BodyPtr()) - : channel(ch), methodBody(method) {} - - /** - * Channel on which the method being processed arrived. - * 0 if the method was constructed by the caller - * rather than received from a channel. - */ - ChannelAdapter* channel; - - /** - * Body of the method being processed. - * It's useful for passing around instead of unpacking all its parameters. - * It's also provides the request ID when constructing a response. - */ - BodyPtr methodBody; - - /** - * Return methodBody's request ID. - * It is an error to call this if methodBody is not a request. - */ - RequestId getRequestId() const; -}; - - -}} // namespace qpid::framing - - - -#endif /*!_framing_MethodContext_h*/ diff --git a/cpp/src/framing/OutputHandler.h b/cpp/src/framing/OutputHandler.h deleted file mode 100644 index 9ffd4227d8..0000000000 --- a/cpp/src/framing/OutputHandler.h +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef _OutputHandler_ -#define _OutputHandler_ - -/* - * - * 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 - -namespace qpid { -namespace framing { -class AMQFrame; - -class OutputHandler : private boost::noncopyable { - public: - virtual ~OutputHandler() {} - virtual void send(AMQFrame* frame) = 0; -}; - -}} - - -#endif diff --git a/cpp/src/framing/ProtocolInitiation.cpp b/cpp/src/framing/ProtocolInitiation.cpp deleted file mode 100644 index a6d1b17f6e..0000000000 --- a/cpp/src/framing/ProtocolInitiation.cpp +++ /dev/null @@ -1,63 +0,0 @@ -/* - * - * 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 "ProtocolInitiation.h" - -namespace qpid { -namespace framing { - -ProtocolInitiation::ProtocolInitiation(){} - -ProtocolInitiation::ProtocolInitiation(uint8_t _major, uint8_t _minor) : version(_major, _minor) {} - -ProtocolInitiation::ProtocolInitiation(ProtocolVersion p) : version(p) {} - -ProtocolInitiation::~ProtocolInitiation(){} - -void ProtocolInitiation::encode(Buffer& buffer){ - buffer.putOctet('A'); - buffer.putOctet('M'); - buffer.putOctet('Q'); - buffer.putOctet('P'); - buffer.putOctet(1);//class - buffer.putOctet(1);//instance - buffer.putOctet(version.getMajor()); - buffer.putOctet(version.getMinor()); -} - -bool ProtocolInitiation::decode(Buffer& buffer){ - if(buffer.available() >= 8){ - buffer.getOctet();//A - buffer.getOctet();//M - buffer.getOctet();//Q - buffer.getOctet();//P - buffer.getOctet();//class - buffer.getOctet();//instance - version.setMajor(buffer.getOctet()); - version.setMinor(buffer.getOctet()); - return true; - }else{ - return false; - } -} - -//TODO: this should prbably be generated from the spec at some point to keep the version numbers up to date - -}} // namespace qpid::framing diff --git a/cpp/src/framing/ProtocolInitiation.h b/cpp/src/framing/ProtocolInitiation.h deleted file mode 100644 index adfdc8215d..0000000000 --- a/cpp/src/framing/ProtocolInitiation.h +++ /dev/null @@ -1,54 +0,0 @@ -/* - * - * 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 "amqp_types.h" -#include "Buffer.h" -#include "AMQDataBlock.h" -#include "ProtocolVersion.h" - -#ifndef _ProtocolInitiation_ -#define _ProtocolInitiation_ - -namespace qpid { -namespace framing { - -class ProtocolInitiation : public AMQDataBlock -{ -private: - ProtocolVersion version; - -public: - ProtocolInitiation(); - ProtocolInitiation(uint8_t major, uint8_t minor); - ProtocolInitiation(ProtocolVersion p); - virtual ~ProtocolInitiation(); - virtual void encode(Buffer& buffer); - virtual bool decode(Buffer& buffer); - inline virtual uint32_t size() const { return 8; } - inline uint8_t getMajor() const { return version.getMajor(); } - inline uint8_t getMinor() const { return version.getMinor(); } - inline ProtocolVersion getVersion() const { return version; } -}; - -} -} - - -#endif diff --git a/cpp/src/framing/ProtocolVersion.cpp b/cpp/src/framing/ProtocolVersion.cpp deleted file mode 100644 index 7a96bfa925..0000000000 --- a/cpp/src/framing/ProtocolVersion.cpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * - * 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 "ProtocolVersion.h" -#include - -using namespace qpid::framing; - -const std::string ProtocolVersion::toString() const -{ - std::stringstream ss; - ss << major_ << "-" << minor_; - return ss.str(); -} - -ProtocolVersion& ProtocolVersion::operator=(ProtocolVersion p) -{ - major_ = p.major_; - minor_ = p.minor_; - return *this; -} - -bool ProtocolVersion::operator==(ProtocolVersion p) const -{ - return major_ == p.major_ && minor_ == p.minor_; -} - diff --git a/cpp/src/framing/ProtocolVersion.h b/cpp/src/framing/ProtocolVersion.h deleted file mode 100644 index a2a755397b..0000000000 --- a/cpp/src/framing/ProtocolVersion.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * - * 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 _ProtocolVersion_ -#define _ProtocolVersion_ - -#include "amqp_types.h" - -namespace qpid -{ -namespace framing -{ - -class ProtocolVersion -{ -private: - uint8_t major_; - uint8_t minor_; - -public: - ProtocolVersion(uint8_t _major=0, uint8_t _minor=0) - : major_(_major), minor_(_minor) {} - - uint8_t getMajor() const { return major_; } - void setMajor(uint8_t major) { major_ = major; } - uint8_t getMinor() const { return minor_; } - void setMinor(uint8_t minor) { minor_ = minor; } - const std::string toString() const; - - ProtocolVersion& operator=(ProtocolVersion p); - - bool operator==(ProtocolVersion p) const; - bool operator!=(ProtocolVersion p) const { return ! (*this == p); } -}; - -} // namespace framing -} // namespace qpid - - -#endif // ifndef _ProtocolVersion_ diff --git a/cpp/src/framing/ProtocolVersionException.cpp b/cpp/src/framing/ProtocolVersionException.cpp deleted file mode 100644 index b68b3af1f9..0000000000 --- a/cpp/src/framing/ProtocolVersionException.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * - * 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 -#include "ProtocolVersionException.h" - - -using namespace qpid::framing; - -void ProtocolVersionException::init(const std::string& msg) -{ - whatStr = boost::str( - boost::format("ProtocolVersionException: %s found: %s") - % versionFound.toString() % msg); -} - diff --git a/cpp/src/framing/ProtocolVersionException.h b/cpp/src/framing/ProtocolVersionException.h deleted file mode 100644 index 0186ed7441..0000000000 --- a/cpp/src/framing/ProtocolVersionException.h +++ /dev/null @@ -1,56 +0,0 @@ -/* - * - * 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 _ProtocolVersionException_ -#define _ProtocolVersionException_ - -#include "../Exception.h" -#include "ProtocolVersion.h" -#include -#include - -namespace qpid { -namespace framing { - -class ProtocolVersionException : public qpid::Exception -{ -protected: - ProtocolVersion versionFound; - -public: - ~ProtocolVersionException() throw() {} - - template - ProtocolVersionException( - ProtocolVersion ver, const T& msg) throw () : versionFound(ver) - { init(boost::lexical_cast(msg)); } - - template - ProtocolVersionException(const T& msg) throw () - { init(boost::lexical_cast(msg)); } - - private: - void init(const std::string& msg); -}; - -}} // namespace qpid::framing - -#endif //ifndef _ProtocolVersionException_ diff --git a/cpp/src/framing/Proxy.cpp b/cpp/src/framing/Proxy.cpp deleted file mode 100644 index 0b2a882a49..0000000000 --- a/cpp/src/framing/Proxy.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * - * 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 "Proxy.h" -#include "ChannelAdapter.h" -#include "ProtocolVersion.h" - -namespace qpid { -namespace framing { - -Proxy::~Proxy() {} - -ProtocolVersion Proxy::getProtocolVersion() const { - return channel.getVersion(); -} - -}} // namespace qpid::framing diff --git a/cpp/src/framing/Proxy.h b/cpp/src/framing/Proxy.h deleted file mode 100644 index 8ed46ed748..0000000000 --- a/cpp/src/framing/Proxy.h +++ /dev/null @@ -1,51 +0,0 @@ -#ifndef _framing_Proxy_h -#define _framing_Proxy_h - -/* - * - * 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 "ProtocolVersion.h" - -namespace qpid { -namespace framing { - -class ChannelAdapter; -class FieldTable; -class Content; - -/** - * Base class for proxies. - */ -class Proxy -{ - - public: - Proxy(ChannelAdapter& ch) : channel(ch) {} - virtual ~Proxy(); - - ProtocolVersion getProtocolVersion() const; - - protected: - ChannelAdapter& channel; -}; - -}} // namespace qpid::framing - - - -#endif /*!_framing_Proxy_h*/ diff --git a/cpp/src/framing/Requester.cpp b/cpp/src/framing/Requester.cpp deleted file mode 100644 index 42cf4c392f..0000000000 --- a/cpp/src/framing/Requester.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* - * - * 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 - -#include "Requester.h" -#include "../QpidError.h" - -namespace qpid { -namespace framing { - -Requester::Requester() : lastId(0), responseMark(0) {} - -void Requester::sending(AMQRequestBody::Data& request) { - request.requestId = ++lastId; - request.responseMark = responseMark; -} - -void Requester::processed(const AMQResponseBody::Data& response) { - responseMark = response.responseId; - firstAckRequest = response.requestId; - lastAckRequest = firstAckRequest + response.batchOffset; -} - -}} // namespace qpid::framing diff --git a/cpp/src/framing/Requester.h b/cpp/src/framing/Requester.h deleted file mode 100644 index 65bdc9a5a1..0000000000 --- a/cpp/src/framing/Requester.h +++ /dev/null @@ -1,68 +0,0 @@ -#ifndef _framing_Requester_h -#define _framing_Requester_h - -/* - * - * 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 -#include "AMQRequestBody.h" -#include "AMQResponseBody.h" - -namespace qpid { -namespace framing { - -class AMQRequestBody; -class AMQResponseBody; - -/** - * Manage request IDs and the response mark for locally initiated requests. - * - * THREAD UNSAFE: must be locked externally. - */ -class Requester -{ - public: - Requester(); - - /** Called before sending a request to set request data. */ - void sending(AMQRequestBody::Data&); - - /** Called after processing a response. */ - void processed(const AMQResponseBody::Data&); - - /** Get the next request id to be used. */ - RequestId getNextId() { return lastId + 1; } - - /** Get the first request acked by last response */ - RequestId getFirstAckRequest() { return firstAckRequest; } - - /** Get the last request acked by last response */ - RequestId getLastAckRequest() { return lastAckRequest; } - - private: - RequestId lastId; - ResponseId responseMark; - ResponseId firstAckRequest; - ResponseId lastAckRequest; -}; - -}} // namespace qpid::framing - - - -#endif /*!_framing_Requester_h*/ diff --git a/cpp/src/framing/Responder.cpp b/cpp/src/framing/Responder.cpp deleted file mode 100644 index e8c8135387..0000000000 --- a/cpp/src/framing/Responder.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * - * 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 - -#include "Responder.h" -#include "../QpidError.h" - -namespace qpid { -namespace framing { - -Responder::Responder() : lastId(0), responseMark(0) {} - -void Responder::received(const AMQRequestBody::Data& request) { - if (request.responseMark < responseMark || request.responseMark > lastId) - THROW_QPID_ERROR( - PROTOCOL_ERROR, boost::format("Invalid response mark %d.") - %request.responseMark); - responseMark = request.responseMark; -} - -void Responder::sending(AMQResponseBody::Data& response) { - response.responseId = ++lastId; - assert(response.requestId); // Should be already set. - response.batchOffset = 0; -} - -}} // namespace qpid::framing - diff --git a/cpp/src/framing/Responder.h b/cpp/src/framing/Responder.h deleted file mode 100644 index 0e1785256b..0000000000 --- a/cpp/src/framing/Responder.h +++ /dev/null @@ -1,61 +0,0 @@ -#ifndef _framing_Responder_h -#define _framing_Responder_h - -/* - * - * 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 "AMQRequestBody.h" -#include "AMQResponseBody.h" - -namespace qpid { -namespace framing { - -/** - * Manage response ids and response mark remotely initianted requests. - * - * THREAD UNSAFE: This class is called as frames are sent or received - * sequentially on a connection, so it does not need to be thread safe. - */ -class Responder -{ - public: - Responder(); - - /** Called after receiving a request. */ - void received(const AMQRequestBody::Data& request); - - /** Called before sending a response to set respose data. */ - void sending(AMQResponseBody::Data& response); - - /** Get the ID of the highest response acknowledged by the peer. */ - ResponseId getResponseMark() { return responseMark; } - - // TODO aconway 2007-01-14: Batching support - store unsent - // Response for equality comparison with subsequent responses. - // - - private: - ResponseId lastId; - ResponseId responseMark; -}; - -}} // namespace qpid::framing - - - -#endif /*!_framing_Responder_h*/ diff --git a/cpp/src/framing/Value.cpp b/cpp/src/framing/Value.cpp deleted file mode 100644 index b5016f3dd0..0000000000 --- a/cpp/src/framing/Value.cpp +++ /dev/null @@ -1,122 +0,0 @@ -/* - * - * 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 "Value.h" -#include "Buffer.h" -#include "FieldTable.h" -#include "../QpidError.h" -#include - -namespace qpid { -namespace framing { - -Value::~Value() {} - -void StringValue::encode(Buffer& buffer){ - buffer.putLongString(value); -} -void StringValue::decode(Buffer& buffer){ - buffer.getLongString(value); -} - -void IntegerValue::encode(Buffer& buffer){ - buffer.putLong((uint32_t) value); -} -void IntegerValue::decode(Buffer& buffer){ - value = buffer.getLong(); -} - -void TimeValue::encode(Buffer& buffer){ - buffer.putLongLong(value); -} -void TimeValue::decode(Buffer& buffer){ - value = buffer.getLongLong(); -} - -void DecimalValue::encode(Buffer& buffer){ - buffer.putOctet(value.decimals); - buffer.putLong(value.value); -} -void DecimalValue::decode(Buffer& buffer){ - value = Decimal(buffer.getLong(), buffer.getOctet()); -} - -void FieldTableValue::encode(Buffer& buffer){ - buffer.putFieldTable(value); -} -void FieldTableValue::decode(Buffer& buffer){ - buffer.getFieldTable(value); -} - -std::auto_ptr Value::decode_value(Buffer& buffer) -{ - std::auto_ptr value; - uint8_t type = buffer.getOctet(); - switch(type){ - case 'S': - value.reset(new StringValue()); - break; - case 'I': - value.reset(new IntegerValue()); - break; - case 'D': - value.reset(new DecimalValue()); - break; - case 'T': - value.reset(new TimeValue()); - break; - case 'F': - value.reset(new FieldTableValue()); - break; - - //non-standard types, introduced in java client for JMS compliance - case 'x': - value.reset(new BinaryValue()); - break; - default: - std::stringstream out; - out << "Unknown field table value type: " << type; - THROW_QPID_ERROR(FRAMING_ERROR, out.str()); - } - value->decode(buffer); - return value; -} - -EmptyValue::~EmptyValue() {} - -void EmptyValue::print(std::ostream& out) const -{ - out << ""; -} - -std::ostream& operator<<(std::ostream& out, const Value& v) { - v.print(out); - return out; -} - -std::ostream& operator<<(std::ostream& out, const Decimal& d) -{ - return out << "Decimal(" << d.value << "," << d.decimals << ")"; -} - -}} - - - diff --git a/cpp/src/framing/Value.h b/cpp/src/framing/Value.h deleted file mode 100644 index a6cff8ec88..0000000000 --- a/cpp/src/framing/Value.h +++ /dev/null @@ -1,171 +0,0 @@ -/* - * - * 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 -#include -#include "amqp_types.h" -#include "FieldTable.h" - -#ifndef _Value_ -#define _Value_ - -namespace qpid { -namespace framing { - -class Buffer; - -/** - * Represents a decimal value. - * No arithmetic functionality for now, we only care about encoding/decoding. - */ -struct Decimal { - uint32_t value; - uint8_t decimals; - - Decimal(uint32_t value_=0, uint8_t decimals_=0) : value(value_), decimals(decimals_) {} - bool operator==(const Decimal& d) const { - return decimals == d.decimals && value == d.value; - } - bool operator!=(const Decimal& d) const { return !(*this == d); } -}; - -std::ostream& operator<<(std::ostream& out, const Decimal& d); - -/** - * Polymorpic base class for values. - */ -class Value { - public: - virtual ~Value(); - virtual uint32_t size() const = 0; - virtual char getType() const = 0; - virtual void encode(Buffer& buffer) = 0; - virtual void decode(Buffer& buffer) = 0; - virtual bool operator==(const Value&) const = 0; - bool operator!=(const Value& v) const { return !(*this == v); } - virtual void print(std::ostream& out) const = 0; - - /** Create a new value by decoding from the buffer */ - static std::auto_ptr decode_value(Buffer& buffer); -}; - -std::ostream& operator<<(std::ostream& out, const Value& d); - - -/** - * Template for common operations on Value sub-classes. - */ -template -class ValueOps : public Value -{ - protected: - T value; - public: - ValueOps() {} - ValueOps(const T& v) : value(v) {} - const T& getValue() const { return value; } - T& getValue() { return value; } - - virtual bool operator==(const Value& v) const { - const ValueOps* vo = dynamic_cast*>(&v); - if (vo == 0) return false; - else return value == vo->value; - } - - void print(std::ostream& out) const { out << value; } -}; - - -class StringValue : public ValueOps { - public: - StringValue(const std::string& v) : ValueOps(v) {} - StringValue() {} - virtual uint32_t size() const { return 4 + value.length(); } - virtual char getType() const { return 'S'; } - virtual void encode(Buffer& buffer); - virtual void decode(Buffer& buffer); -}; - -class IntegerValue : public ValueOps { - public: - IntegerValue(int v) : ValueOps(v) {} - IntegerValue(){} - virtual uint32_t size() const { return 4; } - virtual char getType() const { return 'I'; } - virtual void encode(Buffer& buffer); - virtual void decode(Buffer& buffer); -}; - -class TimeValue : public ValueOps { - public: - TimeValue(uint64_t v) : ValueOps(v){} - TimeValue(){} - virtual uint32_t size() const { return 8; } - virtual char getType() const { return 'T'; } - virtual void encode(Buffer& buffer); - virtual void decode(Buffer& buffer); -}; - -class DecimalValue : public ValueOps { - public: - DecimalValue(const Decimal& d) : ValueOps(d) {} - DecimalValue(uint32_t value_=0, uint8_t decimals_=0) : - ValueOps(Decimal(value_, decimals_)){} - virtual uint32_t size() const { return 5; } - virtual char getType() const { return 'D'; } - virtual void encode(Buffer& buffer); - virtual void decode(Buffer& buffer); -}; - - -class FieldTableValue : public ValueOps { - public: - FieldTableValue(const FieldTable& v) : ValueOps(v){} - FieldTableValue(){} - virtual uint32_t size() const { return 4 + value.size(); } - virtual char getType() const { return 'F'; } - virtual void encode(Buffer& buffer); - virtual void decode(Buffer& buffer); -}; - -class EmptyValue : public Value { - public: - ~EmptyValue(); - virtual uint32_t size() const { return 0; } - virtual char getType() const { return 0; } - virtual void encode(Buffer& ) {} - virtual void decode(Buffer& ) {} - virtual bool operator==(const Value& v) const { - return dynamic_cast(&v); - } - virtual void print(std::ostream& out) const; -}; - -//non-standard types, introduced in java client for JMS compliance -class BinaryValue : public StringValue { - public: - BinaryValue(const std::string& v) : StringValue(v) {} - BinaryValue() {} - virtual char getType() const { return 'x'; } -}; - -}} // qpid::framing - -#endif diff --git a/cpp/src/framing/amqp_framing.h b/cpp/src/framing/amqp_framing.h deleted file mode 100644 index c99dd2bb64..0000000000 --- a/cpp/src/framing/amqp_framing.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * - * 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 "amqp_types.h" -#include "AMQFrame.h" -#include "AMQBody.h" -#include "BodyHandler.h" -#include "AMQMethodBody.h" -#include "AMQHeaderBody.h" -#include "AMQContentBody.h" -#include "AMQHeartbeatBody.h" -#include "../gen/AMQP_MethodVersionMap.h" -#include "InputHandler.h" -#include "OutputHandler.h" -#include "InitiationHandler.h" -#include "ProtocolInitiation.h" -#include "BasicHeaderProperties.h" -#include "ProtocolVersion.h" -#include "ProtocolVersionException.h" diff --git a/cpp/src/framing/amqp_types.h b/cpp/src/framing/amqp_types.h deleted file mode 100644 index f0bd0ce427..0000000000 --- a/cpp/src/framing/amqp_types.h +++ /dev/null @@ -1,57 +0,0 @@ -#ifndef AMQP_TYPES_H -#define AMQP_TYPES_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. - * - */ - -/** \file - * Type definitions and forward declarations of all types used to - * in AMQP messages. - */ - -#include -#ifdef _WINDOWS -#include "windows.h" -typedef unsigned char uint8_t; -typedef unsigned short uint16_t; -typedef unsigned int uint32_t; -typedef unsigned __int64 uint64_t; -#endif -#ifndef _WINDOWS -#include -#endif - -namespace qpid { -namespace framing { - -using std::string; -typedef uint16_t ChannelId; -typedef uint64_t RequestId; -typedef uint64_t ResponseId; -typedef uint32_t BatchOffset; -typedef uint16_t ClassId; -typedef uint16_t MethodId; -typedef uint16_t ReplyCode; - -// Types represented by classes. -class Content; -class FieldTable; -}} // namespace qpid::framing -#endif diff --git a/cpp/src/framing/amqp_types_full.h b/cpp/src/framing/amqp_types_full.h deleted file mode 100644 index 6a24a99d38..0000000000 --- a/cpp/src/framing/amqp_types_full.h +++ /dev/null @@ -1,36 +0,0 @@ -#ifndef _framing_amqp_types_decl_h -#define _framing_amqp_types_decl_h - -/* - * - * 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. - * - */ - -/** \file - * Type definitions and full declarations of all types used to - * in AMQP messages. - * - * Its better to include amqp_types.h in another header instead of this file - * unless the header actually needs the full declarations. Including - * full declarations when forward declarations would do increases compile - * times. - */ - -#include "amqp_types.h" -#include "FramingContent.h" -#include "FieldTable.h" - -#endif /*!_framing_amqp_types_decl_h*/ diff --git a/cpp/src/make-gen-src-mk.sh b/cpp/src/make-gen-src-mk.sh index 0a6dd3e326..21655e5512 100755 --- a/cpp/src/make-gen-src-mk.sh +++ b/cpp/src/make-gen-src-mk.sh @@ -4,13 +4,14 @@ gentools_dir=$1 gentools_srcdir=$2 +gen=$3 wildcard() { echo `ls $* 2>/dev/null` ; } cat < +#include +#include +#include +#include + +#include "framing/amqp_types.h" + +namespace qpid +{ +/** + * Exception base class for all Qpid exceptions. + */ +class Exception : public std::exception +{ + protected: + std::string whatStr; + + public: + Exception() throw(); + Exception(const std::string& str) throw(); + Exception(const char* str) throw(); + Exception(const std::exception&) throw(); + + /** Allow any type that has ostream operator<< to act as message */ + template + Exception(const T& message) + : whatStr(boost::lexical_cast(message)) {} + + virtual ~Exception() throw(); + + virtual const char* what() const throw(); + virtual std::string toString() const throw(); + + virtual Exception* clone() const throw(); + virtual void throwSelf() const; + + typedef boost::shared_ptr shared_ptr; +}; + +struct ChannelException : public Exception { + framing::ReplyCode code; + template + ChannelException(framing::ReplyCode code_, const T& message) + : Exception(message), code(code_) {} + void throwSelf() const { throw *this; } +}; + +struct ConnectionException : public Exception { + framing::ReplyCode code; + template + ConnectionException(framing::ReplyCode code_, const T& message) + : Exception(message), code(code_) {} + void throwSelf() const { throw *this; } +}; + +/** + * Exception used to indicate that a thread should shut down. + * Does not indicate an error that should be signalled to the user. + */ +struct ShutdownException : public Exception { + ShutdownException(); + void throwSelf() const { throw *this; } +}; + +/** Exception to indicate empty queue or other empty state */ +struct EmptyException : public Exception { + EmptyException(); + void throwSelf() const { throw *this; } +}; + +} + +#endif /*!_Exception_*/ diff --git a/cpp/src/qpid/ExceptionHolder.cpp b/cpp/src/qpid/ExceptionHolder.cpp new file mode 100644 index 0000000000..de8d7b2487 --- /dev/null +++ b/cpp/src/qpid/ExceptionHolder.cpp @@ -0,0 +1,32 @@ +/* + * + * 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 "ExceptionHolder.h" + +namespace qpid { + +ExceptionHolder::ExceptionHolder(const std::exception& e) { + const Exception* ex = dynamic_cast(&e); + if (ex) { + reset(ex->clone()); + } else { + reset(new Exception(e.what())); + } +} + +} diff --git a/cpp/src/qpid/ExceptionHolder.h b/cpp/src/qpid/ExceptionHolder.h new file mode 100644 index 0000000000..723dac4b2d --- /dev/null +++ b/cpp/src/qpid/ExceptionHolder.h @@ -0,0 +1,67 @@ +#ifndef _qpid_ExceptionHolder_h +#define _qpid_ExceptionHolder_h + +/* + * + * 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 +#include "Exception.h" +#include + +namespace qpid { + +// FIXME aconway 2007-02-20: Not necessary, a simple +// Exception::shared_ptr will do the job. Remove +// +/** + * Holder for a heap-allocated exc eption that can be stack allocated + * and thrown safely. + * + * Basically this is a shared_ptr with the Exception functions added + * so the catcher need not be aware that it is a pointer rather than a + * reference. + * + * shared_ptr is chosen over auto_ptr because it has normal + * copy semantics. + */ +class ExceptionHolder : public Exception, public boost::shared_ptr +{ + public: + typedef boost::shared_ptr shared_ptr; + + ExceptionHolder() throw() {} + ExceptionHolder(Exception* p) throw() : shared_ptr(p) {} + ExceptionHolder(shared_ptr p) throw() : shared_ptr(p) {} + + ExceptionHolder(const Exception& e) throw() : shared_ptr(e.clone()) {} + ExceptionHolder(const std::exception& e); + + ~ExceptionHolder() throw() {} + + const char* what() const throw() { return get()->what(); } + std::string toString() const throw() { return get()->toString(); } + Exception* clone() const throw() { return get()->clone(); } + void throwIf() const { if (get()) get()->throwSelf(); } + void throwSelf() const { assert(get()); get()->throwSelf(); } +}; + +} // namespace qpid + + + +#endif /*!_qpid_ExceptionHolder_h*/ diff --git a/cpp/src/qpid/QpidError.cpp b/cpp/src/qpid/QpidError.cpp new file mode 100644 index 0000000000..365c84ea1d --- /dev/null +++ b/cpp/src/qpid/QpidError.cpp @@ -0,0 +1,42 @@ +/* + * + * 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 + +#include "QpidError.h" +#include + +using namespace qpid; + +QpidError::QpidError() : code(0) {} + +QpidError::~QpidError() throw() {} + +Exception* QpidError::clone() const throw() { return new QpidError(*this); } + +void QpidError::throwSelf() const { throw *this; } + +void QpidError::init() { + whatStr = boost::str(boost::format("Error [%d] %s (%s:%d)") + % code % msg % loc.file % loc.line); +} + + diff --git a/cpp/src/qpid/QpidError.h b/cpp/src/qpid/QpidError.h new file mode 100644 index 0000000000..12e80c9bd9 --- /dev/null +++ b/cpp/src/qpid/QpidError.h @@ -0,0 +1,78 @@ +#ifndef __QpidError__ +#define __QpidError__ +/* + * + * 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 +#include +#include + +#include "Exception.h" + +namespace qpid { + +struct SrcLine { + public: + SrcLine(const std::string& file_="", int line_=0) : + file(file_), line(line_) {} + + std::string file; + int line; +}; + +class QpidError : public Exception +{ + public: + const int code; + SrcLine loc; + std::string msg; + + QpidError(); + + template + QpidError(int code_, const T& msg_, const SrcLine& loc_) throw() + : code(code_), loc(loc_), msg(boost::lexical_cast(msg_)) + { init(); } + + ~QpidError() throw(); + Exception* clone() const throw(); + void throwSelf() const; + + private: + + void init(); +}; + + +} // namespace qpid + +#define SRCLINE ::qpid::SrcLine(__FILE__, __LINE__) + +#define QPID_ERROR(CODE, MESSAGE) ::qpid::QpidError((CODE), (MESSAGE), SRCLINE) + +#define THROW_QPID_ERROR(CODE, MESSAGE) throw QPID_ERROR(CODE,MESSAGE) + +const int PROTOCOL_ERROR = 10000; +const int APR_ERROR = 20000; +const int FRAMING_ERROR = 30000; +const int CLIENT_ERROR = 40000; +const int INTERNAL_ERROR = 50000; + +#endif diff --git a/cpp/src/qpid/SharedObject.h b/cpp/src/qpid/SharedObject.h new file mode 100644 index 0000000000..852a036ab9 --- /dev/null +++ b/cpp/src/qpid/SharedObject.h @@ -0,0 +1,55 @@ +#ifndef _SharedObject_ +#define _SharedObject_ + +/* + * + * 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 +#include + +namespace qpid { + /** + * Template to enforce shared object conventions. + * Shared object classes should inherit : public qpid::SharedObject + * That ensures Foo: + * - has typedef boost::shared_ptr shared_ptr + * - has virtual destructor + * - is boost::noncopyable (no default copy or assign) + * - has a protected default constructor. + * + * Shared objects should not have public constructors. + * Make constructors protected and provide public statc create() + * functions that return a shared_ptr. + */ + template + class SharedObject : private boost::noncopyable + { + public: + typedef boost::shared_ptr shared_ptr; + + virtual ~SharedObject() {}; + + protected: + SharedObject() {} + }; +} + +#endif /*!_SharedObject_*/ diff --git a/cpp/src/qpid/broker/AccumulatedAck.cpp b/cpp/src/qpid/broker/AccumulatedAck.cpp new file mode 100644 index 0000000000..ff471b0287 --- /dev/null +++ b/cpp/src/qpid/broker/AccumulatedAck.cpp @@ -0,0 +1,57 @@ +/* + * + * 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 "AccumulatedAck.h" + +#include + +using std::less_equal; +using std::bind2nd; +using namespace qpid::broker; + +void AccumulatedAck::update(uint64_t firstTag, uint64_t lastTag){ + assert(firstTag<=lastTag); + if (firstTag <= range + 1) { + if (lastTag > range) range = lastTag; + } else { + for (uint64_t tag = firstTag; tag<=lastTag; tag++) + individual.push_back(tag); + } +} + +void AccumulatedAck::consolidate(){ + individual.sort(); + //remove any individual tags that are covered by range + individual.remove_if(bind2nd(less_equal(), range)); + //update range if possible (using <= allows for duplicates from overlapping ranges) + while (individual.front() <= range + 1) { + range = individual.front(); + individual.pop_front(); + } +} + +void AccumulatedAck::clear(){ + range = 0; + individual.clear(); +} + +bool AccumulatedAck::covers(uint64_t tag) const{ + return tag <= range || find(individual.begin(), individual.end(), tag) != individual.end(); +} diff --git a/cpp/src/qpid/broker/AccumulatedAck.h b/cpp/src/qpid/broker/AccumulatedAck.h new file mode 100644 index 0000000000..c4a6e3b79b --- /dev/null +++ b/cpp/src/qpid/broker/AccumulatedAck.h @@ -0,0 +1,57 @@ +/* + * + * 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 _AccumulatedAck_ +#define _AccumulatedAck_ + +#include +#include +#include + +namespace qpid { + namespace broker { + /** + * Keeps an accumulated record of acked messages (by delivery + * tag). + */ + class AccumulatedAck { + public: + /** + * If not zero, then everything up to this value has been + * acked. + */ + uint64_t range; + /** + * List of individually acked messages that are not + * included in the range marked by 'range'. + */ + std::list individual; + + AccumulatedAck(uint64_t r) : range(r) {} + void update(uint64_t firstTag, uint64_t lastTag); + void consolidate(); + void clear(); + bool covers(uint64_t tag) const; + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/AutoDelete.cpp b/cpp/src/qpid/broker/AutoDelete.cpp new file mode 100644 index 0000000000..bfdb8decd2 --- /dev/null +++ b/cpp/src/qpid/broker/AutoDelete.cpp @@ -0,0 +1,86 @@ +/* + * + * 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 "AutoDelete.h" +#include "qpid/sys/Time.h" + +using namespace qpid::broker; +using namespace qpid::sys; + +AutoDelete::AutoDelete(QueueRegistry* const _registry, uint32_t _period) + : registry(_registry), period(_period), stopped(true) { } + +void AutoDelete::add(Queue::shared_ptr const queue){ + Mutex::ScopedLock l(lock); + queues.push(queue); +} + +Queue::shared_ptr const AutoDelete::pop(){ + Queue::shared_ptr next; + Mutex::ScopedLock l(lock); + if(!queues.empty()){ + next = queues.front(); + queues.pop(); + } + return next; +} + +void AutoDelete::process(){ + Queue::shared_ptr seen; + for(Queue::shared_ptr q = pop(); q; q = pop()){ + if(seen == q){ + add(q); + break; + }else if(q->canAutoDelete()){ + std::string name(q->getName()); + registry->destroy(name); + std::cout << "INFO: Auto-deleted queue named " << name << std::endl; + }else{ + add(q); + if(!seen) seen = q; + } + } +} + +void AutoDelete::run(){ + Monitor::ScopedLock l(monitor); + while(!stopped){ + process(); + monitor.wait(period*TIME_MSEC); + } +} + +void AutoDelete::start(){ + Monitor::ScopedLock l(monitor); + if(stopped){ + stopped = false; + runner = Thread(this); + } +} + +void AutoDelete::stop(){ + { + Monitor::ScopedLock l(monitor); + if(stopped) return; + stopped = true; + } + monitor.notify(); + runner.join(); +} diff --git a/cpp/src/qpid/broker/AutoDelete.h b/cpp/src/qpid/broker/AutoDelete.h new file mode 100644 index 0000000000..24e9c18a68 --- /dev/null +++ b/cpp/src/qpid/broker/AutoDelete.h @@ -0,0 +1,55 @@ +#ifndef _AutoDelete_ +#define _AutoDelete_ +/* + * + * 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 +#include +#include "qpid/sys/Monitor.h" +#include "BrokerQueue.h" +#include "QueueRegistry.h" +#include "qpid/sys/Thread.h" + +namespace qpid { + namespace broker{ + class AutoDelete : private qpid::sys::Runnable { + qpid::sys::Mutex lock; + qpid::sys::Monitor monitor; + std::queue queues; + QueueRegistry* const registry; + uint32_t period; + volatile bool stopped; + qpid::sys::Thread runner; + + Queue::shared_ptr const pop(); + void process(); + virtual void run(); + + public: + AutoDelete(QueueRegistry* const registry, uint32_t period); + void add(Queue::shared_ptr const); + void start(); + void stop(); + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/Broker.cpp b/cpp/src/qpid/broker/Broker.cpp new file mode 100644 index 0000000000..5ab65bf573 --- /dev/null +++ b/cpp/src/qpid/broker/Broker.cpp @@ -0,0 +1,122 @@ +/* + * + * 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 +#include + +#include "qpid/framing/AMQFrame.h" +#include "DirectExchange.h" +#include "TopicExchange.h" +#include "FanOutExchange.h" +#include "HeadersExchange.h" +#include "MessageStoreModule.h" +#include "NullMessageStore.h" +#include "qpid/framing/ProtocolInitiation.h" +#include "RecoveryManagerImpl.h" +#include "Connection.h" +#include "qpid/sys/ConnectionInputHandler.h" +#include "qpid/sys/ConnectionInputHandlerFactory.h" +#include "qpid/sys/TimeoutHandler.h" + +#include "Broker.h" + +namespace qpid { +namespace broker { + +const std::string empty; +const std::string amq_direct("amq.direct"); +const std::string amq_topic("amq.topic"); +const std::string amq_fanout("amq.fanout"); +const std::string amq_match("amq.match"); + +Broker::Broker(const Configuration& conf) : + config(conf), + store(createStore(conf)), + queues(store.get()), + timeout(30000), + stagingThreshold(0), + cleaner(&queues, timeout/10), + factory(*this) +{ + exchanges.declare(empty, DirectExchange::typeName); // Default exchange. + exchanges.declare(amq_direct, DirectExchange::typeName); + exchanges.declare(amq_topic, TopicExchange::typeName); + exchanges.declare(amq_fanout, FanOutExchange::typeName); + exchanges.declare(amq_match, HeadersExchange::typeName); + + if(store.get()) { + RecoveryManagerImpl recoverer(queues, exchanges, conf.getStagingThreshold()); + store->recover(recoverer); + } + + cleaner.start(); +} + + +Broker::shared_ptr Broker::create(int16_t port) +{ + Configuration config; + config.setPort(port); + return create(config); +} + +Broker::shared_ptr Broker::create(const Configuration& config) { + return Broker::shared_ptr(new Broker(config)); +} + +MessageStore* Broker::createStore(const Configuration& config) { + if (config.getStore().empty()) + return new NullMessageStore(config.isTrace()); + else + return new MessageStoreModule(config.getStore()); +} + +void Broker::run() { + getAcceptor().run(&factory); +} + +void Broker::shutdown() { + if (acceptor) + acceptor->shutdown(); + cleaner.stop(); +} + +Broker::~Broker() { + shutdown(); +} + +int16_t Broker::getPort() const { return getAcceptor().getPort(); } + +Acceptor& Broker::getAcceptor() const { + if (!acceptor) + const_cast(acceptor) = + Acceptor::create(config.getPort(), + config.getConnectionBacklog(), + config.getWorkerThreads(), + config.isTrace()); + return *acceptor; +} + + +const int16_t Broker::DEFAULT_PORT(5672); + + +}} // namespace qpid::broker + diff --git a/cpp/src/qpid/broker/Broker.h b/cpp/src/qpid/broker/Broker.h new file mode 100644 index 0000000000..57bd86f72e --- /dev/null +++ b/cpp/src/qpid/broker/Broker.h @@ -0,0 +1,108 @@ +#ifndef _Broker_ +#define _Broker_ + +/* + * + * 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 "Configuration.h" +#include "ConnectionFactory.h" +#include "qpid/sys/Runnable.h" +#include "qpid/sys/Acceptor.h" +#include "qpid/SharedObject.h" +#include "MessageStore.h" +#include "AutoDelete.h" +#include "ExchangeRegistry.h" +#include "ConnectionToken.h" +#include "DirectExchange.h" +#include "qpid/framing/OutputHandler.h" +#include "qpid/framing/ProtocolInitiation.h" +#include "QueueRegistry.h" + +namespace qpid { +namespace broker { +/** + * A broker instance. + */ +class Broker : public sys::Runnable, + public SharedObject +{ + public: + static const int16_t DEFAULT_PORT; + + virtual ~Broker(); + + /** + * Create a broker. + * @param port Port to listen on or 0 to pick a port dynamically. + */ + static shared_ptr create(int16_t port = DEFAULT_PORT); + + /** + * Create a broker using a Configuration. + */ + static shared_ptr create(const Configuration& config); + + /** + * Return listening port. If called before bind this is + * the configured port. If called after it is the actual + * port, which will be different if the configured port is + * 0. + */ + virtual int16_t getPort() const; + + /** + * Run the broker. Implements Runnable::run() so the broker + * can be run in a separate thread. + */ + virtual void run(); + + /** Shut down the broker */ + virtual void shutdown(); + + MessageStore& getStore() { return *store; } + QueueRegistry& getQueues() { return queues; } + ExchangeRegistry& getExchanges() { return exchanges; } + uint32_t getTimeout() { return timeout; } + uint64_t getStagingThreshold() { return stagingThreshold; } + AutoDelete& getCleaner() { return cleaner; } + + private: + Broker(const Configuration& config); + sys::Acceptor& getAcceptor() const; + + Configuration config; + sys::Acceptor::shared_ptr acceptor; + const std::auto_ptr store; + QueueRegistry queues; + ExchangeRegistry exchanges; + uint32_t timeout; + uint64_t stagingThreshold; + AutoDelete cleaner; + ConnectionFactory factory; + + static MessageStore* createStore(const Configuration& config); +}; + +}} + + + +#endif /*!_Broker_*/ diff --git a/cpp/src/qpid/broker/BrokerAdapter.cpp b/cpp/src/qpid/broker/BrokerAdapter.cpp new file mode 100644 index 0000000000..240973b060 --- /dev/null +++ b/cpp/src/qpid/broker/BrokerAdapter.cpp @@ -0,0 +1,388 @@ +/* + * + * 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 + +#include "BrokerAdapter.h" +#include "BrokerChannel.h" +#include "Connection.h" +#include "qpid/framing/AMQMethodBody.h" +#include "qpid/Exception.h" + +namespace qpid { +namespace broker { + +using boost::format; +using namespace qpid; +using namespace qpid::framing; + +typedef std::vector QueueVector; + + +BrokerAdapter::BrokerAdapter(Channel& ch, Connection& c, Broker& b) : + CoreRefs(ch, c, b), + connection(c), + basicHandler(*this), + channelHandler(*this), + connectionHandler(*this), + exchangeHandler(*this), + messageHandler(*this), + queueHandler(*this), + txHandler(*this) +{} + + +ProtocolVersion BrokerAdapter::getVersion() const { + return connection.getVersion(); +} + +void BrokerAdapter::ConnectionHandlerImpl::startOk( + const MethodContext&, const FieldTable& /*clientProperties*/, + const string& /*mechanism*/, + const string& /*response*/, const string& /*locale*/) +{ + client.tune( + 100, connection.getFrameMax(), connection.getHeartbeat()); +} + +void BrokerAdapter::ConnectionHandlerImpl::secureOk( + const MethodContext&, const string& /*response*/){} + +void BrokerAdapter::ConnectionHandlerImpl::tuneOk( + const MethodContext&, uint16_t /*channelmax*/, + uint32_t framemax, uint16_t heartbeat) +{ + connection.setFrameMax(framemax); + connection.setHeartbeat(heartbeat); +} + +void BrokerAdapter::ConnectionHandlerImpl::open( + const MethodContext& context, const string& /*virtualHost*/, + const string& /*capabilities*/, bool /*insist*/) +{ + string knownhosts; + client.openOk( + knownhosts, context.getRequestId()); +} + +void BrokerAdapter::ConnectionHandlerImpl::close( + const MethodContext& context, uint16_t /*replyCode*/, const string& /*replyText*/, + uint16_t /*classId*/, uint16_t /*methodId*/) +{ + client.closeOk(context.getRequestId()); + connection.getOutput().close(); +} + +void BrokerAdapter::ConnectionHandlerImpl::closeOk(const MethodContext&){ + connection.getOutput().close(); +} + +void BrokerAdapter::ChannelHandlerImpl::open( + const MethodContext& context, const string& /*outOfBand*/){ + channel.open(); + // FIXME aconway 2007-01-04: provide valid ID as per ampq 0-9 + client.openOk( + std::string()/* ID */, context.getRequestId()); +} + +void BrokerAdapter::ChannelHandlerImpl::flow(const MethodContext&, bool /*active*/){} +void BrokerAdapter::ChannelHandlerImpl::flowOk(const MethodContext&, bool /*active*/){} + +void BrokerAdapter::ChannelHandlerImpl::close( + const MethodContext& context, uint16_t /*replyCode*/, + const string& /*replyText*/, + uint16_t /*classId*/, uint16_t /*methodId*/) +{ + client.closeOk(context.getRequestId()); + // FIXME aconway 2007-01-18: Following line will "delete this". Ugly. + connection.closeChannel(channel.getId()); +} + +void BrokerAdapter::ChannelHandlerImpl::closeOk(const MethodContext&){} + + + +void BrokerAdapter::ExchangeHandlerImpl::declare(const MethodContext& context, uint16_t /*ticket*/, const string& exchange, const string& type, + bool passive, bool /*durable*/, bool /*autoDelete*/, bool /*internal*/, bool nowait, + const FieldTable& /*arguments*/){ + + if(passive){ + if(!broker.getExchanges().get(exchange)) { + throw ChannelException(404, "Exchange not found: " + exchange); + } + }else{ + try{ + std::pair response = broker.getExchanges().declare(exchange, type); + if(!response.second && response.first->getType() != type){ + throw ConnectionException( + 530, + "Exchange already declared to be of type " + + response.first->getType() + ", requested " + type); + } + }catch(UnknownExchangeTypeException& e){ + throw ConnectionException( + 503, "Exchange type not implemented: " + type); + } + } + if(!nowait){ + client.declareOk(context.getRequestId()); + } +} + +void BrokerAdapter::ExchangeHandlerImpl::delete_(const MethodContext& context, uint16_t /*ticket*/, + const string& exchange, bool /*ifUnused*/, bool nowait){ + + //TODO: implement unused + broker.getExchanges().destroy(exchange); + if(!nowait) client.deleteOk(context.getRequestId()); +} + +void BrokerAdapter::QueueHandlerImpl::declare(const MethodContext& context, uint16_t /*ticket*/, const string& name, + bool passive, bool durable, bool exclusive, + bool autoDelete, bool nowait, const qpid::framing::FieldTable& arguments){ + Queue::shared_ptr queue; + if (passive && !name.empty()) { + queue = connection.getQueue(name, channel.getId()); + } else { + std::pair queue_created = + broker.getQueues().declare( + name, durable, + autoDelete ? connection.getTimeout() : 0, + exclusive ? &connection : 0); + queue = queue_created.first; + assert(queue); + if (queue_created.second) { // This is a new queue + channel.setDefaultQueue(queue); + + //apply settings & create persistent record if required + queue_created.first->create(arguments); + + //add default binding: + broker.getExchanges().getDefault()->bind(queue, name, 0); + if (exclusive) { + connection.exclusiveQueues.push_back(queue); + } else if(autoDelete){ + broker.getCleaner().add(queue); + } + } + } + if (exclusive && !queue->isExclusiveOwner(&connection)) + throw ChannelException( + 405, + format("Cannot grant exclusive access to queue '%s'") + % queue->getName()); + if (!nowait) { + string queueName = queue->getName(); + client.declareOk( + queueName, queue->getMessageCount(), queue->getConsumerCount(), + context.getRequestId()); + } +} + +void BrokerAdapter::QueueHandlerImpl::bind(const MethodContext& context, uint16_t /*ticket*/, const string& queueName, + const string& exchangeName, const string& routingKey, bool nowait, + const FieldTable& arguments){ + + Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); + Exchange::shared_ptr exchange = broker.getExchanges().get(exchangeName); + if(exchange){ + string exchangeRoutingKey = routingKey.empty() && queueName.empty() ? queue->getName() : routingKey; + exchange->bind(queue, exchangeRoutingKey, &arguments); + if(!nowait) client.bindOk(context.getRequestId()); + }else{ + throw ChannelException( + 404, "Bind failed. No such exchange: " + exchangeName); + } +} + +void +BrokerAdapter::QueueHandlerImpl::unbind( + const MethodContext& context, + uint16_t /*ticket*/, + const string& queueName, + const string& exchangeName, + const string& routingKey, + const qpid::framing::FieldTable& arguments ) +{ + Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); + if (!queue.get()) throw ChannelException(404, "Unbind failed. No such exchange: " + exchangeName); + + Exchange::shared_ptr exchange = broker.getExchanges().get(exchangeName); + if (!exchange.get()) throw ChannelException(404, "Unbind failed. No such exchange: " + exchangeName); + + exchange->unbind(queue, routingKey, &arguments); + + client.unbindOk(context.getRequestId()); +} + +void BrokerAdapter::QueueHandlerImpl::purge(const MethodContext& context, uint16_t /*ticket*/, const string& queueName, bool nowait){ + + Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); + int count = queue->purge(); + if(!nowait) client.purgeOk( count, context.getRequestId()); +} + +void BrokerAdapter::QueueHandlerImpl::delete_(const MethodContext& context, uint16_t /*ticket*/, const string& queue, + bool ifUnused, bool ifEmpty, bool nowait){ + ChannelException error(0, ""); + int count(0); + Queue::shared_ptr q = connection.getQueue(queue, channel.getId()); + if(ifEmpty && q->getMessageCount() > 0){ + throw ChannelException(406, "Queue not empty."); + }else if(ifUnused && q->getConsumerCount() > 0){ + throw ChannelException(406, "Queue in use."); + }else{ + //remove the queue from the list of exclusive queues if necessary + if(q->isExclusiveOwner(&connection)){ + QueueVector::iterator i = find(connection.exclusiveQueues.begin(), connection.exclusiveQueues.end(), q); + if(i < connection.exclusiveQueues.end()) connection.exclusiveQueues.erase(i); + } + count = q->getMessageCount(); + q->destroy(); + broker.getQueues().destroy(queue); + } + + if(!nowait) + client.deleteOk(count, context.getRequestId()); +} + + + + +void BrokerAdapter::BasicHandlerImpl::qos(const MethodContext& context, uint32_t prefetchSize, uint16_t prefetchCount, bool /*global*/){ + //TODO: handle global + channel.setPrefetchSize(prefetchSize); + channel.setPrefetchCount(prefetchCount); + client.qosOk(context.getRequestId()); +} + +void BrokerAdapter::BasicHandlerImpl::consume( + const MethodContext& context, uint16_t /*ticket*/, + const string& queueName, const string& consumerTag, + bool noLocal, bool noAck, bool exclusive, + bool nowait, const FieldTable& fields) +{ + + Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); + if(!consumerTag.empty() && channel.exists(consumerTag)){ + throw ConnectionException(530, "Consumer tags must be unique"); + } + + string newTag = consumerTag; + channel.consume( + newTag, queue, !noAck, exclusive, noLocal ? &connection : 0, &fields); + + if(!nowait) client.consumeOk(newTag, context.getRequestId()); + + //allow messages to be dispatched if required as there is now a consumer: + queue->dispatch(); +} + +void BrokerAdapter::BasicHandlerImpl::cancel(const MethodContext& context, const string& consumerTag, bool nowait){ + channel.cancel(consumerTag); + + if(!nowait) client.cancelOk(consumerTag, context.getRequestId()); +} + +void BrokerAdapter::BasicHandlerImpl::publish( + const MethodContext& context, uint16_t /*ticket*/, + const string& exchangeName, const string& routingKey, + bool mandatory, bool immediate) +{ + + Exchange::shared_ptr exchange = exchangeName.empty() ? broker.getExchanges().getDefault() : broker.getExchanges().get(exchangeName); + if(exchange){ + BasicMessage* msg = new BasicMessage( + &connection, exchangeName, routingKey, mandatory, immediate, + context.methodBody); + channel.handlePublish(msg); + }else{ + throw ChannelException( + 404, "Exchange not found '" + exchangeName + "'"); + } +} + +void BrokerAdapter::BasicHandlerImpl::get(const MethodContext& context, uint16_t /*ticket*/, const string& queueName, bool noAck){ + Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); + if(!connection.getChannel(channel.getId()).get(queue, "", !noAck)){ + string clusterId;//not used, part of an imatix hack + + client.getEmpty(clusterId, context.getRequestId()); + } +} + +void BrokerAdapter::BasicHandlerImpl::ack(const MethodContext&, uint64_t deliveryTag, bool multiple){ + channel.ack(deliveryTag, multiple); +} + +void BrokerAdapter::BasicHandlerImpl::reject(const MethodContext&, uint64_t /*deliveryTag*/, bool /*requeue*/){} + +void BrokerAdapter::BasicHandlerImpl::recover(const MethodContext&, bool requeue){ + channel.recover(requeue); +} + +void BrokerAdapter::TxHandlerImpl::select(const MethodContext& context){ + channel.begin(); + client.selectOk(context.getRequestId()); +} + +void BrokerAdapter::TxHandlerImpl::commit(const MethodContext& context){ + channel.commit(); + client.commitOk(context.getRequestId()); +} + +void BrokerAdapter::TxHandlerImpl::rollback(const MethodContext& context){ + + channel.rollback(); + client.rollbackOk(context.getRequestId()); + channel.recover(false); +} + +void +BrokerAdapter::ChannelHandlerImpl::ok( const MethodContext& ) +{ + //no specific action required, generic response handling should be sufficient +} + + +// +// Message class method handlers +// +void +BrokerAdapter::ChannelHandlerImpl::ping( const MethodContext& context) +{ + client.ok(context.getRequestId()); + client.pong(); +} + + +void +BrokerAdapter::ChannelHandlerImpl::pong( const MethodContext& context) +{ + client.ok(context.getRequestId()); +} + +void +BrokerAdapter::ChannelHandlerImpl::resume( + const MethodContext&, + const string& /*channel*/ ) +{ + assert(0); // FIXME aconway 2007-01-04: 0-9 feature +} + +}} // namespace qpid::broker + diff --git a/cpp/src/qpid/broker/BrokerAdapter.h b/cpp/src/qpid/broker/BrokerAdapter.h new file mode 100644 index 0000000000..0c7c9edba8 --- /dev/null +++ b/cpp/src/qpid/broker/BrokerAdapter.h @@ -0,0 +1,222 @@ +#ifndef _broker_BrokerAdapter_h +#define _broker_BrokerAdapter_h + +/* + * + * 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/framing/AMQP_ServerOperations.h" +#include "HandlerImpl.h" +#include "MessageHandlerImpl.h" +#include "qpid/Exception.h" + +namespace qpid { +namespace broker { + +class Channel; +class Connection; +class Broker; +class ChannelHandler; +class ConnectionHandler; +class BasicHandler; +class ExchangeHandler; +class QueueHandler; +class TxHandler; +class MessageHandler; +class AccessHandler; +class FileHandler; +class StreamHandler; +class DtxHandler; +class TunnelHandler; +class MessageHandlerImpl; + +/** + * Per-channel protocol adapter. + * + * A container for a collection of AMQP-class adapters that translate + * AMQP method bodies into calls on the core Channel, Connection and + * Broker objects. Each adapter class also provides a client proxy + * to send methods to the peer. + * + */ +class BrokerAdapter : public CoreRefs, public framing::AMQP_ServerOperations +{ + public: + BrokerAdapter(Channel& ch, Connection& c, Broker& b); + + framing::ProtocolVersion getVersion() const; + ChannelHandler* getChannelHandler() { return &channelHandler; } + ConnectionHandler* getConnectionHandler() { return &connectionHandler; } + BasicHandler* getBasicHandler() { return &basicHandler; } + ExchangeHandler* getExchangeHandler() { return &exchangeHandler; } + QueueHandler* getQueueHandler() { return &queueHandler; } + TxHandler* getTxHandler() { return &txHandler; } + MessageHandler* getMessageHandler() { return &messageHandler; } + AccessHandler* getAccessHandler() { + throw ConnectionException(540, "Access class not implemented"); } + FileHandler* getFileHandler() { + throw ConnectionException(540, "File class not implemented"); } + StreamHandler* getStreamHandler() { + throw ConnectionException(540, "Stream class not implemented"); } + DtxHandler* getDtxHandler() { + throw ConnectionException(540, "Dtx class not implemented"); } + TunnelHandler* getTunnelHandler() { + throw ConnectionException(540, "Tunnel class not implemented"); } + + framing::AMQP_ClientProxy& getProxy() { return proxy; } + + private: + + class ConnectionHandlerImpl : + public ConnectionHandler, + public HandlerImpl + { + public: + ConnectionHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} + + void startOk(const framing::MethodContext& context, + const qpid::framing::FieldTable& clientProperties, + const std::string& mechanism, const std::string& response, + const std::string& locale); + void secureOk(const framing::MethodContext& context, + const std::string& response); + void tuneOk(const framing::MethodContext& context, + uint16_t channelMax, + uint32_t frameMax, uint16_t heartbeat); + void open(const framing::MethodContext& context, + const std::string& virtualHost, + const std::string& capabilities, bool insist); + void close(const framing::MethodContext& context, uint16_t replyCode, + const std::string& replyText, + uint16_t classId, uint16_t methodId); + void closeOk(const framing::MethodContext& context); + }; + + class ChannelHandlerImpl : + public ChannelHandler, + public HandlerImpl + { + public: + ChannelHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} + + void open(const framing::MethodContext& context, const std::string& outOfBand); + void flow(const framing::MethodContext& context, bool active); + void flowOk(const framing::MethodContext& context, bool active); + void ok( const framing::MethodContext& context ); + void ping( const framing::MethodContext& context ); + void pong( const framing::MethodContext& context ); + void resume( const framing::MethodContext& context, const std::string& channelId ); + void close(const framing::MethodContext& context, uint16_t replyCode, const + std::string& replyText, uint16_t classId, uint16_t methodId); + void closeOk(const framing::MethodContext& context); + }; + + class ExchangeHandlerImpl : + public ExchangeHandler, + public HandlerImpl + { + public: + ExchangeHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} + + void declare(const framing::MethodContext& context, uint16_t ticket, + const std::string& exchange, const std::string& type, + bool passive, bool durable, bool autoDelete, + bool internal, bool nowait, + const qpid::framing::FieldTable& arguments); + void delete_(const framing::MethodContext& context, uint16_t ticket, + const std::string& exchange, bool ifUnused, bool nowait); + }; + + class QueueHandlerImpl : + public QueueHandler, + public HandlerImpl + { + public: + QueueHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} + + void declare(const framing::MethodContext& context, uint16_t ticket, const std::string& queue, + bool passive, bool durable, bool exclusive, + bool autoDelete, bool nowait, + const qpid::framing::FieldTable& arguments); + void bind(const framing::MethodContext& context, uint16_t ticket, const std::string& queue, + const std::string& exchange, const std::string& routingKey, + bool nowait, const qpid::framing::FieldTable& arguments); + void unbind(const framing::MethodContext& context, + uint16_t ticket, + const std::string& queue, + const std::string& exchange, + const std::string& routingKey, + const qpid::framing::FieldTable& arguments ); + void purge(const framing::MethodContext& context, uint16_t ticket, const std::string& queue, + bool nowait); + void delete_(const framing::MethodContext& context, uint16_t ticket, const std::string& queue, + bool ifUnused, bool ifEmpty, + bool nowait); + }; + + class BasicHandlerImpl : + public BasicHandler, + public HandlerImpl + { + public: + BasicHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} + + void qos(const framing::MethodContext& context, uint32_t prefetchSize, + uint16_t prefetchCount, bool global); + void consume( + const framing::MethodContext& context, uint16_t ticket, const std::string& queue, + const std::string& consumerTag, bool noLocal, bool noAck, + bool exclusive, bool nowait, + const qpid::framing::FieldTable& fields); + void cancel(const framing::MethodContext& context, const std::string& consumerTag, + bool nowait); + void publish(const framing::MethodContext& context, uint16_t ticket, + const std::string& exchange, const std::string& routingKey, + bool mandatory, bool immediate); + void get(const framing::MethodContext& context, uint16_t ticket, const std::string& queue, + bool noAck); + void ack(const framing::MethodContext& context, uint64_t deliveryTag, bool multiple); + void reject(const framing::MethodContext& context, uint64_t deliveryTag, bool requeue); + void recover(const framing::MethodContext& context, bool requeue); + }; + + class TxHandlerImpl : + public TxHandler, + public HandlerImpl + { + public: + TxHandlerImpl(BrokerAdapter& parent) : HandlerImplType(parent) {} + + void select(const framing::MethodContext& context); + void commit(const framing::MethodContext& context); + void rollback(const framing::MethodContext& context); + }; + + Connection& connection; + BasicHandlerImpl basicHandler; + ChannelHandlerImpl channelHandler; + ConnectionHandlerImpl connectionHandler; + ExchangeHandlerImpl exchangeHandler; + MessageHandlerImpl messageHandler; + QueueHandlerImpl queueHandler; + TxHandlerImpl txHandler; + +}; +}} // namespace qpid::broker + + + +#endif /*!_broker_BrokerAdapter_h*/ diff --git a/cpp/src/qpid/broker/BrokerChannel.cpp b/cpp/src/qpid/broker/BrokerChannel.cpp new file mode 100644 index 0000000000..afbbed5c29 --- /dev/null +++ b/cpp/src/qpid/broker/BrokerChannel.cpp @@ -0,0 +1,347 @@ +/* + * + * 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 + +#include +#include +#include +#include + +#include + +#include "BrokerChannel.h" +#include "qpid/framing/ChannelAdapter.h" +#include "qpid/QpidError.h" +#include "DeliverableMessage.h" +#include "BrokerQueue.h" +#include "BrokerMessage.h" +#include "MessageStore.h" +#include "TxAck.h" +#include "TxPublish.h" +#include "BrokerAdapter.h" +#include "Connection.h" + +using std::mem_fun_ref; +using std::bind2nd; +using namespace qpid::broker; +using namespace qpid::framing; +using namespace qpid::sys; + + +Channel::Channel( + Connection& con, ChannelId id, + uint32_t _framesize, MessageStore* const _store, + uint64_t _stagingThreshold +) : + ChannelAdapter(id, &con.getOutput(), con.getVersion()), + connection(con), + currentDeliveryTag(1), + transactional(false), + prefetchSize(0), + prefetchCount(0), + framesize(_framesize), + tagGenerator("sgen"), + accumulatedAck(0), + store(_store), + messageBuilder(this, _store, _stagingThreshold), + opened(id == 0),//channel 0 is automatically open, other must be explicitly opened + adapter(new BrokerAdapter(*this, con, con.broker)) +{ + outstanding.reset(); +} + +Channel::~Channel(){ + close(); +} + +bool Channel::exists(const string& consumerTag){ + return consumers.find(consumerTag) != consumers.end(); +} + +// TODO aconway 2007-02-12: Why is connection token passed in instead +// of using the channel's parent connection? +void Channel::consume(string& tagInOut, Queue::shared_ptr queue, bool acks, + bool exclusive, ConnectionToken* const connection, + const FieldTable*) +{ + if(tagInOut.empty()) + tagInOut = tagGenerator.generate(); + std::auto_ptr c( + new ConsumerImpl(this, tagInOut, queue, connection, acks)); + queue->consume(c.get(), exclusive);//may throw exception + consumers.insert(tagInOut, c.release()); +} + +void Channel::cancel(const string& tag){ + // consumers is a ptr_map so erase will delete the consumer + // which will call cancel. + ConsumerImplMap::iterator i = consumers.find(tag); + if (i != consumers.end()) + consumers.erase(i); +} + +void Channel::close(){ + opened = false; + consumers.clear(); + recover(true); +} + +void Channel::begin(){ + transactional = true; +} + +void Channel::commit(){ + TxOp::shared_ptr txAck(new TxAck(accumulatedAck, unacked)); + txBuffer.enlist(txAck); + if(txBuffer.prepare(store)){ + txBuffer.commit(); + } + accumulatedAck.clear(); +} + +void Channel::rollback(){ + txBuffer.rollback(); + accumulatedAck.clear(); +} + +void Channel::deliver( + Message::shared_ptr& msg, const string& consumerTag, + Queue::shared_ptr& queue, bool ackExpected) +{ + Mutex::ScopedLock locker(deliveryLock); + + // Key the delivered messages to the id of the request in which they're sent + uint64_t deliveryTag = getNextSendRequestId(); + + if(ackExpected){ + unacked.push_back(DeliveryRecord(msg, queue, consumerTag, deliveryTag)); + outstanding.size += msg->contentSize(); + outstanding.count++; + } + //send deliver method, header and content(s) + msg->deliver(*this, consumerTag, deliveryTag, framesize); +} + +bool Channel::checkPrefetch(Message::shared_ptr& msg){ + Mutex::ScopedLock locker(deliveryLock); + bool countOk = !prefetchCount || prefetchCount > unacked.size(); + bool sizeOk = !prefetchSize || prefetchSize > msg->contentSize() + outstanding.size || unacked.empty(); + return countOk && sizeOk; +} + +Channel::ConsumerImpl::ConsumerImpl(Channel* _parent, const string& _tag, + Queue::shared_ptr _queue, + ConnectionToken* const _connection, bool ack +) : parent(_parent), tag(_tag), queue(_queue), connection(_connection), + ackExpected(ack), blocked(false) {} + +bool Channel::ConsumerImpl::deliver(Message::shared_ptr& msg){ + if(!connection || connection != msg->getPublisher()){//check for no_local + if(ackExpected && !parent->checkPrefetch(msg)){ + blocked = true; + }else{ + blocked = false; + parent->deliver(msg, tag, queue, ackExpected); + return true; + } + } + return false; +} + +Channel::ConsumerImpl::~ConsumerImpl() { + cancel(); +} + +void Channel::ConsumerImpl::cancel(){ + if(queue) + queue->cancel(this); +} + +void Channel::ConsumerImpl::requestDispatch(){ + if(blocked) + queue->dispatch(); +} + +void Channel::handleInlineTransfer(Message::shared_ptr msg) +{ + Exchange::shared_ptr exchange = + connection.broker.getExchanges().get(msg->getExchange()); + if(transactional){ + TxPublish* deliverable(new TxPublish(msg)); + TxOp::shared_ptr op(deliverable); + exchange->route( + *deliverable, msg->getRoutingKey(), + &(msg->getApplicationHeaders())); + txBuffer.enlist(op); + }else{ + DeliverableMessage deliverable(msg); + exchange->route( + deliverable, msg->getRoutingKey(), + &(msg->getApplicationHeaders())); + } +} + +void Channel::handlePublish(Message* _message){ + Message::shared_ptr message(_message); + messageBuilder.initialise(message); +} + +void Channel::handleHeader(AMQHeaderBody::shared_ptr header){ + messageBuilder.setHeader(header); + //at this point, decide based on the size of the message whether we want + //to stage it by saving content directly to disk as it arrives +} + +void Channel::handleContent(AMQContentBody::shared_ptr content){ + messageBuilder.addContent(content); +} + +void Channel::handleHeartbeat(boost::shared_ptr) { + // TODO aconway 2007-01-17: Implement heartbeating. +} + +void Channel::complete(Message::shared_ptr msg) { + Exchange::shared_ptr exchange = + connection.broker.getExchanges().get(msg->getExchange()); + assert(exchange.get()); + if(transactional) { + TxPublish* deliverable(new TxPublish(msg)); + TxOp::shared_ptr op(deliverable); + exchange->route(*deliverable, msg->getRoutingKey(), + &(msg->getApplicationHeaders())); + txBuffer.enlist(op); + } else { + DeliverableMessage deliverable(msg); + exchange->route(deliverable, msg->getRoutingKey(), + &(msg->getApplicationHeaders())); + } +} + +void Channel::ack(){ + ack(getFirstAckRequest(), getLastAckRequest()); +} + +// Used by Basic +void Channel::ack(uint64_t deliveryTag, bool multiple){ + if (multiple) + ack(0, deliveryTag); + else + ack(deliveryTag, deliveryTag); +} + +void Channel::ack(uint64_t firstTag, uint64_t lastTag){ + if(transactional){ + accumulatedAck.update(firstTag, lastTag); + + //TODO: I think the outstanding prefetch size & count should be updated at this point... + //TODO: ...this may then necessitate dispatching to consumers + }else{ + Mutex::ScopedLock locker(deliveryLock);//need to synchronize with possible concurrent delivery + + ack_iterator i = find_if(unacked.begin(), unacked.end(), bind2nd(mem_fun_ref(&DeliveryRecord::matches), lastTag)); + ack_iterator j = (firstTag == 0) ? + unacked.begin() : + find_if(unacked.begin(), unacked.end(), bind2nd(mem_fun_ref(&DeliveryRecord::matches), firstTag)); + + if(i == unacked.end()){ + throw ConnectionException(530, "Received ack for unrecognised delivery tag"); + }else if(i!=j){ + ack_iterator end = ++i; + for_each(j, end, bind2nd(mem_fun_ref(&DeliveryRecord::discard), 0)); + unacked.erase(unacked.begin(), end); + + //recalculate the prefetch: + outstanding.reset(); + for_each(unacked.begin(), unacked.end(), bind2nd(mem_fun_ref(&DeliveryRecord::addTo), &outstanding)); + }else{ + i->discard(); + i->subtractFrom(&outstanding); + unacked.erase(i); + } + + //if the prefetch limit had previously been reached, there may + //be messages that can be now be delivered + std::for_each(consumers.begin(), consumers.end(), + boost::bind(&ConsumerImpl::requestDispatch, _1)); + } +} + +void Channel::recover(bool requeue){ + Mutex::ScopedLock locker(deliveryLock);//need to synchronize with possible concurrent delivery + + if(requeue){ + outstanding.reset(); + std::list copy = unacked; + unacked.clear(); + for_each(copy.begin(), copy.end(), mem_fun_ref(&DeliveryRecord::requeue)); + }else{ + for_each(unacked.begin(), unacked.end(), bind2nd(mem_fun_ref(&DeliveryRecord::redeliver), this)); + } +} + +bool Channel::get(Queue::shared_ptr queue, const string& destination, bool ackExpected){ + Message::shared_ptr msg = queue->dequeue(); + if(msg){ + Mutex::ScopedLock locker(deliveryLock); + uint64_t myDeliveryTag = getNextSendRequestId(); + msg->sendGetOk(MethodContext(this, msg->getRespondTo()), + destination, + queue->getMessageCount() + 1, myDeliveryTag, + framesize); + if(ackExpected){ + unacked.push_back(DeliveryRecord(msg, queue, myDeliveryTag)); + } + return true; + }else{ + return false; + } +} + +void Channel::deliver(Message::shared_ptr& msg, const string& consumerTag, + uint64_t deliveryTag) +{ + msg->deliver(*this, consumerTag, deliveryTag, framesize); +} + +void Channel::handleMethodInContext( + boost::shared_ptr method, + const MethodContext& context +) +{ + try{ + if(getId() != 0 && !method->isA() && !isOpen()) { + std::stringstream out; + out << "Attempt to use unopened channel: " << getId(); + throw ConnectionException(504, out.str()); + } else { + method->invoke(*adapter, context); + } + }catch(ChannelException& e){ + adapter->getProxy().getChannel().close( + e.code, e.toString(), + method->amqpClassId(), method->amqpMethodId()); + connection.closeChannel(getId()); + }catch(ConnectionException& e){ + connection.close(e.code, e.toString(), method->amqpClassId(), method->amqpMethodId()); + }catch(std::exception& e){ + connection.close(541/*internal error*/, e.what(), method->amqpClassId(), method->amqpMethodId()); + } +} diff --git a/cpp/src/qpid/broker/BrokerChannel.h b/cpp/src/qpid/broker/BrokerChannel.h new file mode 100644 index 0000000000..9515485d37 --- /dev/null +++ b/cpp/src/qpid/broker/BrokerChannel.h @@ -0,0 +1,159 @@ +#ifndef _broker_BrokerChannel_h +#define _broker_BrokerChannel_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 + +#include +#include +#include + +#include "AccumulatedAck.h" +#include "Consumer.h" +#include "DeliveryRecord.h" +#include "MessageBuilder.h" +#include "NameGenerator.h" +#include "Prefetch.h" +#include "TxBuffer.h" +#include "qpid/framing/ChannelAdapter.h" +#include "qpid/framing/ChannelOpenBody.h" +#include "CompletionHandler.h" + +namespace qpid { +namespace broker { + +class ConnectionToken; +class Connection; +class Queue; +class BrokerAdapter; + +using framing::string; + +/** + * Maintains state for an AMQP channel. Handles incoming and + * outgoing messages for that channel. + */ +class Channel : public framing::ChannelAdapter, + public CompletionHandler +{ + class ConsumerImpl : public Consumer + { + Channel* parent; + const string tag; + Queue::shared_ptr queue; + ConnectionToken* const connection; + const bool ackExpected; + bool blocked; + + public: + ConsumerImpl(Channel* parent, const string& tag, + Queue::shared_ptr queue, + ConnectionToken* const connection, bool ack); + ~ConsumerImpl(); + virtual bool deliver(Message::shared_ptr& msg); + void cancel(); + void requestDispatch(); + }; + + typedef boost::ptr_map ConsumerImplMap; + + Connection& connection; + uint64_t currentDeliveryTag; + Queue::shared_ptr defaultQueue; + bool transactional; + ConsumerImplMap consumers; + uint32_t prefetchSize; + uint16_t prefetchCount; + Prefetch outstanding; + uint32_t framesize; + NameGenerator tagGenerator; + std::list unacked; + sys::Mutex deliveryLock; + TxBuffer txBuffer; + AccumulatedAck accumulatedAck; + MessageStore* const store; + MessageBuilder messageBuilder;//builder for in-progress message + bool opened; + boost::scoped_ptr adapter; + + // completion handler for MessageBuilder + void complete(Message::shared_ptr msg); + + void deliver(Message::shared_ptr& msg, const string& tag, + Queue::shared_ptr& queue, bool ackExpected); + bool checkPrefetch(Message::shared_ptr& msg); + + public: + Channel(Connection& parent, + framing::ChannelId id, + uint32_t framesize, + MessageStore* const _store = 0, + uint64_t stagingThreshold = 0); + + ~Channel(); + + bool isOpen() const { return opened; } + BrokerAdapter& getAdatper() { return *adapter; } + + void open() { opened = true; } + void setDefaultQueue(Queue::shared_ptr queue){ defaultQueue = queue; } + Queue::shared_ptr getDefaultQueue() const { return defaultQueue; } + uint32_t setPrefetchSize(uint32_t size){ return prefetchSize = size; } + uint16_t setPrefetchCount(uint16_t n){ return prefetchCount = n; } + + bool exists(const string& consumerTag); + + /** + *@param tagInOut - if empty it is updated with the generated token. + */ + void consume(string& tagInOut, Queue::shared_ptr queue, bool acks, + bool exclusive, ConnectionToken* const connection = 0, + const framing::FieldTable* = 0); + void cancel(const string& tag); + bool get(Queue::shared_ptr queue, const std::string& destination, bool ackExpected); + void begin(); + void close(); + void commit(); + void rollback(); + void ack(); + void ack(uint64_t deliveryTag, bool multiple); + void ack(uint64_t deliveryTag, uint64_t endTag); + void recover(bool requeue); + void deliver(Message::shared_ptr& msg, const string& consumerTag, uint64_t deliveryTag); + void handlePublish(Message* msg); + void handleHeader(boost::shared_ptr); + void handleContent(boost::shared_ptr); + void handleHeartbeat(boost::shared_ptr); + + void handleInlineTransfer(Message::shared_ptr msg); + + // For ChannelAdapter + void handleMethodInContext( + boost::shared_ptr method, + const framing::MethodContext& context); +}; + +}} // namespace broker + + +#endif /*!_broker_BrokerChannel_h*/ diff --git a/cpp/src/qpid/broker/BrokerExchange.h b/cpp/src/qpid/broker/BrokerExchange.h new file mode 100644 index 0000000000..d4877a5110 --- /dev/null +++ b/cpp/src/qpid/broker/BrokerExchange.h @@ -0,0 +1,51 @@ +#ifndef _broker_BrokerExchange_h +#define _broker_BrokerExchange_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 +#include "Deliverable.h" +#include "BrokerQueue.h" +#include "qpid/framing/FieldTable.h" + +namespace qpid { + namespace broker { + using std::string; + + class Exchange{ + const string name; + public: + typedef boost::shared_ptr shared_ptr; + + explicit Exchange(const string& _name) : name(_name){} + virtual ~Exchange(){} + string getName() { return name; } + virtual string getType() = 0; + virtual void bind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args) = 0; + virtual void unbind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args) = 0; + virtual void route(Deliverable& msg, const string& routingKey, const qpid::framing::FieldTable* args) = 0; + }; + } +} + + +#endif /*!_broker_BrokerExchange_h*/ diff --git a/cpp/src/qpid/broker/BrokerMessage.cpp b/cpp/src/qpid/broker/BrokerMessage.cpp new file mode 100644 index 0000000000..ce21a2cca4 --- /dev/null +++ b/cpp/src/qpid/broker/BrokerMessage.cpp @@ -0,0 +1,244 @@ +/* + * + * 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 + +#include "BrokerMessage.h" +#include + +#include "InMemoryContent.h" +#include "LazyLoadedContent.h" +#include "MessageStore.h" +#include "qpid/framing/BasicDeliverBody.h" +#include "qpid/framing/BasicGetOkBody.h" +#include "qpid/framing/AMQContentBody.h" +#include "qpid/framing/AMQHeaderBody.h" +#include "qpid/framing/AMQMethodBody.h" +#include "qpid/framing/AMQFrame.h" +#include "qpid/framing/ChannelAdapter.h" +#include "RecoveryManagerImpl.h" + +using namespace boost; +using namespace qpid::broker; +using namespace qpid::framing; +using namespace qpid::sys; + +BasicMessage::BasicMessage( + const ConnectionToken* const _publisher, + const string& _exchange, const string& _routingKey, + bool _mandatory, bool _immediate, framing::AMQMethodBody::shared_ptr respondTo +) : + Message(_publisher, _exchange, _routingKey, _mandatory, + _immediate, respondTo), + size(0) +{} + +// For tests only. +BasicMessage::BasicMessage() : size(0) +{} + +BasicMessage::~BasicMessage(){} + +void BasicMessage::setHeader(AMQHeaderBody::shared_ptr _header){ + this->header = _header; +} + +void BasicMessage::addContent(AMQContentBody::shared_ptr data){ + if (!content.get()) { + content = std::auto_ptr(new InMemoryContent()); + } + content->add(data); + size += data->size(); +} + +bool BasicMessage::isComplete(){ + return header.get() && (header->getContentSize() == contentSize()); +} + +void BasicMessage::deliver(ChannelAdapter& channel, + const string& consumerTag, uint64_t deliveryTag, + uint32_t framesize) +{ + // CCT -- TODO - Update code generator to take pointer/ not + // instance to avoid extra contruction + channel.send( + new BasicDeliverBody( + channel.getVersion(), consumerTag, deliveryTag, + getRedelivered(), getExchange(), getRoutingKey())); + sendContent(channel, framesize); +} + +void BasicMessage::sendGetOk(const MethodContext& context, + const std::string& /*destination*/, + uint32_t messageCount, + uint64_t deliveryTag, + uint32_t framesize) +{ + // CCT -- TODO - Update code generator to take pointer/ not + // instance to avoid extra contruction + context.channel->send( + new BasicGetOkBody( + context.channel->getVersion(), + context.methodBody->getRequestId(), + deliveryTag, getRedelivered(), getExchange(), + getRoutingKey(), messageCount)); + sendContent(*context.channel, framesize); +} + +void BasicMessage::sendContent( + ChannelAdapter& channel, uint32_t framesize) +{ + channel.send(header); + Mutex::ScopedLock locker(contentLock); + if (content.get()) + content->send(channel, framesize); +} + +BasicHeaderProperties* BasicMessage::getHeaderProperties(){ + return boost::polymorphic_downcast( + header->getProperties()); +} + +const FieldTable& BasicMessage::getApplicationHeaders(){ + return getHeaderProperties()->getHeaders(); +} + +bool BasicMessage::isPersistent() +{ + if(!header) return false; + BasicHeaderProperties* props = getHeaderProperties(); + return props && props->getDeliveryMode() == PERSISTENT; +} + +void BasicMessage::decode(Buffer& buffer, bool headersOnly, uint32_t contentChunkSize) +{ + decodeHeader(buffer); + if (!headersOnly) decodeContent(buffer, contentChunkSize); +} + +void BasicMessage::decodeHeader(Buffer& buffer) +{ + //don't care about the type here, but want encode/decode to be symmetric + RecoveryManagerImpl::decodeMessageType(buffer); + + string exchange; + string routingKey; + + buffer.getShortString(exchange); + buffer.getShortString(routingKey); + setRouting(exchange, routingKey); + + uint32_t headerSize = buffer.getLong(); + AMQHeaderBody::shared_ptr headerBody(new AMQHeaderBody()); + headerBody->decode(buffer, headerSize); + setHeader(headerBody); +} + +void BasicMessage::decodeContent(Buffer& buffer, uint32_t chunkSize) +{ + uint64_t expected = expectedContentSize(); + if (expected != buffer.available()) { + std::cout << "WARN: Expected " << expectedContentSize() << " bytes, got " << buffer.available() << std::endl; + throw Exception("Cannot decode content, buffer not large enough."); + } + + if (!chunkSize || chunkSize > expected) { + chunkSize = expected; + } + + uint64_t total = 0; + while (total < expectedContentSize()) { + uint64_t remaining = expected - total; + AMQContentBody::shared_ptr contentBody(new AMQContentBody()); + contentBody->decode(buffer, remaining < chunkSize ? remaining : chunkSize); + addContent(contentBody); + total += chunkSize; + } +} + +void BasicMessage::encode(Buffer& buffer) const +{ + encodeHeader(buffer); + encodeContent(buffer); +} + +void BasicMessage::encodeHeader(Buffer& buffer) const +{ + RecoveryManagerImpl::encodeMessageType(*this, buffer); + buffer.putShortString(getExchange()); + buffer.putShortString(getRoutingKey()); + buffer.putLong(header->size()); + header->encode(buffer); +} + +void BasicMessage::encodeContent(Buffer& buffer) const +{ + Mutex::ScopedLock locker(contentLock); + if (content.get()) content->encode(buffer); +} + +uint32_t BasicMessage::encodedSize() const +{ + return encodedHeaderSize() + encodedContentSize(); +} + +uint32_t BasicMessage::encodedContentSize() const +{ + Mutex::ScopedLock locker(contentLock); + return content.get() ? content->size() : 0; +} + +uint32_t BasicMessage::encodedHeaderSize() const +{ + return RecoveryManagerImpl::encodedMessageTypeSize() + +getExchange().size() + 1 + + getRoutingKey().size() + 1 + + header->size() + 4;//4 extra bytes for size +} + +uint64_t BasicMessage::expectedContentSize() +{ + return header.get() ? header->getContentSize() : 0; +} + +void BasicMessage::releaseContent(MessageStore* store) +{ + Mutex::ScopedLock locker(contentLock); + if (!isPersistent() && getPersistenceId() == 0) { + store->stage(*this); + } + if (!content.get() || content->size() > 0) { + //set content to lazy loading mode (but only if there is + //stored content): + + //Note: the LazyLoadedContent instance contains a raw pointer + //to the message, however it is then set as a member of that + //message so its lifetime is guaranteed to be no longer than + //that of the message itself + content = std::auto_ptr( + new LazyLoadedContent(store, this, expectedContentSize())); + } +} + +void BasicMessage::setContent(std::auto_ptr& _content) +{ + Mutex::ScopedLock locker(contentLock); + content = _content; +} diff --git a/cpp/src/qpid/broker/BrokerMessage.h b/cpp/src/qpid/broker/BrokerMessage.h new file mode 100644 index 0000000000..aee5092dab --- /dev/null +++ b/cpp/src/qpid/broker/BrokerMessage.h @@ -0,0 +1,137 @@ +#ifndef _broker_BrokerMessage_h +#define _broker_BrokerMessage_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 +#include + +#include "BrokerMessageBase.h" +#include "qpid/framing/BasicHeaderProperties.h" +#include "ConnectionToken.h" +#include "Content.h" +#include "qpid/sys/Mutex.h" +#include "TxBuffer.h" + +namespace qpid { + +namespace framing { +class MethodContext; +class ChannelAdapter; +class AMQHeaderBody; +} + +namespace broker { + +class MessageStore; +using framing::string; + +/** + * Represents an AMQP message, i.e. a header body, a list of + * content bodies and some details about the publication + * request. + */ +class BasicMessage : public Message { + boost::shared_ptr header; + std::auto_ptr content; + mutable sys::Mutex contentLock; + uint64_t size; + + void sendContent(framing::ChannelAdapter&, uint32_t framesize); + + public: + typedef boost::shared_ptr shared_ptr; + + BasicMessage(const ConnectionToken* const publisher, + const string& exchange, const string& routingKey, + bool mandatory, bool immediate, + boost::shared_ptr respondTo); + BasicMessage(); + ~BasicMessage(); + void setHeader(boost::shared_ptr header); + void addContent(framing::AMQContentBody::shared_ptr data); + bool isComplete(); + + void deliver(framing::ChannelAdapter&, + const string& consumerTag, + uint64_t deliveryTag, + uint32_t framesize); + + void sendGetOk(const framing::MethodContext&, + const std::string& destination, + uint32_t messageCount, + uint64_t deliveryTag, + uint32_t framesize); + + framing::BasicHeaderProperties* getHeaderProperties(); + const framing::FieldTable& getApplicationHeaders(); + bool isPersistent(); + uint64_t contentSize() const { return size; } + + void decode(framing::Buffer& buffer, bool headersOnly = false, + uint32_t contentChunkSize = 0); + void decodeHeader(framing::Buffer& buffer); + void decodeContent(framing::Buffer& buffer, uint32_t contentChunkSize = 0); + + void encode(framing::Buffer& buffer) const; + void encodeHeader(framing::Buffer& buffer) const; + void encodeContent(framing::Buffer& buffer) const; + /** + * @returns the size of the buffer needed to encode this + * message in its entirety + */ + uint32_t encodedSize() const; + /** + * @returns the size of the buffer needed to encode the + * 'header' of this message (not just the header frame, + * but other meta data e.g.routing key and exchange) + */ + uint32_t encodedHeaderSize() const; + /** + * @returns the size of the buffer needed to encode the + * (possibly partial) content held by this message + */ + uint32_t encodedContentSize() const; + /** + * Releases the in-memory content data held by this + * message. Must pass in a store from which the data can + * be reloaded. + */ + void releaseContent(MessageStore* store); + /** + * If headers have been received, returns the expected + * content size else returns 0. + */ + uint64_t expectedContentSize(); + /** + * Sets the 'content' implementation of this message (the + * message controls the lifecycle of the content instance + * it uses). + */ + void setContent(std::auto_ptr& content); +}; + +} +} + + +#endif /*!_broker_BrokerMessage_h*/ diff --git a/cpp/src/qpid/broker/BrokerMessageBase.h b/cpp/src/qpid/broker/BrokerMessageBase.h new file mode 100644 index 0000000000..a254986fd9 --- /dev/null +++ b/cpp/src/qpid/broker/BrokerMessageBase.h @@ -0,0 +1,182 @@ +#ifndef _broker_BrokerMessageBase_h +#define _broker_BrokerMessageBase_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 +#include +#include "Content.h" +#include "PersistableMessage.h" +#include "qpid/framing/amqp_types.h" + +namespace qpid { + +namespace framing { +class MethodContext; +class ChannelAdapter; +class BasicHeaderProperties; +class FieldTable; +class AMQMethodBody; +class AMQContentBody; +class AMQHeaderBody; +} + + +namespace broker { +class ConnectionToken; +class MessageStore; + +/** + * Base class for all types of internal broker messages + * abstracting away the operations + * TODO; AMS: for the moment this is mostly a placeholder + */ +class Message : public PersistableMessage{ + public: + typedef boost::shared_ptr shared_ptr; + typedef boost::shared_ptr AMQMethodBodyPtr; + + + Message(const ConnectionToken* publisher_, + const std::string& _exchange, + const std::string& _routingKey, + bool _mandatory, bool _immediate, + AMQMethodBodyPtr respondTo_) : + publisher(publisher_), + exchange(_exchange), + routingKey(_routingKey), + mandatory(_mandatory), + immediate(_immediate), + persistenceId(0), + redelivered(false), + respondTo(respondTo_) + {} + + Message() : + mandatory(false), + immediate(false), + persistenceId(0), + redelivered(false) + {} + + virtual ~Message() {}; + + // Accessors + const std::string& getRoutingKey() const { return routingKey; } + const std::string& getExchange() const { return exchange; } + uint64_t getPersistenceId() const { return persistenceId; } + bool getRedelivered() const { return redelivered; } + AMQMethodBodyPtr getRespondTo() const { return respondTo; } + + void setRouting(const std::string& _exchange, const std::string& _routingKey) + { exchange = _exchange; routingKey = _routingKey; } + void setPersistenceId(uint64_t _persistenceId) const { persistenceId = _persistenceId; } + void redeliver() { redelivered = true; } + + /** + * Used to deliver the message from the queue + */ + virtual void deliver(framing::ChannelAdapter& channel, + const std::string& consumerTag, + uint64_t deliveryTag, + uint32_t framesize) = 0; + /** + * Used to return a message in response to a get from a queue + */ + virtual void sendGetOk(const framing::MethodContext& context, + const std::string& destination, + uint32_t messageCount, + uint64_t deliveryTag, + uint32_t framesize) = 0; + + virtual bool isComplete() = 0; + + virtual uint64_t contentSize() const = 0; + virtual framing::BasicHeaderProperties* getHeaderProperties() = 0; + virtual const framing::FieldTable& getApplicationHeaders() = 0; + virtual bool isPersistent() = 0; + virtual const ConnectionToken* getPublisher() const { + return publisher; + } + + virtual void encode(framing::Buffer& buffer) const = 0; + virtual void encodeHeader(framing::Buffer& buffer) const = 0; + + /** + * @returns the size of the buffer needed to encode this + * message in its entirety + */ + virtual uint32_t encodedSize() const = 0; + /** + * @returns the size of the buffer needed to encode the + * 'header' of this message (not just the header frame, + * but other meta data e.g.routing key and exchange) + */ + virtual uint32_t encodedHeaderSize() const = 0; + /** + * @returns the size of the buffer needed to encode the + * (possibly partial) content held by this message + */ + virtual uint32_t encodedContentSize() const = 0; + /** + * If headers have been received, returns the expected + * content size else returns 0. + */ + virtual uint64_t expectedContentSize() = 0; + + virtual void decodeHeader(framing::Buffer& buffer) = 0; + virtual void decodeContent(framing::Buffer& buffer, uint32_t contentChunkSize = 0) = 0; + + static shared_ptr decode(framing::Buffer& buffer); + + // TODO: AMS 29/1/2007 Don't think these are really part of base class + + /** + * Sets the 'content' implementation of this message (the + * message controls the lifecycle of the content instance + * it uses). + */ + virtual void setContent(std::auto_ptr& /*content*/) {}; + virtual void setHeader(boost::shared_ptr) {}; + virtual void addContent(boost::shared_ptr) {}; + /** + * Releases the in-memory content data held by this + * message. Must pass in a store from which the data can + * be reloaded. + */ + virtual void releaseContent(MessageStore* /*store*/) {}; + + private: + const ConnectionToken* publisher; + std::string exchange; + std::string routingKey; + const bool mandatory; + const bool immediate; + mutable uint64_t persistenceId; + bool redelivered; + AMQMethodBodyPtr respondTo; +}; + +}} + + +#endif /*!_broker_BrokerMessage_h*/ diff --git a/cpp/src/qpid/broker/BrokerMessageMessage.cpp b/cpp/src/qpid/broker/BrokerMessageMessage.cpp new file mode 100644 index 0000000000..17ccdc03b8 --- /dev/null +++ b/cpp/src/qpid/broker/BrokerMessageMessage.cpp @@ -0,0 +1,320 @@ +/* + * + * 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/QpidError.h" +#include "BrokerMessageMessage.h" +#include "qpid/framing/ChannelAdapter.h" +#include "qpid/framing/MessageTransferBody.h" +#include "qpid/framing/MessageOpenBody.h" +#include "qpid/framing/MessageCloseBody.h" +#include "qpid/framing/MessageAppendBody.h" +#include "Reference.h" +#include "qpid/framing/AMQFrame.h" +#include "qpid/framing/FieldTable.h" +#include "qpid/framing/BasicHeaderProperties.h" +#include "RecoveryManagerImpl.h" + +#include + +using namespace std; +using namespace qpid::framing; + +namespace qpid { +namespace broker { + +MessageMessage::MessageMessage( + ConnectionToken* publisher, RequestId requestId_, TransferPtr transfer_ +) : Message(publisher, transfer_->getDestination(), + transfer_->getRoutingKey(), + transfer_->getMandatory(), + transfer_->getImmediate(), + transfer_), + requestId(requestId_), + transfer(transfer_) +{ + assert(transfer->getBody().isInline()); +} + +MessageMessage::MessageMessage( + ConnectionToken* publisher, RequestId requestId_, TransferPtr transfer_, + ReferencePtr reference_ +) : Message(publisher, transfer_->getDestination(), + transfer_->getRoutingKey(), + transfer_->getMandatory(), + transfer_->getImmediate(), + transfer_), + requestId(requestId_), + transfer(transfer_), + reference(reference_) +{ + assert(!transfer->getBody().isInline()); + assert(reference_); +} + +/** + * Currently used by message store impls to recover messages + */ +MessageMessage::MessageMessage() : transfer(new MessageTransferBody(qpid::framing::highestProtocolVersion)) {} + +// TODO: astitcher 1-Mar-2007: This code desperately needs better factoring +void MessageMessage::transferMessage( + framing::ChannelAdapter& channel, + const std::string& consumerTag, + uint32_t framesize) +{ + const framing::Content& body = transfer->getBody(); + // Send any reference data + ReferencePtr ref= getReference(); + if (ref){ + + // Open + channel.send(new MessageOpenBody(channel.getVersion(), ref->getId())); + // Appends + for(Reference::Appends::const_iterator a = ref->getAppends().begin(); + a != ref->getAppends().end(); + ++a) { + uint32_t sizeleft = (*a)->size(); + const string& content = (*a)->getBytes(); + // Calculate overhead bytes + // Assume that the overhead is constant as the reference name doesn't change + uint32_t overhead = sizeleft - content.size(); + string::size_type contentStart = 0; + while (sizeleft) { + string::size_type contentSize = sizeleft <= framesize ? sizeleft : framesize-overhead; + channel.send(new MessageAppendBody(channel.getVersion(), ref->getId(), + string(content, contentStart, contentSize))); + sizeleft -= contentSize; + contentStart += contentSize; + } + } + } + + // The transfer + if ( transfer->size()<=framesize ) { + channel.send( + new MessageTransferBody(channel.getVersion(), + transfer->getTicket(), + consumerTag, + getRedelivered(), + transfer->getImmediate(), + transfer->getTtl(), + transfer->getPriority(), + transfer->getTimestamp(), + transfer->getDeliveryMode(), + transfer->getExpiration(), + getExchange(), + getRoutingKey(), + transfer->getMessageId(), + transfer->getCorrelationId(), + transfer->getReplyTo(), + transfer->getContentType(), + transfer->getContentEncoding(), + transfer->getUserId(), + transfer->getAppId(), + transfer->getTransactionId(), + transfer->getSecurityToken(), + transfer->getApplicationHeaders(), + body, + transfer->getMandatory())); + } else { + // Thing to do here is to construct a simple reference message then deliver that instead + // fragmentation will be taken care of in the delivery if necessary; + string content = body.getValue(); + string refname = "dummy"; + TransferPtr newTransfer( + new MessageTransferBody(channel.getVersion(), + transfer->getTicket(), + consumerTag, + getRedelivered(), + transfer->getImmediate(), + transfer->getTtl(), + transfer->getPriority(), + transfer->getTimestamp(), + transfer->getDeliveryMode(), + transfer->getExpiration(), + getExchange(), + getRoutingKey(), + transfer->getMessageId(), + transfer->getCorrelationId(), + transfer->getReplyTo(), + transfer->getContentType(), + transfer->getContentEncoding(), + transfer->getUserId(), + transfer->getAppId(), + transfer->getTransactionId(), + transfer->getSecurityToken(), + transfer->getApplicationHeaders(), + framing::Content(REFERENCE, refname), + transfer->getMandatory())); + ReferencePtr newRef(new Reference(refname)); + Reference::AppendPtr newAppend(new MessageAppendBody(channel.getVersion(), refname, content)); + newRef->append(newAppend); + MessageMessage newMsg(const_cast(getPublisher()), 0, newTransfer, newRef); + newMsg.transferMessage(channel, consumerTag, framesize); + return; + } + // Close any reference data + if (ref) + channel.send(new MessageCloseBody(channel.getVersion(), ref->getId())); +} + +void MessageMessage::deliver( + framing::ChannelAdapter& channel, + const std::string& consumerTag, + uint64_t /*deliveryTag*/, + uint32_t framesize) +{ + transferMessage(channel, consumerTag, framesize); +} + +void MessageMessage::sendGetOk( + const framing::MethodContext& context, + const std::string& destination, + uint32_t /*messageCount*/, + uint64_t /*deliveryTag*/, + uint32_t framesize) +{ + framing::ChannelAdapter* channel = context.channel; + transferMessage(*channel, destination, framesize); +} + +bool MessageMessage::isComplete() +{ + return true; +} + +uint64_t MessageMessage::contentSize() const +{ + if (transfer->getBody().isInline()) + return transfer->getBody().getValue().size(); + else { + assert(getReference()); + return getReference()->getSize(); + } +} + +qpid::framing::BasicHeaderProperties* MessageMessage::getHeaderProperties() +{ + return 0; // FIXME aconway 2007-02-05: +} + +const FieldTable& MessageMessage::getApplicationHeaders() +{ + return transfer->getApplicationHeaders(); +} +bool MessageMessage::isPersistent() +{ + return transfer->getDeliveryMode() == PERSISTENT; +} + +uint32_t MessageMessage::encodedSize() const +{ + return encodedHeaderSize() + encodedContentSize(); +} + +uint32_t MessageMessage::encodedHeaderSize() const +{ + return RecoveryManagerImpl::encodedMessageTypeSize() + transfer->size() - transfer->baseSize(); +} + +uint32_t MessageMessage::encodedContentSize() const +{ + return 0; +} + +uint64_t MessageMessage::expectedContentSize() +{ + return 0; +} + +void MessageMessage::encode(Buffer& buffer) const +{ + encodeHeader(buffer); +} + +void MessageMessage::encodeHeader(Buffer& buffer) const +{ + RecoveryManagerImpl::encodeMessageType(*this, buffer); + if (transfer->getBody().isInline()) { + transfer->encodeContent(buffer); + } else { + assert(getReference()); + string data; + const Reference::Appends& appends = getReference()->getAppends(); + for(Reference::Appends::const_iterator a = appends.begin(); a != appends.end(); ++a) { + data += (*a)->getBytes(); + } + framing::Content body(INLINE, data); + std::auto_ptr copy(copyTransfer(transfer->version, transfer->getDestination(), body)); + copy->encodeContent(buffer); + } +} + +void MessageMessage::decodeHeader(Buffer& buffer) +{ + //don't care about the type here, but want encode/decode to be symmetric + RecoveryManagerImpl::decodeMessageType(buffer); + + transfer->decodeContent(buffer); +} + +void MessageMessage::decodeContent(Buffer& /*buffer*/, uint32_t /*chunkSize*/) +{ +} + + +MessageTransferBody* MessageMessage::copyTransfer(const ProtocolVersion& version, + const string& destination, + const framing::Content& body) const +{ + return new MessageTransferBody(version, + transfer->getTicket(), + destination, + getRedelivered(), + transfer->getImmediate(), + transfer->getTtl(), + transfer->getPriority(), + transfer->getTimestamp(), + transfer->getDeliveryMode(), + transfer->getExpiration(), + getExchange(), + getRoutingKey(), + transfer->getMessageId(), + transfer->getCorrelationId(), + transfer->getReplyTo(), + transfer->getContentType(), + transfer->getContentEncoding(), + transfer->getUserId(), + transfer->getAppId(), + transfer->getTransactionId(), + transfer->getSecurityToken(), + transfer->getApplicationHeaders(), + body, + transfer->getMandatory()); + +} + +MessageMessage::ReferencePtr MessageMessage::getReference() const { + return reference; +} + + +}} // namespace qpid::broker + diff --git a/cpp/src/qpid/broker/BrokerMessageMessage.h b/cpp/src/qpid/broker/BrokerMessageMessage.h new file mode 100644 index 0000000000..e64e08fcef --- /dev/null +++ b/cpp/src/qpid/broker/BrokerMessageMessage.h @@ -0,0 +1,102 @@ +#ifndef _broker_BrokerMessageMessage_h +#define _broker_BrokerMessageMessage_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 "BrokerMessageBase.h" +#include "qpid/framing/MessageTransferBody.h" +#include "qpid/framing/amqp_types.h" +#include +#include + +namespace qpid { + +namespace framing { +class MessageTransferBody; +} + +namespace broker { +class ConnectionToken; +class Reference; + +class MessageMessage: public Message{ + public: + typedef boost::shared_ptr shared_ptr; + typedef boost::shared_ptr TransferPtr; + typedef boost::shared_ptr ReferencePtr; + + MessageMessage(ConnectionToken* publisher, framing::RequestId, TransferPtr transfer); + MessageMessage(ConnectionToken* publisher, framing::RequestId, TransferPtr transfer, ReferencePtr reference); + MessageMessage(); + + // Default destructor okay + + framing::RequestId getRequestId() const {return requestId; } + TransferPtr getTransfer() const { return transfer; } + ReferencePtr getReference() const ; + + void deliver(framing::ChannelAdapter& channel, + const std::string& consumerTag, + uint64_t deliveryTag, + uint32_t framesize); + + void sendGetOk(const framing::MethodContext& context, + const std::string& destination, + uint32_t messageCount, + uint64_t deliveryTag, + uint32_t framesize); + + bool isComplete(); + + uint64_t contentSize() const; + framing::BasicHeaderProperties* getHeaderProperties(); + const framing::FieldTable& getApplicationHeaders(); + bool isPersistent(); + + void encode(framing::Buffer& buffer) const; + void encodeHeader(framing::Buffer& buffer) const; + uint32_t encodedSize() const; + uint32_t encodedHeaderSize() const; + uint32_t encodedContentSize() const; + uint64_t expectedContentSize(); + void decodeHeader(framing::Buffer& buffer); + void decodeContent(framing::Buffer& buffer, uint32_t contentChunkSize = 0); + + private: + void transferMessage( + framing::ChannelAdapter& channel, + const std::string& consumerTag, + uint32_t framesize); + + framing::MessageTransferBody* copyTransfer( + const framing::ProtocolVersion& version, + const std::string& destination, + const framing::Content& body) const; + + framing::RequestId requestId; + const TransferPtr transfer; + const boost::shared_ptr reference; +}; + +}} + + +#endif /*!_broker_BrokerMessage_h*/ diff --git a/cpp/src/qpid/broker/BrokerQueue.cpp b/cpp/src/qpid/broker/BrokerQueue.cpp new file mode 100644 index 0000000000..34c8d79e76 --- /dev/null +++ b/cpp/src/qpid/broker/BrokerQueue.cpp @@ -0,0 +1,284 @@ +/* + * + * 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 + +#include "BrokerQueue.h" +#include "MessageStore.h" +#include "qpid/sys/Monitor.h" +#include "qpid/sys/Time.h" +#include +#include "QueueRegistry.h" + +using namespace qpid::broker; +using namespace qpid::sys; +using namespace qpid::framing; +using boost::format; + +Queue::Queue(const string& _name, uint32_t _autodelete, + MessageStore* const _store, + const ConnectionToken* const _owner) : + + name(_name), + autodelete(_autodelete), + store(_store), + owner(_owner), + queueing(false), + dispatching(false), + next(0), + lastUsed(0), + exclusive(0), + persistenceId(0) +{ + if(autodelete) lastUsed = now()/TIME_MSEC; +} + +Queue::~Queue(){} + +void Queue::deliver(Message::shared_ptr& msg){ + enqueue(0, msg); + process(msg); +} + +void Queue::recover(Message::shared_ptr& msg){ + push(msg); + if (store && msg->expectedContentSize() != msg->encodedContentSize()) { + //content has not been loaded, need to ensure that lazy loading mode is set: + //TODO: find a nicer way to do this + msg->releaseContent(store); + } +} + +void Queue::process(Message::shared_ptr& msg){ + Mutex::ScopedLock locker(lock); + if(queueing || !dispatch(msg)){ + push(msg); + } +} + +bool Queue::dispatch(Message::shared_ptr& msg){ + if(consumers.empty()){ + return false; + }else if(exclusive){ + if(!exclusive->deliver(msg)){ + std::cout << "WARNING: Dropping undeliverable message from queue with exclusive consumer." << std::endl; + } + return true; + }else{ + //deliver to next consumer + next = next % consumers.size(); + Consumer* c = consumers[next]; + int start = next; + while(c){ + next++; + if(c->deliver(msg)) return true; + + next = next % consumers.size(); + c = next == start ? 0 : consumers[next]; + } + return false; + } +} + +bool Queue::startDispatching(){ + Mutex::ScopedLock locker(lock); + if(queueing && !dispatching){ + dispatching = true; + return true; + }else{ + return false; + } +} + +void Queue::dispatch(){ + bool proceed = startDispatching(); + while(proceed){ + Mutex::ScopedLock locker(lock); + if(!messages.empty() && dispatch(messages.front())){ + pop(); + }else{ + dispatching = false; + proceed = false; + queueing = !messages.empty(); + } + } +} + +void Queue::consume(Consumer* c, bool requestExclusive){ + Mutex::ScopedLock locker(lock); + if(exclusive) + throw ChannelException( + 403, format("Queue '%s' has an exclusive consumer." + " No more consumers allowed.") % getName()); + if(requestExclusive) { + if(!consumers.empty()) + throw ChannelException( + 403, format("Queue '%s' already has conumers." + "Exclusive access denied.") %getName()); + exclusive = c; + } + if(autodelete && consumers.empty()) lastUsed = 0; + consumers.push_back(c); +} + +void Queue::cancel(Consumer* c){ + Mutex::ScopedLock locker(lock); + Consumers::iterator i = std::find(consumers.begin(), consumers.end(), c); + if (i != consumers.end()) + consumers.erase(i); + if(autodelete && consumers.empty()) lastUsed = now()*TIME_MSEC; + if(exclusive == c) exclusive = 0; +} + +Message::shared_ptr Queue::dequeue(){ + Mutex::ScopedLock locker(lock); + Message::shared_ptr msg; + if(!messages.empty()){ + msg = messages.front(); + pop(); + } + return msg; +} + +uint32_t Queue::purge(){ + Mutex::ScopedLock locker(lock); + int count = messages.size(); + while(!messages.empty()) pop(); + return count; +} + +void Queue::pop(){ + if (policy.get()) policy->dequeued(messages.front()->contentSize()); + messages.pop(); +} + +void Queue::push(Message::shared_ptr& msg){ + queueing = true; + messages.push(msg); + if (policy.get()) { + policy->enqueued(msg->contentSize()); + if (policy->limitExceeded()) { + msg->releaseContent(store); + } + } +} + +uint32_t Queue::getMessageCount() const{ + Mutex::ScopedLock locker(lock); + return messages.size(); +} + +uint32_t Queue::getConsumerCount() const{ + Mutex::ScopedLock locker(lock); + return consumers.size(); +} + +bool Queue::canAutoDelete() const{ + Mutex::ScopedLock locker(lock); + return lastUsed && (now()*TIME_MSEC - lastUsed > autodelete); +} + +void Queue::enqueue(TransactionContext* ctxt, Message::shared_ptr& msg) +{ + if (msg->isPersistent() && store) { + store->enqueue(ctxt, *msg.get(), *this); + } +} + +void Queue::dequeue(TransactionContext* ctxt, Message::shared_ptr& msg) +{ + if (msg->isPersistent() && store) { + store->dequeue(ctxt, *msg.get(), *this); + } +} + +namespace +{ + const std::string qpidMaxSize("qpid.max_size"); + const std::string qpidMaxCount("qpid.max_count"); +} + +void Queue::create(const FieldTable& _settings) +{ + settings = _settings; + //TODO: hold onto settings and persist them as part of encode + // in fact settings should be passed in on construction + if (store) { + store->create(*this); + } + configure(_settings); +} + +void Queue::configure(const FieldTable& _settings) +{ + std::auto_ptr _policy(new QueuePolicy(_settings)); + if (_policy->getMaxCount() || _policy->getMaxSize()) + setPolicy(_policy); +} + +void Queue::destroy() +{ + if (store) { + store->destroy(*this); + } +} + +void Queue::setPolicy(std::auto_ptr _policy) +{ + policy = _policy; +} + +const QueuePolicy* const Queue::getPolicy() +{ + return policy.get(); +} + +uint64_t Queue::getPersistenceId() const +{ + return persistenceId; +} + +void Queue::setPersistenceId(uint64_t _persistenceId) const +{ + persistenceId = _persistenceId; +} + +void Queue::encode(framing::Buffer& buffer) const +{ + buffer.putShortString(name); + buffer.putFieldTable(settings); +} + +uint32_t Queue::encodedSize() const +{ + return name.size() + 1/*short string size octet*/ + settings.size(); +} + +Queue::shared_ptr Queue::decode(QueueRegistry& queues, framing::Buffer& buffer) +{ + string name; + buffer.getShortString(name); + std::pair result = queues.declare(name, true); + buffer.getFieldTable(result.first->settings); + result.first->configure(result.first->settings); + return result.first; +} + diff --git a/cpp/src/qpid/broker/BrokerQueue.h b/cpp/src/qpid/broker/BrokerQueue.h new file mode 100644 index 0000000000..28f6cfce8f --- /dev/null +++ b/cpp/src/qpid/broker/BrokerQueue.h @@ -0,0 +1,152 @@ +#ifndef _broker_BrokerQueue_h +#define _broker_BrokerQueue_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 +#include +#include +#include +#include "qpid/framing/amqp_types.h" +#include "ConnectionToken.h" +#include "Consumer.h" +#include "BrokerMessage.h" +#include "qpid/framing/FieldTable.h" +#include "qpid/sys/Monitor.h" +#include "PersistableQueue.h" +#include "QueuePolicy.h" + +// TODO aconway 2007-02-06: Use auto_ptr and boost::ptr_vector to +// enforce ownership of Consumers. + +namespace qpid { + namespace broker { + class MessageStore; + class QueueRegistry; + + /** + * Thrown when exclusive access would be violated. + */ + using std::string; + + /** + * The brokers representation of an amqp queue. Messages are + * delivered to a queue from where they can be dispatched to + * registered consumers or be stored until dequeued or until one + * or more consumers registers. + */ + class Queue : public PersistableQueue{ + typedef std::vector Consumers; + typedef std::queue Messages; + + const string name; + const uint32_t autodelete; + MessageStore* const store; + const ConnectionToken* const owner; + Consumers consumers; + Messages messages; + bool queueing; + bool dispatching; + int next; + mutable qpid::sys::Mutex lock; + int64_t lastUsed; + Consumer* exclusive; + mutable uint64_t persistenceId; + framing::FieldTable settings; + std::auto_ptr policy; + + void pop(); + void push(Message::shared_ptr& msg); + bool startDispatching(); + bool dispatch(Message::shared_ptr& msg); + void setPolicy(std::auto_ptr policy); + + public: + + typedef boost::shared_ptr shared_ptr; + + typedef std::vector vector; + + Queue(const string& name, uint32_t autodelete = 0, + MessageStore* const store = 0, + const ConnectionToken* const owner = 0); + ~Queue(); + + void create(const qpid::framing::FieldTable& settings); + void configure(const qpid::framing::FieldTable& settings); + void destroy(); + /** + * Delivers a message to the queue. Will record it as + * enqueued if persistent then process it. + */ + void deliver(Message::shared_ptr& msg); + /** + * Dispatches the messages immediately to a consumer if + * one is available or stores it for later if not. + */ + void process(Message::shared_ptr& msg); + /** + * Used during recovery to add stored messages back to the queue + */ + void recover(Message::shared_ptr& msg); + /** + * Dispatch any queued messages providing there are + * consumers for them. Only one thread can be dispatching + * at any time, but this method (rather than the caller) + * is responsible for ensuring that. + */ + void dispatch(); + void consume(Consumer* c, bool exclusive = false); + void cancel(Consumer* c); + uint32_t purge(); + uint32_t getMessageCount() const; + uint32_t getConsumerCount() const; + inline const string& getName() const { return name; } + inline const bool isExclusiveOwner(const ConnectionToken* const o) const { return o == owner; } + inline bool hasExclusiveConsumer() const { return exclusive; } + + bool canAutoDelete() const; + + void enqueue(TransactionContext* ctxt, Message::shared_ptr& msg); + /** + * dequeue from store (only done once messages is acknowledged) + */ + void dequeue(TransactionContext* ctxt, Message::shared_ptr& msg); + /** + * dequeues from memory only + */ + Message::shared_ptr dequeue(); + + const QueuePolicy* const getPolicy(); + + //PersistableQueue support: + uint64_t getPersistenceId() const; + void setPersistenceId(uint64_t persistenceId) const; + void encode(framing::Buffer& buffer) const; + uint32_t encodedSize() const; + + static Queue::shared_ptr decode(QueueRegistry& queues, framing::Buffer& buffer); + }; + } +} + + +#endif /*!_broker_BrokerQueue_h*/ diff --git a/cpp/src/qpid/broker/BrokerSingleton.cpp b/cpp/src/qpid/broker/BrokerSingleton.cpp new file mode 100644 index 0000000000..4571764850 --- /dev/null +++ b/cpp/src/qpid/broker/BrokerSingleton.cpp @@ -0,0 +1,36 @@ +/* + * + * 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 "BrokerSingleton.h" + +namespace qpid { +namespace broker { + +BrokerSingleton::BrokerSingleton() { + if (broker.get() == 0) + broker = Broker::create(); + Broker::shared_ptr::operator=(broker); +} + +BrokerSingleton::~BrokerSingleton() { + broker->shutdown(); +} + +Broker::shared_ptr BrokerSingleton::broker; + +}} // namespace qpid::broker diff --git a/cpp/src/qpid/broker/BrokerSingleton.h b/cpp/src/qpid/broker/BrokerSingleton.h new file mode 100644 index 0000000000..139e02a5fd --- /dev/null +++ b/cpp/src/qpid/broker/BrokerSingleton.h @@ -0,0 +1,52 @@ +#ifndef _broker_BrokerSingleton_h +#define _broker_BrokerSingleton_h + +/* + * + * 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 "Broker.h" + +namespace qpid { +namespace broker { + +/** + * BrokerSingleton is a smart pointer to a process-wide singleton broker + * started on an os-chosen port. The broker starts the first time + * an instance of BrokerSingleton is created and runs untill the process exits. + * + * Useful for unit tests that want to share a broker between multiple + * tests to reduce overhead of starting/stopping a broker for every test. + * + * Tests that need a new broker can call Broker::create directly. + * + * THREAD UNSAFE. + */ +class BrokerSingleton : public Broker::shared_ptr +{ + public: + BrokerSingleton(); + ~BrokerSingleton(); + private: + static Broker::shared_ptr broker; +}; + +}} // namespace qpid::broker + + + +#endif /*!_broker_BrokerSingleton_h*/ diff --git a/cpp/src/qpid/broker/CompletionHandler.h b/cpp/src/qpid/broker/CompletionHandler.h new file mode 100644 index 0000000000..9d51656282 --- /dev/null +++ b/cpp/src/qpid/broker/CompletionHandler.h @@ -0,0 +1,39 @@ +#ifndef _broker_CompletionHandler_h +#define _broker_CompletionHandler_h + +/* + * + * 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. + * + */ + +namespace qpid { +namespace broker { + +/** + * Callback interface to handle completion of a message. + */ +class CompletionHandler +{ + public: + virtual ~CompletionHandler(){} + virtual void complete(Message::shared_ptr) = 0; +}; + +}} // namespace qpid::broker + + + +#endif /*!_broker_CompletionHandler_h*/ diff --git a/cpp/src/qpid/broker/Configuration.cpp b/cpp/src/qpid/broker/Configuration.cpp new file mode 100644 index 0000000000..3449f6f179 --- /dev/null +++ b/cpp/src/qpid/broker/Configuration.cpp @@ -0,0 +1,252 @@ +/* + * + * 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 "Configuration.h" +#include +#include "config.h" + +using namespace qpid::broker; +using namespace std; + +Configuration::Configuration() : + daemon('d', "daemon", "Run as system daemon, detached from terminal.", false), + trace('t', "trace", "Print incoming & outgoing frames to the console", false), + port('p', "port", "Set the port to listen on (default=5672)", 5672), + workerThreads("worker-threads", "Set the number of worker threads to use (default=5).", 5), + maxConnections("max-connections", "Set the maximum number of connections the broker can accept (default=500).", 500), + connectionBacklog("connection-backlog", "Set the connection backlog for the servers socket (default=10)", 10), + store('s', "store", "Set the message store module to use (default='' which implies no store)", ""), + stagingThreshold("staging-threshold", "Set the message size threshold above which messages will be written to disk as they arrive (default=5,000,000)", 5000000), + help("help", "Print usage information", false), + version("version", "Print version information", false) +{ + options.push_back(&daemon); + options.push_back(&trace); + options.push_back(&port); + options.push_back(&workerThreads); + options.push_back(&maxConnections); + options.push_back(&connectionBacklog); + options.push_back(&store); + options.push_back(&stagingThreshold); + options.push_back(&help); + options.push_back(&version); +} + +Configuration::~Configuration(){} + +void Configuration::parse(char const *progName, int argc, char** argv){ + programName = progName; + int position = 1; + while(position < argc){ + bool matched(false); + for(op_iterator i = options.begin(); i < options.end() && !matched; i++){ + matched = (*i)->parse(position, argv, argc); + } + if(!matched) { + throw BadOptionException( + std::string("Unrecognised option: ")+argv[position]); + } + } +} + +void Configuration::usage(){ + std::cout << "Usage: " << programName << " [OPTION]..." << std::endl + << "Start the Qpid AMQP broker daemon." << std::endl << std::endl + << "Options:" << std::endl; + for(op_iterator i = options.begin(); i < options.end(); i++){ + (*i)->print(std::cout); + } + + std::cout << std::endl << "Report bugs to <" << PACKAGE_BUGREPORT << ">." + << std::endl; +} + +bool Configuration::isHelp() const { + return help.getValue(); +} + +bool Configuration::isVersion() const { + return version.getValue(); +} + +bool Configuration::isDaemon() const { + return daemon.getValue(); +} + +bool Configuration::isTrace() const { + return trace.getValue(); +} + +int Configuration::getPort() const { + return port.getValue(); +} + +int Configuration::getWorkerThreads() const { + return workerThreads.getValue(); +} + +int Configuration::getMaxConnections() const { + return maxConnections.getValue(); +} + +int Configuration::getConnectionBacklog() const { + return connectionBacklog.getValue(); +} + +const std::string& Configuration::getStore() const { + return store.getValue(); +} + +long Configuration::getStagingThreshold() const { + return stagingThreshold.getValue(); +} + + +Configuration::Option::Option(const char _flag, const string& _name, const string& _desc) : + flag(string("-") + _flag), name("--" +_name), desc(_desc) {} + +Configuration::Option::Option(const string& _name, const string& _desc) : + flag(""), name("--" + _name), desc(_desc) {} + +Configuration::Option::~Option(){} + +bool Configuration::Option::match(const string& arg){ + return flag == arg || name == arg; +} + +bool Configuration::Option::parse(int& i, char** argv, int argc){ + const string arg(argv[i]); + if(match(arg)){ + if(needsValue()){ + if(++i < argc) setValue(argv[i]); + else throw ParseException("Argument " + arg + " requires a value!"); + }else{ + setValue(""); + } + i++; + return true; + }else{ + return false; + } +} + +void Configuration::Option::print(ostream& out) const { + out << " "; + if(flag.length() > 0){ + out << flag << ", "; + } else { + out << " "; + } + out << name; + if(needsValue()) out << " "; + out << std::endl; + out << " " << desc << std::endl; +} + + +// String Option: + +Configuration::StringOption::StringOption(const char _flag, const string& _name, const string& _desc, const string _value) : + Option(_flag,_name,_desc), defaultValue(_value), value(_value) {} + +Configuration::StringOption::StringOption(const string& _name, const string& _desc, const string _value) : + Option(_name,_desc), defaultValue(_value), value(_value) {} + +Configuration::StringOption::~StringOption(){} + +const string& Configuration::StringOption::getValue() const { + return value; +} + +bool Configuration::StringOption::needsValue() const { + return true; +} + +void Configuration::StringOption::setValue(const std::string& _value){ + value = _value; +} + +// Int Option: + +Configuration::IntOption::IntOption(const char _flag, const string& _name, const string& _desc, const int _value) : + Option(_flag,_name,_desc), defaultValue(_value), value(_value) {} + +Configuration::IntOption::IntOption(const string& _name, const string& _desc, const int _value) : + Option(_name,_desc), defaultValue(_value), value(_value) {} + +Configuration::IntOption::~IntOption(){} + +int Configuration::IntOption::getValue() const { + return value; +} + +bool Configuration::IntOption::needsValue() const { + return true; +} + +void Configuration::IntOption::setValue(const std::string& _value){ + value = atoi(_value.c_str()); +} + +// Long Option: + +Configuration::LongOption::LongOption(const char _flag, const string& _name, const string& _desc, const long _value) : + Option(_flag,_name,_desc), defaultValue(_value), value(_value) {} + +Configuration::LongOption::LongOption(const string& _name, const string& _desc, const long _value) : + Option(_name,_desc), defaultValue(_value), value(_value) {} + +Configuration::LongOption::~LongOption(){} + +long Configuration::LongOption::getValue() const { + return value; +} + +bool Configuration::LongOption::needsValue() const { + return true; +} + +void Configuration::LongOption::setValue(const std::string& _value){ + value = atol(_value.c_str()); +} + +// Bool Option: + +Configuration::BoolOption::BoolOption(const char _flag, const string& _name, const string& _desc, const bool _value) : + Option(_flag,_name,_desc), defaultValue(_value), value(_value) {} + +Configuration::BoolOption::BoolOption(const string& _name, const string& _desc, const bool _value) : + Option(_name,_desc), defaultValue(_value), value(_value) {} + +Configuration::BoolOption::~BoolOption(){} + +bool Configuration::BoolOption::getValue() const { + return value; +} + +bool Configuration::BoolOption::needsValue() const { + return false; +} + +void Configuration::BoolOption::setValue(const std::string& /*not required*/){ + //BoolOptions have no value. The fact that the option is specified + //implies the value is true. + value = true; +} diff --git a/cpp/src/qpid/broker/Configuration.h b/cpp/src/qpid/broker/Configuration.h new file mode 100644 index 0000000000..174a061637 --- /dev/null +++ b/cpp/src/qpid/broker/Configuration.h @@ -0,0 +1,171 @@ +/* + * + * 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 _Configuration_ +#define _Configuration_ + +#include +#include +#include +#include "qpid/Exception.h" + +namespace qpid { +namespace broker { +class Configuration{ + + class Option { + const std::string flag; + const std::string name; + const std::string desc; + + bool match(const std::string& arg); + + protected: + virtual bool needsValue() const = 0; + virtual void setValue(const std::string& value) = 0; + + public: + Option(const char flag, const std::string& name, const std::string& desc); + Option(const std::string& name, const std::string& desc); + virtual ~Option(); + + bool parse(int& i, char** argv, int argc); + void print(std::ostream& out) const; + }; + + class IntOption : public Option{ + const int defaultValue; + int value; + public: + IntOption(char flag, const std::string& name, const std::string& desc, const int value = 0); + IntOption(const std::string& name, const std::string& desc, const int value = 0); + virtual ~IntOption(); + + int getValue() const; + virtual bool needsValue() const; + virtual void setValue(const std::string& value); + virtual void setValue(int _value) { value = _value; } + }; + + class LongOption : public Option{ + const long defaultValue; + int value; + public: + LongOption(char flag, const std::string& name, const std::string& desc, const long value = 0); + LongOption(const std::string& name, const std::string& desc, const long value = 0); + virtual ~LongOption(); + + long getValue() const; + virtual bool needsValue() const; + virtual void setValue(const std::string& value); + virtual void setValue(int _value) { value = _value; } + }; + + class StringOption : public Option{ + const std::string defaultValue; + std::string value; + public: + StringOption(char flag, const std::string& name, const std::string& desc, const std::string value = ""); + StringOption(const std::string& name, const std::string& desc, const std::string value = ""); + virtual ~StringOption(); + + const std::string& getValue() const; + virtual bool needsValue() const; + virtual void setValue(const std::string& value); + }; + + class BoolOption : public Option{ + const bool defaultValue; + bool value; + public: + BoolOption(char flag, const std::string& name, const std::string& desc, const bool value = 0); + BoolOption(const std::string& name, const std::string& desc, const bool value = 0); + virtual ~BoolOption(); + + bool getValue() const; + virtual bool needsValue() const; + virtual void setValue(const std::string& value); + virtual void setValue(bool _value) { value = _value; } + }; + + BoolOption daemon; + BoolOption trace; + IntOption port; + IntOption workerThreads; + IntOption maxConnections; + IntOption connectionBacklog; + StringOption store; + LongOption stagingThreshold; + BoolOption help; + BoolOption version; + char const *programName; + + typedef std::vector::iterator op_iterator; + std::vector options; + + public: + + struct BadOptionException : public Exception { + template + BadOptionException(const T& msg) : Exception(msg) {} + }; + + + class ParseException : public Exception { + public: + template + ParseException(const T& msg) : Exception(msg) {} + }; + + + Configuration(); + ~Configuration(); + + void parse(char const*, int argc, char** argv); + + bool isHelp() const; + bool isVersion() const; + bool isDaemon() const; + bool isTrace() const; + int getPort() const; + int getWorkerThreads() const; + int getMaxConnections() const; + int getConnectionBacklog() const; + const std::string& getStore() const; + long getStagingThreshold() const; + + void setHelp(bool b) { help.setValue(b); } + void setVersion(bool b) { version.setValue(b); } + void setDaemon(bool b) { daemon.setValue(b); } + void setTrace(bool b) { trace.setValue(b); } + void setPort(int i) { port.setValue(i); } + void setWorkerThreads(int i) { workerThreads.setValue(i); } + void setMaxConnections(int i) { maxConnections.setValue(i); } + void setConnectionBacklog(int i) { connectionBacklog.setValue(i); } + void setStore(const std::string& s) { store.setValue(s); } + void setStagingThreshold(long l) { stagingThreshold.setValue(l); } + + void usage(); +}; +} +} + + +#endif diff --git a/cpp/src/qpid/broker/Connection.cpp b/cpp/src/qpid/broker/Connection.cpp new file mode 100644 index 0000000000..b2174de44f --- /dev/null +++ b/cpp/src/qpid/broker/Connection.cpp @@ -0,0 +1,128 @@ +/* + * + * 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 +#include + +#include "Connection.h" +#include "BrokerChannel.h" +#include "qpid/framing/AMQP_ClientProxy.h" +#include "BrokerAdapter.h" + +using namespace boost; +using namespace qpid::sys; +using namespace qpid::framing; +using namespace qpid::sys; + +namespace qpid { +namespace broker { + +Connection::Connection(ConnectionOutputHandler* out_, Broker& broker_) : + broker(broker_), + out(out_), + framemax(65536), + heartbeat(0), + client(0), + timeout(broker.getTimeout()), + stagingThreshold(broker.getStagingThreshold()) +{} + + +Queue::shared_ptr Connection::getQueue(const string& name, uint16_t channel){ + Queue::shared_ptr queue; + if (name.empty()) { + queue = getChannel(channel).getDefaultQueue(); + if (!queue) throw ConnectionException( 530, "Queue must be specified or previously declared" ); + } else { + queue = broker.getQueues().find(name); + if (queue == 0) { + throw ChannelException( 404, "Queue not found: " + name); + } + } + return queue; +} + + +Exchange::shared_ptr Connection::findExchange(const string& name){ + return broker.getExchanges().get(name); +} + + +void Connection::received(framing::AMQFrame* frame){ + getChannel(frame->getChannel()).handleBody(frame->getBody()); +} + +void Connection::close( + ReplyCode code, const string& text, ClassId classId, MethodId methodId) +{ + client->close(code, text, classId, methodId); + getOutput().close(); +} + +void Connection::initiated(const framing::ProtocolInitiation& header) { + version = ProtocolVersion(header.getMajor(), header.getMinor()); + FieldTable properties; + string mechanisms("PLAIN"); + string locales("en_US"); + getChannel(0).init(0, *out, getVersion()); + client = &getChannel(0).getAdatper().getProxy().getConnection(); + client->start( + header.getMajor(), header.getMinor(), + properties, mechanisms, locales); +} + +void Connection::idleOut(){} + +void Connection::idleIn(){} + +void Connection::closed(){ + try { + while (!exclusiveQueues.empty()) { + broker.getQueues().destroy(exclusiveQueues.front()->getName()); + exclusiveQueues.erase(exclusiveQueues.begin()); + } + } catch(std::exception& e) { + std::cout << "Caught unhandled exception while closing session: " << + e.what() << std::endl; + assert(0); + } +} + +void Connection::closeChannel(uint16_t id) { + ChannelMap::iterator i = channels.find(id); + if (i != channels.end()) + i->close(); +} + + +Channel& Connection::getChannel(ChannelId id) { + ChannelMap::iterator i = channels.find(id); + if (i == channels.end()) { + i = channels.insert( + id, new Channel( + *this, id, framemax, broker.getQueues().getStore(), + broker.getStagingThreshold())).first; + } + return *i; +} + + +}} + diff --git a/cpp/src/qpid/broker/Connection.h b/cpp/src/qpid/broker/Connection.h new file mode 100644 index 0000000000..3fc575280a --- /dev/null +++ b/cpp/src/qpid/broker/Connection.h @@ -0,0 +1,108 @@ +/* + * + * 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 _Connection_ +#define _Connection_ + +#include +#include + +#include + +#include "qpid/framing/AMQFrame.h" +#include "qpid/framing/AMQP_ServerOperations.h" +#include "qpid/framing/AMQP_ClientProxy.h" +#include "qpid/sys/ConnectionOutputHandler.h" +#include "qpid/sys/ConnectionInputHandler.h" +#include "qpid/sys/TimeoutHandler.h" +#include "qpid/framing/ProtocolVersion.h" +#include "Broker.h" +#include "qpid/Exception.h" +#include "BrokerChannel.h" + +namespace qpid { +namespace broker { + +class Channel; + +class Connection : public sys::ConnectionInputHandler, + public ConnectionToken +{ + public: + Connection(sys::ConnectionOutputHandler* out, Broker& broker); + + /** Get a channel. Create if it does not already exist */ + Channel& getChannel(framing::ChannelId channel); + + /** Close a channel */ + void closeChannel(framing::ChannelId channel); + + /** Close the connection */ + void close(framing::ReplyCode code, const string& text, framing::ClassId classId, framing::MethodId methodId); + + sys::ConnectionOutputHandler& getOutput() const { return *out; } + framing::ProtocolVersion getVersion() const { return version; } + + uint32_t getFrameMax() const { return framemax; } + uint16_t getHeartbeat() const { return heartbeat; } + uint32_t getTimeout() const { return timeout; } + uint64_t getStagingThreshold() const { return stagingThreshold; } + + void setFrameMax(uint32_t fm) { framemax = fm; } + void setHeartbeat(uint16_t hb) { heartbeat = hb; } + + /** + * Get named queue, never returns 0. + * @return: named queue or default queue for channel if name="" + * @exception: ChannelException if no queue of that name is found. + * @exception: ConnectionException if name="" and channel has no default. + */ + Queue::shared_ptr getQueue(const string& name, uint16_t channel); + + Broker& broker; + std::vector exclusiveQueues; + + // ConnectionInputHandler methods + void received(framing::AMQFrame* frame); + void initiated(const framing::ProtocolInitiation& header); + void idleOut(); + void idleIn(); + void closed(); + + private: + typedef boost::ptr_map ChannelMap; + + typedef std::vector::iterator queue_iterator; + Exchange::shared_ptr findExchange(const string& name); + + framing::ProtocolVersion version; + ChannelMap channels; + sys::ConnectionOutputHandler* out; + uint32_t framemax; + uint16_t heartbeat; + framing::AMQP_ClientProxy::Connection* client; + const uint32_t timeout; //timeout for auto-deleted queues (in ms) + const uint64_t stagingThreshold; + +}; + +}} + +#endif diff --git a/cpp/src/qpid/broker/ConnectionFactory.cpp b/cpp/src/qpid/broker/ConnectionFactory.cpp new file mode 100644 index 0000000000..5372bfe377 --- /dev/null +++ b/cpp/src/qpid/broker/ConnectionFactory.cpp @@ -0,0 +1,43 @@ +/* + * + * 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 "ConnectionFactory.h" +#include "Connection.h" + +namespace qpid { +namespace broker { + + +ConnectionFactory::ConnectionFactory(Broker& b) : broker(b) +{} + + +ConnectionFactory::~ConnectionFactory() +{ + broker.getCleaner().stop(); +} + +qpid::sys::ConnectionInputHandler* +ConnectionFactory::create(qpid::sys::ConnectionOutputHandler* out) +{ + return new Connection(out, broker); +} + +}} // namespace qpid::broker diff --git a/cpp/src/qpid/broker/ConnectionFactory.h b/cpp/src/qpid/broker/ConnectionFactory.h new file mode 100644 index 0000000000..6af3dda7a6 --- /dev/null +++ b/cpp/src/qpid/broker/ConnectionFactory.h @@ -0,0 +1,47 @@ +/* + * + * 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 _ConnectionFactory_ +#define _ConnectionFactory_ + +#include "qpid/sys/ConnectionInputHandlerFactory.h" + +namespace qpid { +namespace broker { +class Broker; + +class ConnectionFactory : public qpid::sys::ConnectionInputHandlerFactory +{ + public: + ConnectionFactory(Broker& b); + + virtual qpid::sys::ConnectionInputHandler* create( + qpid::sys::ConnectionOutputHandler* ctxt); + + virtual ~ConnectionFactory(); + + private: + Broker& broker; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/broker/ConnectionToken.h b/cpp/src/qpid/broker/ConnectionToken.h new file mode 100644 index 0000000000..7e7f813d0e --- /dev/null +++ b/cpp/src/qpid/broker/ConnectionToken.h @@ -0,0 +1,38 @@ +/* + * + * 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 _ConnectionToken_ +#define _ConnectionToken_ + +namespace qpid { + namespace broker { + /** + * An empty interface allowing opaque implementations of some + * form of token to identify a connection. + */ + class ConnectionToken{ + public: + virtual ~ConnectionToken(){} + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/Consumer.h b/cpp/src/qpid/broker/Consumer.h new file mode 100644 index 0000000000..d0c397d184 --- /dev/null +++ b/cpp/src/qpid/broker/Consumer.h @@ -0,0 +1,37 @@ +/* + * + * 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 _Consumer_ +#define _Consumer_ + +#include "BrokerMessage.h" + +namespace qpid { + namespace broker { + class Consumer{ + public: + virtual bool deliver(Message::shared_ptr& msg) = 0; + virtual ~Consumer(){} + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/Content.h b/cpp/src/qpid/broker/Content.h new file mode 100644 index 0000000000..df9e7a1132 --- /dev/null +++ b/cpp/src/qpid/broker/Content.h @@ -0,0 +1,64 @@ +/* + * + * 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 _Content_ +#define _Content_ + +#include + +#include "qpid/framing/AMQContentBody.h" +#include "qpid/framing/Buffer.h" +#include "qpid/framing/OutputHandler.h" + +namespace qpid { + +namespace framing { +class ChannelAdapter; +} + +namespace broker { +class Content{ + public: + typedef std::string DataBlock; + typedef boost::function1 SendFn; + + virtual ~Content(){} + + /** Add a block of data to the content */ + virtual void add(framing::AMQContentBody::shared_ptr data) = 0; + + /** Total size of content in bytes */ + virtual uint32_t size() = 0; + + /** + * Iterate over the content calling SendFn for each block. + * Subdivide blocks if necessary to ensure each block is + * <= framesize bytes long. + */ + virtual void send(framing::ChannelAdapter& channel, uint32_t framesize) = 0; + + //FIXME aconway 2007-02-07: This is inconsistently implemented + //find out what is needed. + virtual void encode(qpid::framing::Buffer& buffer) = 0; +}; +}} + + +#endif diff --git a/cpp/src/qpid/broker/Deliverable.h b/cpp/src/qpid/broker/Deliverable.h new file mode 100644 index 0000000000..1570917849 --- /dev/null +++ b/cpp/src/qpid/broker/Deliverable.h @@ -0,0 +1,37 @@ +/* + * + * 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 _Deliverable_ +#define _Deliverable_ + +#include "BrokerQueue.h" + +namespace qpid { + namespace broker { + class Deliverable{ + public: + virtual void deliverTo(Queue::shared_ptr& queue) = 0; + virtual ~Deliverable(){} + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/DeliverableMessage.cpp b/cpp/src/qpid/broker/DeliverableMessage.cpp new file mode 100644 index 0000000000..0c2e46ccce --- /dev/null +++ b/cpp/src/qpid/broker/DeliverableMessage.cpp @@ -0,0 +1,33 @@ +/* + * + * 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 "DeliverableMessage.h" + +using namespace qpid::broker; + +DeliverableMessage::DeliverableMessage(Message::shared_ptr& _msg) : msg(_msg) +{ +} + +void DeliverableMessage::deliverTo(Queue::shared_ptr& queue) +{ + queue->deliver(msg); +} + diff --git a/cpp/src/qpid/broker/DeliverableMessage.h b/cpp/src/qpid/broker/DeliverableMessage.h new file mode 100644 index 0000000000..43e738a96a --- /dev/null +++ b/cpp/src/qpid/broker/DeliverableMessage.h @@ -0,0 +1,41 @@ +/* + * + * 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 _DeliverableMessage_ +#define _DeliverableMessage_ + +#include "Deliverable.h" +#include "BrokerMessage.h" +#include "BrokerQueue.h" + +namespace qpid { + namespace broker { + class DeliverableMessage : public Deliverable{ + Message::shared_ptr msg; + public: + DeliverableMessage(Message::shared_ptr& msg); + virtual void deliverTo(Queue::shared_ptr& queue); + virtual ~DeliverableMessage(){} + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/DeliveryRecord.cpp b/cpp/src/qpid/broker/DeliveryRecord.cpp new file mode 100644 index 0000000000..95650b7b23 --- /dev/null +++ b/cpp/src/qpid/broker/DeliveryRecord.cpp @@ -0,0 +1,87 @@ +/* + * + * 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 "DeliveryRecord.h" +#include "BrokerChannel.h" + +using namespace qpid::broker; +using std::string; + +DeliveryRecord::DeliveryRecord(Message::shared_ptr _msg, + Queue::shared_ptr _queue, + const string _consumerTag, + const uint64_t _deliveryTag) : msg(_msg), + queue(_queue), + consumerTag(_consumerTag), + deliveryTag(_deliveryTag), + pull(false){} + +DeliveryRecord::DeliveryRecord(Message::shared_ptr _msg, + Queue::shared_ptr _queue, + const uint64_t _deliveryTag) : msg(_msg), + queue(_queue), + consumerTag(""), + deliveryTag(_deliveryTag), + pull(true){} + + +void DeliveryRecord::discard(TransactionContext* ctxt) const{ + queue->dequeue(ctxt, msg); +} + +bool DeliveryRecord::matches(uint64_t tag) const{ + return deliveryTag == tag; +} + +bool DeliveryRecord::coveredBy(const AccumulatedAck* const range) const{ + return range->covers(deliveryTag); +} + +void DeliveryRecord::redeliver(Channel* const channel) const{ + if(pull){ + //if message was originally sent as response to get, we must requeue it + requeue(); + }else{ + channel->deliver(msg, consumerTag, deliveryTag); + } +} + +void DeliveryRecord::requeue() const{ + msg->redeliver(); + queue->process(msg); +} + +void DeliveryRecord::addTo(Prefetch* const prefetch) const{ + if(!pull){ + //ignore 'pulled' messages (i.e. those that were sent in + //response to get) when calculating prefetch + prefetch->size += msg->contentSize(); + prefetch->count++; + } +} + +void DeliveryRecord::subtractFrom(Prefetch* const prefetch) const{ + if(!pull){ + //ignore 'pulled' messages (i.e. those that were sent in + //response to get) when calculating prefetch + prefetch->size -= msg->contentSize(); + prefetch->count--; + } +} diff --git a/cpp/src/qpid/broker/DeliveryRecord.h b/cpp/src/qpid/broker/DeliveryRecord.h new file mode 100644 index 0000000000..15c207ce44 --- /dev/null +++ b/cpp/src/qpid/broker/DeliveryRecord.h @@ -0,0 +1,63 @@ +/* + * + * 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 _DeliveryRecord_ +#define _DeliveryRecord_ + +#include +#include +#include "AccumulatedAck.h" +#include "BrokerMessage.h" +#include "Prefetch.h" +#include "BrokerQueue.h" + +namespace qpid { + namespace broker { + class Channel; + + /** + * Record of a delivery for which an ack is outstanding. + */ + class DeliveryRecord{ + mutable Message::shared_ptr msg; + mutable Queue::shared_ptr queue; + std::string consumerTag; + uint64_t deliveryTag; + bool pull; + + public: + DeliveryRecord(Message::shared_ptr msg, Queue::shared_ptr queue, const std::string consumerTag, const uint64_t deliveryTag); + DeliveryRecord(Message::shared_ptr msg, Queue::shared_ptr queue, const uint64_t deliveryTag); + + void discard(TransactionContext* ctxt = 0) const; + bool matches(uint64_t tag) const; + bool coveredBy(const AccumulatedAck* const range) const; + void requeue() const; + void redeliver(Channel* const) const; + void addTo(Prefetch* const prefetch) const; + void subtractFrom(Prefetch* const prefetch) const; + }; + + typedef std::list::iterator ack_iterator; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/DirectExchange.cpp b/cpp/src/qpid/broker/DirectExchange.cpp new file mode 100644 index 0000000000..7d15410374 --- /dev/null +++ b/cpp/src/qpid/broker/DirectExchange.cpp @@ -0,0 +1,71 @@ +/* + * + * 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 "DirectExchange.h" +#include + +using namespace qpid::broker; +using namespace qpid::framing; +using namespace qpid::sys; + +DirectExchange::DirectExchange(const string& _name) : Exchange(_name) { + +} + +void DirectExchange::bind(Queue::shared_ptr queue, const string& routingKey, const FieldTable*){ + Mutex::ScopedLock l(lock); + std::vector& queues(bindings[routingKey]); + std::vector::iterator i = find(queues.begin(), queues.end(), queue); + if(i == queues.end()){ + bindings[routingKey].push_back(queue); + } +} + +void DirectExchange::unbind(Queue::shared_ptr queue, const string& routingKey, const FieldTable* /*args*/){ + Mutex::ScopedLock l(lock); + std::vector& queues(bindings[routingKey]); + + std::vector::iterator i = find(queues.begin(), queues.end(), queue); + if(i < queues.end()){ + queues.erase(i); + if(queues.empty()){ + bindings.erase(routingKey); + } + } +} + +void DirectExchange::route(Deliverable& msg, const string& routingKey, const FieldTable* /*args*/){ + Mutex::ScopedLock l(lock); + std::vector& queues(bindings[routingKey]); + int count(0); + for(std::vector::iterator i = queues.begin(); i != queues.end(); i++, count++){ + msg.deliverTo(*i); + } + if(!count){ + std::cout << "WARNING: DirectExchange " << getName() << " could not route message with key " << routingKey << std::endl; + } +} + +DirectExchange::~DirectExchange(){ + +} + + +const std::string DirectExchange::typeName("direct"); diff --git a/cpp/src/qpid/broker/DirectExchange.h b/cpp/src/qpid/broker/DirectExchange.h new file mode 100644 index 0000000000..886b59be30 --- /dev/null +++ b/cpp/src/qpid/broker/DirectExchange.h @@ -0,0 +1,57 @@ +/* + * + * 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 _DirectExchange_ +#define _DirectExchange_ + +#include +#include +#include "BrokerExchange.h" +#include "qpid/framing/FieldTable.h" +#include "BrokerMessage.h" +#include "qpid/sys/Monitor.h" +#include "BrokerQueue.h" + +namespace qpid { +namespace broker { + class DirectExchange : public virtual Exchange{ + std::map > bindings; + qpid::sys::Mutex lock; + + public: + static const std::string typeName; + + DirectExchange(const std::string& name); + + virtual std::string getType(){ return typeName; } + + virtual void bind(Queue::shared_ptr queue, const std::string& routingKey, const qpid::framing::FieldTable* args); + + virtual void unbind(Queue::shared_ptr queue, const std::string& routingKey, const qpid::framing::FieldTable* args); + + virtual void route(Deliverable& msg, const std::string& routingKey, const qpid::framing::FieldTable* args); + + virtual ~DirectExchange(); + }; +} +} + + +#endif diff --git a/cpp/src/qpid/broker/ExchangeRegistry.cpp b/cpp/src/qpid/broker/ExchangeRegistry.cpp new file mode 100644 index 0000000000..03863673df --- /dev/null +++ b/cpp/src/qpid/broker/ExchangeRegistry.cpp @@ -0,0 +1,76 @@ +/* + * + * 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 "ExchangeRegistry.h" +#include "DirectExchange.h" +#include "FanOutExchange.h" +#include "HeadersExchange.h" +#include "TopicExchange.h" + +using namespace qpid::broker; +using namespace qpid::sys; +using std::pair; + +pair ExchangeRegistry::declare(const string& name, const string& type) throw(UnknownExchangeTypeException){ + Mutex::ScopedLock locker(lock); + ExchangeMap::iterator i = exchanges.find(name); + if (i == exchanges.end()) { + Exchange::shared_ptr exchange; + + if(type == TopicExchange::typeName){ + exchange = Exchange::shared_ptr(new TopicExchange(name)); + }else if(type == DirectExchange::typeName){ + exchange = Exchange::shared_ptr(new DirectExchange(name)); + }else if(type == FanOutExchange::typeName){ + exchange = Exchange::shared_ptr(new FanOutExchange(name)); + }else if (type == HeadersExchange::typeName) { + exchange = Exchange::shared_ptr(new HeadersExchange(name)); + }else{ + throw UnknownExchangeTypeException(); + } + exchanges[name] = exchange; + return std::pair(exchange, true); + } else { + return std::pair(i->second, false); + } +} + +void ExchangeRegistry::destroy(const string& name){ + Mutex::ScopedLock locker(lock); + exchanges.erase(name); +} + +Exchange::shared_ptr ExchangeRegistry::get(const string& name){ + Mutex::ScopedLock locker(lock); + Exchange::shared_ptr exchange =exchanges[name]; + if (!exchange) + throw ChannelException(404, "Exchange not found:" + name); + return exchange; +} + +namespace +{ +const std::string empty; +} + +Exchange::shared_ptr ExchangeRegistry::getDefault() +{ + return get(empty); +} diff --git a/cpp/src/qpid/broker/ExchangeRegistry.h b/cpp/src/qpid/broker/ExchangeRegistry.h new file mode 100644 index 0000000000..ff7399ba22 --- /dev/null +++ b/cpp/src/qpid/broker/ExchangeRegistry.h @@ -0,0 +1,47 @@ +#ifndef _broker_ExchangeRegistry_h +#define _broker_ExchangeRegistry_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 +#include "BrokerExchange.h" +#include "qpid/sys/Monitor.h" + +namespace qpid { +namespace broker { + struct UnknownExchangeTypeException{}; + + class ExchangeRegistry{ + typedef std::map ExchangeMap; + ExchangeMap exchanges; + qpid::sys::Mutex lock; + public: + std::pair declare(const std::string& name, const std::string& type) throw(UnknownExchangeTypeException); + void destroy(const std::string& name); + Exchange::shared_ptr get(const std::string& name); + Exchange::shared_ptr getDefault(); + }; +} +} + + +#endif /*!_broker_ExchangeRegistry_h*/ diff --git a/cpp/src/qpid/broker/FanOutExchange.cpp b/cpp/src/qpid/broker/FanOutExchange.cpp new file mode 100644 index 0000000000..1ac92c89e2 --- /dev/null +++ b/cpp/src/qpid/broker/FanOutExchange.cpp @@ -0,0 +1,56 @@ +/* + * + * 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 "FanOutExchange.h" +#include + +using namespace qpid::broker; +using namespace qpid::framing; +using namespace qpid::sys; + +FanOutExchange::FanOutExchange(const std::string& _name) : Exchange(_name) {} + +void FanOutExchange::bind(Queue::shared_ptr queue, const string& /*routingKey*/, const FieldTable* /*args*/){ + Mutex::ScopedLock locker(lock); + // Add if not already present. + Queue::vector::iterator i = std::find(bindings.begin(), bindings.end(), queue); + if (i == bindings.end()) { + bindings.push_back(queue); + } +} + +void FanOutExchange::unbind(Queue::shared_ptr queue, const string& /*routingKey*/, const FieldTable* /*args*/){ + Mutex::ScopedLock locker(lock); + Queue::vector::iterator i = std::find(bindings.begin(), bindings.end(), queue); + if (i != bindings.end()) { + bindings.erase(i); + } +} + +void FanOutExchange::route(Deliverable& msg, const string& /*routingKey*/, const FieldTable* /*args*/){ + Mutex::ScopedLock locker(lock); + for(Queue::vector::iterator i = bindings.begin(); i != bindings.end(); ++i){ + msg.deliverTo(*i); + } +} + +FanOutExchange::~FanOutExchange() {} + +const std::string FanOutExchange::typeName("fanout"); diff --git a/cpp/src/qpid/broker/FanOutExchange.h b/cpp/src/qpid/broker/FanOutExchange.h new file mode 100644 index 0000000000..b6a803673f --- /dev/null +++ b/cpp/src/qpid/broker/FanOutExchange.h @@ -0,0 +1,60 @@ +/* + * + * 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 _FanOutExchange_ +#define _FanOutExchange_ + +#include +#include +#include "BrokerExchange.h" +#include "qpid/framing/FieldTable.h" +#include "BrokerMessage.h" +#include "qpid/sys/Monitor.h" +#include "BrokerQueue.h" + +namespace qpid { +namespace broker { + +class FanOutExchange : public virtual Exchange { + std::vector bindings; + qpid::sys::Mutex lock; + + public: + static const std::string typeName; + + FanOutExchange(const std::string& name); + + virtual std::string getType(){ return typeName; } + + virtual void bind(Queue::shared_ptr queue, const std::string& routingKey, const qpid::framing::FieldTable* args); + + virtual void unbind(Queue::shared_ptr queue, const std::string& routingKey, const qpid::framing::FieldTable* args); + + virtual void route(Deliverable& msg, const std::string& routingKey, const qpid::framing::FieldTable* args); + + virtual ~FanOutExchange(); +}; + +} +} + + + +#endif diff --git a/cpp/src/qpid/broker/HandlerImpl.h b/cpp/src/qpid/broker/HandlerImpl.h new file mode 100644 index 0000000000..338ebca4b7 --- /dev/null +++ b/cpp/src/qpid/broker/HandlerImpl.h @@ -0,0 +1,71 @@ +#ifndef _broker_HandlerImpl_h +#define _broker_HandlerImpl_h + +/* + * + * 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 "BrokerChannel.h" +#include "qpid/framing/AMQP_ClientProxy.h" + +namespace qpid { + +namespace framing { +class AMQP_ClientProxy; +} + +namespace broker { + +class Broker; +class Channel; +class Connection; + +/** + * A collection of references to the core objects required by an adapter, + * and a client proxy. + */ +struct CoreRefs +{ + CoreRefs(Channel& ch, Connection& c, Broker& b) + : channel(ch), connection(c), broker(b), proxy(ch) {} + + Channel& channel; + Connection& connection; + Broker& broker; + framing::AMQP_ClientProxy proxy; +}; + + +/** + * Base template for protocol handler implementations. + * Provides the core references and appropriate AMQP class proxy. + */ +template +struct HandlerImpl : public CoreRefs { + typedef HandlerImpl HandlerImplType; + HandlerImpl(CoreRefs& parent) + : CoreRefs(parent), client(ProxyType::get(proxy)) {} + ProxyType client; +}; + + + +}} // namespace qpid::broker + + + +#endif /*!_broker_HandlerImpl_h*/ diff --git a/cpp/src/qpid/broker/HeadersExchange.cpp b/cpp/src/qpid/broker/HeadersExchange.cpp new file mode 100644 index 0000000000..fef1f3fa4b --- /dev/null +++ b/cpp/src/qpid/broker/HeadersExchange.cpp @@ -0,0 +1,119 @@ +/* + * + * 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 "HeadersExchange.h" +#include "qpid/framing/Value.h" +#include "qpid/QpidError.h" +#include + + +using namespace qpid::broker; +using namespace qpid::framing; +using namespace qpid::sys; + +// TODO aconway 2006-09-20: More efficient matching algorithm. +// The current search algorithm really sucks. +// Fieldtables are heavy, maybe use shared_ptr to do handle-body. + +using namespace qpid::broker; + +namespace { + const std::string all("all"); + const std::string any("any"); + const std::string x_match("x-match"); +} + +HeadersExchange::HeadersExchange(const string& _name) : Exchange(_name) { } + +void HeadersExchange::bind(Queue::shared_ptr queue, const string& /*routingKey*/, const FieldTable* args){ + Mutex::ScopedLock locker(lock); + std::string what = args->getString("x-match"); + if (what != all && what != any) { + THROW_QPID_ERROR(PROTOCOL_ERROR, "Invalid x-match value binding to headers exchange."); + } + bindings.push_back(Binding(*args, queue)); +} + +void HeadersExchange::unbind(Queue::shared_ptr queue, const string& /*routingKey*/, const FieldTable* args){ + Mutex::ScopedLock locker(lock); + Bindings::iterator i = + std::find(bindings.begin(),bindings.end(), Binding(*args, queue)); + if (i != bindings.end()) bindings.erase(i); +} + + +void HeadersExchange::route(Deliverable& msg, const string& /*routingKey*/, const FieldTable* args){ + Mutex::ScopedLock locker(lock);; + for (Bindings::iterator i = bindings.begin(); i != bindings.end(); ++i) { + if (match(i->first, *args)) msg.deliverTo(i->second); + } +} + +HeadersExchange::~HeadersExchange() {} + +const std::string HeadersExchange::typeName("headers"); + +namespace +{ + + bool match_values(const Value& bind, const Value& msg) { + return dynamic_cast(&bind) || bind == msg; + } + +} + + +bool HeadersExchange::match(const FieldTable& bind, const FieldTable& msg) { + typedef FieldTable::ValueMap Map; + std::string what = bind.getString(x_match); + if (what == all) { + for (Map::const_iterator i = bind.getMap().begin(); + i != bind.getMap().end(); + ++i) + { + if (i->first != x_match) + { + Map::const_iterator j = msg.getMap().find(i->first); + if (j == msg.getMap().end()) return false; + if (!match_values(*(i->second), *(j->second))) return false; + } + } + return true; + } else if (what == any) { + for (Map::const_iterator i = bind.getMap().begin(); + i != bind.getMap().end(); + ++i) + { + if (i->first != x_match) + { + Map::const_iterator j = msg.getMap().find(i->first); + if (j != msg.getMap().end()) { + if (match_values(*(i->second), *(j->second))) return true; + } + } + } + return false; + } else { + return false; + } +} + + + diff --git a/cpp/src/qpid/broker/HeadersExchange.h b/cpp/src/qpid/broker/HeadersExchange.h new file mode 100644 index 0000000000..e92b6f19cf --- /dev/null +++ b/cpp/src/qpid/broker/HeadersExchange.h @@ -0,0 +1,65 @@ +/* + * + * 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 _HeadersExchange_ +#define _HeadersExchange_ + +#include +#include "BrokerExchange.h" +#include "qpid/framing/FieldTable.h" +#include "BrokerMessage.h" +#include "qpid/sys/Monitor.h" +#include "BrokerQueue.h" + +namespace qpid { +namespace broker { + + +class HeadersExchange : public virtual Exchange { + typedef std::pair Binding; + typedef std::vector Bindings; + + Bindings bindings; + qpid::sys::Mutex lock; + + public: + static const std::string typeName; + + HeadersExchange(const string& name); + + virtual std::string getType(){ return typeName; } + + virtual void bind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args); + + virtual void unbind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args); + + virtual void route(Deliverable& msg, const string& routingKey, const qpid::framing::FieldTable* args); + + virtual ~HeadersExchange(); + + static bool match(const qpid::framing::FieldTable& bindArgs, const qpid::framing::FieldTable& msgArgs); +}; + + + +} +} + +#endif diff --git a/cpp/src/qpid/broker/InMemoryContent.cpp b/cpp/src/qpid/broker/InMemoryContent.cpp new file mode 100644 index 0000000000..96563b0329 --- /dev/null +++ b/cpp/src/qpid/broker/InMemoryContent.cpp @@ -0,0 +1,72 @@ +/* + * + * 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 "InMemoryContent.h" +#include "qpid/framing/AMQFrame.h" +#include "qpid/framing/ChannelAdapter.h" + +using namespace qpid::broker; +using namespace qpid::framing; +using boost::static_pointer_cast; + +void InMemoryContent::add(AMQContentBody::shared_ptr data) +{ + content.push_back(data); +} + +uint32_t InMemoryContent::size() +{ + int sum(0); + for (content_iterator i = content.begin(); i != content.end(); i++) { + sum += (*i)->size(); + } + return sum; +} + +void InMemoryContent::send(ChannelAdapter& channel, uint32_t framesize) +{ + for (content_iterator i = content.begin(); i != content.end(); i++) { + if ((*i)->size() > framesize) { + uint32_t offset = 0; + for (int chunk = (*i)->size() / framesize; chunk > 0; chunk--) { + string data = (*i)->getData().substr(offset, framesize); + channel.send(new AMQContentBody(data)); + offset += framesize; + } + uint32_t remainder = (*i)->size() % framesize; + if (remainder) { + string data = (*i)->getData().substr(offset, remainder); + channel.send(new AMQContentBody(data)); + } + } else { + AMQBody::shared_ptr contentBody = + static_pointer_cast(*i); + channel.send(contentBody); + } + } +} + +void InMemoryContent::encode(Buffer& buffer) +{ + for (content_iterator i = content.begin(); i != content.end(); i++) { + (*i)->encode(buffer); + } +} + diff --git a/cpp/src/qpid/broker/InMemoryContent.h b/cpp/src/qpid/broker/InMemoryContent.h new file mode 100644 index 0000000000..425f0e4e26 --- /dev/null +++ b/cpp/src/qpid/broker/InMemoryContent.h @@ -0,0 +1,45 @@ +/* + * + * 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 _InMemoryContent_ +#define _InMemoryContent_ + +#include "Content.h" +#include + + +namespace qpid { + namespace broker { + class InMemoryContent : public Content{ + typedef std::vector content_list; + typedef content_list::iterator content_iterator; + + content_list content; + public: + void add(qpid::framing::AMQContentBody::shared_ptr data); + uint32_t size(); + void send(framing::ChannelAdapter&, uint32_t framesize); + void encode(qpid::framing::Buffer& buffer); + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/LazyLoadedContent.cpp b/cpp/src/qpid/broker/LazyLoadedContent.cpp new file mode 100644 index 0000000000..c11d049317 --- /dev/null +++ b/cpp/src/qpid/broker/LazyLoadedContent.cpp @@ -0,0 +1,68 @@ +/* + * + * 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 "LazyLoadedContent.h" +#include "qpid/framing/AMQFrame.h" +#include "qpid/framing/ChannelAdapter.h" + +using namespace qpid::broker; +using namespace qpid::framing; + +LazyLoadedContent::~LazyLoadedContent() +{ + store->destroy(*msg); +} + +LazyLoadedContent::LazyLoadedContent(MessageStore* const _store, Message* const _msg, uint64_t _expectedSize) : + store(_store), msg(_msg), expectedSize(_expectedSize) {} + +void LazyLoadedContent::add(AMQContentBody::shared_ptr data) +{ + store->appendContent(*msg, data->getData()); +} + +uint32_t LazyLoadedContent::size() +{ + return 0;//all content is written as soon as it is added +} + +void LazyLoadedContent::send(ChannelAdapter& channel, uint32_t framesize) +{ + if (expectedSize > framesize) { + for (uint64_t offset = 0; offset < expectedSize; offset += framesize) + { + uint64_t remaining = expectedSize - offset; + string data; + store->loadContent(*msg, data, offset, + remaining > framesize ? framesize : remaining); + channel.send(new AMQContentBody(data)); + } + } else { + string data; + store->loadContent(*msg, data, 0, expectedSize); + channel.send(new AMQContentBody(data)); + } +} + +void LazyLoadedContent::encode(Buffer&) +{ + //do nothing as all content is written as soon as it is added +} + diff --git a/cpp/src/qpid/broker/LazyLoadedContent.h b/cpp/src/qpid/broker/LazyLoadedContent.h new file mode 100644 index 0000000000..9dff6158a5 --- /dev/null +++ b/cpp/src/qpid/broker/LazyLoadedContent.h @@ -0,0 +1,50 @@ +/* + * + * 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 _LazyLoadedContent_ +#define _LazyLoadedContent_ + +#include "Content.h" +#include "MessageStore.h" +#include "BrokerMessageBase.h" + +namespace qpid { + namespace broker { + class LazyLoadedContent : public Content{ + MessageStore* const store; + Message* const msg; + const uint64_t expectedSize; + public: + LazyLoadedContent( + MessageStore* const store, Message* const msg, + uint64_t expectedSize); + ~LazyLoadedContent(); + void add(qpid::framing::AMQContentBody::shared_ptr data); + uint32_t size(); + void send( + framing::ChannelAdapter&, + uint32_t framesize); + void encode(qpid::framing::Buffer& buffer); + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/MessageBuilder.cpp b/cpp/src/qpid/broker/MessageBuilder.cpp new file mode 100644 index 0000000000..6c33b38e72 --- /dev/null +++ b/cpp/src/qpid/broker/MessageBuilder.cpp @@ -0,0 +1,74 @@ +/* + * + * 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 "MessageBuilder.h" + +#include "InMemoryContent.h" +#include "LazyLoadedContent.h" + +using namespace qpid::broker; +using namespace qpid::framing; +using std::auto_ptr; + +MessageBuilder::MessageBuilder(CompletionHandler* _handler, + MessageStore* const _store, + uint64_t _stagingThreshold +) : + handler(_handler), + store(_store), + stagingThreshold(_stagingThreshold) +{} + +void MessageBuilder::route(){ + if (message->isComplete()) { + if (handler) handler->complete(message); + message.reset(); + } +} + +void MessageBuilder::initialise(Message::shared_ptr& msg){ + if(message.get()){ + THROW_QPID_ERROR(PROTOCOL_ERROR + 504, "Invalid message sequence: got publish before previous content was completed."); + } + message = msg; +} + +void MessageBuilder::setHeader(AMQHeaderBody::shared_ptr& header){ + if(!message.get()){ + THROW_QPID_ERROR(PROTOCOL_ERROR + 504, "Invalid message sequence: got header before publish."); + } + message->setHeader(header); + if (stagingThreshold && header->getContentSize() >= stagingThreshold) { + store->stage(*message); + message->releaseContent(store); + } else { + auto_ptr content(new InMemoryContent()); + message->setContent(content); + } + route(); +} + +void MessageBuilder::addContent(AMQContentBody::shared_ptr& content){ + if(!message.get()){ + THROW_QPID_ERROR(PROTOCOL_ERROR + 504, "Invalid message sequence: got content before publish."); + } + message->addContent(content); + route(); +} diff --git a/cpp/src/qpid/broker/MessageBuilder.h b/cpp/src/qpid/broker/MessageBuilder.h new file mode 100644 index 0000000000..7c4a529a64 --- /dev/null +++ b/cpp/src/qpid/broker/MessageBuilder.h @@ -0,0 +1,57 @@ +/* + * + * 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 _MessageBuilder_ +#define _MessageBuilder_ + +#include +#include "qpid/QpidError.h" +#include "BrokerExchange.h" +#include "BrokerMessage.h" +#include "MessageStore.h" +#include "qpid/framing/AMQContentBody.h" +#include "qpid/framing/AMQHeaderBody.h" +#include "qpid/framing/BasicPublishBody.h" +#include "CompletionHandler.h" + +namespace qpid { + namespace broker { + class MessageBuilder{ + public: + MessageBuilder(CompletionHandler* _handler, + MessageStore* const store = 0, + uint64_t stagingThreshold = 0); + void initialise(Message::shared_ptr& msg); + void setHeader(framing::AMQHeaderBody::shared_ptr& header); + void addContent(framing::AMQContentBody::shared_ptr& content); + Message::shared_ptr getMessage() { return message; } + private: + Message::shared_ptr message; + CompletionHandler* handler; + MessageStore* const store; + const uint64_t stagingThreshold; + + void route(); + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/MessageHandlerImpl.cpp b/cpp/src/qpid/broker/MessageHandlerImpl.cpp new file mode 100644 index 0000000000..22011169a2 --- /dev/null +++ b/cpp/src/qpid/broker/MessageHandlerImpl.cpp @@ -0,0 +1,243 @@ +/* + * + * 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/QpidError.h" +#include "MessageHandlerImpl.h" +#include "BrokerChannel.h" +#include "qpid/framing/FramingContent.h" +#include "Connection.h" +#include "Broker.h" +#include "BrokerMessageMessage.h" +#include "qpid/framing/MessageAppendBody.h" +#include "qpid/framing/MessageTransferBody.h" +#include "BrokerAdapter.h" + +namespace qpid { +namespace broker { + +using namespace framing; + +MessageHandlerImpl::MessageHandlerImpl(CoreRefs& parent) + : HandlerImplType(parent) {} + +// +// Message class method handlers +// + +void +MessageHandlerImpl::cancel(const MethodContext& context, + const string& destination ) +{ + channel.cancel(destination); + client.ok(context.getRequestId()); +} + +void +MessageHandlerImpl::open(const MethodContext& context, + const string& reference) +{ + references.open(reference); + client.ok(context.getRequestId()); +} + +void +MessageHandlerImpl::append(const MethodContext& context, + const string& reference, + const string& /*bytes*/ ) +{ + references.get(reference)->append( + boost::shared_polymorphic_downcast( + context.methodBody)); + client.ok(context.getRequestId()); +} + +void +MessageHandlerImpl::close(const MethodContext& context, + const string& reference) +{ + Reference::shared_ptr ref = references.get(reference); + client.ok(context.getRequestId()); + + // Send any transfer messages to their correct exchanges and okay them + const Reference::Messages& msgs = ref->getMessages(); + for (Reference::Messages::const_iterator m = msgs.begin(); m != msgs.end(); ++m) { + channel.handleInlineTransfer(*m); + client.ok((*m)->getRequestId()); + } + ref->close(); +} + +void +MessageHandlerImpl::checkpoint(const MethodContext& context, + const string& /*reference*/, + const string& /*identifier*/ ) +{ + // Initial implementation (which is conforming) is to do nothing here + // and return offset zero for the resume + client.ok(context.getRequestId()); +} + +void +MessageHandlerImpl::resume(const MethodContext& context, + const string& reference, + const string& /*identifier*/ ) +{ + // Initial (null) implementation + // open reference and return 0 offset + references.open(reference); + client.offset(0, context.getRequestId()); +} + +void +MessageHandlerImpl::offset(const MethodContext&, + uint64_t /*value*/ ) +{ + // Shouldn't ever receive this as it is reponse to resume + // which is never sent + // TODO astitcher 2007-02-16 What is the correct exception to throw here? + THROW_QPID_ERROR(INTERNAL_ERROR, "impossible"); +} + +void +MessageHandlerImpl::consume(const MethodContext& context, + uint16_t /*ticket*/, + const string& queueName, + const string& destination, + bool noLocal, + bool noAck, + bool exclusive, + const framing::FieldTable& filter ) +{ + Queue::shared_ptr queue = connection.getQueue(queueName, channel.getId()); + if(!destination.empty() && channel.exists(destination)) + throw ConnectionException(530, "Consumer tags must be unique"); + string tag = destination; + channel.consume( + tag, queue, !noAck, exclusive, + noLocal ? &connection : 0, &filter); + client.ok(context.getRequestId()); + // Dispatch messages as there is now a consumer. + queue->dispatch(); +} + +void +MessageHandlerImpl::get( const MethodContext& context, + uint16_t /*ticket*/, + const string& queueName, + const string& destination, + bool noAck ) +{ + Queue::shared_ptr queue = + connection.getQueue(queueName, context.channel->getId()); + + if(channel.get(queue, destination, !noAck)) + client.ok(context.getRequestId()); + else + client.empty(context.getRequestId()); +} + +void +MessageHandlerImpl::empty( const MethodContext& ) +{ + // Shouldn't ever receive this as it is a response to get + // which is never sent + // TODO astitcher 2007-02-09 What is the correct exception to throw here? + THROW_QPID_ERROR(INTERNAL_ERROR, "Impossible"); +} + +void +MessageHandlerImpl::ok(const MethodContext& /*context*/) +{ + channel.ack(); +} + +void +MessageHandlerImpl::qos(const MethodContext& context, + uint32_t prefetchSize, + uint16_t prefetchCount, + bool /*global*/ ) +{ + //TODO: handle global + channel.setPrefetchSize(prefetchSize); + channel.setPrefetchCount(prefetchCount); + client.ok(context.getRequestId()); +} + +void +MessageHandlerImpl::recover(const MethodContext& context, + bool requeue) +{ + channel.recover(requeue); + client.ok(context.getRequestId()); +} + +void +MessageHandlerImpl::reject(const MethodContext& /*context*/, + uint16_t /*code*/, + const string& /*text*/ ) +{ + channel.ack(); + // channel.requeue(); +} + +void +MessageHandlerImpl::transfer(const MethodContext& context, + uint16_t /*ticket*/, + const string& /* destination */, + bool /*redelivered*/, + bool /*immediate*/, + uint64_t /*ttl*/, + uint8_t /*priority*/, + uint64_t /*timestamp*/, + uint8_t /*deliveryMode*/, + uint64_t /*expiration*/, + const string& /*exchangeName*/, + const string& /*routingKey*/, + const string& /*messageId*/, + const string& /*correlationId*/, + const string& /*replyTo*/, + const string& /*contentType*/, + const string& /*contentEncoding*/, + const string& /*userId*/, + const string& /*appId*/, + const string& /*transactionId*/, + const string& /*securityToken*/, + const framing::FieldTable& /*applicationHeaders*/, + const framing::Content& body, + bool /*mandatory*/) +{ + MessageTransferBody::shared_ptr transfer( + boost::shared_polymorphic_downcast( + context.methodBody)); + RequestId requestId = context.getRequestId(); + + if (body.isInline()) { + MessageMessage::shared_ptr message( + new MessageMessage(&connection, requestId, transfer)); + channel.handleInlineTransfer(message); + client.ok(requestId); + } else { + Reference::shared_ptr ref(references.get(body.getValue())); + MessageMessage::shared_ptr message( + new MessageMessage(&connection, requestId, transfer, ref)); + ref->addMessage(message); + } +} + + +}} // namespace qpid::broker diff --git a/cpp/src/qpid/broker/MessageHandlerImpl.h b/cpp/src/qpid/broker/MessageHandlerImpl.h new file mode 100644 index 0000000000..47c8cf0ce0 --- /dev/null +++ b/cpp/src/qpid/broker/MessageHandlerImpl.h @@ -0,0 +1,130 @@ +#ifndef _broker_MessageHandlerImpl_h +#define _broker_MessageHandlerImpl_h + +/* + * + * 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 + +#include "qpid/framing/AMQP_ServerOperations.h" +#include "qpid/framing/AMQP_ClientProxy.h" +#include "Reference.h" +#include "HandlerImpl.h" + +namespace qpid { +namespace broker { + +class Connection; +class Broker; +class MessageMessage; + +class MessageHandlerImpl : + public framing::AMQP_ServerOperations::MessageHandler, + public HandlerImpl +{ + public: + MessageHandlerImpl(CoreRefs& parent); + + void append(const framing::MethodContext&, + const std::string& reference, + const std::string& bytes ); + + void cancel(const framing::MethodContext&, + const std::string& destination ); + + void checkpoint(const framing::MethodContext&, + const std::string& reference, + const std::string& identifier ); + + void close(const framing::MethodContext&, + const std::string& reference ); + + void consume(const framing::MethodContext&, + uint16_t ticket, + const std::string& queue, + const std::string& destination, + bool noLocal, + bool noAck, + bool exclusive, + const framing::FieldTable& filter ); + + void empty( const framing::MethodContext& ); + + void get(const framing::MethodContext&, + uint16_t ticket, + const std::string& queue, + const std::string& destination, + bool noAck ); + + void offset(const framing::MethodContext&, + uint64_t value ); + + void ok( const framing::MethodContext& ); + + void open(const framing::MethodContext&, + const std::string& reference ); + + void qos(const framing::MethodContext&, + uint32_t prefetchSize, + uint16_t prefetchCount, + bool global ); + + void recover(const framing::MethodContext&, + bool requeue ); + + void reject(const framing::MethodContext&, + uint16_t code, + const std::string& text ); + + void resume(const framing::MethodContext&, + const std::string& reference, + const std::string& identifier ); + + void transfer(const framing::MethodContext&, + uint16_t ticket, + const std::string& destination, + bool redelivered, + bool immediate, + uint64_t ttl, + uint8_t priority, + uint64_t timestamp, + uint8_t deliveryMode, + uint64_t expiration, + const std::string& exchange, + const std::string& routingKey, + const std::string& messageId, + const std::string& correlationId, + const std::string& replyTo, + const std::string& contentType, + const std::string& contentEncoding, + const std::string& userId, + const std::string& appId, + const std::string& transactionId, + const std::string& securityToken, + const framing::FieldTable& applicationHeaders, + const framing::Content& body, + bool mandatory ); + private: + ReferenceRegistry references; +}; + +}} // namespace qpid::broker + + + +#endif /*!_broker_MessageHandlerImpl_h*/ diff --git a/cpp/src/qpid/broker/MessageStore.h b/cpp/src/qpid/broker/MessageStore.h new file mode 100644 index 0000000000..1d9ee86e48 --- /dev/null +++ b/cpp/src/qpid/broker/MessageStore.h @@ -0,0 +1,129 @@ +/* + * + * 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 _MessageStore_ +#define _MessageStore_ + +#include "PersistableExchange.h" +#include "PersistableMessage.h" +#include "PersistableQueue.h" +#include "RecoveryManager.h" +#include "TransactionalStore.h" + +namespace qpid { +namespace broker { + +/** + * An abstraction of the persistent storage for messages. (In + * all methods, any pointers/references to queues or messages + * are valid only for the duration of the call). + */ +class MessageStore : public TransactionalStore{ +public: + /** + * Record the existence of a durable queue + */ + virtual void create(const PersistableQueue& queue) = 0; + /** + * Destroy a durable queue + */ + virtual void destroy(const PersistableQueue& queue) = 0; + + /** + * Record the existence of a durable exchange + */ + virtual void create(const PersistableExchange& exchange) = 0; + /** + * Destroy a durable exchange + */ + virtual void destroy(const PersistableExchange& exchange) = 0; + + /** + * Request recovery of queue and message state from store + */ + virtual void recover(RecoveryManager& queues) = 0; + + /** + * Stores a messages before it has been enqueued + * (enqueueing automatically stores the message so this is + * only required if storage is required prior to that + * point). If the message has not yet been stored it will + * store the headers as well as any content passed in. A + * persistence id will be set on the message which can be + * used to load the content or to append to it. + */ + virtual void stage(PersistableMessage& msg) = 0; + + /** + * Destroys a previously staged message. This only needs + * to be called if the message is never enqueued. (Once + * enqueued, deletion will be automatic when the message + * is dequeued from all queues it was enqueued onto). + */ + virtual void destroy(PersistableMessage& msg) = 0; + + /** + * Appends content to a previously staged message + */ + virtual void appendContent(PersistableMessage& msg, const std::string& data) = 0; + + /** + * Loads (a section) of content data for the specified + * message (previously stored through a call to stage or + * enqueue) into data. The offset refers to the content + * only (i.e. an offset of 0 implies that the start of the + * content should be loaded, not the headers or related + * meta-data). + */ + virtual void loadContent(PersistableMessage& msg, std::string& data, uint64_t offset, uint32_t length) = 0; + + /** + * Enqueues a message, storing the message if it has not + * been previously stored and recording that the given + * message is on the given queue. + * + * @param msg the message to enqueue + * @param queue the name of the queue onto which it is to be enqueued + * @param xid (a pointer to) an identifier of the + * distributed transaction in which the operation takes + * place or null for 'local' transactions + */ + virtual void enqueue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue) = 0; + /** + * Dequeues a message, recording that the given message is + * no longer on the given queue and deleting the message + * if it is no longer on any other queue. + * + * @param msg the message to dequeue + * @param queue the name of th queue from which it is to be dequeued + * @param xid (a pointer to) an identifier of the + * distributed transaction in which the operation takes + * place or null for 'local' transactions + */ + virtual void dequeue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue) = 0; + + virtual ~MessageStore(){} +}; + +} +} + + +#endif diff --git a/cpp/src/qpid/broker/MessageStoreModule.cpp b/cpp/src/qpid/broker/MessageStoreModule.cpp new file mode 100644 index 0000000000..17e5d3cca8 --- /dev/null +++ b/cpp/src/qpid/broker/MessageStoreModule.cpp @@ -0,0 +1,114 @@ +/* + * + * 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 "MessageStoreModule.h" +#include + +using namespace qpid::broker; + +MessageStoreModule::MessageStoreModule(const std::string& name) : store(name) +{ +} + +void MessageStoreModule::create(const PersistableQueue& queue) +{ + store->create(queue); +} + +void MessageStoreModule::destroy(const PersistableQueue& queue) +{ + store->destroy(queue); +} + +void MessageStoreModule::create(const PersistableExchange& exchange) +{ + store->create(exchange); +} + +void MessageStoreModule::destroy(const PersistableExchange& exchange) +{ + store->destroy(exchange); +} + +void MessageStoreModule::recover(RecoveryManager& registry) +{ + store->recover(registry); +} + +void MessageStoreModule::stage(PersistableMessage& msg) +{ + store->stage(msg); +} + +void MessageStoreModule::destroy(PersistableMessage& msg) +{ + store->destroy(msg); +} + +void MessageStoreModule::appendContent(PersistableMessage& msg, const std::string& data) +{ + store->appendContent(msg, data); +} + +void MessageStoreModule::loadContent(PersistableMessage& msg, string& data, uint64_t offset, uint32_t length) +{ + store->loadContent(msg, data, offset, length); +} + +void MessageStoreModule::enqueue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue) +{ + store->enqueue(ctxt, msg, queue); +} + +void MessageStoreModule::dequeue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue) +{ + store->dequeue(ctxt, msg, queue); +} + +std::auto_ptr MessageStoreModule::begin() +{ + return store->begin(); +} + +std::auto_ptr MessageStoreModule::begin(const std::string& xid) +{ + return store->begin(xid); +} + +void MessageStoreModule::prepare(TPCTransactionContext& txn) +{ + store->prepare(txn); +} + +void MessageStoreModule::commit(TransactionContext& ctxt) +{ + store->commit(ctxt); +} + +void MessageStoreModule::abort(TransactionContext& ctxt) +{ + store->abort(ctxt); +} + +void MessageStoreModule::collectPreparedXids(std::set& xids) +{ + store->collectPreparedXids(xids); +} diff --git a/cpp/src/qpid/broker/MessageStoreModule.h b/cpp/src/qpid/broker/MessageStoreModule.h new file mode 100644 index 0000000000..59c45f68f6 --- /dev/null +++ b/cpp/src/qpid/broker/MessageStoreModule.h @@ -0,0 +1,68 @@ +/* + * + * 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 _MessageStoreModule_ +#define _MessageStoreModule_ + +#include "BrokerMessage.h" +#include "MessageStore.h" +#include "BrokerQueue.h" +#include "RecoveryManager.h" +#include "qpid/sys/Module.h" + +namespace qpid { +namespace broker { + +/** + * A null implementation of the MessageStore interface + */ +class MessageStoreModule : public MessageStore +{ + qpid::sys::Module store; +public: + MessageStoreModule(const std::string& name); + + std::auto_ptr begin(); + std::auto_ptr begin(const std::string& xid); + void prepare(TPCTransactionContext& txn); + void commit(TransactionContext& txn); + void abort(TransactionContext& txn); + void collectPreparedXids(std::set& xids); + + void create(const PersistableQueue& queue); + void destroy(const PersistableQueue& queue); + void create(const PersistableExchange& exchange); + void destroy(const PersistableExchange& exchange); + void recover(RecoveryManager& queues); + void stage(PersistableMessage& msg); + void destroy(PersistableMessage& msg); + void appendContent(PersistableMessage& msg, const std::string& data); + void loadContent(PersistableMessage& msg, std::string& data, uint64_t offset, uint32_t length); + void enqueue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue); + void dequeue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue); + + ~MessageStoreModule(){} +}; + +} +} + + +#endif diff --git a/cpp/src/qpid/broker/NameGenerator.cpp b/cpp/src/qpid/broker/NameGenerator.cpp new file mode 100644 index 0000000000..8484f921e9 --- /dev/null +++ b/cpp/src/qpid/broker/NameGenerator.cpp @@ -0,0 +1,32 @@ +/* + * + * 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 "NameGenerator.h" +#include + +using namespace qpid::broker; + +NameGenerator::NameGenerator(const std::string& _base) : base(_base), counter(1) {} + +std::string NameGenerator::generate(){ + std::stringstream ss; + ss << base << counter++; + return ss.str(); +} diff --git a/cpp/src/qpid/broker/NameGenerator.h b/cpp/src/qpid/broker/NameGenerator.h new file mode 100644 index 0000000000..affcedba41 --- /dev/null +++ b/cpp/src/qpid/broker/NameGenerator.h @@ -0,0 +1,39 @@ +/* + * + * 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 _NameGenerator_ +#define _NameGenerator_ + +#include "BrokerMessage.h" + +namespace qpid { + namespace broker { + class NameGenerator{ + const std::string base; + unsigned int counter; + public: + NameGenerator(const std::string& base); + std::string generate(); + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/NullMessageStore.cpp b/cpp/src/qpid/broker/NullMessageStore.cpp new file mode 100644 index 0000000000..393bbb8f02 --- /dev/null +++ b/cpp/src/qpid/broker/NullMessageStore.cpp @@ -0,0 +1,110 @@ +/* + * + * 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 "NullMessageStore.h" + +#include "RecoveryManager.h" + +#include + +using namespace qpid::broker; + +NullMessageStore::NullMessageStore(bool _warn) : warn(_warn){} + +void NullMessageStore::create(const PersistableQueue& queue) +{ + if (warn) std::cout << "WARNING: Can't create durable queue '" << queue.getName() << "'. Persistence not enabled." << std::endl; +} + +void NullMessageStore::destroy(const PersistableQueue& queue) +{ + if (warn) std::cout << "WARNING: Can't destroy durable queue '" << queue.getName() << "'. Persistence not enabled." << std::endl; +} + +void NullMessageStore::create(const PersistableExchange&) +{ +} + +void NullMessageStore::destroy(const PersistableExchange&) +{ +} + +void NullMessageStore::recover(RecoveryManager&) +{ + if (warn) std::cout << "WARNING: Persistence not enabled, no recovery of queues or messages." << std::endl; +} + +void NullMessageStore::stage(PersistableMessage&) +{ + if (warn) std::cout << "WARNING: Can't stage message. Persistence not enabled." << std::endl; +} + +void NullMessageStore::destroy(PersistableMessage&) +{ + if (warn) std::cout << "WARNING: No need to destroy staged message. Persistence not enabled." << std::endl; +} + +void NullMessageStore::appendContent(PersistableMessage&, const string&) +{ + if (warn) std::cout << "WARNING: Can't append content. Persistence not enabled." << std::endl; +} + +void NullMessageStore::loadContent(PersistableMessage&, string&, uint64_t, uint32_t) +{ + if (warn) std::cout << "WARNING: Can't load content. Persistence not enabled." << std::endl; +} + +void NullMessageStore::enqueue(TransactionContext*, PersistableMessage&, const PersistableQueue& queue) +{ + if (warn) std::cout << "WARNING: Can't enqueue message onto '" << queue.getName() << "'. Persistence not enabled." << std::endl; +} + +void NullMessageStore::dequeue(TransactionContext*, PersistableMessage&, const PersistableQueue& queue) +{ + if (warn) std::cout << "WARNING: Can't dequeue message from '" << queue.getName() << "'. Persistence not enabled." << std::endl; +} + +std::auto_ptr NullMessageStore::begin() +{ + return std::auto_ptr(); +} + +std::auto_ptr NullMessageStore::begin(const std::string&) +{ + return std::auto_ptr(); +} + +void NullMessageStore::prepare(TPCTransactionContext&) +{ +} + +void NullMessageStore::commit(TransactionContext&) +{ +} + +void NullMessageStore::abort(TransactionContext&) +{ +} + +void NullMessageStore::collectPreparedXids(std::set&) +{ + +} diff --git a/cpp/src/qpid/broker/NullMessageStore.h b/cpp/src/qpid/broker/NullMessageStore.h new file mode 100644 index 0000000000..e0b215bb39 --- /dev/null +++ b/cpp/src/qpid/broker/NullMessageStore.h @@ -0,0 +1,65 @@ +/* + * + * 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 _NullMessageStore_ +#define _NullMessageStore_ + +#include "BrokerMessage.h" +#include "MessageStore.h" +#include "BrokerQueue.h" + +namespace qpid { +namespace broker { + +/** + * A null implementation of the MessageStore interface + */ +class NullMessageStore : public MessageStore +{ + const bool warn; +public: + NullMessageStore(bool warn = false); + + virtual std::auto_ptr begin(); + virtual std::auto_ptr begin(const std::string& xid); + virtual void prepare(TPCTransactionContext& txn); + virtual void commit(TransactionContext& txn); + virtual void abort(TransactionContext& txn); + virtual void collectPreparedXids(std::set& xids); + + virtual void create(const PersistableQueue& queue); + virtual void destroy(const PersistableQueue& queue); + virtual void create(const PersistableExchange& exchange); + virtual void destroy(const PersistableExchange& exchange); + virtual void recover(RecoveryManager& queues); + virtual void stage(PersistableMessage& msg); + virtual void destroy(PersistableMessage& msg); + virtual void appendContent(PersistableMessage& msg, const std::string& data); + virtual void loadContent(PersistableMessage& msg, std::string& data, uint64_t offset, uint32_t length); + virtual void enqueue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue); + virtual void dequeue(TransactionContext* ctxt, PersistableMessage& msg, const PersistableQueue& queue); + ~NullMessageStore(){} +}; + +} +} + + +#endif diff --git a/cpp/src/qpid/broker/Persistable.h b/cpp/src/qpid/broker/Persistable.h new file mode 100644 index 0000000000..3fb96d8568 --- /dev/null +++ b/cpp/src/qpid/broker/Persistable.h @@ -0,0 +1,62 @@ +#ifndef _broker_Persistable_h +#define _broker_Persistable_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/amqp_types.h" +#include "qpid/framing/Buffer.h" + +namespace qpid { +namespace broker { + +/** + * Base class for all persistable objects + */ +class Persistable +{ +public: + /** + * Allows the store to attach its own identifier to this object + */ + virtual void setPersistenceId(uint64_t id) const = 0; + /** + * Returns any identifier the store may have attached to this + * object + */ + virtual uint64_t getPersistenceId() const = 0; + /** + * Encodes the persistable state of this object into the supplied + * buffer + */ + virtual void encode(framing::Buffer& buffer) const = 0; + /** + * @returns the size of the buffer needed to encode this object + */ + virtual uint32_t encodedSize() const = 0; + + virtual ~Persistable() {}; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/broker/PersistableExchange.h b/cpp/src/qpid/broker/PersistableExchange.h new file mode 100644 index 0000000000..9badf5f609 --- /dev/null +++ b/cpp/src/qpid/broker/PersistableExchange.h @@ -0,0 +1,44 @@ +#ifndef _broker_PersistableExchange_h +#define _broker_PersistableExchange_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 +#include "Persistable.h" + +namespace qpid { +namespace broker { + +/** + * The interface exchanges must expose to the MessageStore in order to be + * persistable. + */ +class PersistableExchange : public Persistable +{ +public: + virtual ~PersistableExchange() {}; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/broker/PersistableMessage.h b/cpp/src/qpid/broker/PersistableMessage.h new file mode 100644 index 0000000000..11a12d68e1 --- /dev/null +++ b/cpp/src/qpid/broker/PersistableMessage.h @@ -0,0 +1,53 @@ +#ifndef _broker_PersistableMessage_h +#define _broker_PersistableMessage_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 +#include +#include "Persistable.h" +#include "qpid/framing/amqp_types.h" + +namespace qpid { +namespace broker { + +/** + * The interface messages must expose to the MessageStore in order to + * be persistable. + */ + class PersistableMessage : public Persistable +{ +public: + typedef boost::shared_ptr shared_ptr; + + /** + * @returns the size of the headers when encoded + */ + virtual uint32_t encodedHeaderSize() const = 0; + + virtual ~PersistableMessage() {}; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/broker/PersistableQueue.h b/cpp/src/qpid/broker/PersistableQueue.h new file mode 100644 index 0000000000..5dd91dde9b --- /dev/null +++ b/cpp/src/qpid/broker/PersistableQueue.h @@ -0,0 +1,45 @@ +#ifndef _broker_PersistableQueue_h +#define _broker_PersistableQueue_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 +#include "Persistable.h" + +namespace qpid { +namespace broker { + +/** + * The interface queues must expose to the MessageStore in order to be + * persistable. + */ +class PersistableQueue : public Persistable +{ +public: + virtual const std::string& getName() const = 0; + virtual ~PersistableQueue() {}; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/broker/Prefetch.h b/cpp/src/qpid/broker/Prefetch.h new file mode 100644 index 0000000000..8eb27a3e21 --- /dev/null +++ b/cpp/src/qpid/broker/Prefetch.h @@ -0,0 +1,42 @@ +/* + * + * 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 _Prefetch_ +#define _Prefetch_ + +#include "qpid/framing/amqp_types.h" + +namespace qpid { + namespace broker { + /** + * Count and total size of asynchronously delivered + * (i.e. pushed) messages that have acks outstanding. + */ + struct Prefetch{ + uint32_t size; + uint16_t count; + + void reset() { size = 0; count = 0; } + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/QueuePolicy.cpp b/cpp/src/qpid/broker/QueuePolicy.cpp new file mode 100644 index 0000000000..620822cddc --- /dev/null +++ b/cpp/src/qpid/broker/QueuePolicy.cpp @@ -0,0 +1,69 @@ +/* + * + * 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 "QueuePolicy.h" + +using namespace qpid::broker; +using namespace qpid::framing; + +QueuePolicy::QueuePolicy(uint32_t _maxCount, uint64_t _maxSize) : + maxCount(_maxCount), maxSize(_maxSize), count(0), size(0) {} + +QueuePolicy::QueuePolicy(const FieldTable& settings) : + maxCount(getInt(settings, maxCountKey, 0)), + maxSize(getInt(settings, maxSizeKey, 0)), count(0), size(0) {} + +void QueuePolicy::enqueued(uint64_t _size) +{ + if (maxCount) count++; + if (maxSize) size += _size; +} + +void QueuePolicy::dequeued(uint64_t _size) +{ + if (maxCount) count--; + if (maxSize) size -= _size; +} + +bool QueuePolicy::limitExceeded() +{ + return (maxSize && size > maxSize) || (maxCount && count > maxCount); +} + +void QueuePolicy::update(FieldTable& settings) +{ + if (maxCount) settings.setInt(maxCountKey, maxCount); + if (maxSize) settings.setInt(maxSizeKey, maxSize); +} + + +int QueuePolicy::getInt(const FieldTable& settings, const std::string& key, int defaultValue) +{ + //Note: currently field table only contain signed 32 bit ints, which + // restricts the values that can be set on the queue policy. + try { + return settings.getInt(key); + } catch (FieldNotFoundException& ignore) { + return defaultValue; + } +} + +const std::string QueuePolicy::maxCountKey("qpid.max_count"); +const std::string QueuePolicy::maxSizeKey("qpid.max_size"); diff --git a/cpp/src/qpid/broker/QueuePolicy.h b/cpp/src/qpid/broker/QueuePolicy.h new file mode 100644 index 0000000000..0cc7070e09 --- /dev/null +++ b/cpp/src/qpid/broker/QueuePolicy.h @@ -0,0 +1,54 @@ +/* + * + * 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 _QueuePolicy_ +#define _QueuePolicy_ + +#include "qpid/framing/FieldTable.h" + +namespace qpid { + namespace broker { + class QueuePolicy + { + static const std::string maxCountKey; + static const std::string maxSizeKey; + + const uint32_t maxCount; + const uint64_t maxSize; + uint32_t count; + uint64_t size; + + static int getInt(const qpid::framing::FieldTable& settings, const std::string& key, int defaultValue); + + public: + QueuePolicy(uint32_t maxCount, uint64_t maxSize); + QueuePolicy(const qpid::framing::FieldTable& settings); + void enqueued(uint64_t size); + void dequeued(uint64_t size); + void update(qpid::framing::FieldTable& settings); + bool limitExceeded(); + uint32_t getMaxCount() const { return maxCount; } + uint64_t getMaxSize() const { return maxSize; } + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/QueueRegistry.cpp b/cpp/src/qpid/broker/QueueRegistry.cpp new file mode 100644 index 0000000000..dcaf7ec0f6 --- /dev/null +++ b/cpp/src/qpid/broker/QueueRegistry.cpp @@ -0,0 +1,78 @@ +/* + * + * 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 "QueueRegistry.h" +#include +#include + +using namespace qpid::broker; +using namespace qpid::sys; + +QueueRegistry::QueueRegistry(MessageStore* const _store) : counter(1), store(_store){} + +QueueRegistry::~QueueRegistry(){} + +std::pair +QueueRegistry::declare(const string& declareName, bool durable, + uint32_t autoDelete, const ConnectionToken* owner) +{ + Mutex::ScopedLock locker(lock); + string name = declareName.empty() ? generateName() : declareName; + assert(!name.empty()); + QueueMap::iterator i = queues.find(name); + if (i == queues.end()) { + Queue::shared_ptr queue(new Queue(name, autoDelete, durable ? store : 0, owner)); + queues[name] = queue; + return std::pair(queue, true); + } else { + return std::pair(i->second, false); + } +} + +void QueueRegistry::destroy(const string& name){ + Mutex::ScopedLock locker(lock); + queues.erase(name); +} + +Queue::shared_ptr QueueRegistry::find(const string& name){ + Mutex::ScopedLock locker(lock); + QueueMap::iterator i = queues.find(name); + if (i == queues.end()) { + return Queue::shared_ptr(); + } else { + return i->second; + } +} + +string QueueRegistry::generateName(){ + string name; + do { + std::stringstream ss; + ss << "tmp_" << counter++; + name = ss.str(); + // Thread safety: Private function, only called with lock held + // so this is OK. + } while(queues.find(name) != queues.end()); + return name; +} + +MessageStore* const QueueRegistry::getStore() const { + return store; +} diff --git a/cpp/src/qpid/broker/QueueRegistry.h b/cpp/src/qpid/broker/QueueRegistry.h new file mode 100644 index 0000000000..bd53cba817 --- /dev/null +++ b/cpp/src/qpid/broker/QueueRegistry.h @@ -0,0 +1,96 @@ +/* + * + * 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 _QueueRegistry_ +#define _QueueRegistry_ + +#include +#include "qpid/sys/Monitor.h" +#include "BrokerQueue.h" + +namespace qpid { +namespace broker { + +/** + * A registry of queues indexed by queue name. + * + * Queues are reference counted using shared_ptr to ensure that they + * are deleted when and only when they are no longer in use. + * + */ +class QueueRegistry{ + + public: + QueueRegistry(MessageStore* const store = 0); + ~QueueRegistry(); + + /** + * Declare a queue. + * + * @return The queue and a boolean flag which is true if the queue + * was created by this declare call false if it already existed. + */ + std::pair declare(const string& name, bool durable = false, uint32_t autodelete = 0, + const ConnectionToken* const owner = 0); + + /** + * Destroy the named queue. + * + * Note: if the queue is in use it is not actually destroyed until + * all shared_ptrs to it are destroyed. During that time it is + * possible that a new queue with the same name may be + * created. This should not create any problems as the new and + * old queues exist independently. The registry has + * forgotten the old queue so there can be no confusion for + * subsequent calls to find or declare with the same name. + * + */ + void destroy(const string& name); + + /** + * Find the named queue. Return 0 if not found. + */ + Queue::shared_ptr find(const string& name); + + /** + * Generate unique queue name. + */ + string generateName(); + + /** + * Return the message store used. + */ + MessageStore* const getStore() const; + + + private: + typedef std::map QueueMap; + QueueMap queues; + qpid::sys::Mutex lock; + int counter; + MessageStore* const store; +}; + + +} +} + + +#endif diff --git a/cpp/src/qpid/broker/RecoverableMessage.h b/cpp/src/qpid/broker/RecoverableMessage.h new file mode 100644 index 0000000000..f755fdf727 --- /dev/null +++ b/cpp/src/qpid/broker/RecoverableMessage.h @@ -0,0 +1,58 @@ +#ifndef _broker_RecoverableMessage_h +#define _broker_RecoverableMessage_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 +#include "qpid/framing/amqp_types.h" +#include "qpid/framing/Buffer.h" + +namespace qpid { +namespace broker { + +/** + * The interface through which messages are reloaded on recovery. + */ +class RecoverableMessage +{ +public: + typedef boost::shared_ptr shared_ptr; + virtual void setPersistenceId(uint64_t id) = 0; + /** + * Used by store to determine whether to load content on recovery + * or let message load its own content as and when it requires it. + * + * @returns true if the content of the message should be loaded + */ + virtual bool loadContent(uint64_t available) = 0; + /** + * Loads the content held in the supplied buffer (may do checking + * of length as necessary) + */ + virtual void decodeContent(framing::Buffer& buffer) = 0; + virtual ~RecoverableMessage() {}; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/broker/RecoverableQueue.h b/cpp/src/qpid/broker/RecoverableQueue.h new file mode 100644 index 0000000000..656a5f0149 --- /dev/null +++ b/cpp/src/qpid/broker/RecoverableQueue.h @@ -0,0 +1,51 @@ +#ifndef _broker_RecoverableQueue_h +#define _broker_RecoverableQueue_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 "RecoverableMessage.h" +#include + +namespace qpid { +namespace broker { + +/** + * The interface through which messages are added back to queues on + * recovery. + */ +class RecoverableQueue +{ +public: + typedef boost::shared_ptr shared_ptr; + + virtual void setPersistenceId(uint64_t id) = 0; + /** + * Used during recovery to add stored messages back to the queue + */ + virtual void recover(RecoverableMessage::shared_ptr msg) = 0; + virtual ~RecoverableQueue() {}; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/broker/RecoveryManager.h b/cpp/src/qpid/broker/RecoveryManager.h new file mode 100644 index 0000000000..27f3d9355c --- /dev/null +++ b/cpp/src/qpid/broker/RecoveryManager.h @@ -0,0 +1,45 @@ +/* + * + * 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 _RecoveryManager_ +#define _RecoveryManager_ + +#include "RecoverableQueue.h" +#include "RecoverableMessage.h" +#include "qpid/framing/Buffer.h" + +namespace qpid { +namespace broker { + + class RecoveryManager{ + public: + virtual ~RecoveryManager(){} + virtual void recoverExchange(framing::Buffer& buffer) = 0; + virtual RecoverableQueue::shared_ptr recoverQueue(framing::Buffer& buffer) = 0; + virtual RecoverableMessage::shared_ptr recoverMessage(framing::Buffer& buffer) = 0; + virtual void recoveryComplete() = 0; + }; + + +} +} + + +#endif diff --git a/cpp/src/qpid/broker/RecoveryManagerImpl.cpp b/cpp/src/qpid/broker/RecoveryManagerImpl.cpp new file mode 100644 index 0000000000..f82399f95c --- /dev/null +++ b/cpp/src/qpid/broker/RecoveryManagerImpl.cpp @@ -0,0 +1,143 @@ +/* + * + * 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 "RecoveryManagerImpl.h" + +#include "BrokerMessage.h" +#include "BrokerMessageMessage.h" +#include "BrokerQueue.h" + +using namespace qpid; +using namespace qpid::broker; +using boost::dynamic_pointer_cast; + + +static const uint8_t BASIC = 1; +static const uint8_t MESSAGE = 2; + +RecoveryManagerImpl::RecoveryManagerImpl(QueueRegistry& _queues, ExchangeRegistry& _exchanges, uint64_t _stagingThreshold) + : queues(_queues), exchanges(_exchanges), stagingThreshold(_stagingThreshold) {} + +RecoveryManagerImpl::~RecoveryManagerImpl() {} + +class RecoverableMessageImpl : public RecoverableMessage +{ + Message::shared_ptr msg; + const uint64_t stagingThreshold; +public: + RecoverableMessageImpl(Message::shared_ptr& _msg, uint64_t _stagingThreshold) + : msg(_msg), stagingThreshold(_stagingThreshold) {} + ~RecoverableMessageImpl() {}; + void setPersistenceId(uint64_t id); + bool loadContent(uint64_t available); + void decodeContent(framing::Buffer& buffer); + void recover(Queue::shared_ptr queue); +}; + +class RecoverableQueueImpl : public RecoverableQueue +{ + Queue::shared_ptr queue; +public: + RecoverableQueueImpl(Queue::shared_ptr& _queue) : queue(_queue) {} + ~RecoverableQueueImpl() {}; + void setPersistenceId(uint64_t id); + void recover(RecoverableMessage::shared_ptr msg); +}; + +void RecoveryManagerImpl::recoverExchange(framing::Buffer&) +{ + //TODO +} + +RecoverableQueue::shared_ptr RecoveryManagerImpl::recoverQueue(framing::Buffer& buffer) +{ + Queue::shared_ptr queue = Queue::decode(queues, buffer); + try { + Exchange::shared_ptr exchange = exchanges.getDefault(); + if (exchange) { + exchange->bind(queue, queue->getName(), 0); + } + } catch (ChannelException& e) { + //assume no default exchange has been declared + } + return RecoverableQueue::shared_ptr(new RecoverableQueueImpl(queue)); +} + +RecoverableMessage::shared_ptr RecoveryManagerImpl::recoverMessage(framing::Buffer& buffer) +{ + buffer.record(); + //peek at type: + Message::shared_ptr message(decodeMessageType(buffer) == MESSAGE ? + ((Message*) new MessageMessage()) : + ((Message*) new BasicMessage())); + buffer.restore(); + message->decodeHeader(buffer); + return RecoverableMessage::shared_ptr(new RecoverableMessageImpl(message, stagingThreshold)); +} + +void RecoveryManagerImpl::recoveryComplete() +{ + //TODO (finalise binding setup etc) +} + +uint8_t RecoveryManagerImpl::decodeMessageType(framing::Buffer& buffer) +{ + return buffer.getOctet(); +} + +void RecoveryManagerImpl::encodeMessageType(const Message& msg, framing::Buffer& buffer) +{ + buffer.putOctet(dynamic_cast(&msg) ? MESSAGE : BASIC); +} + +uint32_t RecoveryManagerImpl::encodedMessageTypeSize() +{ + return 1; +} + +bool RecoverableMessageImpl::loadContent(uint64_t available) +{ + return !stagingThreshold || available < stagingThreshold; +} + +void RecoverableMessageImpl::decodeContent(framing::Buffer& buffer) +{ + msg->decodeContent(buffer); +} + +void RecoverableMessageImpl::recover(Queue::shared_ptr queue) +{ + queue->recover(msg); +} + +void RecoverableMessageImpl::setPersistenceId(uint64_t id) +{ + msg->setPersistenceId(id); +} + +void RecoverableQueueImpl::recover(RecoverableMessage::shared_ptr msg) +{ + dynamic_pointer_cast(msg)->recover(queue); +} + +void RecoverableQueueImpl::setPersistenceId(uint64_t id) +{ + queue->setPersistenceId(id); +} diff --git a/cpp/src/qpid/broker/RecoveryManagerImpl.h b/cpp/src/qpid/broker/RecoveryManagerImpl.h new file mode 100644 index 0000000000..c40de7895f --- /dev/null +++ b/cpp/src/qpid/broker/RecoveryManagerImpl.h @@ -0,0 +1,55 @@ +/* + * + * 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 _RecoveryManagerImpl_ +#define _RecoveryManagerImpl_ + +#include +#include "ExchangeRegistry.h" +#include "QueueRegistry.h" +#include "RecoveryManager.h" + +namespace qpid { +namespace broker { + + class RecoveryManagerImpl : public RecoveryManager{ + QueueRegistry& queues; + ExchangeRegistry& exchanges; + const uint64_t stagingThreshold; + public: + RecoveryManagerImpl(QueueRegistry& queues, ExchangeRegistry& exchanges, uint64_t stagingThreshold); + ~RecoveryManagerImpl(); + + void recoverExchange(framing::Buffer& buffer); + RecoverableQueue::shared_ptr recoverQueue(framing::Buffer& buffer); + RecoverableMessage::shared_ptr recoverMessage(framing::Buffer& buffer); + void recoveryComplete(); + + static uint8_t decodeMessageType(framing::Buffer& buffer); + static void encodeMessageType(const Message& msg, framing::Buffer& buffer); + static uint32_t encodedMessageTypeSize(); + }; + + +} +} + + +#endif diff --git a/cpp/src/qpid/broker/Reference.cpp b/cpp/src/qpid/broker/Reference.cpp new file mode 100644 index 0000000000..c2060f8fee --- /dev/null +++ b/cpp/src/qpid/broker/Reference.cpp @@ -0,0 +1,53 @@ +/* + * + * 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 +#include "Reference.h" +#include "BrokerMessageMessage.h" +#include "qpid/QpidError.h" +#include "qpid/framing/MessageAppendBody.h" +#include "CompletionHandler.h" + +namespace qpid { +namespace broker { + +Reference::shared_ptr ReferenceRegistry::open(const Reference::Id& id) { + ReferenceMap::iterator i = references.find(id); + if (i != references.end()) + throw ConnectionException(503, "Attempt to re-open reference " +id); + return references[id] = Reference::shared_ptr(new Reference(id, this)); +} + +Reference::shared_ptr ReferenceRegistry::get(const Reference::Id& id) { + ReferenceMap::iterator i = references.find(id); + if (i == references.end()) + throw ConnectionException(503, "Attempt to use non-existent reference "+id); + return i->second; +} + +void Reference::append(AppendPtr ptr) { + appends.push_back(ptr); + size += ptr->getBytes().length(); +} + +void Reference::close() { + messages.clear(); + registry->references.erase(getId()); +} + +}} // namespace qpid::broker diff --git a/cpp/src/qpid/broker/Reference.h b/cpp/src/qpid/broker/Reference.h new file mode 100644 index 0000000000..277eb7b917 --- /dev/null +++ b/cpp/src/qpid/broker/Reference.h @@ -0,0 +1,114 @@ +#ifndef _broker_Reference_h +#define _broker_Reference_h + +/* + * + * 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 +#include +#include +#include +#include + +namespace qpid { + +namespace framing { +class MessageAppendBody; +} + +namespace broker { + +class MessageMessage; +class ReferenceRegistry; + +// FIXME aconway 2007-03-27: Merge with client::IncomingMessage +// to common reference handling code. + +/** + * A reference is an accumulation point for data in a multi-frame + * message. A reference can be used by multiple transfer commands to + * create multiple messages, so the reference tracks which commands + * are using it. When the reference is closed, all the associated + * transfers are completed. + * + * THREAD UNSAFE: per-channel resource, access to channels is + * serialized. + */ +class Reference +{ + public: + typedef std::string Id; + typedef boost::shared_ptr shared_ptr; + typedef boost::shared_ptr MessagePtr; + typedef std::vector Messages; + typedef boost::shared_ptr AppendPtr; + typedef std::vector Appends; + + Reference(const Id& id_=Id(), ReferenceRegistry* reg=0) + : id(id_), size(0), registry(reg) {} + + const std::string& getId() const { return id; } + uint64_t getSize() const { return size; } + + /** Add a message to be completed with this reference */ + void addMessage(MessagePtr message) { messages.push_back(message); } + + /** Append more data to the reference */ + void append(AppendPtr ptr); + + /** Close the reference, complete each associated message */ + void close(); + + const Appends& getAppends() const { return appends; } + const Messages& getMessages() const { return messages; } + + private: + Id id; + uint64_t size; + ReferenceRegistry* registry; + Messages messages; + Appends appends; +}; + + +/** + * A registry/factory for references. + * + * THREAD UNSAFE: per-channel resource, access to channels is + * serialized. + */ +class ReferenceRegistry { + public: + ReferenceRegistry() {}; + Reference::shared_ptr open(const Reference::Id& id); + Reference::shared_ptr get(const Reference::Id& id); + + private: + typedef std::map ReferenceMap; + ReferenceMap references; + + // Reference calls references.erase(). + friend class Reference; +}; + + +}} // namespace qpid::broker + + + +#endif /*!_broker_Reference_h*/ diff --git a/cpp/src/qpid/broker/TopicExchange.cpp b/cpp/src/qpid/broker/TopicExchange.cpp new file mode 100644 index 0000000000..f29dfc38ba --- /dev/null +++ b/cpp/src/qpid/broker/TopicExchange.cpp @@ -0,0 +1,154 @@ +/* + * + * 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 "TopicExchange.h" +#include + +using namespace qpid::broker; +using namespace qpid::framing; +using namespace qpid::sys; + +// TODO aconway 2006-09-20: More efficient matching algorithm. +// Areas for improvement: +// - excessive string copying: should be 0 copy, match from original buffer. +// - match/lookup: use descision tree or other more efficient structure. + +Tokens& Tokens::operator=(const std::string& s) { + clear(); + if (s.empty()) return *this; + std::string::const_iterator i = s.begin(); + while (true) { + // Invariant: i is at the beginning of the next untokenized word. + std::string::const_iterator j = find(i, s.end(), '.'); + push_back(std::string(i, j)); + if (j == s.end()) return *this; + i = j + 1; + } + return *this; +} + +TopicPattern& TopicPattern::operator=(const Tokens& tokens) { + Tokens::operator=(tokens); + normalize(); + return *this; +} + +namespace { +const std::string hashmark("#"); +const std::string star("*"); +} + +void TopicPattern::normalize() { + std::string word; + Tokens::iterator i = begin(); + while (i != end()) { + if (*i == hashmark) { + ++i; + while (i != end()) { + // Invariant: *(i-1)==#, [begin()..i-1] is normalized. + if (*i == star) { // Move * before #. + std::swap(*i, *(i-1)); + ++i; + } else if (*i == hashmark) { + erase(i); // Remove extra # + } else { + break; + } + } + } else { + i ++; + } + } +} + + +namespace { +// TODO aconway 2006-09-20: Ineficient to convert every routingKey to a string. +// Need StringRef class that operates on a string in place witout copy. +// Should be applied everywhere strings are extracted from frames. +// +bool do_match(Tokens::const_iterator pattern_begin, Tokens::const_iterator pattern_end, Tokens::const_iterator target_begin, Tokens::const_iterator target_end) +{ + // Invariant: [pattern_begin..p) matches [target_begin..t) + Tokens::const_iterator p = pattern_begin; + Tokens::const_iterator t = target_begin; + while (p != pattern_end && t != target_end) + { + if (*p == star || *p == *t) { + ++p, ++t; + } else if (*p == hashmark) { + ++p; + if (do_match(p, pattern_end, t, target_end)) return true; + while (t != target_end) { + ++t; + if (do_match(p, pattern_end, t, target_end)) return true; + } + return false; + } else { + return false; + } + } + while (p != pattern_end && *p == hashmark) ++p; // Ignore trailing # + return t == target_end && p == pattern_end; +} +} + +bool TopicPattern::match(const Tokens& target) const +{ + return do_match(begin(), end(), target.begin(), target.end()); +} + +TopicExchange::TopicExchange(const string& _name) : Exchange(_name) { } + +void TopicExchange::bind(Queue::shared_ptr queue, const string& routingKey, const FieldTable* /*args*/){ + Monitor::ScopedLock l(lock); + TopicPattern routingPattern(routingKey); + bindings[routingPattern].push_back(queue); +} + +void TopicExchange::unbind(Queue::shared_ptr queue, const string& routingKey, const FieldTable* /*args*/){ + Monitor::ScopedLock l(lock); + BindingMap::iterator bi = bindings.find(TopicPattern(routingKey)); + Queue::vector& qv(bi->second); + if (bi == bindings.end()) return; + Queue::vector::iterator q = find(qv.begin(), qv.end(), queue); + if(q == qv.end()) return; + qv.erase(q); + if(qv.empty()) bindings.erase(bi); +} + + +void TopicExchange::route(Deliverable& msg, const string& routingKey, const FieldTable* /*args*/){ + Monitor::ScopedLock l(lock); + for (BindingMap::iterator i = bindings.begin(); i != bindings.end(); ++i) { + if (i->first.match(routingKey)) { + Queue::vector& qv(i->second); + for(Queue::vector::iterator j = qv.begin(); j != qv.end(); j++){ + msg.deliverTo(*j); + } + } + } +} + +TopicExchange::~TopicExchange() {} + +const std::string TopicExchange::typeName("topic"); + + diff --git a/cpp/src/qpid/broker/TopicExchange.h b/cpp/src/qpid/broker/TopicExchange.h new file mode 100644 index 0000000000..e00731af3a --- /dev/null +++ b/cpp/src/qpid/broker/TopicExchange.h @@ -0,0 +1,100 @@ +/* + * + * 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 _TopicExchange_ +#define _TopicExchange_ + +#include +#include +#include "BrokerExchange.h" +#include "qpid/framing/FieldTable.h" +#include "BrokerMessage.h" +#include "qpid/sys/Monitor.h" +#include "BrokerQueue.h" + +namespace qpid { +namespace broker { + +/** A vector of string tokens */ +class Tokens : public std::vector { + public: + Tokens() {}; + // Default copy, assign, dtor are sufficient. + + /** Tokenize s, provides automatic conversion of string to Tokens */ + Tokens(const std::string& s) { operator=(s); } + /** Tokenizing assignment operator s */ + Tokens & operator=(const std::string& s); + + private: + size_t hash; +}; + + +/** + * Tokens that have been normalized as a pattern and can be matched + * with topic Tokens. Normalized meands all sequences of mixed * and + * # are reduced to a series of * followed by at most one #. + */ +class TopicPattern : public Tokens +{ + public: + TopicPattern() {} + // Default copy, assign, dtor are sufficient. + TopicPattern(const Tokens& tokens) { operator=(tokens); } + TopicPattern(const std::string& str) { operator=(str); } + TopicPattern& operator=(const Tokens&); + TopicPattern& operator=(const std::string& str) { return operator=(Tokens(str)); } + + /** Match a topic */ + bool match(const std::string& topic) { return match(Tokens(topic)); } + bool match(const Tokens& topic) const; + + private: + void normalize(); +}; + +class TopicExchange : public virtual Exchange{ + typedef std::map BindingMap; + BindingMap bindings; + qpid::sys::Mutex lock; + + public: + static const std::string typeName; + + TopicExchange(const string& name); + + virtual std::string getType(){ return typeName; } + + virtual void bind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args); + + virtual void unbind(Queue::shared_ptr queue, const string& routingKey, const qpid::framing::FieldTable* args); + + virtual void route(Deliverable& msg, const string& routingKey, const qpid::framing::FieldTable* args); + + virtual ~TopicExchange(); +}; + + + +} +} + +#endif diff --git a/cpp/src/qpid/broker/TransactionalStore.h b/cpp/src/qpid/broker/TransactionalStore.h new file mode 100644 index 0000000000..2a2bac0c51 --- /dev/null +++ b/cpp/src/qpid/broker/TransactionalStore.h @@ -0,0 +1,60 @@ +/* + * + * 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 _TransactionalStore_ +#define _TransactionalStore_ + +#include +#include +#include + +namespace qpid { +namespace broker { + +struct InvalidTransactionContextException : public std::exception {}; + +class TransactionContext { +public: + virtual ~TransactionContext(){} +}; + +class TPCTransactionContext : public TransactionContext { +public: + virtual ~TPCTransactionContext(){} +}; + +class TransactionalStore { +public: + virtual std::auto_ptr begin() = 0; + virtual std::auto_ptr begin(const std::string& xid) = 0; + virtual void prepare(TPCTransactionContext& txn) = 0; + virtual void commit(TransactionContext& txn) = 0; + virtual void abort(TransactionContext& txn) = 0; + + virtual void collectPreparedXids(std::set& xids) = 0; + + virtual ~TransactionalStore(){} +}; + +} +} + + +#endif diff --git a/cpp/src/qpid/broker/TxAck.cpp b/cpp/src/qpid/broker/TxAck.cpp new file mode 100644 index 0000000000..60d8049102 --- /dev/null +++ b/cpp/src/qpid/broker/TxAck.cpp @@ -0,0 +1,54 @@ +/* + * + * 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 "TxAck.h" + +using std::bind1st; +using std::bind2nd; +using std::mem_fun_ref; +using namespace qpid::broker; + +TxAck::TxAck(AccumulatedAck& _acked, std::list& _unacked) : + acked(_acked), unacked(_unacked){ + +} + +bool TxAck::prepare(TransactionContext* ctxt) throw(){ + try{ + //dequeue all acked messages from their queues + for (ack_iterator i = unacked.begin(); i != unacked.end(); i++) { + if (i->coveredBy(&acked)) { + i->discard(ctxt); + } + } + return true; + }catch(...){ + std::cout << "TxAck::prepare() - Failed to prepare" << std::endl; + return false; + } +} + +void TxAck::commit() throw(){ + //remove all acked records from the list + unacked.remove_if(bind2nd(mem_fun_ref(&DeliveryRecord::coveredBy), &acked)); +} + +void TxAck::rollback() throw(){ +} diff --git a/cpp/src/qpid/broker/TxAck.h b/cpp/src/qpid/broker/TxAck.h new file mode 100644 index 0000000000..5e6d0a370c --- /dev/null +++ b/cpp/src/qpid/broker/TxAck.h @@ -0,0 +1,57 @@ +/* + * + * 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 _TxAck_ +#define _TxAck_ + +#include +#include +#include +#include "AccumulatedAck.h" +#include "DeliveryRecord.h" +#include "TxOp.h" + +namespace qpid { + namespace broker { + /** + * Defines the transactional behaviour for acks received by a + * transactional channel. + */ + class TxAck : public TxOp{ + AccumulatedAck& acked; + std::list& unacked; + + public: + /** + * @param acked a representation of the accumulation of + * acks received + * @param unacked the record of delivered messages + */ + TxAck(AccumulatedAck& acked, std::list& unacked); + virtual bool prepare(TransactionContext* ctxt) throw(); + virtual void commit() throw(); + virtual void rollback() throw(); + virtual ~TxAck(){} + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/TxBuffer.cpp b/cpp/src/qpid/broker/TxBuffer.cpp new file mode 100644 index 0000000000..c526c88c25 --- /dev/null +++ b/cpp/src/qpid/broker/TxBuffer.cpp @@ -0,0 +1,56 @@ +/* + * + * 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 "TxBuffer.h" + +#include +using boost::mem_fn; +using namespace qpid::broker; + +bool TxBuffer::prepare(TransactionalStore* const store) +{ + std::auto_ptr ctxt; + if(store) ctxt = store->begin(); + for(op_iterator i = ops.begin(); i < ops.end(); i++){ + if(!(*i)->prepare(ctxt.get())){ + if(store) store->abort(*ctxt); + return false; + } + } + if(store) store->commit(*ctxt); + return true; +} + +void TxBuffer::commit() +{ + for_each(ops.begin(), ops.end(), mem_fn(&TxOp::commit)); + ops.clear(); +} + +void TxBuffer::rollback() +{ + for_each(ops.begin(), ops.end(), mem_fn(&TxOp::rollback)); + ops.clear(); +} + +void TxBuffer::enlist(TxOp::shared_ptr op) +{ + ops.push_back(op); +} diff --git a/cpp/src/qpid/broker/TxBuffer.h b/cpp/src/qpid/broker/TxBuffer.h new file mode 100644 index 0000000000..86ee8945b5 --- /dev/null +++ b/cpp/src/qpid/broker/TxBuffer.h @@ -0,0 +1,107 @@ +/* + * + * 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 _TxBuffer_ +#define _TxBuffer_ + +#include +#include +#include +#include "TransactionalStore.h" +#include "TxOp.h" + +/** + * Represents a single transaction. As such, an instance of this class + * will hold a list of operations representing the workload of the + * transaction. This work can be committed or rolled back. Committing + * is a two-stage process: first all the operations should be + * prepared, then if that succeeds they can be committed. + * + * In the 2pc case, a successful prepare may be followed by either a + * commit or a rollback. + * + * Atomicity of prepare is ensured by using a lower level + * transactional facility. This saves explicitly rolling back all the + * successfully prepared ops when one of them fails. i.e. we do not + * use 2pc internally, we instead ensure that prepare is atomic at a + * lower level. This makes individual prepare operations easier to + * code. + * + * Transactions on a messaging broker effect three types of 'action': + * (1) updates to persistent storage (2) updates to transient storage + * or cached data (3) network writes. + * + * Of these, (1) should always occur atomically during prepare to + * ensure that if the broker crashes while a transaction is being + * completed the persistent state (which is all that then remains) is + * consistent. (3) can only be done on commit, after a successful + * prepare. There is a little more flexibility with (2) but any + * changes made during prepare should be subject to the control of the + * TransactionalStore in use. + */ +namespace qpid { + namespace broker { + class TxBuffer{ + typedef std::vector::iterator op_iterator; + std::vector ops; + public: + /** + * Requests that all ops are prepared. This should + * primarily involve making sure that a persistent record + * of the operations is stored where necessary. + * + * All ops will be prepared under a transaction on the + * specified store. If any operation fails on prepare, + * this transaction will be rolled back. + * + * Once prepared, a transaction can be committed (or in + * the 2pc case, rolled back). + * + * @returns true if all the operations prepared + * successfully, false if not. + */ + bool prepare(TransactionalStore* const store); + /** + * Signals that the ops all prepared all completed + * successfully and can now commit, i.e. the operation can + * now be fully carried out. + * + * Should only be called after a call to prepare() returns + * true. + */ + void commit(); + /** + * Rolls back all the operations. + * + * Should only be called either after a call to prepare() + * returns true (2pc) or instead of a prepare call + * ('server-local') + */ + void rollback(); + /** + * Adds an operation to the transaction. + */ + void enlist(TxOp::shared_ptr op); + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/TxOp.h b/cpp/src/qpid/broker/TxOp.h new file mode 100644 index 0000000000..e687c437cc --- /dev/null +++ b/cpp/src/qpid/broker/TxOp.h @@ -0,0 +1,42 @@ +/* + * + * 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 _TxOp_ +#define _TxOp_ + +#include "TransactionalStore.h" +#include + +namespace qpid { + namespace broker { + class TxOp{ + public: + typedef boost::shared_ptr shared_ptr; + + virtual bool prepare(TransactionContext*) throw() = 0; + virtual void commit() throw() = 0; + virtual void rollback() throw() = 0; + virtual ~TxOp(){} + }; + } +} + + +#endif diff --git a/cpp/src/qpid/broker/TxPublish.cpp b/cpp/src/qpid/broker/TxPublish.cpp new file mode 100644 index 0000000000..687505909c --- /dev/null +++ b/cpp/src/qpid/broker/TxPublish.cpp @@ -0,0 +1,60 @@ +/* + * + * 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 "TxPublish.h" + +using namespace qpid::broker; + +TxPublish::TxPublish(Message::shared_ptr _msg) : msg(_msg) {} + +bool TxPublish::prepare(TransactionContext* ctxt) throw(){ + try{ + for_each(queues.begin(), queues.end(), Prepare(ctxt, msg)); + return true; + }catch(...){ + std::cout << "TxPublish::prepare() - Failed to prepare" << std::endl; + return false; + } +} + +void TxPublish::commit() throw(){ + for_each(queues.begin(), queues.end(), Commit(msg)); +} + +void TxPublish::rollback() throw(){ +} + +void TxPublish::deliverTo(Queue::shared_ptr& queue){ + queues.push_back(queue); +} + +TxPublish::Prepare::Prepare(TransactionContext* _ctxt, Message::shared_ptr& _msg) + : ctxt(_ctxt), msg(_msg){} + +void TxPublish::Prepare::operator()(Queue::shared_ptr& queue){ + queue->enqueue(ctxt, msg); +} + +TxPublish::Commit::Commit(Message::shared_ptr& _msg) : msg(_msg){} + +void TxPublish::Commit::operator()(Queue::shared_ptr& queue){ + queue->process(msg); +} + diff --git a/cpp/src/qpid/broker/TxPublish.h b/cpp/src/qpid/broker/TxPublish.h new file mode 100644 index 0000000000..29b1dc38af --- /dev/null +++ b/cpp/src/qpid/broker/TxPublish.h @@ -0,0 +1,78 @@ +/* + * + * 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 _TxPublish_ +#define _TxPublish_ + +#include +#include +#include +#include "Deliverable.h" +#include "BrokerMessage.h" +#include "MessageStore.h" +#include "BrokerQueue.h" +#include "TxOp.h" + +namespace qpid { + namespace broker { + /** + * Defines the behaviour for publish operations on a + * transactional channel. Messages are routed through + * exchanges when received but are not at that stage delivered + * to the matching queues, rather the queues are held in an + * instance of this class. On prepare() the message is marked + * enqueued to the relevant queues in the MessagesStore. On + * commit() the messages will be passed to the queue for + * dispatch or to be added to the in-memory queue. + */ + class TxPublish : public TxOp, public Deliverable{ + class Prepare{ + TransactionContext* ctxt; + Message::shared_ptr& msg; + public: + Prepare(TransactionContext* ctxt, Message::shared_ptr& msg); + void operator()(Queue::shared_ptr& queue); + }; + + class Commit{ + Message::shared_ptr& msg; + public: + Commit(Message::shared_ptr& msg); + void operator()(Queue::shared_ptr& queue); + }; + + Message::shared_ptr msg; + std::list queues; + + public: + TxPublish(Message::shared_ptr msg); + virtual bool prepare(TransactionContext* ctxt) throw(); + virtual void commit() throw(); + virtual void rollback() throw(); + + virtual void deliverTo(Queue::shared_ptr& queue); + + virtual ~TxPublish(){} + }; + } +} + + +#endif diff --git a/cpp/src/qpid/client/AckMode.h b/cpp/src/qpid/client/AckMode.h new file mode 100644 index 0000000000..9ad5ef925c --- /dev/null +++ b/cpp/src/qpid/client/AckMode.h @@ -0,0 +1,102 @@ +#ifndef _client_AckMode_h +#define _client_AckMode_h + +/* + * + * 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. + * + */ + +namespace qpid { +namespace client { + +/** + * The available acknowledgements modes. + * + * \ingroup clientapi + */ +enum AckMode { + /** No acknowledgement will be sent, broker can + discard messages as soon as they are delivered + to a consumer using this mode. **/ + NO_ACK = 0, + /** Each message will be automatically + acknowledged as soon as it is delivered to the + application **/ + AUTO_ACK = 1, + /** Acknowledgements will be sent automatically, + but not for each message. **/ + LAZY_ACK = 2, + /** The application is responsible for explicitly + acknowledging messages. **/ + CLIENT_ACK = 3 +}; + +}} // namespace qpid::client + + + +#endif /*!_client_AckMode_h*/ +#ifndef _client_AckMode_h +#define _client_AckMode_h + +/* + * + * 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. + * + */ + +namespace qpid { +namespace client { + +/** + * The available acknowledgements modes. + * + * \ingroup clientapi + */ +enum AckMode { + /** No acknowledgement will be sent, broker can + discard messages as soon as they are delivered + to a consumer using this mode. **/ + NO_ACK = 0, + /** Each message will be automatically + acknowledged as soon as it is delivered to the + application **/ + AUTO_ACK = 1, + /** Acknowledgements will be sent automatically, + but not for each message. **/ + LAZY_ACK = 2, + /** The application is responsible for explicitly + acknowledging messages. **/ + CLIENT_ACK = 3 +}; + +}} // namespace qpid::client + + + +#endif /*!_client_AckMode_h*/ diff --git a/cpp/src/qpid/client/BasicMessageChannel.cpp b/cpp/src/qpid/client/BasicMessageChannel.cpp new file mode 100644 index 0000000000..96aaad05dc --- /dev/null +++ b/cpp/src/qpid/client/BasicMessageChannel.cpp @@ -0,0 +1,345 @@ +/* + * + * 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 "BasicMessageChannel.h" +#include "qpid/framing/AMQMethodBody.h" +#include "ClientChannel.h" +#include "ReturnedMessageHandler.h" +#include "MessageListener.h" +#include "qpid/framing/FieldTable.h" +#include "Connection.h" +#include +#include +#include +#include + +namespace qpid { +namespace client { + +using namespace std; +using namespace sys; +using namespace framing; +using boost::format; + +namespace { + +// Destination name constants +const std::string BASIC_GET("__basic_get__"); +const std::string BASIC_RETURN("__basic_return__"); + +// Reference name constant +const std::string BASIC_REF("__basic_reference__"); +} + +BasicMessageChannel::BasicMessageChannel(Channel& ch) + : channel(ch), returnsHandler(0) +{ + incoming.addDestination(BASIC_RETURN, destDispatch); +} + +void BasicMessageChannel::consume( + Queue& queue, std::string& tag, MessageListener* listener, + AckMode ackMode, bool noLocal, bool synch, const FieldTable* fields) +{ + { + // Note we create a consumer even if tag="". In that case + // It will be renamed when we handle BasicConsumeOkBody. + // + Mutex::ScopedLock l(lock); + ConsumerMap::iterator i = consumers.find(tag); + if (i != consumers.end()) + THROW_QPID_ERROR(CLIENT_ERROR, + "Consumer already exists with tag="+tag); + Consumer& c = consumers[tag]; + c.listener = listener; + c.ackMode = ackMode; + c.lastDeliveryTag = 0; + } + + // FIXME aconway 2007-03-23: get processed in both. + + // BasicConsumeOkBody is really processed in handle(), here + // we just pick up the tag to return to the user. + // + // We can't process it here because messages for the consumer may + // already be arriving. + // + BasicConsumeOkBody::shared_ptr ok = + channel.sendAndReceiveSync( + synch, + make_shared_ptr(new BasicConsumeBody( + channel.version, 0, queue.getName(), tag, noLocal, + ackMode == NO_ACK, false, !synch, + fields ? *fields : FieldTable()))); + tag = ok->getConsumerTag(); +} + + +void BasicMessageChannel::cancel(const std::string& tag, bool synch) { + Consumer c; + { + Mutex::ScopedLock l(lock); + ConsumerMap::iterator i = consumers.find(tag); + if (i == consumers.end()) + return; + c = i->second; + consumers.erase(i); + } + if(c.ackMode == LAZY_ACK && c.lastDeliveryTag > 0) + channel.send(new BasicAckBody(channel.version, c.lastDeliveryTag, true)); + channel.sendAndReceiveSync( + synch, make_shared_ptr(new BasicCancelBody(channel.version, tag, !synch))); +} + +void BasicMessageChannel::close(){ + ConsumerMap consumersCopy; + { + Mutex::ScopedLock l(lock); + consumersCopy = consumers; + consumers.clear(); + } + destGet.shutdown(); + destDispatch.shutdown(); + for (ConsumerMap::iterator i=consumersCopy.begin(); + i != consumersCopy.end(); ++i) + { + Consumer& c = i->second; + if ((c.ackMode == LAZY_ACK || c.ackMode == AUTO_ACK) + && c.lastDeliveryTag > 0) + { + channel.send(new BasicAckBody(channel.version, c.lastDeliveryTag, true)); + } + } +} + + +bool BasicMessageChannel::get( + Message& msg, const Queue& queue, AckMode ackMode) +{ + // Prepare for incoming response + incoming.addDestination(BASIC_GET, destGet); + channel.send( + new BasicGetBody(channel.version, 0, queue.getName(), ackMode)); + bool got = destGet.wait(msg); + return got; +} + +void BasicMessageChannel::publish( + const Message& msg, const Exchange& exchange, + const std::string& routingKey, bool mandatory, bool immediate) +{ + const string e = exchange.getName(); + string key = routingKey; + + // Make a header for the message + AMQHeaderBody::shared_ptr header(new AMQHeaderBody(BASIC)); + BasicHeaderProperties::copy( + *static_cast(header->getProperties()), msg); + header->setContentSize(msg.getData().size()); + + channel.send( + new BasicPublishBody( + channel.version, 0, e, key, mandatory, immediate)); + channel.send(header); + string data = msg.getData(); + u_int64_t data_length = data.length(); + if(data_length > 0){ + //frame itself uses 8 bytes + u_int32_t frag_size = channel.connection->getMaxFrameSize() - 8; + if(data_length < frag_size){ + channel.send(new AMQContentBody(data)); + }else{ + u_int32_t offset = 0; + u_int32_t remaining = data_length - offset; + while (remaining > 0) { + u_int32_t length = remaining > frag_size ? frag_size : remaining; + string frag(data.substr(offset, length)); + channel.send(new AMQContentBody(frag)); + + offset += length; + remaining = data_length - offset; + } + } + } +} + +void BasicMessageChannel::handle(boost::shared_ptr method) { + assert(method->amqpClassId() ==BasicGetBody::CLASS_ID); + switch(method->amqpMethodId()) { + case BasicGetOkBody::METHOD_ID: { + incoming.openReference(BASIC_REF); + incoming.createMessage(BASIC_GET, BASIC_REF); + return; + } + case BasicGetEmptyBody::METHOD_ID: { + incoming.getDestination(BASIC_GET).empty(); + incoming.removeDestination(BASIC_GET); + return; + } + case BasicDeliverBody::METHOD_ID: { + BasicDeliverBody::shared_ptr deliver= + boost::shared_polymorphic_downcast(method); + incoming.openReference(BASIC_REF); + Message& msg = incoming.createMessage( + deliver->getConsumerTag(), BASIC_REF); + msg.setDestination(deliver->getConsumerTag()); + msg.setDeliveryTag(deliver->getDeliveryTag()); + msg.setRedelivered(deliver->getRedelivered()); + return; + } + case BasicReturnBody::METHOD_ID: { + incoming.openReference(BASIC_REF); + incoming.createMessage(BASIC_RETURN, BASIC_REF); + return; + } + case BasicConsumeOkBody::METHOD_ID: { + Mutex::ScopedLock l(lock); + BasicConsumeOkBody::shared_ptr consumeOk = + boost::shared_polymorphic_downcast(method); + std::string tag = consumeOk->getConsumerTag(); + ConsumerMap::iterator i = consumers.find(std::string()); + if (i != consumers.end()) { + // Need to rename the un-named consumer. + if (consumers.find(tag) == consumers.end()) { + consumers[tag] = i->second; + consumers.erase(i); + } + else // Tag already exists. + throw ChannelException(404, "Tag already exists: "+tag); + } + // FIXME aconway 2007-03-23: Integrate consumer & destination + // maps. + incoming.addDestination(tag, destDispatch); + return; + } + } + throw Channel::UnknownMethod(); +} + +void BasicMessageChannel::handle(AMQHeaderBody::shared_ptr header) { + BasicHeaderProperties* props = + boost::polymorphic_downcast( + header->getProperties()); + IncomingMessage::Reference& ref = incoming.getReference(BASIC_REF); + assert (ref.messages.size() == 1); + ref.messages.front().BasicHeaderProperties::operator=(*props); + incoming_size = header->getContentSize(); + if (incoming_size==0) + incoming.closeReference(BASIC_REF); +} + +void BasicMessageChannel::handle(AMQContentBody::shared_ptr content){ + incoming.appendReference(BASIC_REF, content->getData()); + size_t size = incoming.getReference(BASIC_REF).data.size(); + if (size >= incoming_size) { + incoming.closeReference(BASIC_REF); + if (size > incoming_size) + throw ChannelException(502, "Content exceeded declared size"); + } +} + +void BasicMessageChannel::deliver(Consumer& consumer, Message& msg){ + //record delivery tag: + consumer.lastDeliveryTag = msg.getDeliveryTag(); + + //allow registered listener to handle the message + consumer.listener->received(msg); + + if(channel.isOpen()){ + bool multiple(false); + switch(consumer.ackMode){ + case LAZY_ACK: + multiple = true; + if(++(consumer.count) < channel.getPrefetch()) + break; + //else drop-through + case AUTO_ACK: + consumer.lastDeliveryTag = 0; + channel.send( + new BasicAckBody( + channel.version, + msg.getDeliveryTag(), + multiple)); + case NO_ACK: // Nothing to do + case CLIENT_ACK: // User code must ack. + break; + // TODO aconway 2007-02-22: Provide a way for user + // to ack! + } + } + + //as it stands, transactionality is entirely orthogonal to ack + //mode, though the acks will not be processed by the broker under + //a transaction until it commits. +} + + +void BasicMessageChannel::run() { + while(channel.isOpen()) { + try { + Message msg; + bool gotMessge = destDispatch.wait(msg); + if (gotMessge) { + if(msg.getDestination() == BASIC_RETURN) { + ReturnedMessageHandler* handler=0; + { + Mutex::ScopedLock l(lock); + handler=returnsHandler; + } + if(handler != 0) + handler->returned(msg); + } + else { + Consumer consumer; + { + Mutex::ScopedLock l(lock); + ConsumerMap::iterator i = consumers.find( + msg.getDestination()); + if(i == consumers.end()) + THROW_QPID_ERROR(PROTOCOL_ERROR+504, + "Unknown consumer tag=" + + msg.getDestination()); + consumer = i->second; + } + deliver(consumer, msg); + } + } + } + catch (const ShutdownException&) { + /* Orderly shutdown */ + } + catch (const Exception& e) { + // FIXME aconway 2007-02-20: Report exception to user. + cout << "client::BasicMessageChannel::run() terminated by: " + << e.toString() << endl; + } + } +} + +void BasicMessageChannel::setReturnedMessageHandler(ReturnedMessageHandler* handler){ + Mutex::ScopedLock l(lock); + returnsHandler = handler; +} + +void BasicMessageChannel::setQos(){ + channel.sendAndReceive( + make_shared_ptr(new BasicQosBody(channel.version, 0, channel.getPrefetch(), false))); + if(channel.isTransactional()) + channel.sendAndReceive(make_shared_ptr(new TxSelectBody(channel.version))); +} + +}} // namespace qpid::client diff --git a/cpp/src/qpid/client/BasicMessageChannel.h b/cpp/src/qpid/client/BasicMessageChannel.h new file mode 100644 index 0000000000..13e1cf1e00 --- /dev/null +++ b/cpp/src/qpid/client/BasicMessageChannel.h @@ -0,0 +1,90 @@ +#ifndef _client_BasicMessageChannel_h +#define _client_BasicMessageChannel_h + +/* + * + * 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 "MessageChannel.h" +#include "IncomingMessage.h" +#include + +namespace qpid { +namespace client { +/** + * Messaging implementation using AMQP 0-8 BasicMessageChannel class + * to send and receiving messages. + */ +class BasicMessageChannel : public MessageChannel +{ + public: + BasicMessageChannel(Channel& parent); + + void consume( + Queue& queue, std::string& tag, MessageListener* listener, + AckMode ackMode = NO_ACK, bool noLocal = false, bool synch = true, + const framing::FieldTable* fields = 0); + + void cancel(const std::string& tag, bool synch = true); + + bool get(Message& msg, const Queue& queue, AckMode ackMode = NO_ACK); + + void publish(const Message& msg, const Exchange& exchange, + const std::string& routingKey, + bool mandatory = false, bool immediate = false); + + void setReturnedMessageHandler(ReturnedMessageHandler* handler); + + void run(); + + void handle(boost::shared_ptr); + + void handle(shared_ptr); + + void handle(shared_ptr); + + void setQos(); + + void close(); + + private: + + struct Consumer{ + MessageListener* listener; + AckMode ackMode; + int count; + u_int64_t lastDeliveryTag; + }; + typedef std::map ConsumerMap; + + void deliver(Consumer& consumer, Message& msg); + + sys::Mutex lock; + Channel& channel; + IncomingMessage incoming; + uint64_t incoming_size; + ConsumerMap consumers ; + ReturnedMessageHandler* returnsHandler; + IncomingMessage::WaitableDestination destGet; + IncomingMessage::WaitableDestination destDispatch; +}; + +}} // namespace qpid::client + + + +#endif /*!_client_BasicMessageChannel_h*/ diff --git a/cpp/src/qpid/client/ClientAdapter.cpp b/cpp/src/qpid/client/ClientAdapter.cpp new file mode 100644 index 0000000000..ceabe344c9 --- /dev/null +++ b/cpp/src/qpid/client/ClientAdapter.cpp @@ -0,0 +1,70 @@ +/* + * + * 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/framing/AMQP_ClientOperations.h" +#include "ClientAdapter.h" +#include "Connection.h" +#include "qpid/Exception.h" +#include "qpid/framing/AMQMethodBody.h" + +namespace qpid { +namespace client { + +using namespace qpid; +using namespace qpid::framing; + +typedef std::vector::iterator queue_iterator; + +void ClientAdapter::handleMethodInContext( + boost::shared_ptr method, + const MethodContext& context +) +{ + try{ + method->invoke(*clientOps, context); + }catch(ChannelException& e){ + connection.client->getChannel().close( + context, e.code, e.toString(), + method->amqpClassId(), method->amqpMethodId()); + connection.closeChannel(getId()); + }catch(ConnectionException& e){ + connection.client->getConnection().close( + context, e.code, e.toString(), + method->amqpClassId(), method->amqpMethodId()); + }catch(std::exception& e){ + connection.client->getConnection().close( + context, 541/*internal error*/, e.what(), + method->amqpClassId(), method->amqpMethodId()); + } +} + +void ClientAdapter::handleHeader(AMQHeaderBody::shared_ptr body) { + channel->handleHeader(body); +} + +void ClientAdapter::handleContent(AMQContentBody::shared_ptr body) { + channel->handleContent(body); +} + +void ClientAdapter::handleHeartbeat(AMQHeartbeatBody::shared_ptr) { + // TODO aconway 2007-01-17: Implement heartbeats. +} + + + +}} // namespace qpid::client + diff --git a/cpp/src/qpid/client/ClientAdapter.h b/cpp/src/qpid/client/ClientAdapter.h new file mode 100644 index 0000000000..4a6b76077f --- /dev/null +++ b/cpp/src/qpid/client/ClientAdapter.h @@ -0,0 +1,66 @@ +#ifndef _client_ClientAdapter_h +#define _client_ClientAdapter_h + +/* + * + * 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/framing/ChannelAdapter.h" +#include "ClientChannel.h" + +namespace qpid { +namespace client { + +class AMQMethodBody; +class Connection; + +/** + * Per-channel protocol adapter. + * + * Translates protocol bodies into calls on the core Channel, + * Connection and Client objects. + * + * Owns a channel, has references to Connection and Client. + */ +class ClientAdapter : public framing::ChannelAdapter +{ + public: + ClientAdapter(std::auto_ptr ch, Connection&, Client&); + Channel& getChannel() { return *channel; } + + void handleHeader(boost::shared_ptr); + void handleContent(boost::shared_ptr); + void handleHeartbeat(boost::shared_ptr); + + private: + void handleMethodInContext( + boost::shared_ptr method, + const framing::MethodContext& context); + + class ClientOps; + + std::auto_ptr channel; + Connection& connection; + Client& client; + boost::shared_ptr clientOps; +}; + +}} // namespace qpid::client + + + +#endif /*!_client_ClientAdapter_h*/ diff --git a/cpp/src/qpid/client/ClientChannel.cpp b/cpp/src/qpid/client/ClientChannel.cpp new file mode 100644 index 0000000000..05f01227f6 --- /dev/null +++ b/cpp/src/qpid/client/ClientChannel.cpp @@ -0,0 +1,341 @@ +/* + * + * 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 +#include "ClientChannel.h" +#include "qpid/sys/Monitor.h" +#include "ClientMessage.h" +#include "qpid/QpidError.h" +#include "MethodBodyInstances.h" +#include "Connection.h" +#include "BasicMessageChannel.h" +#include "MessageMessageChannel.h" + +// FIXME aconway 2007-01-26: Evaluate all throws, ensure consistent +// handling of errors that should close the connection or the channel. +// Make sure the user thread receives a connection in each case. +// +using namespace std; +using namespace boost; +using namespace qpid::client; +using namespace qpid::framing; +using namespace qpid::sys; + +Channel::Channel(bool _transactional, u_int16_t _prefetch, InteropMode mode) : + connection(0), prefetch(_prefetch), transactional(_transactional) +{ + switch (mode) { + case AMQP_08: messaging.reset(new BasicMessageChannel(*this)); break; + case AMQP_09: messaging.reset(new MessageMessageChannel(*this)); break; + default: assert(0); QPID_ERROR(INTERNAL_ERROR, "Invalid interop-mode."); + } +} + +Channel::~Channel(){ + close(); +} + +void Channel::open(ChannelId id, Connection& con) +{ + if (isOpen()) + THROW_QPID_ERROR(INTERNAL_ERROR, "Attempt to re-open channel "+id); + connection = &con; + init(id, con, con.getVersion()); // ChannelAdapter initialization. + string oob; + if (id != 0) + sendAndReceive(make_shared_ptr(new ChannelOpenBody(version, oob))); +} + +void Channel::protocolInit( + const std::string& uid, const std::string& pwd, const std::string& vhost) { + assert(connection); + responses.expect(); + connection->connector->init(); // Send ProtocolInit block. + ConnectionStartBody::shared_ptr connectionStart = + responses.receive(); + + FieldTable props; + string mechanism("PLAIN"); + string response = ((char)0) + uid + ((char)0) + pwd; + string locale("en_US"); + ConnectionTuneBody::shared_ptr proposal = + sendAndReceive( + make_shared_ptr(new ConnectionStartOkBody( + version, connectionStart->getRequestId(), + props, mechanism, + response, locale))); + + /** + * Assume for now that further challenges will not be required + //receive connection.secure + responses.receive(connection_secure)); + //send connection.secure-ok + connection->send(new AMQFrame(0, new ConnectionSecureOkBody(response))); + **/ + + send(new ConnectionTuneOkBody( + version, proposal->getRequestId(), + proposal->getChannelMax(), connection->getMaxFrameSize(), + proposal->getHeartbeat())); + + uint16_t heartbeat = proposal->getHeartbeat(); + connection->connector->setReadTimeout(heartbeat * 2); + connection->connector->setWriteTimeout(heartbeat); + + // Send connection open. + std::string capabilities; + responses.expect(); + send(new ConnectionOpenBody(version, vhost, capabilities, true)); + //receive connection.open-ok (or redirect, but ignore that for now + //esp. as using force=true). + AMQMethodBody::shared_ptr openResponse = responses.receive(); + if(openResponse->isA()) { + //ok + }else if(openResponse->isA()){ + //ignore for now + ConnectionRedirectBody::shared_ptr redirect( + shared_polymorphic_downcast(openResponse)); + cout << "Received redirection to " << redirect->getHost() + << endl; + } else { + THROW_QPID_ERROR(PROTOCOL_ERROR, "Bad response to Connection.open"); + } +} + +bool Channel::isOpen() const { return connection; } + +void Channel::setQos() { + messaging->setQos(); +} + +void Channel::setPrefetch(uint16_t _prefetch){ + prefetch = _prefetch; + setQos(); +} + +void Channel::declareExchange(Exchange& exchange, bool synch){ + string name = exchange.getName(); + string type = exchange.getType(); + FieldTable args; + sendAndReceiveSync( + synch, + make_shared_ptr(new ExchangeDeclareBody( + version, 0, name, type, false, false, false, false, !synch, args))); +} + +void Channel::deleteExchange(Exchange& exchange, bool synch){ + string name = exchange.getName(); + sendAndReceiveSync( + synch, + make_shared_ptr(new ExchangeDeleteBody(version, 0, name, false, !synch))); +} + +void Channel::declareQueue(Queue& queue, bool synch){ + string name = queue.getName(); + FieldTable args; + QueueDeclareOkBody::shared_ptr response = + sendAndReceiveSync( + synch, + make_shared_ptr(new QueueDeclareBody( + version, 0, name, false/*passive*/, queue.isDurable(), + queue.isExclusive(), queue.isAutoDelete(), !synch, args))); + if(synch) { + if(queue.getName().length() == 0) + queue.setName(response->getQueue()); + } +} + +void Channel::deleteQueue(Queue& queue, bool ifunused, bool ifempty, bool synch){ + //ticket, queue, ifunused, ifempty, nowait + string name = queue.getName(); + sendAndReceiveSync( + synch, + make_shared_ptr(new QueueDeleteBody(version, 0, name, ifunused, ifempty, !synch))); +} + +void Channel::bind(const Exchange& exchange, const Queue& queue, const std::string& key, const FieldTable& args, bool synch){ + string e = exchange.getName(); + string q = queue.getName(); + sendAndReceiveSync( + synch, + make_shared_ptr(new QueueBindBody(version, 0, q, e, key,!synch, args))); +} + +void Channel::commit(){ + sendAndReceive(make_shared_ptr(new TxCommitBody(version))); +} + +void Channel::rollback(){ + sendAndReceive(make_shared_ptr(new TxRollbackBody(version))); +} + +void Channel::handleMethodInContext( + AMQMethodBody::shared_ptr method, const MethodContext&) +{ + // TODO aconway 2007-03-23: Special case for consume OK as it + // is both an expected response and needs handling in this thread. + // Need to review & reationalize the client-side processing model. + if (method->isA()) { + messaging->handle(method); + responses.signalResponse(method); + return; + } + if(responses.isWaiting()) { + responses.signalResponse(method); + return; + } + try { + switch (method->amqpClassId()) { + case MessageOkBody::CLASS_ID: + case BasicGetOkBody::CLASS_ID: messaging->handle(method); break; + case ChannelCloseBody::CLASS_ID: handleChannel(method); break; + case ConnectionCloseBody::CLASS_ID: handleConnection(method); break; + default: throw UnknownMethod(); + } + } + catch (const UnknownMethod&) { + connection->close( + 504, "Unknown method", + method->amqpClassId(), method->amqpMethodId()); + } + } + +void Channel::handleChannel(AMQMethodBody::shared_ptr method) { + switch (method->amqpMethodId()) { + case ChannelCloseBody::METHOD_ID: + peerClose(shared_polymorphic_downcast(method)); + return; + case ChannelFlowBody::METHOD_ID: + // FIXME aconway 2007-02-22: Not yet implemented. + return; + } + throw UnknownMethod(); +} + +void Channel::handleConnection(AMQMethodBody::shared_ptr method) { + if (method->amqpMethodId() == ConnectionCloseBody::METHOD_ID) { + connection->close(); + return; + } + throw UnknownMethod(); +} + +void Channel::handleHeader(AMQHeaderBody::shared_ptr body){ + messaging->handle(body); +} + +void Channel::handleContent(AMQContentBody::shared_ptr body){ + messaging->handle(body); +} + +void Channel::handleHeartbeat(AMQHeartbeatBody::shared_ptr /*body*/){ + THROW_QPID_ERROR(PROTOCOL_ERROR + 504, "Channel received heartbeat"); +} + +void Channel::start(){ + dispatcher = Thread(*messaging); +} + +// Close called by local application. +void Channel::close( + uint16_t code, const std::string& text, + ClassId classId, MethodId methodId) +{ + if (isOpen()) { + try { + if (getId() != 0) { + sendAndReceive( + make_shared_ptr(new ChannelCloseBody( + version, code, text, classId, methodId))); + } + static_cast(connection)->erase(getId()); + closeInternal(); + } catch (...) { + static_cast(connection)->erase(getId()); + closeInternal(); + throw; + } + } +} + +// Channel closed by peer. +void Channel::peerClose(ChannelCloseBody::shared_ptr) { + assert(isOpen()); + closeInternal(); +} + +void Channel::closeInternal() { + if (isOpen()); + { + messaging->close(); + connection = 0; + // A 0 response means we are closed. + responses.signalResponse(AMQMethodBody::shared_ptr()); + } + dispatcher.join(); +} + +AMQMethodBody::shared_ptr Channel::sendAndReceive( + AMQMethodBody::shared_ptr toSend, ClassId c, MethodId m) +{ + responses.expect(); + send(toSend); + return responses.receive(c, m); +} + +AMQMethodBody::shared_ptr Channel::sendAndReceiveSync( + bool sync, AMQMethodBody::shared_ptr body, ClassId c, MethodId m) +{ + if(sync) + return sendAndReceive(body, c, m); + else { + send(body); + return AMQMethodBody::shared_ptr(); + } +} + +void Channel::consume( + Queue& queue, std::string& tag, MessageListener* listener, + AckMode ackMode, bool noLocal, bool synch, const FieldTable* fields) { + messaging->consume(queue, tag, listener, ackMode, noLocal, synch, fields); +} + +void Channel::cancel(const std::string& tag, bool synch) { + messaging->cancel(tag, synch); +} + +bool Channel::get(Message& msg, const Queue& queue, AckMode ackMode) { + return messaging->get(msg, queue, ackMode); +} + +void Channel::publish(const Message& msg, const Exchange& exchange, + const std::string& routingKey, + bool mandatory, bool immediate) { + messaging->publish(msg, exchange, routingKey, mandatory, immediate); +} + +void Channel::setReturnedMessageHandler(ReturnedMessageHandler* handler) { + messaging->setReturnedMessageHandler(handler); +} + +void Channel::run() { + messaging->run(); +} + diff --git a/cpp/src/qpid/client/ClientChannel.h b/cpp/src/qpid/client/ClientChannel.h new file mode 100644 index 0000000000..7f3074ea1b --- /dev/null +++ b/cpp/src/qpid/client/ClientChannel.h @@ -0,0 +1,356 @@ +#ifndef _client_ClientChannel_h +#define _client_ClientChannel_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 +#include "qpid/framing/amqp_framing.h" +#include "ClientExchange.h" +#include "ClientMessage.h" +#include "ClientQueue.h" +#include "ResponseHandler.h" +#include "qpid/framing/ChannelAdapter.h" +#include "qpid/sys/Thread.h" +#include "AckMode.h" + +namespace qpid { + +namespace framing { +class ChannelCloseBody; +class AMQMethodBody; +} + +namespace client { + +class Connection; +class MessageChannel; +class MessageListener; +class ReturnedMessageHandler; + +/** + * Represents an AMQP channel, i.e. loosely a session of work. It + * is through a channel that most of the AMQP 'methods' are + * exposed. + * + * \ingroup clientapi + */ +class Channel : public framing::ChannelAdapter +{ + private: + struct UnknownMethod {}; + typedef shared_ptr MethodPtr; + + sys::Mutex lock; + boost::scoped_ptr messaging; + Connection* connection; + sys::Thread dispatcher; + ResponseHandler responses; + + uint16_t prefetch; + const bool transactional; + framing::ProtocolVersion version; + + void handleHeader(framing::AMQHeaderBody::shared_ptr body); + void handleContent(framing::AMQContentBody::shared_ptr body); + void handleHeartbeat(framing::AMQHeartbeatBody::shared_ptr body); + void handleMethodInContext( + framing::AMQMethodBody::shared_ptr, const framing::MethodContext&); + void handleChannel(framing::AMQMethodBody::shared_ptr method); + void handleConnection(framing::AMQMethodBody::shared_ptr method); + + void setQos(); + + void protocolInit( + const std::string& uid, const std::string& pwd, + const std::string& vhost); + + framing::AMQMethodBody::shared_ptr sendAndReceive( + framing::AMQMethodBody::shared_ptr, + framing::ClassId, framing::MethodId); + + framing::AMQMethodBody::shared_ptr sendAndReceiveSync( + bool sync, + framing::AMQMethodBody::shared_ptr, + framing::ClassId, framing::MethodId); + + template + boost::shared_ptr sendAndReceive(framing::AMQMethodBody::shared_ptr body) { + return boost::shared_polymorphic_downcast( + sendAndReceive(body, BodyType::CLASS_ID, BodyType::METHOD_ID)); + } + + template + boost::shared_ptr sendAndReceiveSync( + bool sync, framing::AMQMethodBody::shared_ptr body) { + return boost::shared_polymorphic_downcast( + sendAndReceiveSync( + sync, body, BodyType::CLASS_ID, BodyType::METHOD_ID)); + } + + void open(framing::ChannelId, Connection&); + void closeInternal(); + void peerClose(boost::shared_ptr); + + // FIXME aconway 2007-02-23: Get rid of friendships. + friend class Connection; + friend class BasicMessageChannel; // for sendAndReceive. + friend class MessageMessageChannel; // for sendAndReceive. + + public: + enum InteropMode { AMQP_08, AMQP_09 }; + + /** + * Creates a channel object. + * + * @param transactional if true, the publishing and acknowledgement + * of messages will be transactional and can be committed or + * aborted in atomic units (@see commit(), @see rollback()) + * + * @param prefetch specifies the number of unacknowledged + * messages the channel is willing to have sent to it + * asynchronously + * + * @param messageImpl Alternate messaging implementation class to + * allow alternate protocol implementations of messaging + * operations. Takes ownership. + */ + Channel( + bool transactional = false, u_int16_t prefetch = 500, + InteropMode=AMQP_08); + + ~Channel(); + + /** + * Declares an exchange. + * + * In AMQP Exchanges are the destinations to which messages + * are published. They have Queues bound to them and route + * messages they receive to those queues. The routing rules + * depend on the type of the exchange. + * + * @param exchange an Exchange object representing the + * exchange to declare + * + * @param synch if true this call will block until a response + * is received from the broker + */ + void declareExchange(Exchange& exchange, bool synch = true); + /** + * Deletes an exchange + * + * @param exchange an Exchange object representing the exchange to delete + * + * @param synch if true this call will block until a response + * is received from the broker + */ + void deleteExchange(Exchange& exchange, bool synch = true); + /** + * Declares a Queue + * + * @param queue a Queue object representing the queue to declare + * + * @param synch if true this call will block until a response + * is received from the broker + */ + void declareQueue(Queue& queue, bool synch = true); + /** + * Deletes a Queue + * + * @param queue a Queue object representing the queue to delete + * + * @param synch if true this call will block until a response + * is received from the broker + */ + void deleteQueue(Queue& queue, bool ifunused = false, bool ifempty = false, bool synch = true); + /** + * Binds a queue to an exchange. The exact semantics of this + * (in particular how 'routing keys' and 'binding arguments' + * are used) depends on the type of the exchange. + * + * @param exchange an Exchange object representing the + * exchange to bind to + * + * @param queue a Queue object representing the queue to be + * bound + * + * @param key the 'routing key' for the binding + * + * @param args the 'binding arguments' for the binding + * + * @param synch if true this call will block until a response + * is received from the broker + */ + void bind(const Exchange& exchange, const Queue& queue, + const std::string& key, const framing::FieldTable& args, + bool synch = true); + + /** + * For a transactional channel this will commit all + * publications and acknowledgements since the last commit (or + * the channel was opened if there has been no previous + * commit). This will cause published messages to become + * available to consumers and acknowledged messages to be + * consumed and removed from the queues they were dispatched + * from. + * + * Transactionailty of a channel is specified when the channel + * object is created (@see Channel()). + */ + void commit(); + + /** + * For a transactional channel, this will rollback any + * publications or acknowledgements. It will be as if the + * ppblished messages were never sent and the acknowledged + * messages were never consumed. + */ + void rollback(); + + /** + * Change the prefetch in use. + */ + void setPrefetch(uint16_t prefetch); + + uint16_t getPrefetch() { return prefetch; } + + /** + * Start message dispatching on a new thread + */ + void start(); + + /** + * Close the channel with optional error information. + * Closing a channel that is not open has no effect. + */ + void close( + framing::ReplyCode = 200, const std::string& ="OK", + framing::ClassId = 0, framing::MethodId = 0); + + /** True if the channel is transactional */ + bool isTransactional() { return transactional; } + + /** True if the channel is open */ + bool isOpen() const; + + /** Get the connection associated with this channel */ + Connection& getConnection() { return *connection; } + + /** Return the protocol version */ + framing::ProtocolVersion getVersion() const { return version ; } + + /** + * Creates a 'consumer' for a queue. Messages in (or arriving + * at) that queue will be delivered to consumers + * asynchronously. + * + * @param queue a Queue instance representing the queue to + * consume from + * + * @param tag an identifier to associate with the consumer + * that can be used to cancel its subscription (if empty, this + * will be assigned by the broker) + * + * @param listener a pointer to an instance of an + * implementation of the MessageListener interface. Messages + * received from this queue for this consumer will result in + * invocation of the received() method on the listener, with + * the message itself passed in. + * + * @param ackMode the mode of acknowledgement that the broker + * should assume for this consumer. @see AckMode + * + * @param noLocal if true, this consumer will not be sent any + * message published by this connection + * + * @param synch if true this call will block until a response + * is received from the broker + */ + void consume( + Queue& queue, std::string& tag, MessageListener* listener, + AckMode ackMode = NO_ACK, bool noLocal = false, bool synch = true, + const framing::FieldTable* fields = 0); + + /** + * Cancels a subscription previously set up through a call to consume(). + * + * @param tag the identifier used (or assigned) in the consume + * request that set up the subscription to be cancelled. + * + * @param synch if true this call will block until a response + * is received from the broker + */ + void cancel(const std::string& tag, bool synch = true); + /** + * Synchronous pull of a message from a queue. + * + * @param msg a message object that will contain the message + * headers and content if the call completes. + * + * @param queue the queue to consume from + * + * @param ackMode the acknowledgement mode to use (@see + * AckMode) + * + * @return true if a message was succcessfully dequeued from + * the queue, false if the queue was empty. + */ + bool get(Message& msg, const Queue& queue, AckMode ackMode = NO_ACK); + + /** + * Publishes (i.e. sends a message to the broker). + * + * @param msg the message to publish + * + * @param exchange the exchange to publish the message to + * + * @param routingKey the routing key to publish with + * + * @param mandatory if true and the exchange to which this + * publish is directed has no matching bindings, the message + * will be returned (see setReturnedMessageHandler()). + * + * @param immediate if true and there is no consumer to + * receive this message on publication, the message will be + * returned (see setReturnedMessageHandler()). + */ + void publish(const Message& msg, const Exchange& exchange, + const std::string& routingKey, + bool mandatory = false, bool immediate = false); + + /** + * Set a handler for this channel that will process any + * returned messages + * + * @see publish() + */ + void setReturnedMessageHandler(ReturnedMessageHandler* handler); + + /** + * Deliver messages from the broker to the appropriate MessageListener. + */ + void run(); + + +}; + +}} + +#endif /*!_client_ClientChannel_h*/ diff --git a/cpp/src/qpid/client/ClientConnection.cpp b/cpp/src/qpid/client/ClientConnection.cpp new file mode 100644 index 0000000000..48616cf3d9 --- /dev/null +++ b/cpp/src/qpid/client/ClientConnection.cpp @@ -0,0 +1,156 @@ +/* + * + * 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 +#include +#include + +#include "Connection.h" +#include "ClientChannel.h" +#include "ClientMessage.h" +#include "qpid/QpidError.h" +#include +#include +#include "MethodBodyInstances.h" +#include + +using namespace qpid::framing; +using namespace qpid::sys; + + +namespace qpid { +namespace client { + +const std::string Connection::OK("OK"); + +Connection::Connection( + bool _debug, uint32_t _max_frame_size, + framing::ProtocolVersion _version +) : channelIdCounter(0), version(_version), max_frame_size(_max_frame_size), + defaultConnector(version, _debug, _max_frame_size), + isOpen(false), debug(_debug) +{ + setConnector(defaultConnector); +} + +Connection::~Connection(){} + +void Connection::setConnector(Connector& con) +{ + connector = &con; + connector->setInputHandler(this); + connector->setTimeoutHandler(this); + connector->setShutdownHandler(this); + out = connector->getOutputHandler(); +} + +void Connection::open( + const std::string& host, int port, + const std::string& uid, const std::string& pwd, const std::string& vhost) +{ + if (isOpen) + THROW_QPID_ERROR(INTERNAL_ERROR, "Channel object is already open"); + connector->connect(host, port); + channels[0] = &channel0; + channel0.open(0, *this); + channel0.protocolInit(uid, pwd, vhost); + isOpen = true; +} + +void Connection::shutdown() { + close(); +} + +void Connection::close( + ReplyCode code, const string& msg, ClassId classId, MethodId methodId +) +{ + if(isOpen) { + // TODO aconway 2007-01-29: Exception handling - could end up + // partly closed with threads left unjoined. + isOpen = false; + channel0.sendAndReceive( + make_shared_ptr(new ConnectionCloseBody( + getVersion(), code, msg, classId, methodId))); + + using boost::bind; + for_each(channels.begin(), channels.end(), + bind(&Channel::closeInternal, + bind(&ChannelMap::value_type::second, _1))); + channels.clear(); + connector->close(); + } +} + +void Connection::openChannel(Channel& channel) { + ChannelId id = ++channelIdCounter; + assert (channels.find(id) == channels.end()); + assert(out); + channels[id] = &channel; + channel.open(id, *this); +} + +void Connection::erase(ChannelId id) { + channels.erase(id); +} + +void Connection::received(AMQFrame* frame){ + // FIXME aconway 2007-01-25: Mutex + ChannelId id = frame->getChannel(); + Channel* channel = channels[id]; + // FIXME aconway 2007-01-26: Exception thrown here is hanging the + // client. Need to review use of exceptions. + if (channel == 0) + THROW_QPID_ERROR( + PROTOCOL_ERROR+504, + (boost::format("Invalid channel number %g") % id).str()); + try{ + channel->handleBody(frame->getBody()); + }catch(const qpid::QpidError& e){ + channelException( + *channel, dynamic_cast(frame->getBody().get()), e); + } +} + +void Connection::send(AMQFrame* frame) { + out->send(frame); +} + +void Connection::channelException( + Channel& channel, AMQMethodBody* method, const QpidError& e) +{ + int code = (e.code >= PROTOCOL_ERROR) ? e.code - PROTOCOL_ERROR : 500; + string msg = e.msg; + if(method == 0) + channel.close(code, msg); + else + channel.close( + code, msg, method->amqpClassId(), method->amqpMethodId()); +} + +void Connection::idleIn(){ + connector->close(); +} + +void Connection::idleOut(){ + out->send(new AMQFrame(version, 0, new AMQHeartbeatBody())); +} + +}} // namespace qpid::client diff --git a/cpp/src/qpid/client/ClientExchange.cpp b/cpp/src/qpid/client/ClientExchange.cpp new file mode 100644 index 0000000000..d5914beea2 --- /dev/null +++ b/cpp/src/qpid/client/ClientExchange.cpp @@ -0,0 +1,34 @@ +/* + * + * 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 "ClientExchange.h" + +qpid::client::Exchange::Exchange(std::string _name, std::string _type) : name(_name), type(_type){} +const std::string& qpid::client::Exchange::getName() const { return name; } +const std::string& qpid::client::Exchange::getType() const { return type; } + +const std::string qpid::client::Exchange::DIRECT_EXCHANGE = "direct"; +const std::string qpid::client::Exchange::TOPIC_EXCHANGE = "topic"; +const std::string qpid::client::Exchange::HEADERS_EXCHANGE = "headers"; + +const qpid::client::Exchange qpid::client::Exchange::DEFAULT_EXCHANGE("", DIRECT_EXCHANGE); +const qpid::client::Exchange qpid::client::Exchange::STANDARD_DIRECT_EXCHANGE("amq.direct", DIRECT_EXCHANGE); +const qpid::client::Exchange qpid::client::Exchange::STANDARD_TOPIC_EXCHANGE("amq.topic", TOPIC_EXCHANGE); +const qpid::client::Exchange qpid::client::Exchange::STANDARD_HEADERS_EXCHANGE("amq.headers", HEADERS_EXCHANGE); diff --git a/cpp/src/qpid/client/ClientExchange.h b/cpp/src/qpid/client/ClientExchange.h new file mode 100644 index 0000000000..a8ac21fa9b --- /dev/null +++ b/cpp/src/qpid/client/ClientExchange.h @@ -0,0 +1,106 @@ +/* + * + * 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 + +#ifndef _Exchange_ +#define _Exchange_ + +namespace qpid { +namespace client { + + /** + * A 'handle' used to represent an AMQP exchange in the Channel + * methods. Exchanges are the destinations to which messages are + * published. + * + * There are different types of exchange (the standard types are + * available as static constants, see DIRECT_EXCHANGE, + * TOPIC_EXCHANGE and HEADERS_EXCHANGE). A Queue can be bound to + * an exchange using Channel::bind() and messages published to + * that exchange are then routed to the queue based on the details + * of the binding and the type of exchange. + * + * There are some standard exchange instances that are predeclared + * on all AMQP brokers. These are defined as static members + * STANDARD_DIRECT_EXCHANGE, STANDARD_TOPIC_EXCHANGE and + * STANDARD_HEADERS_EXCHANGE. There is also the 'default' exchange + * (member DEFAULT_EXCHANGE) which is nameless and of type + * 'direct' and has every declared queue bound to it by queue + * name. + * + * \ingroup clientapi + */ + class Exchange{ + const std::string name; + const std::string type; + + public: + /** + * A direct exchange routes messages published with routing + * key X to any queue bound with key X (i.e. an exact match is + * used). + */ + static const std::string DIRECT_EXCHANGE; + /** + * A topic exchange treat the key with which a queue is bound + * as a pattern and routes all messages whose routing keys + * match that pattern to the bound queue. The routing key for + * a message must consist of zero or more alpha-numeric words + * delimited by dots. The pattern is of a similar form but * + * can be used to match excatly one word and # can be used to + * match zero or more words. + */ + static const std::string TOPIC_EXCHANGE; + /** + * The headers exchange routes messages based on whether their + * headers match the binding arguments specified when + * binding. (see the AMQP spec for more details). + */ + static const std::string HEADERS_EXCHANGE; + + /** + * The 'default' exchange, nameless and of type 'direct'. Has + * every declared queue bound to it by name. + */ + static const Exchange DEFAULT_EXCHANGE; + /** + * The standard direct exchange, named amq.direct. + */ + static const Exchange STANDARD_DIRECT_EXCHANGE; + /** + * The standard topic exchange, named amq.topic. + */ + static const Exchange STANDARD_TOPIC_EXCHANGE; + /** + * The standard headers exchange, named amq.header. + */ + static const Exchange STANDARD_HEADERS_EXCHANGE; + + Exchange(std::string name, std::string type = DIRECT_EXCHANGE); + const std::string& getName() const; + const std::string& getType() const; + }; + +} +} + + +#endif diff --git a/cpp/src/qpid/client/ClientMessage.h b/cpp/src/qpid/client/ClientMessage.h new file mode 100644 index 0000000000..3de3a693b9 --- /dev/null +++ b/cpp/src/qpid/client/ClientMessage.h @@ -0,0 +1,64 @@ +#ifndef _client_ClientMessage_h +#define _client_ClientMessage_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 +#include "qpid/framing/BasicHeaderProperties.h" + +namespace qpid { +namespace client { + +/** + * A representation of messages for sent or recived through the + * client api. + * + * \ingroup clientapi + */ +// FIXME aconway 2007-04-05: Should be based on MessageTransfer properties not +// basic header properties. +class Message : public framing::BasicHeaderProperties { + public: + Message(const std::string& data_=std::string()) : data(data_) {} + + std::string getData() const { return data; } + void setData(const std::string& _data) { data = _data; } + + std::string getDestination() const { return destination; } + void setDestination(const std::string& dest) { destination = dest; } + + // TODO aconway 2007-03-22: only needed for Basic.deliver support. + uint64_t getDeliveryTag() const { return deliveryTag; } + void setDeliveryTag(uint64_t dt) { deliveryTag = dt; } + + bool isRedelivered() const { return redelivered; } + void setRedelivered(bool _redelivered){ redelivered = _redelivered; } + + private: + std::string data; + std::string destination; + bool redelivered; + uint64_t deliveryTag; +}; + +}} + +#endif /*!_client_ClientMessage_h*/ diff --git a/cpp/src/qpid/client/ClientQueue.cpp b/cpp/src/qpid/client/ClientQueue.cpp new file mode 100644 index 0000000000..613cf8d288 --- /dev/null +++ b/cpp/src/qpid/client/ClientQueue.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 "ClientQueue.h" + +qpid::client::Queue::Queue() : name(""), autodelete(true), exclusive(true), durable(false){} + +qpid::client::Queue::Queue(std::string _name) : name(_name), autodelete(false), exclusive(false), durable(false){} + +qpid::client::Queue::Queue(std::string _name, bool temp) : name(_name), autodelete(temp), exclusive(temp), durable(false){} + +qpid::client::Queue::Queue(std::string _name, bool _autodelete, bool _exclusive, bool _durable) + : name(_name), autodelete(_autodelete), exclusive(_exclusive), durable(_durable){} + +const std::string& qpid::client::Queue::getName() const{ + return name; +} + +void qpid::client::Queue::setName(const std::string& _name){ + name = _name; +} + +bool qpid::client::Queue::isAutoDelete() const{ + return autodelete; +} + +bool qpid::client::Queue::isExclusive() const{ + return exclusive; +} + +bool qpid::client::Queue::isDurable() const{ + return durable; +} + +void qpid::client::Queue::setDurable(bool _durable){ + durable = _durable; +} + + + + diff --git a/cpp/src/qpid/client/ClientQueue.h b/cpp/src/qpid/client/ClientQueue.h new file mode 100644 index 0000000000..b37a44b004 --- /dev/null +++ b/cpp/src/qpid/client/ClientQueue.h @@ -0,0 +1,103 @@ +#ifndef _client_ClientQueue_h +#define _client_ClientQueue_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 + +namespace qpid { +namespace client { + + /** + * A 'handle' used to represent an AMQP queue in the Channel + * methods. Creating an instance of this class does not cause the + * queue to be created on the broker. Rather, an instance of this + * class should be passed to Channel::declareQueue() to ensure + * that the queue exists or is created. + * + * Queues hold messages and allow clients to consume + * (see Channel::consume()) or get (see Channel::get()) those messags. A + * queue receives messages by being bound to one or more Exchange; + * messages published to that exchange may then be routed to the + * queue based on the details of the binding and the type of the + * exchange (see Channel::bind()). + * + * Queues are identified by a name. They can be exclusive (in which + * case they can only be used in the context of the connection + * over which they were declared, and are deleted when then + * connection closes), or they can be shared. Shared queues can be + * auto deleted when they have no consumers. + * + * We use the term 'temporary queue' to refer to an exclusive + * queue. + * + * \ingroup clientapi + */ + class Queue{ + std::string name; + const bool autodelete; + const bool exclusive; + bool durable; + + public: + + /** + * Creates an unnamed, non-durable, temporary queue. A name + * will be assigned to this queue instance by a call to + * Channel::declareQueue(). + */ + Queue(); + /** + * Creates a shared, non-durable, queue with a given name, + * that will not be autodeleted. + * + * @param name the name of the queue + */ + Queue(std::string name); + /** + * Creates a non-durable queue with a given name. + * + * @param name the name of the queue + * + * @param temp if true the queue will be a temporary queue, if + * false it will be shared and not autodeleted. + */ + Queue(std::string name, bool temp); + /** + * This constructor allows the autodelete, exclusive and + * durable propeties to be explictly set. Note however that if + * exclusive is true, autodelete has no meaning as exclusive + * queues are always destroyed when the connection that + * created them is closed. + */ + Queue(std::string name, bool autodelete, bool exclusive, bool durable); + const std::string& getName() const; + void setName(const std::string&); + bool isAutoDelete() const; + bool isExclusive() const; + bool isDurable() const; + void setDurable(bool durable); + }; + +} +} + +#endif /*!_client_ClientQueue_h*/ diff --git a/cpp/src/qpid/client/Connection.h b/cpp/src/qpid/client/Connection.h new file mode 100644 index 0000000000..071a1d9446 --- /dev/null +++ b/cpp/src/qpid/client/Connection.h @@ -0,0 +1,179 @@ +#ifndef _client_Connection_ +#define _client_Connection_ + +/* + * + * 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 +#include +#include "qpid/QpidError.h" +#include "ClientChannel.h" +#include "Connector.h" +#include "qpid/sys/ShutdownHandler.h" +#include "qpid/sys/TimeoutHandler.h" + + +namespace qpid { + +/** + * The client namespace contains all classes that make up a client + * implementation of the AMQP protocol. The key classes that form + * the basis of the client API to be used by applications are + * Connection and Channel. + */ +namespace client { + +/** + * \internal provide access to selected private channel functions + * for the Connection without making it a friend of the entire channel. + */ +class ConnectionForChannel : + public framing::InputHandler, + public framing::OutputHandler, + public sys::TimeoutHandler, + public sys::ShutdownHandler + +{ + private: + friend class Channel; + virtual void erase(framing::ChannelId) = 0; +}; + + +/** + * \defgroup clientapi Application API for an AMQP client + */ + +/** + * Represents a connection to an AMQP broker. All communication is + * initiated by establishing a connection, then opening one or + * more Channels over that connection. + * + * \ingroup clientapi + */ +class Connection : public ConnectionForChannel +{ + typedef std::map ChannelMap; + + framing::ChannelId channelIdCounter; + static const std::string OK; + + framing::ProtocolVersion version; + const uint32_t max_frame_size; + ChannelMap channels; + Connector defaultConnector; + Connector* connector; + framing::OutputHandler* out; + volatile bool isOpen; + Channel channel0; + bool debug; + + void erase(framing::ChannelId); + void channelException( + Channel&, framing::AMQMethodBody*, const QpidError&); + + // TODO aconway 2007-01-26: too many friendships, untagle these classes. + friend class Channel; + + public: + /** + * Creates a connection object, but does not open the + * connection. + * + * @param _version the version of the protocol to connect with + * + * @param debug turns on tracing for the connection + * (i.e. prints details of the frames sent and received to std + * out). Optional and defaults to false. + * + * @param max_frame_size the maximum frame size that the + * client will accept. Optional and defaults to 65536. + */ + Connection(bool debug = false, uint32_t max_frame_size = 65536, + framing::ProtocolVersion=framing::highestProtocolVersion); + ~Connection(); + + /** + * Opens a connection to a broker. + * + * @param host the host on which the broker is running + * + * @param port the port on the which the broker is listening + * + * @param uid the userid to connect with + * + * @param pwd the password to connect with (currently SASL + * PLAIN is the only authentication method supported so this + * is sent in clear text) + * + * @param virtualhost the AMQP virtual host to use (virtual + * hosts, where implemented(!), provide namespace partitioning + * within a single broker). + */ + void open(const std::string& host, int port = 5672, + const std::string& uid = "guest", + const std::string& pwd = "guest", + const std::string& virtualhost = "/"); + + /** + * Close the connection with optional error information for the peer. + * + * Any further use of this connection (without reopening it) will + * not succeed. + */ + void close(framing::ReplyCode=200, const std::string& msg=OK, + framing::ClassId = 0, framing::MethodId = 0); + + /** + * Associate a Channel with this connection and open it for use. + * + * In AMQP channels are like multi-plexed 'sessions' of work over + * a connection. Almost all the interaction with AMQP is done over + * a channel. + * + * @param connection the connection object to be associated with + * the channel. Call Channel::close() to close the channel. + */ + void openChannel(Channel&); + + + // TODO aconway 2007-01-26: can these be private? + void send(framing::AMQFrame*); + void received(framing::AMQFrame*); + void idleOut(); + void idleIn(); + void shutdown(); + + /**\internal used for testing */ + void setConnector(Connector& connector); + + /** + * @return the maximum frame size in use on this connection + */ + inline uint32_t getMaxFrameSize(){ return max_frame_size; } + + /** @return protocol version in use on this connection. */ + framing::ProtocolVersion getVersion() const { return version; } +}; + +}} // namespace qpid::client + + +#endif diff --git a/cpp/src/qpid/client/Connector.cpp b/cpp/src/qpid/client/Connector.cpp new file mode 100644 index 0000000000..9230050ff7 --- /dev/null +++ b/cpp/src/qpid/client/Connector.cpp @@ -0,0 +1,188 @@ +/* + * + * 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 +#include "qpid/QpidError.h" +#include "qpid/sys/Time.h" +#include "Connector.h" + +namespace qpid { +namespace client { + +using namespace qpid::sys; +using namespace qpid::framing; +using qpid::QpidError; + +Connector::Connector( + ProtocolVersion ver, bool _debug, uint32_t buffer_size +) : debug(_debug), + receive_buffer_size(buffer_size), + send_buffer_size(buffer_size), + version(ver), + closed(true), + lastIn(0), lastOut(0), + timeout(0), + idleIn(0), idleOut(0), + timeoutHandler(0), + shutdownHandler(0), + inbuf(receive_buffer_size), + outbuf(send_buffer_size) +{ } + +Connector::~Connector(){ } + +void Connector::connect(const std::string& host, int port){ + socket = Socket::createTcp(); + socket.connect(host, port); + closed = false; + receiver = Thread(this); +} + +void Connector::init(){ + ProtocolInitiation init(version); + writeBlock(&init); +} + +void Connector::close(){ + closed = true; + socket.close(); + receiver.join(); +} + +void Connector::setInputHandler(InputHandler* handler){ + input = handler; +} + +void Connector::setShutdownHandler(ShutdownHandler* handler){ + shutdownHandler = handler; +} + +OutputHandler* Connector::getOutputHandler(){ + return this; +} + +void Connector::send(AMQFrame* f){ + std::auto_ptr frame(f); + AMQBody::shared_ptr body = frame->getBody(); + writeBlock(frame.get()); + if(debug) std::cout << "SENT: " << *frame << std::endl; +} + +void Connector::writeBlock(AMQDataBlock* data){ + Mutex::ScopedLock l(writeLock); + data->encode(outbuf); + //transfer data to wire + outbuf.flip(); + writeToSocket(outbuf.start(), outbuf.available()); + outbuf.clear(); +} + +void Connector::writeToSocket(char* data, size_t available){ + size_t written = 0; + while(written < available && !closed){ + ssize_t sent = socket.send(data + written, available-written); + if(sent > 0) { + lastOut = now() * TIME_MSEC; + written += sent; + } + } +} + +void Connector::handleClosed(){ + closed = true; + socket.close(); + if(shutdownHandler) shutdownHandler->shutdown(); +} + +void Connector::checkIdle(ssize_t status){ + if(timeoutHandler){ + Time t = now() * TIME_MSEC; + if(status == Socket::SOCKET_TIMEOUT) { + if(idleIn && (t - lastIn > idleIn)){ + timeoutHandler->idleIn(); + } + } + else if(status == 0 || status == Socket::SOCKET_EOF) { + handleClosed(); + } + else { + lastIn = t; + } + if(idleOut && (t - lastOut > idleOut)){ + timeoutHandler->idleOut(); + } + } +} + +void Connector::setReadTimeout(uint16_t t){ + idleIn = t * 1000;//t is in secs + if(idleIn && (!timeout || idleIn < timeout)){ + timeout = idleIn; + setSocketTimeout(); + } + +} + +void Connector::setWriteTimeout(uint16_t t){ + idleOut = t * 1000;//t is in secs + if(idleOut && (!timeout || idleOut < timeout)){ + timeout = idleOut; + setSocketTimeout(); + } +} + +void Connector::setSocketTimeout(){ + socket.setTimeout(timeout*TIME_MSEC); +} + +void Connector::setTimeoutHandler(TimeoutHandler* handler){ + timeoutHandler = handler; +} + +void Connector::run(){ + try{ + while(!closed){ + ssize_t available = inbuf.available(); + if(available < 1){ + THROW_QPID_ERROR(INTERNAL_ERROR, "Frame exceeds buffer size."); + } + ssize_t received = socket.recv(inbuf.start(), available); + checkIdle(received); + + if(!closed && received > 0){ + inbuf.move(received); + inbuf.flip();//position = 0, limit = total data read + + AMQFrame frame(version); + while(frame.decode(inbuf)){ + if(debug) std::cout << "RECV: " << frame << std::endl; + input->received(&frame); + } + //need to compact buffer to preserve any 'extra' data + inbuf.compact(); + } + } + } catch (const std::exception& e) { + std::cout << e.what() << std::endl; + handleClosed(); + } +} + +}} // namespace qpid::client diff --git a/cpp/src/qpid/client/Connector.h b/cpp/src/qpid/client/Connector.h new file mode 100644 index 0000000000..c63a1ce8ac --- /dev/null +++ b/cpp/src/qpid/client/Connector.h @@ -0,0 +1,98 @@ +/* + * + * 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 _Connector_ +#define _Connector_ + + +#include "qpid/framing/InputHandler.h" +#include "qpid/framing/OutputHandler.h" +#include "qpid/framing/InitiationHandler.h" +#include "qpid/framing/ProtocolInitiation.h" +#include "qpid/framing/ProtocolVersion.h" +#include "qpid/sys/ShutdownHandler.h" +#include "qpid/sys/TimeoutHandler.h" +#include "qpid/sys/Thread.h" +#include "qpid/sys/Monitor.h" +#include "qpid/sys/Socket.h" + +namespace qpid { + +namespace client { + +class Connector : public framing::OutputHandler, + private sys::Runnable +{ + const bool debug; + const int receive_buffer_size; + const int send_buffer_size; + framing::ProtocolVersion version; + + bool closed; + + int64_t lastIn; + int64_t lastOut; + int64_t timeout; + uint32_t idleIn; + uint32_t idleOut; + + sys::TimeoutHandler* timeoutHandler; + sys::ShutdownHandler* shutdownHandler; + framing::InputHandler* input; + framing::InitiationHandler* initialiser; + framing::OutputHandler* output; + + framing::Buffer inbuf; + framing::Buffer outbuf; + + sys::Mutex writeLock; + sys::Thread receiver; + + sys::Socket socket; + + void checkIdle(ssize_t status); + void writeBlock(framing::AMQDataBlock* data); + void writeToSocket(char* data, size_t available); + void setSocketTimeout(); + + void run(); + void handleClosed(); + + friend class Channel; + public: + Connector(framing::ProtocolVersion pVersion, + bool debug = false, uint32_t buffer_size = 1024); + virtual ~Connector(); + virtual void connect(const std::string& host, int port); + virtual void init(); + virtual void close(); + virtual void setInputHandler(framing::InputHandler* handler); + virtual void setTimeoutHandler(sys::TimeoutHandler* handler); + virtual void setShutdownHandler(sys::ShutdownHandler* handler); + virtual framing::OutputHandler* getOutputHandler(); + virtual void send(framing::AMQFrame* frame); + virtual void setReadTimeout(uint16_t timeout); + virtual void setWriteTimeout(uint16_t timeout); +}; + +}} + + +#endif diff --git a/cpp/src/qpid/client/IncomingMessage.cpp b/cpp/src/qpid/client/IncomingMessage.cpp new file mode 100644 index 0000000000..059e644464 --- /dev/null +++ b/cpp/src/qpid/client/IncomingMessage.cpp @@ -0,0 +1,168 @@ +/* + * + * 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 "IncomingMessage.h" +#include "qpid/Exception.h" +#include "ClientMessage.h" +#include + +namespace qpid { +namespace client { + +using boost::format; +using sys::Mutex; + +IncomingMessage::Destination::~Destination() {} + + +IncomingMessage::WaitableDestination::WaitableDestination() + : shutdownFlag(false) {} + +void IncomingMessage::WaitableDestination::message(const Message& msg) { + Mutex::ScopedLock l(monitor); + queue.push(msg); + monitor.notify(); +} + +void IncomingMessage::WaitableDestination::empty() { + Mutex::ScopedLock l(monitor); + queue.push(Empty()); + monitor.notify(); +} + +bool IncomingMessage::WaitableDestination::wait(Message& msgOut) { + Mutex::ScopedLock l(monitor); + while (queue.empty() && !shutdownFlag) + monitor.wait(); + if (shutdownFlag) + return false; + Message* msg = boost::get(&queue.front()); + bool success = msg; + if (success) + msgOut=*msg; + queue.pop(); + if (!queue.empty()) + monitor.notify(); // Wake another waiter. + return success; +} + +void IncomingMessage::WaitableDestination::shutdown() { + Mutex::ScopedLock l(monitor); + shutdownFlag = true; + monitor.notifyAll(); +} + +void IncomingMessage::openReference(const std::string& name) { + Mutex::ScopedLock l(lock); + if (references.find(name) != references.end()) + throw ConnectionException( + 503, format("Attempt to open existing reference %s.") % name); + references[name]; + return; +} + +void IncomingMessage::appendReference( + const std::string& name, const std::string& data) +{ + Mutex::ScopedLock l(lock); + getRefUnlocked(name).data += data; +} + +Message& IncomingMessage::createMessage( + const std::string& destination, const std::string& reference) +{ + Mutex::ScopedLock l(lock); + getDestUnlocked(destination); // Verify destination. + Reference& ref = getRefUnlocked(reference); + ref.messages.resize(ref.messages.size() +1); + ref.messages.back().setDestination(destination); + return ref.messages.back(); +} + +void IncomingMessage::closeReference(const std::string& name) { + Reference refCopy; + { + Mutex::ScopedLock l(lock); + refCopy = getRefUnlocked(name); + references.erase(name); + } + for (std::vector::iterator i = refCopy.messages.begin(); + i != refCopy.messages.end(); + ++i) + { + i->setData(refCopy.data); + // TODO aconway 2007-03-23: Thread safety, + // can a destination be removed while we're doing this? + getDestination(i->getDestination()).message(*i); + } +} + + +void IncomingMessage::addDestination(std::string name, Destination& dest) { + Mutex::ScopedLock l(lock); + DestinationMap::iterator i = destinations.find(name); + if (i == destinations.end()) + destinations[name]=&dest; + else if (i->second != &dest) + throw ConnectionException( + 503, format("Destination already exists: %s.") % name); +} + +void IncomingMessage::removeDestination(std::string name) { + Mutex::ScopedLock l(lock); + DestinationMap::iterator i = destinations.find(name); + if (i == destinations.end()) + throw ConnectionException( + 503, format("No such destination: %s.") % name); + destinations.erase(i); +} + +IncomingMessage::Destination& IncomingMessage::getDestination( + const std::string& name) { + return getDestUnlocked(name); +} + +IncomingMessage::Reference& IncomingMessage::getReference( + const std::string& name) { + return getRefUnlocked(name); +} + +IncomingMessage::Reference& IncomingMessage::getRefUnlocked( + const std::string& name) { + Mutex::ScopedLock l(lock); + ReferenceMap::iterator i = references.find(name); + if (i == references.end()) + throw ConnectionException( + 503, format("No such reference: %s.") % name); + return i->second; +} + +IncomingMessage::Destination& IncomingMessage::getDestUnlocked( + const std::string& name) { + Mutex::ScopedLock l(lock); + DestinationMap::iterator i = destinations.find(name); + if (i == destinations.end()) + throw ConnectionException( + 503, format("No such destination: %s.") % name); + return *i->second; +} + +}} // namespace qpid::client diff --git a/cpp/src/qpid/client/IncomingMessage.h b/cpp/src/qpid/client/IncomingMessage.h new file mode 100644 index 0000000000..7aa8e33df2 --- /dev/null +++ b/cpp/src/qpid/client/IncomingMessage.h @@ -0,0 +1,136 @@ +#ifndef _IncomingMessage_ +#define _IncomingMessage_ + +/* + * + * 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/sys/Monitor.h" +#include +#include +#include +#include + +namespace qpid { +namespace client { + +class Message; + +/** + * Manage incoming messages. + * + * Uses reference and destination concepts from 0-9 Messsage class. + * + * Basic messages use special destination and reference names to indicate + * get-ok, return etc. messages. + * + */ +class IncomingMessage { + public: + /** Accumulate data associated with a set of messages. */ + struct Reference { + std::string data; + std::vector messages; + }; + + /** Interface to a destination for messages. */ + class Destination { + public: + virtual ~Destination(); + + /** Pass a message to the destination */ + virtual void message(const Message&) = 0; + + /** Notify destination of queue-empty contition */ + virtual void empty() = 0; + }; + + + /** A destination that a thread can wait on till a message arrives. */ + class WaitableDestination : public Destination + { + public: + WaitableDestination(); + void message(const Message& msg); + void empty(); + /** Wait till message() or empty() is called. True for message() */ + bool wait(Message& msgOut); + void shutdown(); + + private: + struct Empty {}; + typedef boost::variant Item; + sys::Monitor monitor; + std::queue queue; + bool shutdownFlag; + }; + + + + /** Add a reference. Throws if already open. */ + void openReference(const std::string& name); + + /** Get a reference. Throws if not already open. */ + void appendReference(const std::string& name, + const std::string& data); + + /** Create a message to destination associated with reference + *@exception if destination or reference non-existent. + */ + Message& createMessage(const std::string& destination, + const std::string& reference); + + /** Get a reference. + *@exception if non-existent. + */ + Reference& getReference(const std::string& name); + + /** Close a reference and deliver all its messages. + * Throws if not open or a message has an invalid destination. + */ + void closeReference(const std::string& name); + + /** Add a destination. + *@exception if a different Destination is already registered + * under name. + */ + void addDestination(std::string name, Destination&); + + /** Remove a destination. Throws if does not exist */ + void removeDestination(std::string name); + + /** Get a destination. Throws if does not exist */ + Destination& getDestination(const std::string& name); + private: + + typedef std::map ReferenceMap; + typedef std::map DestinationMap; + + Reference& getRefUnlocked(const std::string& name); + Destination& getDestUnlocked(const std::string& name); + + mutable sys::Mutex lock; + ReferenceMap references; + DestinationMap destinations; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/client/MessageChannel.h b/cpp/src/qpid/client/MessageChannel.h new file mode 100644 index 0000000000..a830a47986 --- /dev/null +++ b/cpp/src/qpid/client/MessageChannel.h @@ -0,0 +1,94 @@ +#ifndef _client_MessageChannel_h +#define _client_MessageChannel_h + +/* + * + * 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/shared_ptr.h" +#include "qpid/sys/Runnable.h" +#include "AckMode.h" + +namespace qpid { + +namespace framing { +class AMQMethodBody; +class AMQHeaderBody; +class AMQContentBody; +class FieldTable; +} + +namespace client { + +class Channel; +class Message; +class Queue; +class Exchange; +class MessageListener; +class ReturnedMessageHandler; + +/** + * Abstract interface for messaging implementation for a channel. + * + *@see Channel for documentation. + */ +class MessageChannel : public sys::Runnable +{ + public: + /**@see Channel::consume */ + virtual void consume( + Queue& queue, std::string& tag, MessageListener* listener, + AckMode ackMode = NO_ACK, bool noLocal = false, bool synch = true, + const framing::FieldTable* fields = 0) = 0; + + /**@see Channel::cancel */ + virtual void cancel(const std::string& tag, bool synch = true) = 0; + + /**@see Channel::get */ + virtual bool get( + Message& msg, const Queue& queue, AckMode ackMode = NO_ACK) = 0; + + /**@see Channel::get */ + virtual void publish(const Message& msg, const Exchange& exchange, + const std::string& routingKey, + bool mandatory = false, bool immediate = false) = 0; + + /**@see Channel::setReturnedMessageHandler */ + virtual void setReturnedMessageHandler( + ReturnedMessageHandler* handler) = 0; + + /** Handle an incoming method. */ + virtual void handle(shared_ptr) = 0; + + /** Handle an incoming header */ + virtual void handle(shared_ptr) = 0; + + /** Handle an incoming content */ + virtual void handle(shared_ptr) = 0; + + /** Send channel's QOS settings */ + virtual void setQos() = 0; + + /** Channel is closing */ + virtual void close() = 0; +}; + +}} // namespace qpid::client + + + +#endif /*!_client_MessageChannel_h*/ diff --git a/cpp/src/qpid/client/MessageListener.cpp b/cpp/src/qpid/client/MessageListener.cpp new file mode 100644 index 0000000000..68ebedeb0d --- /dev/null +++ b/cpp/src/qpid/client/MessageListener.cpp @@ -0,0 +1,24 @@ +/* + * + * 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 "MessageListener.h" + +qpid::client::MessageListener::~MessageListener() {} diff --git a/cpp/src/qpid/client/MessageListener.h b/cpp/src/qpid/client/MessageListener.h new file mode 100644 index 0000000000..501862a3ef --- /dev/null +++ b/cpp/src/qpid/client/MessageListener.h @@ -0,0 +1,49 @@ +/* + * + * 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 + +#ifndef _MessageListener_ +#define _MessageListener_ + +#include "ClientMessage.h" + +namespace qpid { +namespace client { + + /** + * An interface through which asynchronously delivered messages + * can be received by an application. + * + * @see Channel::consume() + * + * \ingroup clientapi + */ + class MessageListener{ + public: + virtual ~MessageListener(); + virtual void received(Message& msg) = 0; + }; + +} +} + + +#endif diff --git a/cpp/src/qpid/client/MessageMessageChannel.cpp b/cpp/src/qpid/client/MessageMessageChannel.cpp new file mode 100644 index 0000000000..6ba5e00153 --- /dev/null +++ b/cpp/src/qpid/client/MessageMessageChannel.cpp @@ -0,0 +1,431 @@ +/* + * + * 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 +#include +#include "MessageMessageChannel.h" +#include "qpid/framing/AMQMethodBody.h" +#include "ClientChannel.h" +#include "ReturnedMessageHandler.h" +#include "MessageListener.h" +#include "qpid/framing/FieldTable.h" +#include "Connection.h" +#include "qpid/shared_ptr.h" +#include + +namespace qpid { +namespace client { + +using namespace std; +using namespace sys; +using namespace framing; + +MessageMessageChannel::MessageMessageChannel(Channel& ch) + : channel(ch), tagCount(0) {} + +string MessageMessageChannel::newTag() { + Mutex::ScopedLock l(lock); + return (boost::format("__tag%d")%++tagCount).str(); +} + +void MessageMessageChannel::consume( + Queue& queue, std::string& tag, MessageListener* /*listener*/, + AckMode ackMode, bool noLocal, bool /*synch*/, const FieldTable* fields) +{ + if (tag.empty()) + tag = newTag(); + channel.sendAndReceive( + make_shared_ptr(new MessageConsumeBody( + channel.getVersion(), 0, queue.getName(), tag, noLocal, + ackMode == NO_ACK, false, fields ? *fields : FieldTable()))); + +// // FIXME aconway 2007-02-20: Race condition! +// // We could receive the first message for the consumer +// // before we create the consumer below. +// // Move consumer creation to handler for MessageConsumeOkBody +// { +// Mutex::ScopedLock l(lock); +// ConsumerMap::iterator i = consumers.find(tag); +// if (i != consumers.end()) +// THROW_QPID_ERROR(CLIENT_ERROR, +// "Consumer already exists with tag="+tag); +// Consumer& c = consumers[tag]; +// c.listener = listener; +// c.ackMode = ackMode; +// c.lastDeliveryTag = 0; +// } +} + + +void MessageMessageChannel::cancel(const std::string& /*tag*/, bool /*synch*/) { + // FIXME aconway 2007-02-23: +// Consumer c; +// { +// Mutex::ScopedLock l(lock); +// ConsumerMap::iterator i = consumers.find(tag); +// if (i == consumers.end()) +// return; +// c = i->second; +// consumers.erase(i); +// } +// if(c.ackMode == LAZY_ACK && c.lastDeliveryTag > 0) +// channel.send(new MessageAckBody(channel.version, c.lastDeliveryTag, true)); +// channel.sendAndReceiveSync( +// synch, new MessageCancelBody(channel.version, tag, !synch)); +} + +void MessageMessageChannel::close(){ + // FIXME aconway 2007-02-23: +// ConsumerMap consumersCopy; +// { +// Mutex::ScopedLock l(lock); +// consumersCopy = consumers; +// consumers.clear(); +// } +// for (ConsumerMap::iterator i=consumersCopy.begin(); +// i != consumersCopy.end(); ++i) +// { +// Consumer& c = i->second; +// if ((c.ackMode == LAZY_ACK || c.ackMode == AUTO_ACK) +// && c.lastDeliveryTag > 0) +// { +// channel.send(new MessageAckBody(channel.version, c.lastDeliveryTag, true)); +// } +// } +// incoming.shutdown(); +} + + +/** Destination ID for the current get. + * Must not clash with a generated consumer ID. + * TODO aconway 2007-03-06: support multiple outstanding gets? + */ +const string getDestinationId("__get__"); + +/** + * A destination that provides a Correlator::Action to handle + * MessageEmpty responses. + */ +struct MessageGetDestination : public IncomingMessage::WaitableDestination +{ + void response(shared_ptr response) { + if (response->amqpClassId() == MessageOkBody::CLASS_ID) { + switch (response->amqpMethodId()) { + case MessageOkBody::METHOD_ID: + // Nothing to do, wait for transfer. + return; + case MessageEmptyBody::METHOD_ID: + empty(); // Wake up waiter with empty queue. + return; + } + } + throw QPID_ERROR(PROTOCOL_ERROR, "Invalid response"); + } + + Correlator::Action action() { + return boost::bind(&MessageGetDestination::response, this, _1); + } +}; + +bool MessageMessageChannel::get( + Message& msg, const Queue& queue, AckMode ackMode) +{ + Mutex::ScopedLock l(lock); + std::string destName=newTag(); + MessageGetDestination dest; + incoming.addDestination(destName, dest); + channel.send( + make_shared_ptr( + new MessageGetBody( + channel.version, 0, queue.getName(), destName, ackMode)), + dest.action()); + return dest.wait(msg); +} + + +/** Convert a message to a transfer command. */ +MessageTransferBody::shared_ptr makeTransfer( + ProtocolVersion version, + const Message& msg, const string& destination, + const std::string& routingKey, bool mandatory, bool immediate) +{ + return MessageTransferBody::shared_ptr( + new MessageTransferBody( + version, + 0, // FIXME aconway 2007-04-03: ticket. + destination, + msg.isRedelivered(), + immediate, + 0, // FIXME aconway 2007-02-23: ttl + msg.getPriority(), + msg.getTimestamp(), + static_cast(msg.getDeliveryMode()), + 0, // FIXME aconway 2007-04-03: Expiration + string(), // Exchange: for broker use only. + routingKey, + msg.getMessageId(), + msg.getCorrelationId(), + msg.getReplyTo(), + msg.getContentType(), + msg.getContentEncoding(), + msg.getUserId(), + msg.getAppId(), + string(), // FIXME aconway 2007-04-03: TransactionId + string(), //FIXME aconway 2007-04-03: SecurityToken + msg.getHeaders(), + Content(INLINE, msg.getData()), + mandatory + )); +} + +// FIXME aconway 2007-04-05: Generated code should provide this. +/** + * Calculate the size of a frame containing the given body type + * if all variable-lengths parts are empty. + */ +template size_t overhead() { + static AMQFrame frame( + ProtocolVersion(), 0, make_shared_ptr(new T(ProtocolVersion()))); + return frame.size(); +} + +void MessageMessageChannel::publish( + const Message& msg, const Exchange& exchange, + const std::string& routingKey, bool mandatory, bool immediate) +{ + MessageTransferBody::shared_ptr transfer = makeTransfer( + channel.getVersion(), + msg, exchange.getName(), routingKey, mandatory, immediate); + // Frame itself uses 8 bytes. + u_int32_t frameMax = channel.connection->getMaxFrameSize() - 8; + if (transfer->size() <= frameMax) { + channel.sendAndReceive(transfer); + } + else { + std::string ref = newTag(); + std::string data = transfer->getBody().getValue(); + size_t chunk = + channel.connection->getMaxFrameSize() - + (overhead() + ref.size()); + // TODO aconway 2007-04-05: cast around lack of generated setters + const_cast(transfer->getBody()) = Content(REFERENCE,ref); + channel.send( + make_shared_ptr(new MessageOpenBody(channel.version, ref))); + channel.send(transfer); + const char* p = data.data(); + const char* end = data.data()+data.size(); + while (p+chunk <= end) { + channel.send( + make_shared_ptr( + new MessageAppendBody(channel.version, ref, std::string(p, chunk)))); + p += chunk; + } + if (p < end) { + channel.send( + make_shared_ptr( + new MessageAppendBody(channel.version, ref, std::string(p, end-p)))); + } + channel.send(make_shared_ptr(new MessageCloseBody(channel.version, ref))); + } +} + +void copy(Message& msg, MessageTransferBody& transfer) { + // FIXME aconway 2007-04-05: Verify all required fields + // are copied. + msg.setContentType(transfer.getContentType()); + msg.setContentEncoding(transfer.getContentEncoding()); + msg.setHeaders(transfer.getApplicationHeaders()); + msg.setDeliveryMode(DeliveryMode(transfer.getDeliveryMode())); + msg.setPriority(transfer.getPriority()); + msg.setCorrelationId(transfer.getCorrelationId()); + msg.setReplyTo(transfer.getReplyTo()); + // FIXME aconway 2007-04-05: TTL/Expiration + msg.setMessageId(transfer.getMessageId()); + msg.setTimestamp(transfer.getTimestamp()); + msg.setUserId(transfer.getUserId()); + msg.setAppId(transfer.getAppId()); + msg.setDestination(transfer.getDestination()); + msg.setRedelivered(transfer.getRedelivered()); + msg.setDeliveryTag(0); // No meaning in 0-9 + if (transfer.getBody().isInline()) + msg.setData(transfer.getBody().getValue()); +} + +void MessageMessageChannel::handle(boost::shared_ptr method) { + assert(method->amqpClassId() ==MessageTransferBody::CLASS_ID); + switch(method->amqpMethodId()) { + case MessageAppendBody::METHOD_ID: { + MessageAppendBody::shared_ptr append = + shared_polymorphic_downcast(method); + incoming.appendReference(append->getReference(), append->getBytes()); + break; + } + case MessageOpenBody::METHOD_ID: { + MessageOpenBody::shared_ptr open = + shared_polymorphic_downcast(method); + incoming.openReference(open->getReference()); + break; + } + + case MessageCloseBody::METHOD_ID: { + MessageCloseBody::shared_ptr close = + shared_polymorphic_downcast(method); + incoming.closeReference(close->getReference()); + break; + } + + case MessageTransferBody::METHOD_ID: { + MessageTransferBody::shared_ptr transfer= + shared_polymorphic_downcast(method); + if (transfer->getBody().isInline()) { + Message msg; + copy(msg, *transfer); + // Deliver it. + incoming.getDestination(transfer->getDestination()).message(msg); + } + else { + Message& msg=incoming.createMessage( + transfer->getDestination(), transfer->getBody().getValue()); + copy(msg, *transfer); + // Will be delivered when reference closes. + } + break; + } + + case MessageEmptyBody::METHOD_ID: + case MessageOkBody::METHOD_ID: + // Nothing to do + break; + + // FIXME aconway 2007-04-03: TODO + case MessageCancelBody::METHOD_ID: + case MessageCheckpointBody::METHOD_ID: + case MessageOffsetBody::METHOD_ID: + case MessageQosBody::METHOD_ID: + case MessageRecoverBody::METHOD_ID: + case MessageRejectBody::METHOD_ID: + case MessageResumeBody::METHOD_ID: + break; + default: + throw Channel::UnknownMethod(); + } +} + +void MessageMessageChannel::handle(AMQHeaderBody::shared_ptr ){ + throw QPID_ERROR(INTERNAL_ERROR, "Basic protocol not supported"); +} + +void MessageMessageChannel::handle(AMQContentBody::shared_ptr ){ + throw QPID_ERROR(INTERNAL_ERROR, "Basic protocol not supported"); +} + +// FIXME aconway 2007-02-23: +// void MessageMessageChannel::deliver(IncomingMessage::Destination& consumer, Message& msg){ +// //record delivery tag: +// consumer.lastDeliveryTag = msg.getDeliveryTag(); + +// //allow registered listener to handle the message +// consumer.listener->received(msg); + +// if(channel.isOpen()){ +// bool multiple(false); +// switch(consumer.ackMode){ +// case LAZY_ACK: +// multiple = true; +// if(++(consumer.count) < channel.getPrefetch()) +// break; +// //else drop-through +// case AUTO_ACK: +// consumer.lastDeliveryTag = 0; +// channel.send( +// new MessageAckBody( +// channel.version, msg.getDeliveryTag(), multiple)); +// case NO_ACK: // Nothing to do +// case CLIENT_ACK: // User code must ack. +// break; +// // TODO aconway 2007-02-22: Provide a way for user +// // to ack! +// } +// } + +// //as it stands, transactionality is entirely orthogonal to ack +// //mode, though the acks will not be processed by the broker under +// //a transaction until it commits. +// } + + +void MessageMessageChannel::run() { + // FIXME aconway 2007-02-23: +// while(channel.isOpen()) { +// try { +// Message msg = incoming.waitDispatch(); +// if(msg.getMethod()->isA()) { +// ReturnedMessageHandler* handler=0; +// { +// Mutex::ScopedLock l(lock); +// handler=returnsHandler; +// } +// if(handler == 0) { +// // TODO aconway 2007-02-20: proper logging. +// cout << "Message returned: " << msg.getData() << endl; +// } +// else +// handler->returned(msg); +// } +// else { +// MessageDeliverBody::shared_ptr deliverBody = +// boost::shared_polymorphic_downcast( +// msg.getMethod()); +// std::string tag = deliverBody->getConsumerTag(); +// Consumer consumer; +// { +// Mutex::ScopedLock l(lock); +// ConsumerMap::iterator i = consumers.find(tag); +// if(i == consumers.end()) +// THROW_QPID_ERROR(PROTOCOL_ERROR+504, +// "Unknown consumer tag=" + tag); +// consumer = i->second; +// } +// deliver(consumer, msg); +// } +// } +// catch (const ShutdownException&) { +// /* Orderly shutdown */ +// } +// catch (const Exception& e) { +// // FIXME aconway 2007-02-20: Report exception to user. +// cout << "client::Message::run() terminated by: " << e.toString() +// << "(" << typeid(e).name() << ")" << endl; +// } +// } +} + +void MessageMessageChannel::setReturnedMessageHandler( + ReturnedMessageHandler* ) +{ + throw QPID_ERROR(INTERNAL_ERROR, "Message class does not support returns"); +} + +void MessageMessageChannel::setQos(){ + channel.sendAndReceive( + make_shared_ptr(new MessageQosBody(channel.version, 0, channel.getPrefetch(), false))); + if(channel.isTransactional()) + channel.sendAndReceive( + make_shared_ptr(new TxSelectBody(channel.version))); +} + +}} // namespace qpid::client diff --git a/cpp/src/qpid/client/MessageMessageChannel.h b/cpp/src/qpid/client/MessageMessageChannel.h new file mode 100644 index 0000000000..12c4786b81 --- /dev/null +++ b/cpp/src/qpid/client/MessageMessageChannel.h @@ -0,0 +1,82 @@ +#ifndef _client_MessageMessageChannel_h +#define _client_MessageMessageChannel_h + +/* + * + * 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 "MessageChannel.h" +#include "IncomingMessage.h" +#include "qpid/sys/Monitor.h" +#include + +namespace qpid { +namespace client { +/** + * Messaging implementation using AMQP 0-9 MessageMessageChannel class + * to send and receiving messages. + */ +class MessageMessageChannel : public MessageChannel +{ + public: + MessageMessageChannel(Channel& parent); + + void consume( + Queue& queue, std::string& tag, MessageListener* listener, + AckMode ackMode = NO_ACK, bool noLocal = false, bool synch = true, + const framing::FieldTable* fields = 0); + + void cancel(const std::string& tag, bool synch = true); + + bool get(Message& msg, const Queue& queue, AckMode ackMode = NO_ACK); + + void publish(const Message& msg, const Exchange& exchange, + const std::string& routingKey, + bool mandatory = false, bool immediate = false); + + void setReturnedMessageHandler(ReturnedMessageHandler* handler); + + void run(); + + void handle(boost::shared_ptr); + + void handle(shared_ptr); + + void handle(shared_ptr); + + void setQos(); + + void close(); + + private: + typedef boost::ptr_map + Destinations; + + std::string newTag(); + + sys::Mutex lock; + Channel& channel; + IncomingMessage incoming; + long tagCount; +}; + +}} // namespace qpid::client + + + +#endif /*!_client_MessageMessageChannel_h*/ + diff --git a/cpp/src/qpid/client/MethodBodyInstances.h b/cpp/src/qpid/client/MethodBodyInstances.h new file mode 100644 index 0000000000..516ba6e4e3 --- /dev/null +++ b/cpp/src/qpid/client/MethodBodyInstances.h @@ -0,0 +1,100 @@ +/* + * + * 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/amqp_framing.h" + +#ifndef _MethodBodyInstances_h_ +#define _MethodBodyInstances_h_ + +namespace qpid { +namespace client { + +/** + * A list of method body instances that can be used to compare against + * incoming bodies. + */ +class MethodBodyInstances +{ +private: + qpid::framing::ProtocolVersion version; +public: + const qpid::framing::BasicCancelOkBody basic_cancel_ok; + const qpid::framing::BasicConsumeOkBody basic_consume_ok; + const qpid::framing::BasicDeliverBody basic_deliver; + const qpid::framing::BasicGetEmptyBody basic_get_empty; + const qpid::framing::BasicGetOkBody basic_get_ok; + const qpid::framing::BasicQosOkBody basic_qos_ok; + const qpid::framing::BasicReturnBody basic_return; + const qpid::framing::ChannelCloseBody channel_close; + const qpid::framing::ChannelCloseOkBody channel_close_ok; + const qpid::framing::ChannelFlowBody channel_flow; + const qpid::framing::ChannelOpenOkBody channel_open_ok; + const qpid::framing::ConnectionCloseBody connection_close; + const qpid::framing::ConnectionCloseOkBody connection_close_ok; + const qpid::framing::ConnectionOpenOkBody connection_open_ok; + const qpid::framing::ConnectionRedirectBody connection_redirect; + const qpid::framing::ConnectionStartBody connection_start; + const qpid::framing::ConnectionTuneBody connection_tune; + const qpid::framing::ExchangeDeclareOkBody exchange_declare_ok; + const qpid::framing::ExchangeDeleteOkBody exchange_delete_ok; + const qpid::framing::QueueDeclareOkBody queue_declare_ok; + const qpid::framing::QueueDeleteOkBody queue_delete_ok; + const qpid::framing::QueueBindOkBody queue_bind_ok; + const qpid::framing::TxCommitOkBody tx_commit_ok; + const qpid::framing::TxRollbackOkBody tx_rollback_ok; + const qpid::framing::TxSelectOkBody tx_select_ok; + + MethodBodyInstances(uint8_t major, uint8_t minor) : + version(major, minor), + basic_cancel_ok(version), + basic_consume_ok(version), + basic_deliver(version), + basic_get_empty(version), + basic_get_ok(version), + basic_qos_ok(version), + basic_return(version), + channel_close(version), + channel_close_ok(version), + channel_flow(version), + channel_open_ok(version), + connection_close(version), + connection_close_ok(version), + connection_open_ok(version), + connection_redirect(version), + connection_start(version), + connection_tune(version), + exchange_declare_ok(version), + exchange_delete_ok(version), + queue_declare_ok(version), + queue_delete_ok(version), + queue_bind_ok(version), + tx_commit_ok(version), + tx_rollback_ok(version), + tx_select_ok(version) + {} + +}; + +static MethodBodyInstances method_bodies(8, 0); + +} // namespace client +} // namespace qpid + +#endif diff --git a/cpp/src/qpid/client/ResponseHandler.cpp b/cpp/src/qpid/client/ResponseHandler.cpp new file mode 100644 index 0000000000..10ad5c6fa2 --- /dev/null +++ b/cpp/src/qpid/client/ResponseHandler.cpp @@ -0,0 +1,79 @@ +/* + * + * 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/QpidError.h" +#include +#include "ResponseHandler.h" +#include "qpid/framing/AMQMethodBody.h" + +using namespace qpid::sys; +using namespace qpid::framing; + +namespace qpid { +namespace client { + +ResponseHandler::ResponseHandler() : waiting(false), shutdownFlag(false) {} + +ResponseHandler::~ResponseHandler(){} + +bool ResponseHandler::isWaiting() { + Monitor::ScopedLock l(monitor); + return waiting; +} + +void ResponseHandler::expect(){ + Monitor::ScopedLock l(monitor); + waiting = true; +} + +void ResponseHandler::signalResponse(MethodPtr _response) +{ + Monitor::ScopedLock l(monitor); + response = _response; + if (!response) + shutdownFlag=true; + waiting = false; + monitor.notify(); +} + +ResponseHandler::MethodPtr ResponseHandler::receive() { + Monitor::ScopedLock l(monitor); + while (!response && !shutdownFlag) + monitor.wait(); + if (shutdownFlag) + THROW_QPID_ERROR( + PROTOCOL_ERROR, "Channel closed unexpectedly."); + MethodPtr result = response; + response.reset(); + return result; +} + +ResponseHandler::MethodPtr ResponseHandler::receive(ClassId c, MethodId m) { + MethodPtr response = receive(); + if(c != response->amqpClassId() || m != response->amqpMethodId()) { + THROW_QPID_ERROR( + PROTOCOL_ERROR, + boost::format("Expected class:method %d:%d, got %d:%d") + % c % m % response->amqpClassId() % response->amqpMethodId()); + } + return response; +} + +}} // namespace qpid::client diff --git a/cpp/src/qpid/client/ResponseHandler.h b/cpp/src/qpid/client/ResponseHandler.h new file mode 100644 index 0000000000..e599e4c8d1 --- /dev/null +++ b/cpp/src/qpid/client/ResponseHandler.h @@ -0,0 +1,75 @@ +/* + * + * 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/shared_ptr.h" +#include "qpid/sys/Monitor.h" + +#ifndef _ResponseHandler_ +#define _ResponseHandler_ + +namespace qpid { + +namespace framing { +class AMQMethodBody; +} + +namespace client { + +/** + * Holds a response from the broker peer for the client. + */ +class ResponseHandler{ + typedef shared_ptr MethodPtr; + bool waiting; + bool shutdownFlag; + MethodPtr response; + sys::Monitor monitor; + + public: + ResponseHandler(); + ~ResponseHandler(); + + /** Is a response expected? */ + bool isWaiting(); + + /** Provide a response to the waiting thread */ + void signalResponse(MethodPtr response); + + /** Indicate a message is expected. */ + void expect(); + + /** Wait for a response. */ + MethodPtr receive(); + + /** Wait for a specific response. */ + MethodPtr receive(framing::ClassId, framing::MethodId); + + /** Template version of receive returns typed pointer. */ + template + shared_ptr receive() { + return shared_polymorphic_downcast( + receive(BodyType::CLASS_ID, BodyType::METHOD_ID)); + } +}; + +}} + + +#endif diff --git a/cpp/src/qpid/client/ReturnedMessageHandler.cpp b/cpp/src/qpid/client/ReturnedMessageHandler.cpp new file mode 100644 index 0000000000..35d0b5c0a8 --- /dev/null +++ b/cpp/src/qpid/client/ReturnedMessageHandler.cpp @@ -0,0 +1,24 @@ +/* + * + * 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 "ReturnedMessageHandler.h" + +qpid::client::ReturnedMessageHandler::~ReturnedMessageHandler() {} diff --git a/cpp/src/qpid/client/ReturnedMessageHandler.h b/cpp/src/qpid/client/ReturnedMessageHandler.h new file mode 100644 index 0000000000..8b42fc0764 --- /dev/null +++ b/cpp/src/qpid/client/ReturnedMessageHandler.h @@ -0,0 +1,49 @@ +/* + * + * 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 + +#ifndef _ReturnedMessageHandler_ +#define _ReturnedMessageHandler_ + +#include "ClientMessage.h" + +namespace qpid { +namespace client { + + /** + * An interface through which returned messages can be received by + * an application. + * + * @see Channel::setReturnedMessageHandler() + * + * \ingroup clientapi + */ + class ReturnedMessageHandler{ + public: + virtual ~ReturnedMessageHandler(); + virtual void returned(Message& msg) = 0; + }; + +} +} + + +#endif diff --git a/cpp/src/qpid/doxygen_mainpage.h b/cpp/src/qpid/doxygen_mainpage.h new file mode 100644 index 0000000000..b354238cd0 --- /dev/null +++ b/cpp/src/qpid/doxygen_mainpage.h @@ -0,0 +1,45 @@ +// This header file is just for doxygen documentation purposes. + +/*!\mainpage Qpid C++ Developer Kit. + * + *\section intro_sec Introduction + * + * The Qpid project provides implementations of the AMQP messaging specification in several programming language. + * + * Qpidc provides APIs and libraries to implement AMQP + * clients in C++. Qpidc clients can interact with any compliant AMQP + * message broker. The Qpid project also provides an AMQP broker + * daemon called qpidd that you can use with your qpidc clients. + * + *\section install_sec Installation + * + * If you are installing from the source distribution +
+   > ./configure && make
+   > make install  
+ * This will build and install the client development kit and the broker + * in standard places. Use + * ./configure --help for more options. + * + * You can also install from RPMs with the rpm -i command. + * You will need + * - qpidc for core libraries. + * - qpidc-devel for header files and developer documentation. + * - qpidd for the broker daemon. + * + *\section getstart_sec Getting Started + * + * If you have installed in the standard places you should use + * these compile flags: + * + * -I/usr/include/qpidc -I/usr/include/qpidc/framing -I/usr/include/qpidc/sys + * + * and these link flags: + * + * -lqpidcommon -lqpidclient + * + * If you have installed somewhere else you should modify the flags + * appropriately. + * + * See the \ref clientapi "client API module" for more on the client API. + */ diff --git a/cpp/src/qpid/framing/AMQBody.cpp b/cpp/src/qpid/framing/AMQBody.cpp new file mode 100644 index 0000000000..a64d224a86 --- /dev/null +++ b/cpp/src/qpid/framing/AMQBody.cpp @@ -0,0 +1,33 @@ +/* + * + * 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 "AMQBody.h" +#include + +std::ostream& qpid::framing::operator<<(std::ostream& out, const qpid::framing::AMQBody& body) +{ + body.print(out); + return out; +} + +qpid::framing::AMQBody::~AMQBody() {} + + diff --git a/cpp/src/qpid/framing/AMQBody.h b/cpp/src/qpid/framing/AMQBody.h new file mode 100644 index 0000000000..eaa568c06a --- /dev/null +++ b/cpp/src/qpid/framing/AMQBody.h @@ -0,0 +1,59 @@ +/* + * + * 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 +#include "amqp_types.h" +#include "Buffer.h" + +#ifndef _AMQBody_ +#define _AMQBody_ + +namespace qpid { + namespace framing { + + class AMQBody + { + public: + typedef boost::shared_ptr shared_ptr; + + virtual ~AMQBody(); + virtual uint32_t size() const = 0; + virtual uint8_t type() const = 0; + virtual void encode(Buffer& buffer) const = 0; + virtual void decode(Buffer& buffer, uint32_t size) = 0; + + virtual void print(std::ostream& out) const = 0; + }; + + std::ostream& operator<<(std::ostream& out, const AMQBody& body) ; + + enum BodyTypes { + METHOD_BODY = 1, + HEADER_BODY = 2, + CONTENT_BODY = 3, + HEARTBEAT_BODY = 8, + REQUEST_BODY = 9, + RESPONSE_BODY = 10 + }; + } +} + + +#endif diff --git a/cpp/src/qpid/framing/AMQContentBody.cpp b/cpp/src/qpid/framing/AMQContentBody.cpp new file mode 100644 index 0000000000..19d2e5714a --- /dev/null +++ b/cpp/src/qpid/framing/AMQContentBody.cpp @@ -0,0 +1,43 @@ +/* + * + * 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 "AMQContentBody.h" +#include + +qpid::framing::AMQContentBody::AMQContentBody(){ +} + +qpid::framing::AMQContentBody::AMQContentBody(const string& _data) : data(_data){ +} + +uint32_t qpid::framing::AMQContentBody::size() const{ + return data.size(); +} +void qpid::framing::AMQContentBody::encode(Buffer& buffer) const{ + buffer.putRawData(data); +} +void qpid::framing::AMQContentBody::decode(Buffer& buffer, uint32_t _size){ + buffer.getRawData(data, _size); +} + +void qpid::framing::AMQContentBody::print(std::ostream& out) const +{ + out << "content (" << size() << " bytes)"; +} diff --git a/cpp/src/qpid/framing/AMQContentBody.h b/cpp/src/qpid/framing/AMQContentBody.h new file mode 100644 index 0000000000..6d4c1ef4b6 --- /dev/null +++ b/cpp/src/qpid/framing/AMQContentBody.h @@ -0,0 +1,53 @@ +/* + * + * 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 "amqp_types.h" +#include "AMQBody.h" +#include "Buffer.h" + +#ifndef _AMQContentBody_ +#define _AMQContentBody_ + +namespace qpid { +namespace framing { + +class AMQContentBody : public AMQBody +{ + string data; + +public: + typedef boost::shared_ptr shared_ptr; + + AMQContentBody(); + AMQContentBody(const string& data); + inline virtual ~AMQContentBody(){} + inline uint8_t type() const { return CONTENT_BODY; }; + inline string& getData(){ return data; } + uint32_t size() const; + void encode(Buffer& buffer) const; + void decode(Buffer& buffer, uint32_t size); + void print(std::ostream& out) const; +}; + +} +} + + +#endif diff --git a/cpp/src/qpid/framing/AMQDataBlock.h b/cpp/src/qpid/framing/AMQDataBlock.h new file mode 100644 index 0000000000..6ff61b185e --- /dev/null +++ b/cpp/src/qpid/framing/AMQDataBlock.h @@ -0,0 +1,42 @@ +/* + * + * 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 "Buffer.h" + +#ifndef _AMQDataBlock_ +#define _AMQDataBlock_ + +namespace qpid { +namespace framing { + +class AMQDataBlock +{ +public: + virtual ~AMQDataBlock() {} + virtual void encode(Buffer& buffer) = 0; + virtual bool decode(Buffer& buffer) = 0; + virtual uint32_t size() const = 0; +}; + +} +} + + +#endif diff --git a/cpp/src/qpid/framing/AMQFrame.cpp b/cpp/src/qpid/framing/AMQFrame.cpp new file mode 100644 index 0000000000..6541a0e788 --- /dev/null +++ b/cpp/src/qpid/framing/AMQFrame.cpp @@ -0,0 +1,139 @@ + +/* + * + * 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 + +#include "AMQFrame.h" +#include "qpid/QpidError.h" +#include "AMQRequestBody.h" +#include "AMQResponseBody.h" + + +namespace qpid { +namespace framing { + + +AMQP_MethodVersionMap AMQFrame::versionMap; + +AMQFrame::AMQFrame(ProtocolVersion _version): +version(_version) + { + assert(version != ProtocolVersion(0,0)); + } + +AMQFrame::AMQFrame(ProtocolVersion _version, uint16_t _channel, AMQBody* _body) : +version(_version), channel(_channel), body(_body) +{} + +AMQFrame::AMQFrame(ProtocolVersion _version, uint16_t _channel, const AMQBody::shared_ptr& _body) : +version(_version), channel(_channel), body(_body) +{} + +AMQFrame::~AMQFrame() {} + +uint16_t AMQFrame::getChannel(){ + return channel; +} + +AMQBody::shared_ptr AMQFrame::getBody(){ + return body; +} + +void AMQFrame::encode(Buffer& buffer) +{ + buffer.putOctet(body->type()); + buffer.putShort(channel); + buffer.putLong(body->size()); + body->encode(buffer); + buffer.putOctet(0xCE); +} + +uint32_t AMQFrame::size() const{ + assert(body.get()); + return 1/*type*/ + 2/*channel*/ + 4/*body size*/ + body->size() + + 1/*0xCE*/; +} + +bool AMQFrame::decode(Buffer& buffer) +{ + if(buffer.available() < 7) + return false; + buffer.record(); + uint32_t frameSize = decodeHead(buffer); + if(buffer.available() < frameSize + 1){ + buffer.restore(); + return false; + } + decodeBody(buffer, frameSize); + uint8_t end = buffer.getOctet(); + if(end != 0xCE) THROW_QPID_ERROR(FRAMING_ERROR, "Frame end not found"); + return true; +} + +uint32_t AMQFrame::decodeHead(Buffer& buffer){ + type = buffer.getOctet(); + channel = buffer.getShort(); + return buffer.getLong(); +} + +void AMQFrame::decodeBody(Buffer& buffer, uint32_t size) +{ + switch(type) + { + case METHOD_BODY: + body = AMQMethodBody::create(versionMap, version, buffer); + break; + case REQUEST_BODY: + body = AMQRequestBody::create(versionMap, version, buffer); + break; + case RESPONSE_BODY: + body = AMQResponseBody::create(versionMap, version, buffer); + break; + case HEADER_BODY: + body = AMQBody::shared_ptr(new AMQHeaderBody()); + break; + case CONTENT_BODY: + body = AMQBody::shared_ptr(new AMQContentBody()); + break; + case HEARTBEAT_BODY: + body = AMQBody::shared_ptr(new AMQHeartbeatBody()); + break; + default: + THROW_QPID_ERROR( + FRAMING_ERROR, + boost::format("Unknown frame type %d") % type); + } + body->decode(buffer, size); +} + +std::ostream& operator<<(std::ostream& out, const AMQFrame& t) +{ + out << "Frame[channel=" << t.channel << "; "; + if (t.body.get() == 0) + out << "empty"; + else + out << *t.body; + out << "]"; + return out; +} + + +}} // namespace qpid::framing diff --git a/cpp/src/qpid/framing/AMQFrame.h b/cpp/src/qpid/framing/AMQFrame.h new file mode 100644 index 0000000000..bef1b01df4 --- /dev/null +++ b/cpp/src/qpid/framing/AMQFrame.h @@ -0,0 +1,78 @@ +#ifndef _AMQFrame_ +#define _AMQFrame_ + +/* + * + * 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 + +#include "amqp_types.h" +#include "AMQBody.h" +#include "AMQDataBlock.h" +#include "AMQMethodBody.h" +#include "AMQHeaderBody.h" +#include "AMQContentBody.h" +#include "AMQHeartbeatBody.h" +#include "qpid/framing/AMQP_MethodVersionMap.h" +#include "qpid/framing/AMQP_HighestVersion.h" +#include "Buffer.h" + +namespace qpid { +namespace framing { + + +class AMQFrame : public AMQDataBlock +{ + public: + AMQFrame(ProtocolVersion _version = highestProtocolVersion); + AMQFrame(ProtocolVersion _version, uint16_t channel, AMQBody* body); + AMQFrame(ProtocolVersion _version, uint16_t channel, const AMQBody::shared_ptr& body); + virtual ~AMQFrame(); + virtual void encode(Buffer& buffer); + virtual bool decode(Buffer& buffer); + virtual uint32_t size() const; + uint16_t getChannel(); + AMQBody::shared_ptr getBody(); + + /** Convenience template to cast the body to an expected type */ + template boost::shared_ptr castBody() { + assert(dynamic_cast(getBody().get())); + boost::static_pointer_cast(getBody()); + } + + uint32_t decodeHead(Buffer& buffer); + void decodeBody(Buffer& buffer, uint32_t size); + + private: + static AMQP_MethodVersionMap versionMap; + ProtocolVersion version; + + uint16_t channel; + uint8_t type; + AMQBody::shared_ptr body; + + + friend std::ostream& operator<<(std::ostream& out, const AMQFrame& body); +}; + +}} // namespace qpid::framing + + +#endif diff --git a/cpp/src/qpid/framing/AMQHeaderBody.cpp b/cpp/src/qpid/framing/AMQHeaderBody.cpp new file mode 100644 index 0000000000..4d3611eb40 --- /dev/null +++ b/cpp/src/qpid/framing/AMQHeaderBody.cpp @@ -0,0 +1,75 @@ +/* + * + * 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 "AMQHeaderBody.h" +#include "qpid/QpidError.h" +#include "BasicHeaderProperties.h" + +qpid::framing::AMQHeaderBody::AMQHeaderBody(int classId) : weight(0), contentSize(0){ + createProperties(classId); +} + +qpid::framing::AMQHeaderBody::AMQHeaderBody() : properties(0), weight(0), contentSize(0){ +} + +qpid::framing::AMQHeaderBody::~AMQHeaderBody(){ + delete properties; +} + +uint32_t qpid::framing::AMQHeaderBody::size() const{ + return 12 + properties->size(); +} + +void qpid::framing::AMQHeaderBody::encode(Buffer& buffer) const { + buffer.putShort(properties->classId()); + buffer.putShort(weight); + buffer.putLongLong(contentSize); + properties->encode(buffer); +} + +void qpid::framing::AMQHeaderBody::decode(Buffer& buffer, uint32_t bufSize){ + uint16_t classId = buffer.getShort(); + weight = buffer.getShort(); + contentSize = buffer.getLongLong(); + createProperties(classId); + properties->decode(buffer, bufSize - 12); +} + +void qpid::framing::AMQHeaderBody::createProperties(int classId){ + switch(classId){ + case BASIC: + properties = new qpid::framing::BasicHeaderProperties(); + break; + default: + THROW_QPID_ERROR(FRAMING_ERROR, "Unknown header class"); + } +} + +void qpid::framing::AMQHeaderBody::print(std::ostream& out) const +{ + out << "header (" << size() << " bytes)" << " content_size=" << getContentSize(); + const BasicHeaderProperties* props = + dynamic_cast(getProperties()); + if (props) { + out << ", message_id=" << props->getMessageId(); + out << ", delivery_mode=" << (int) props->getDeliveryMode(); + out << ", headers=" << const_cast(props)->getHeaders(); + } +} diff --git a/cpp/src/qpid/framing/AMQHeaderBody.h b/cpp/src/qpid/framing/AMQHeaderBody.h new file mode 100644 index 0000000000..691ceeff73 --- /dev/null +++ b/cpp/src/qpid/framing/AMQHeaderBody.h @@ -0,0 +1,60 @@ +/* + * + * 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 "amqp_types.h" +#include "AMQBody.h" +#include "Buffer.h" +#include "HeaderProperties.h" + +#ifndef _AMQHeaderBody_ +#define _AMQHeaderBody_ + +namespace qpid { +namespace framing { + +class AMQHeaderBody : public AMQBody +{ + HeaderProperties* properties; + uint16_t weight; + uint64_t contentSize; + + void createProperties(int classId); +public: + typedef boost::shared_ptr shared_ptr; + + AMQHeaderBody(int classId); + AMQHeaderBody(); + inline uint8_t type() const { return HEADER_BODY; } + HeaderProperties* getProperties(){ return properties; } + const HeaderProperties* getProperties() const { return properties; } + inline uint64_t getContentSize() const { return contentSize; } + inline void setContentSize(uint64_t _size) { contentSize = _size; } + virtual ~AMQHeaderBody(); + virtual uint32_t size() const; + virtual void encode(Buffer& buffer) const; + virtual void decode(Buffer& buffer, uint32_t size); + virtual void print(std::ostream& out) const; +}; + +} +} + + +#endif diff --git a/cpp/src/qpid/framing/AMQHeartbeatBody.cpp b/cpp/src/qpid/framing/AMQHeartbeatBody.cpp new file mode 100644 index 0000000000..140ce2e794 --- /dev/null +++ b/cpp/src/qpid/framing/AMQHeartbeatBody.cpp @@ -0,0 +1,29 @@ +/* + * + * 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 "AMQHeartbeatBody.h" +#include + +qpid::framing::AMQHeartbeatBody::~AMQHeartbeatBody() {} + +void qpid::framing::AMQHeartbeatBody::print(std::ostream& out) const { + out << "heartbeat"; +} diff --git a/cpp/src/qpid/framing/AMQHeartbeatBody.h b/cpp/src/qpid/framing/AMQHeartbeatBody.h new file mode 100644 index 0000000000..4c046b81a7 --- /dev/null +++ b/cpp/src/qpid/framing/AMQHeartbeatBody.h @@ -0,0 +1,47 @@ +/* + * + * 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 "amqp_types.h" +#include "AMQBody.h" +#include "Buffer.h" + +#ifndef _AMQHeartbeatBody_ +#define _AMQHeartbeatBody_ + +namespace qpid { +namespace framing { + +class AMQHeartbeatBody : public AMQBody +{ +public: + typedef boost::shared_ptr shared_ptr; + + virtual ~AMQHeartbeatBody(); + inline uint32_t size() const { return 0; } + inline uint8_t type() const { return HEARTBEAT_BODY; } + inline void encode(Buffer& ) const {} + inline void decode(Buffer& , uint32_t /*size*/) {} + virtual void print(std::ostream& out) const; +}; + +} +} + +#endif diff --git a/cpp/src/qpid/framing/AMQMethodBody.cpp b/cpp/src/qpid/framing/AMQMethodBody.cpp new file mode 100644 index 0000000000..a7df0578c2 --- /dev/null +++ b/cpp/src/qpid/framing/AMQMethodBody.cpp @@ -0,0 +1,59 @@ +/* + * + * 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 "AMQFrame.h" +#include "AMQMethodBody.h" +#include "qpid/QpidError.h" +#include "qpid/framing/AMQP_MethodVersionMap.h" + +namespace qpid { +namespace framing { + +void AMQMethodBody::encodeId(Buffer& buffer) const{ + buffer.putShort(amqpClassId()); + buffer.putShort(amqpMethodId()); +} + +void AMQMethodBody::invoke(AMQP_ServerOperations&, const MethodContext&){ + assert(0); + THROW_QPID_ERROR(PROTOCOL_ERROR, "Method not supported by AMQP Server."); +} + +AMQMethodBody::shared_ptr AMQMethodBody::create( + AMQP_MethodVersionMap& versionMap, ProtocolVersion version, + Buffer& buffer) +{ + ClassMethodId id; + id.decode(buffer); + return AMQMethodBody::shared_ptr( + versionMap.createMethodBody( + id.classId, id.methodId, version.getMajor(), version.getMinor())); +} + +void AMQMethodBody::ClassMethodId::decode(Buffer& buffer) { + classId = buffer.getShort(); + methodId = buffer.getShort(); +} + +void AMQMethodBody::decode(Buffer& buffer, uint32_t /*size*/) { + decodeContent(buffer); +} + +}} // namespace qpid::framing diff --git a/cpp/src/qpid/framing/AMQMethodBody.h b/cpp/src/qpid/framing/AMQMethodBody.h new file mode 100644 index 0000000000..306c26cd27 --- /dev/null +++ b/cpp/src/qpid/framing/AMQMethodBody.h @@ -0,0 +1,84 @@ +#ifndef _AMQMethodBody_ +#define _AMQMethodBody_ + +/* + * + * 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 +#include "amqp_types.h" +#include "AMQBody.h" +#include "Buffer.h" +#include "qpid/framing/AMQP_ServerOperations.h" +#include "MethodContext.h" + +namespace qpid { +namespace framing { + +class AMQP_MethodVersionMap; + +class AMQMethodBody : public AMQBody +{ + public: + typedef boost::shared_ptr shared_ptr; + + static shared_ptr create( + AMQP_MethodVersionMap& map, ProtocolVersion version, Buffer& buf); + + ProtocolVersion version; + uint8_t type() const { return METHOD_BODY; } + AMQMethodBody(uint8_t major, uint8_t minor) : version(major, minor) {} + AMQMethodBody(ProtocolVersion ver) : version(ver) {} + virtual ~AMQMethodBody() {} + void decode(Buffer&, uint32_t); + + virtual MethodId amqpMethodId() const = 0; + virtual ClassId amqpClassId() const = 0; + + virtual void invoke(AMQP_ServerOperations&, const MethodContext&); + + template bool isA() { + return amqpClassId()==T::CLASS_ID && amqpMethodId()==T::METHOD_ID; + } + + /** Return request ID or response correlationID */ + virtual RequestId getRequestId() const { return 0; } + + virtual bool isRequest() const { return false; } + virtual bool isResponse() const { return false; } + + protected: + static uint32_t baseSize() { return 4; } + + struct ClassMethodId { + uint16_t classId; + uint16_t methodId; + void decode(Buffer& b); + }; + + void encodeId(Buffer& buffer) const; + virtual void encodeContent(Buffer& buffer) const = 0; + virtual void decodeContent(Buffer& buffer) = 0; +}; + + +}} // namespace qpid::framing + + +#endif diff --git a/cpp/src/qpid/framing/AMQRequestBody.cpp b/cpp/src/qpid/framing/AMQRequestBody.cpp new file mode 100644 index 0000000000..9e7b307a47 --- /dev/null +++ b/cpp/src/qpid/framing/AMQRequestBody.cpp @@ -0,0 +1,66 @@ +/* + * + * 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 "AMQRequestBody.h" +#include "qpid/framing/AMQP_MethodVersionMap.h" + +namespace qpid { +namespace framing { + +void AMQRequestBody::Data::encode(Buffer& buffer) const { + buffer.putLongLong(requestId); + buffer.putLongLong(responseMark); + buffer.putLong(0); // Reserved long in spec. +} + +void AMQRequestBody::Data::decode(Buffer& buffer) { + requestId = buffer.getLongLong(); + responseMark = buffer.getLongLong(); + buffer.getLong(); // Ignore reserved long. +} + +void AMQRequestBody::encode(Buffer& buffer) const { + data.encode(buffer); + encodeId(buffer); + encodeContent(buffer); +} + +AMQRequestBody::shared_ptr +AMQRequestBody::create( + AMQP_MethodVersionMap& versionMap, ProtocolVersion version, + Buffer& buffer) +{ + ClassMethodId id; + Data data; + data.decode(buffer); + id.decode(buffer); + AMQRequestBody* body = dynamic_cast( + versionMap.createMethodBody( + id.classId, id.methodId, version.getMajor(), version.getMinor())); + assert(body); + body->data = data; + return AMQRequestBody::shared_ptr(body); +} + +void AMQRequestBody::printPrefix(std::ostream& out) const { + out << "request(id=" << data.requestId << ",mark=" + << data.responseMark << "): "; +} + +}} // namespace qpid::framing + diff --git a/cpp/src/qpid/framing/AMQRequestBody.h b/cpp/src/qpid/framing/AMQRequestBody.h new file mode 100644 index 0000000000..691a4c8768 --- /dev/null +++ b/cpp/src/qpid/framing/AMQRequestBody.h @@ -0,0 +1,78 @@ +#ifndef _framing_AMQRequestBody_h +#define _framing_AMQRequestBody_h + +/* + * + * 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/framing/AMQMethodBody.h" + +namespace qpid { +namespace framing { + +/** + * Body of a request method frame. + */ +class AMQRequestBody : public AMQMethodBody +{ + public: + typedef boost::shared_ptr shared_ptr; + + struct Data { + Data(RequestId id=0, ResponseId mark=0) + : requestId(id), responseMark(mark) {} + void encode(Buffer&) const; + void decode(Buffer&); + + RequestId requestId; + ResponseId responseMark; + }; + + static Data& getData(const AMQBody::shared_ptr& body) { + return boost::dynamic_pointer_cast(body)->getData(); + } + + static shared_ptr create( + AMQP_MethodVersionMap& versionMap, ProtocolVersion version, + Buffer& buffer); + + AMQRequestBody(ProtocolVersion v, RequestId id=0, ResponseId mark=0) + : AMQMethodBody(v), data(id, mark) {} + + uint8_t type() const { return REQUEST_BODY; } + void encode(Buffer& buffer) const; + + Data& getData() { return data; } + RequestId getRequestId() const { return data.requestId; } + ResponseId getResponseMark() const { return data.responseMark; } + void setRequestId(RequestId id) { data.requestId=id; } + void setResponseMark(ResponseId mark) { data.responseMark=mark; } + + bool isRequest()const { return true; } + static const uint32_t baseSize() { return AMQMethodBody::baseSize()+20; } + protected: + void printPrefix(std::ostream& out) const; + + private: + Data data; +}; + +}} // namespace qpid::framing + + + +#endif /*!_framing_AMQRequestBody_h*/ diff --git a/cpp/src/qpid/framing/AMQResponseBody.cpp b/cpp/src/qpid/framing/AMQResponseBody.cpp new file mode 100644 index 0000000000..4354a9c564 --- /dev/null +++ b/cpp/src/qpid/framing/AMQResponseBody.cpp @@ -0,0 +1,65 @@ +/* + * + * 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 "AMQFrame.h" +#include "AMQResponseBody.h" +#include "qpid/framing/AMQP_MethodVersionMap.h" + +namespace qpid { +namespace framing { + +void AMQResponseBody::Data::encode(Buffer& buffer) const { + buffer.putLongLong(responseId); + buffer.putLongLong(requestId); + buffer.putLong(batchOffset); +} + +void AMQResponseBody::Data::decode(Buffer& buffer) { + responseId = buffer.getLongLong(); + requestId = buffer.getLongLong(); + batchOffset = buffer.getLong(); +} + +void AMQResponseBody::encode(Buffer& buffer) const { + data.encode(buffer); + encodeId(buffer); + encodeContent(buffer); +} + +AMQResponseBody::shared_ptr AMQResponseBody::create( + AMQP_MethodVersionMap& versionMap, ProtocolVersion version, + Buffer& buffer) +{ + ClassMethodId id; + Data data; + data.decode(buffer); + id.decode(buffer); + AMQResponseBody* body = dynamic_cast( + versionMap.createMethodBody( + id.classId, id.methodId, version.getMajor(), version.getMinor())); + assert(body); + body->data = data; + return AMQResponseBody::shared_ptr(body); +} + +void AMQResponseBody::printPrefix(std::ostream& out) const { + out << "response(id=" << data.responseId << ",request=" << data.requestId + << ",batch=" << data.batchOffset << "): "; +} + +}} // namespace qpid::framing diff --git a/cpp/src/qpid/framing/AMQResponseBody.h b/cpp/src/qpid/framing/AMQResponseBody.h new file mode 100644 index 0000000000..fa381baddd --- /dev/null +++ b/cpp/src/qpid/framing/AMQResponseBody.h @@ -0,0 +1,85 @@ +#ifndef _framing_AMQResponseBody_h +#define _framing_AMQResponseBody_h + +/* + * + * 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 "AMQMethodBody.h" + +namespace qpid { +namespace framing { + +class AMQP_MethodVersionMap; + +/** + * Body of a response method frame. + */ +class AMQResponseBody : public AMQMethodBody +{ + + public: + typedef boost::shared_ptr shared_ptr; + + struct Data { + Data(ResponseId id=0, RequestId req=0, BatchOffset off=0) + : responseId(id), requestId(req), batchOffset(off) {} + void encode(Buffer&) const; + void decode(Buffer&); + + uint64_t responseId; + uint64_t requestId; + uint32_t batchOffset; + }; + + static Data& getData(const AMQBody::shared_ptr& body) { + return boost::dynamic_pointer_cast(body)->getData(); + } + + static shared_ptr create( + AMQP_MethodVersionMap& versionMap, ProtocolVersion version, + Buffer& buffer); + + AMQResponseBody( + ProtocolVersion v, ResponseId id=0, RequestId req=0, BatchOffset off=0) + : AMQMethodBody(v), data(id, req, off) {} + + uint8_t type() const { return RESPONSE_BODY; } + void encode(Buffer& buffer) const; + + Data& getData() { return data; } + ResponseId getResponseId() const { return data.responseId; } + RequestId getRequestId() const { return data.requestId; } + BatchOffset getBatchOffset() const { return data.batchOffset; } + void setResponseId(ResponseId id) { data.responseId = id; } + void setRequestId(RequestId id) { data.requestId = id; } + void setBatchOffset(BatchOffset id) { data.batchOffset = id; } + + bool isResponse() const { return true; } + protected: + static const uint32_t baseSize() { return AMQMethodBody::baseSize()+20; } + void printPrefix(std::ostream& out) const; + + private: + Data data; +}; + +}} // namespace qpid::framing + + + +#endif /*!_framing_AMQResponseBody_h*/ diff --git a/cpp/src/qpid/framing/BasicHeaderProperties.cpp b/cpp/src/qpid/framing/BasicHeaderProperties.cpp new file mode 100644 index 0000000000..dfa5e1bc3f --- /dev/null +++ b/cpp/src/qpid/framing/BasicHeaderProperties.cpp @@ -0,0 +1,103 @@ +/* + * + * 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 "BasicHeaderProperties.h" + +//TODO: This could be easily generated from the spec + +qpid::framing::BasicHeaderProperties::BasicHeaderProperties() : deliveryMode(DeliveryMode(0)), priority(0), timestamp(0){} +qpid::framing::BasicHeaderProperties::~BasicHeaderProperties(){} + +uint32_t qpid::framing::BasicHeaderProperties::size() const{ + uint32_t bytes = 2;//flags + if(contentType.length() > 0) bytes += contentType.length() + 1; + if(contentEncoding.length() > 0) bytes += contentEncoding.length() + 1; + if(headers.count() > 0) bytes += headers.size(); + if(deliveryMode != 0) bytes += 1; + if(priority != 0) bytes += 1; + if(correlationId.length() > 0) bytes += correlationId.length() + 1; + if(replyTo.length() > 0) bytes += replyTo.length() + 1; + if(expiration.length() > 0) bytes += expiration.length() + 1; + if(messageId.length() > 0) bytes += messageId.length() + 1; + if(timestamp != 0) bytes += 8; + if(type.length() > 0) bytes += type.length() + 1; + if(userId.length() > 0) bytes += userId.length() + 1; + if(appId.length() > 0) bytes += appId.length() + 1; + if(clusterId.length() > 0) bytes += clusterId.length() + 1; + + return bytes; +} + +void qpid::framing::BasicHeaderProperties::encode(qpid::framing::Buffer& buffer) const{ + uint16_t flags = getFlags(); + buffer.putShort(flags); + + if(contentType.length() > 0) buffer.putShortString(contentType); + if(contentEncoding.length() > 0) buffer.putShortString(contentEncoding); + if(headers.count() > 0) buffer.putFieldTable(headers); + if(deliveryMode != 0) buffer.putOctet(deliveryMode); + if(priority != 0) buffer.putOctet(priority); + if(correlationId.length() > 0) buffer.putShortString(correlationId); + if(replyTo.length() > 0) buffer.putShortString(replyTo); + if(expiration.length() > 0) buffer.putShortString(expiration); + if(messageId.length() > 0) buffer.putShortString(messageId); + if(timestamp != 0) buffer.putLongLong(timestamp);; + if(type.length() > 0) buffer.putShortString(type); + if(userId.length() > 0) buffer.putShortString(userId); + if(appId.length() > 0) buffer.putShortString(appId); + if(clusterId.length() > 0) buffer.putShortString(clusterId); +} + +void qpid::framing::BasicHeaderProperties::decode(qpid::framing::Buffer& buffer, uint32_t /*size*/){ + uint16_t flags = buffer.getShort(); + if(flags & (1 << 15)) buffer.getShortString(contentType); + if(flags & (1 << 14)) buffer.getShortString(contentEncoding); + if(flags & (1 << 13)) buffer.getFieldTable(headers); + if(flags & (1 << 12)) deliveryMode = DeliveryMode(buffer.getOctet()); + if(flags & (1 << 11)) priority = buffer.getOctet(); + if(flags & (1 << 10)) buffer.getShortString(correlationId); + if(flags & (1 << 9)) buffer.getShortString(replyTo); + if(flags & (1 << 8)) buffer.getShortString(expiration); + if(flags & (1 << 7)) buffer.getShortString(messageId); + if(flags & (1 << 6)) timestamp = buffer.getLongLong(); + if(flags & (1 << 5)) buffer.getShortString(type); + if(flags & (1 << 4)) buffer.getShortString(userId); + if(flags & (1 << 3)) buffer.getShortString(appId); + if(flags & (1 << 2)) buffer.getShortString(clusterId); +} + +uint16_t qpid::framing::BasicHeaderProperties::getFlags() const{ + uint16_t flags(0); + if(contentType.length() > 0) flags |= (1 << 15); + if(contentEncoding.length() > 0) flags |= (1 << 14); + if(headers.count() > 0) flags |= (1 << 13); + if(deliveryMode != 0) flags |= (1 << 12); + if(priority != 0) flags |= (1 << 11); + if(correlationId.length() > 0) flags |= (1 << 10); + if(replyTo.length() > 0) flags |= (1 << 9); + if(expiration.length() > 0) flags |= (1 << 8); + if(messageId.length() > 0) flags |= (1 << 7); + if(timestamp != 0) flags |= (1 << 6); + if(type.length() > 0) flags |= (1 << 5); + if(userId.length() > 0) flags |= (1 << 4); + if(appId.length() > 0) flags |= (1 << 3); + if(clusterId.length() > 0) flags |= (1 << 2); + return flags; +} diff --git a/cpp/src/qpid/framing/BasicHeaderProperties.h b/cpp/src/qpid/framing/BasicHeaderProperties.h new file mode 100644 index 0000000000..a6347b37fd --- /dev/null +++ b/cpp/src/qpid/framing/BasicHeaderProperties.h @@ -0,0 +1,115 @@ +/* + * + * 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 "amqp_types.h" +#include "Buffer.h" +#include "FieldTable.h" +#include "HeaderProperties.h" + +#ifndef _BasicHeaderProperties_ +#define _BasicHeaderProperties_ + +namespace qpid { +namespace framing { + +enum DeliveryMode { TRANSIENT = 1, PERSISTENT = 2}; + +class BasicHeaderProperties : public HeaderProperties +{ + string contentType; + string contentEncoding; + FieldTable headers; + DeliveryMode deliveryMode; + uint8_t priority; + string correlationId; + string replyTo; + string expiration; + string messageId; + uint64_t timestamp; + string type; + string userId; + string appId; + string clusterId; + + uint16_t getFlags() const; + + public: + BasicHeaderProperties(); + virtual ~BasicHeaderProperties(); + virtual uint32_t size() const; + virtual void encode(Buffer& buffer) const; + virtual void decode(Buffer& buffer, uint32_t size); + + virtual uint8_t classId() { return BASIC; } + + string getContentType() const { return contentType; } + string getContentEncoding() const { return contentEncoding; } + FieldTable& getHeaders() { return headers; } + const FieldTable& getHeaders() const { return headers; } + DeliveryMode getDeliveryMode() const { return deliveryMode; } + uint8_t getPriority() const { return priority; } + string getCorrelationId() const {return correlationId; } + string getReplyTo() const { return replyTo; } + string getExpiration() const { return expiration; } + string getMessageId() const {return messageId; } + uint64_t getTimestamp() const { return timestamp; } + string getType() const { return type; } + string getUserId() const { return userId; } + string getAppId() const { return appId; } + string getClusterId() const { return clusterId; } + + void setContentType(const string& _type){ contentType = _type; } + void setContentEncoding(const string& encoding){ contentEncoding = encoding; } + void setHeaders(const FieldTable& _headers){ headers = _headers; } + void setDeliveryMode(DeliveryMode mode){ deliveryMode = mode; } + void setPriority(uint8_t _priority){ priority = _priority; } + void setCorrelationId(const string& _correlationId){ correlationId = _correlationId; } + void setReplyTo(const string& _replyTo){ replyTo = _replyTo;} + void setExpiration(const string& _expiration){ expiration = _expiration; } + void setMessageId(const string& _messageId){ messageId = _messageId; } + void setTimestamp(uint64_t _timestamp){ timestamp = _timestamp; } + void setType(const string& _type){ type = _type; } + void setUserId(const string& _userId){ userId = _userId; } + void setAppId(const string& _appId){appId = _appId; } + void setClusterId(const string& _clusterId){ clusterId = _clusterId; } + + /** \internal + * Template to copy between types like BasicHeaderProperties. + */ + template + static void copy(T& to, const U& from) { + to.setContentType(from.getContentType()); + to.setContentEncoding(from.getContentEncoding()); + to.setHeaders(from.getHeaders()); + to.setDeliveryMode(from.getDeliveryMode()); + to.setPriority(from.getPriority()); + to.setCorrelationId(from.getCorrelationId()); + to.setReplyTo(from.getReplyTo()); + to.setExpiration(from.getExpiration()); + to.setMessageId(from.getMessageId()); + to.setTimestamp(from.getTimestamp()); + to.setType(from.getType()); + to.setUserId(from.getUserId()); + to.setAppId(from.getAppId()); + to.setClusterId(from.getClusterId()); + } +}; +}} +#endif diff --git a/cpp/src/qpid/framing/BodyHandler.cpp b/cpp/src/qpid/framing/BodyHandler.cpp new file mode 100644 index 0000000000..0405b39e5d --- /dev/null +++ b/cpp/src/qpid/framing/BodyHandler.cpp @@ -0,0 +1,60 @@ +/* + * + * 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/QpidError.h" +#include "BodyHandler.h" +#include "AMQRequestBody.h" +#include "AMQResponseBody.h" +#include "AMQMethodBody.h" +#include "AMQHeaderBody.h" +#include "AMQContentBody.h" +#include "AMQHeartbeatBody.h" + +using namespace qpid::framing; +using namespace boost; + +BodyHandler::~BodyHandler() {} + +void BodyHandler::handleBody(shared_ptr body) { + switch(body->type()) + { + case REQUEST_BODY: + handleRequest(shared_polymorphic_cast(body)); + break; + case RESPONSE_BODY: + handleResponse(shared_polymorphic_cast(body)); + break; + case METHOD_BODY: + handleMethod(shared_polymorphic_cast(body)); + break; + case HEADER_BODY: + handleHeader(shared_polymorphic_cast(body)); + break; + case CONTENT_BODY: + handleContent(shared_polymorphic_cast(body)); + break; + case HEARTBEAT_BODY: + handleHeartbeat(shared_polymorphic_cast(body)); + break; + default: + QPID_ERROR(PROTOCOL_ERROR, "Unknown frame type "+body->type()); + } +} + diff --git a/cpp/src/qpid/framing/BodyHandler.h b/cpp/src/qpid/framing/BodyHandler.h new file mode 100644 index 0000000000..cb3f0997b0 --- /dev/null +++ b/cpp/src/qpid/framing/BodyHandler.h @@ -0,0 +1,61 @@ +#ifndef _BodyHandler_ +#define _BodyHandler_ + +/* + * + * 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 + +#include "Requester.h" +#include "Responder.h" + +namespace qpid { +namespace framing { + +class AMQRequestBody; +class AMQResponseBody; +class AMQMethodBody; +class AMQHeaderBody; +class AMQContentBody; +class AMQHeartbeatBody; + +/** + * Interface to handle incoming frame bodies. + * Derived classes provide logic for each frame type. + */ +class BodyHandler { + public: + virtual ~BodyHandler(); + virtual void handleBody(boost::shared_ptr body); + + protected: + virtual void handleRequest(boost::shared_ptr) = 0; + virtual void handleResponse(boost::shared_ptr) = 0; + virtual void handleMethod(boost::shared_ptr) = 0; + virtual void handleHeader(boost::shared_ptr) = 0; + virtual void handleContent(boost::shared_ptr) = 0; + virtual void handleHeartbeat(boost::shared_ptr) = 0; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/framing/Buffer.cpp b/cpp/src/qpid/framing/Buffer.cpp new file mode 100644 index 0000000000..2a14e854d0 --- /dev/null +++ b/cpp/src/qpid/framing/Buffer.cpp @@ -0,0 +1,183 @@ +/* + * + * 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 "Buffer.h" +#include "FramingContent.h" +#include "FieldTable.h" + +qpid::framing::Buffer::Buffer(uint32_t _size) : size(_size), owner(true), position(0), limit(_size){ + data = new char[size]; +} + +qpid::framing::Buffer::Buffer(char* _data, uint32_t _size) : size(_size), owner(false), data(_data), position(0), limit(_size){ +} + +qpid::framing::Buffer::~Buffer(){ + if(owner) delete[] data; +} + +void qpid::framing::Buffer::flip(){ + limit = position; + position = 0; +} + +void qpid::framing::Buffer::clear(){ + limit = size; + position = 0; +} + +void qpid::framing::Buffer::compact(){ + uint32_t p = limit - position; + //copy p chars from position to 0 + memmove(data, data + position, p); + limit = size; + position = p; +} + +void qpid::framing::Buffer::record(){ + r_position = position; + r_limit = limit; +} + +void qpid::framing::Buffer::restore(){ + position = r_position; + limit = r_limit; +} + +uint32_t qpid::framing::Buffer::available(){ + return limit - position; +} + +char* qpid::framing::Buffer::start(){ + return data + position; +} + +void qpid::framing::Buffer::move(uint32_t bytes){ + position += bytes; +} + +void qpid::framing::Buffer::putOctet(uint8_t i){ + data[position++] = i; +} + +void qpid::framing::Buffer::putShort(uint16_t i){ + uint16_t b = i; + data[position++] = (uint8_t) (0xFF & (b >> 8)); + data[position++] = (uint8_t) (0xFF & b); +} + +void qpid::framing::Buffer::putLong(uint32_t i){ + uint32_t b = i; + data[position++] = (uint8_t) (0xFF & (b >> 24)); + data[position++] = (uint8_t) (0xFF & (b >> 16)); + data[position++] = (uint8_t) (0xFF & (b >> 8)); + data[position++] = (uint8_t) (0xFF & b); +} + +void qpid::framing::Buffer::putLongLong(uint64_t i){ + uint32_t hi = i >> 32; + uint32_t lo = i; + putLong(hi); + putLong(lo); +} + +uint8_t qpid::framing::Buffer::getOctet(){ + return (uint8_t) data[position++]; +} + +uint16_t qpid::framing::Buffer::getShort(){ + uint16_t hi = (unsigned char) data[position++]; + hi = hi << 8; + hi |= (unsigned char) data[position++]; + return hi; +} + +uint32_t qpid::framing::Buffer::getLong(){ + uint32_t a = (unsigned char) data[position++]; + uint32_t b = (unsigned char) data[position++]; + uint32_t c = (unsigned char) data[position++]; + uint32_t d = (unsigned char) data[position++]; + a = a << 24; + a |= b << 16; + a |= c << 8; + a |= d; + return a; +} + +uint64_t qpid::framing::Buffer::getLongLong(){ + uint64_t hi = getLong(); + uint64_t lo = getLong(); + hi = hi << 32; + return hi | lo; +} + + +void qpid::framing::Buffer::putShortString(const string& s){ + uint8_t len = s.length(); + putOctet(len); + s.copy(data + position, len); + position += len; +} + +void qpid::framing::Buffer::putLongString(const string& s){ + uint32_t len = s.length(); + putLong(len); + s.copy(data + position, len); + position += len; +} + +void qpid::framing::Buffer::getShortString(string& s){ + uint8_t len = getOctet(); + s.assign(data + position, len); + position += len; +} + +void qpid::framing::Buffer::getLongString(string& s){ + uint32_t len = getLong(); + s.assign(data + position, len); + position += len; +} + +void qpid::framing::Buffer::putFieldTable(const FieldTable& t){ + t.encode(*this); +} + +void qpid::framing::Buffer::getFieldTable(FieldTable& t){ + t.decode(*this); +} + +void qpid::framing::Buffer::putContent(const Content& c){ + c.encode(*this); +} + +void qpid::framing::Buffer::getContent(Content& c){ + c.decode(*this); +} + +void qpid::framing::Buffer::putRawData(const string& s){ + uint32_t len = s.length(); + s.copy(data + position, len); + position += len; +} + +void qpid::framing::Buffer::getRawData(string& s, uint32_t len){ + s.assign(data + position, len); + position += len; +} diff --git a/cpp/src/qpid/framing/Buffer.h b/cpp/src/qpid/framing/Buffer.h new file mode 100644 index 0000000000..e1a3fb065a --- /dev/null +++ b/cpp/src/qpid/framing/Buffer.h @@ -0,0 +1,86 @@ +/* + * + * 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 "amqp_types.h" + +#ifndef _Buffer_ +#define _Buffer_ + +namespace qpid { +namespace framing { + +class Content; +class FieldTable; + +class Buffer +{ + const uint32_t size; + const bool owner;//indicates whether the data is owned by this instance + char* data; + uint32_t position; + uint32_t limit; + uint32_t r_position; + uint32_t r_limit; + +public: + + Buffer(uint32_t size); + Buffer(char* data, uint32_t size); + ~Buffer(); + + void flip(); + void clear(); + void compact(); + void record(); + void restore(); + uint32_t available(); + char* start(); + void move(uint32_t bytes); + + void putOctet(uint8_t i); + void putShort(uint16_t i); + void putLong(uint32_t i); + void putLongLong(uint64_t i); + + uint8_t getOctet(); + uint16_t getShort(); + uint32_t getLong(); + uint64_t getLongLong(); + + void putShortString(const string& s); + void putLongString(const string& s); + void getShortString(string& s); + void getLongString(string& s); + + void putFieldTable(const FieldTable& t); + void getFieldTable(FieldTable& t); + + void putContent(const Content& c); + void getContent(Content& c); + + void putRawData(const string& s); + void getRawData(string& s, uint32_t size); + +}; + +}} // namespace qpid::framing + + +#endif diff --git a/cpp/src/qpid/framing/ChannelAdapter.cpp b/cpp/src/qpid/framing/ChannelAdapter.cpp new file mode 100644 index 0000000000..48a200c4e1 --- /dev/null +++ b/cpp/src/qpid/framing/ChannelAdapter.cpp @@ -0,0 +1,109 @@ +/* + * + * 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 + +#include "ChannelAdapter.h" +#include "AMQFrame.h" +#include "qpid/Exception.h" + +using boost::format; + +namespace qpid { +namespace framing { + +void ChannelAdapter::init( + ChannelId i, OutputHandler& o, ProtocolVersion v) +{ + assertChannelNotOpen(); + id = i; + out = &o; + version = v; +} + +RequestId ChannelAdapter::send( + shared_ptr body, Correlator::Action action) +{ + RequestId requestId = 0; + assertChannelOpen(); + switch (body->type()) { + case REQUEST_BODY: { + AMQRequestBody::shared_ptr request = + boost::shared_polymorphic_downcast(body); + requester.sending(request->getData()); + requestId = request->getData().requestId; + if (!action.empty()) + correlator.request(requestId, action); + break; + } + case RESPONSE_BODY: { + AMQResponseBody::shared_ptr response = + boost::shared_polymorphic_downcast(body); + responder.sending(response->getData()); + break; + } + // No action required for other body types. + } + out->send(new AMQFrame(getVersion(), getId(), body)); + return requestId; +} + +void ChannelAdapter::handleRequest(AMQRequestBody::shared_ptr request) { + assertMethodOk(*request); + AMQRequestBody::Data& requestData = request->getData(); + responder.received(requestData); + handleMethodInContext(request, MethodContext(this, request)); +} + +void ChannelAdapter::handleResponse(AMQResponseBody::shared_ptr response) { + assertMethodOk(*response); + AMQResponseBody::Data& responseData = response->getData(); + + // FIXME aconway 2007-04-05: processed should be last + // but causes problems with InProcessBroker tests because + // we execute client code in handleMethod. + // Need to introduce a queue & 2 threads for inprocess. + requester.processed(responseData); + // FIXME aconway 2007-04-04: exception handling. + correlator.response(response); + handleMethod(response); +} + +void ChannelAdapter::handleMethod(AMQMethodBody::shared_ptr method) { + assertMethodOk(*method); + handleMethodInContext(method, MethodContext(this, method)); +} + +void ChannelAdapter::assertMethodOk(AMQMethodBody& method) const { + if (getId() != 0 && method.amqpClassId() == ConnectionOpenBody::CLASS_ID) + throw ConnectionException( + 504, format("Connection method on non-0 channel %d.")%getId()); +} + +void ChannelAdapter::assertChannelOpen() const { + if (getId() != 0 && !isOpen()) + throw ConnectionException( + 504, format("Channel %d is not open.")%getId()); +} + +void ChannelAdapter::assertChannelNotOpen() const { + if (getId() != 0 && isOpen()) + throw ConnectionException( + 504, format("Channel %d is already open.") % getId()); +} + +}} // namespace qpid::framing diff --git a/cpp/src/qpid/framing/ChannelAdapter.h b/cpp/src/qpid/framing/ChannelAdapter.h new file mode 100644 index 0000000000..75e4d26ab3 --- /dev/null +++ b/cpp/src/qpid/framing/ChannelAdapter.h @@ -0,0 +1,113 @@ +#ifndef _ChannelAdapter_ +#define _ChannelAdapter_ + +/* + * + * 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/shared_ptr.h" +#include "BodyHandler.h" +#include "Requester.h" +#include "Responder.h" +#include "Correlator.h" +#include "amqp_types.h" + +namespace qpid { +namespace framing { + +class MethodContext; + +// FIXME aconway 2007-02-20: Rename as ChannelBase or just Channel. + +/** + * Base class for client and broker channels. + * + * - receives frame bodies from the network. + * - Updates request/response data. + * - Dispatches requests with a MethodContext for responses. + * + * send() + * - Updates request/resposne ID data. + * - Forwards frame to the peer. + * + * Thread safety: OBJECT UNSAFE. Instances must not be called + * concurrently. AMQP defines channels to be serialized. + */ +class ChannelAdapter : public BodyHandler { + public: + /** + *@param output Processed frames are forwarded to this handler. + */ + ChannelAdapter(ChannelId id_=0, OutputHandler* out_=0, + ProtocolVersion ver=ProtocolVersion()) + : id(id_), out(out_), version(ver) {} + + /** Initialize the channel adapter. */ + void init(ChannelId, OutputHandler&, ProtocolVersion); + + ChannelId getId() const { return id; } + ProtocolVersion getVersion() const { return version; } + + /** + * Send a frame. + *@param body Body of the frame. + *@param action optional action to execute when we receive a + *response to this frame. Ignored if body is not a Request. + *@return If body is a request, the ID assigned else 0. + */ + RequestId send(shared_ptr body, + Correlator::Action action=Correlator::Action()); + + // TODO aconway 2007-04-05: remove and use make_shared_ptr at call sites. + /**@deprecated Use make_shared_ptr with the other send() override */ + RequestId send(AMQBody* body) { return send(AMQBody::shared_ptr(body)); } + + void handleMethod(shared_ptr); + void handleRequest(shared_ptr); + void handleResponse(shared_ptr); + + virtual bool isOpen() const = 0; + + protected: + void assertMethodOk(AMQMethodBody& method) const; + void assertChannelOpen() const; + void assertChannelNotOpen() const; + + virtual void handleMethodInContext( + shared_ptr method, + const MethodContext& context) = 0; + + RequestId getFirstAckRequest() { return requester.getFirstAckRequest(); } + RequestId getLastAckRequest() { return requester.getLastAckRequest(); } + RequestId getNextSendRequestId() { return requester.getNextId(); } + + private: + ChannelId id; + OutputHandler* out; + ProtocolVersion version; + Requester requester; + Responder responder; + Correlator correlator; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/framing/Correlator.cpp b/cpp/src/qpid/framing/Correlator.cpp new file mode 100644 index 0000000000..1c18f6b414 --- /dev/null +++ b/cpp/src/qpid/framing/Correlator.cpp @@ -0,0 +1,42 @@ +/* + * + * 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 "Correlator.h" + +namespace qpid { +namespace framing { + +void Correlator::request(RequestId id, Action action) { + actions[id] = action; +} + +bool Correlator::response(shared_ptr r) { + Actions::iterator begin = actions.lower_bound(r->getRequestId()); + Actions::iterator end = + actions.upper_bound(r->getRequestId()+r->getBatchOffset()); + bool didAction = false; + for(Actions::iterator i=begin; i != end; ++i) { + // FIXME aconway 2007-04-04: Exception handling. + didAction = true; + i->second(r); + actions.erase(i); + } + return didAction; +} + +}} // namespace qpid::framing diff --git a/cpp/src/qpid/framing/Correlator.h b/cpp/src/qpid/framing/Correlator.h new file mode 100644 index 0000000000..35dd7514ae --- /dev/null +++ b/cpp/src/qpid/framing/Correlator.h @@ -0,0 +1,68 @@ +#ifndef _framing_Correlator_h +#define _framing_Correlator_h + +/* + * + * 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/shared_ptr.h" +#include "qpid/framing/AMQResponseBody.h" +#include +#include + +namespace qpid { +namespace framing { + +/** + * Correlate responses with actions established when sending the request. + * + * THREAD UNSAFE. + */ +class Correlator +{ + public: + typedef shared_ptr ResponsePtr; + typedef boost::function Action; + + /** + * Note that request with id was sent, record an action to call + * when a response arrives. + */ + void request(RequestId id, Action doOnResponse); + + /** + * Note response received, call action for associated request if any. + * Return true of some action(s) were executed. + */ + bool response(shared_ptr); + + /** + * Note the given execution mark was received, call actions + * for any requests that are impicitly responded to. + */ + void mark(RequestId mark); + + private: + typedef std::map Actions; + Actions actions; +}; + +}} // namespace qpid::framing + + + +#endif /*!_framing_Correlator_h*/ diff --git a/cpp/src/qpid/framing/FieldTable.cpp b/cpp/src/qpid/framing/FieldTable.cpp new file mode 100644 index 0000000000..3eb4e849bc --- /dev/null +++ b/cpp/src/qpid/framing/FieldTable.cpp @@ -0,0 +1,150 @@ +/* + * + * 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 "FieldTable.h" +#include "qpid/QpidError.h" +#include "Buffer.h" +#include "Value.h" +#include + +namespace qpid { +namespace framing { + +FieldTable::~FieldTable() {} + +uint32_t FieldTable::size() const { + uint32_t len(4); + for(ValueMap::const_iterator i = values.begin(); i != values.end(); ++i) { + // 2 = shortstr_len_byyte + type_char_byte + len += 2 + (i->first).size() + (i->second)->size(); + } + return len; +} + +int FieldTable::count() const { + return values.size(); +} + +namespace +{ +std::ostream& operator<<(std::ostream& out, const FieldTable::ValueMap::value_type& i) { + return out << i.first << ":" << *i.second; +} +} + +std::ostream& operator<<(std::ostream& out, const FieldTable& t) { + out << "{"; + FieldTable::ValueMap::const_iterator i = t.getMap().begin(); + if (i != t.getMap().end()) out << *i++; + while (i != t.getMap().end()) + { + out << "," << *i++; + } + return out << "}"; +} + +void FieldTable::setString(const std::string& name, const std::string& value){ + values[name] = ValuePtr(new StringValue(value)); +} + +void FieldTable::setInt(const std::string& name, int value){ + values[name] = ValuePtr(new IntegerValue(value)); +} + +void FieldTable::setTimestamp(const std::string& name, uint64_t value){ + values[name] = ValuePtr(new TimeValue(value)); +} + +void FieldTable::setTable(const std::string& name, const FieldTable& value){ + values[name] = ValuePtr(new FieldTableValue(value)); +} + +namespace { +template T default_value() { return T(); } +template <> int default_value() { return 0; } +template <> uint64_t default_value() { return 0; } +} + +template +T FieldTable::getValue(const std::string& name) const +{ + ValueMap::const_iterator i = values.find(name); + if (i == values.end()) return default_value(); + const ValueOps *vt = dynamic_cast*>(i->second.get()); + return vt->getValue(); +} + +std::string FieldTable::getString(const std::string& name) const { + return getValue(name); +} + +int FieldTable::getInt(const std::string& name) const { + return getValue(name); +} + +uint64_t FieldTable::getTimestamp(const std::string& name) const { + return getValue(name); +} + +void FieldTable::getTable(const std::string& name, FieldTable& value) const { + value = getValue(name); +} + +void FieldTable::encode(Buffer& buffer) const{ + buffer.putLong(size() - 4); + for (ValueMap::const_iterator i = values.begin(); i!=values.end(); ++i) { + buffer.putShortString(i->first); + buffer.putOctet(i->second->getType()); + i->second->encode(buffer); + } +} + +void FieldTable::decode(Buffer& buffer){ + uint32_t len = buffer.getLong(); + uint32_t available = buffer.available(); + if (available < len) + THROW_QPID_ERROR(FRAMING_ERROR, "Not enough data for field table."); + uint32_t leftover = available - len; + while(buffer.available() > leftover){ + std::string name; + buffer.getShortString(name); + std::auto_ptr value(Value::decode_value(buffer)); + values[name] = ValuePtr(value.release()); + } +} + + +bool FieldTable::operator==(const FieldTable& x) const { + if (values.size() != x.values.size()) return false; + for (ValueMap::const_iterator i = values.begin(); i != values.end(); ++i) { + ValueMap::const_iterator j = x.values.find(i->first); + if (j == x.values.end()) return false; + if (*(i->second) != *(j->second)) return false; + } + return true; +} + +void FieldTable::erase(const std::string& name) +{ + values.erase(values.find(name)); +} + +} +} diff --git a/cpp/src/qpid/framing/FieldTable.h b/cpp/src/qpid/framing/FieldTable.h new file mode 100644 index 0000000000..2be3e6d3ff --- /dev/null +++ b/cpp/src/qpid/framing/FieldTable.h @@ -0,0 +1,90 @@ +/* + * + * 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 +#include +#include +#include +#include "amqp_types.h" + +#ifndef _FieldTable_ +#define _FieldTable_ + +namespace qpid { + /** + * The framing namespace contains classes that are used to create, + * send and receive the basic packets from which AMQP is built. + */ +namespace framing { + +class Value; +class Buffer; + +/** + * A set of name-value pairs. (See the AMQP spec for more details on + * AMQP field tables). + * + * \ingroup clientapi + */ +class FieldTable +{ + public: + typedef boost::shared_ptr ValuePtr; + typedef std::map ValueMap; + + ~FieldTable(); + uint32_t size() const; + int count() const; + void setString(const std::string& name, const std::string& value); + void setInt(const std::string& name, int value); + void setTimestamp(const std::string& name, uint64_t value); + void setTable(const std::string& name, const FieldTable& value); + //void setDecimal(string& name, xxx& value); + std::string getString(const std::string& name) const; + int getInt(const std::string& name) const; + uint64_t getTimestamp(const std::string& name) const; + void getTable(const std::string& name, FieldTable& value) const; + //void getDecimal(string& name, xxx& value); + void erase(const std::string& name); + + void encode(Buffer& buffer) const; + void decode(Buffer& buffer); + + bool operator==(const FieldTable& other) const; + + // TODO aconway 2006-09-26: Yeuch! Rework FieldTable to have + // a map-like interface. + const ValueMap& getMap() const { return values; } + ValueMap& getMap() { return values; } + + private: + friend std::ostream& operator<<(std::ostream& out, const FieldTable& body); + ValueMap values; + template T getValue(const std::string& name) const; +}; + +class FieldNotFoundException{}; +class UnknownFieldName : public FieldNotFoundException{}; +class IncorrectFieldType : public FieldNotFoundException{}; +} +} + + +#endif diff --git a/cpp/src/qpid/framing/FramingContent.cpp b/cpp/src/qpid/framing/FramingContent.cpp new file mode 100644 index 0000000000..813e6fb49b --- /dev/null +++ b/cpp/src/qpid/framing/FramingContent.cpp @@ -0,0 +1,75 @@ +/* + * + * 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 + +#include "Buffer.h" +#include "FramingContent.h" +#include "qpid/QpidError.h" +#include + +namespace qpid { +namespace framing { + +Content::Content() : discriminator(0) {} + +Content::Content(uint8_t _discriminator, const string& _value): discriminator(_discriminator), value(_value) { + validate(); +} + +void Content::validate() { + if (discriminator == REFERENCE) { + if(value.empty()) { + THROW_QPID_ERROR(FRAMING_ERROR, "Reference cannot be empty"); + } + }else if (discriminator != INLINE) { + std::stringstream out; + out << "Invalid discriminator: " << (int) discriminator; + THROW_QPID_ERROR(FRAMING_ERROR, out.str()); + } +} + +Content::~Content() {} + +void Content::encode(Buffer& buffer) const { + buffer.putOctet(discriminator); + buffer.putLongString(value); +} + +void Content::decode(Buffer& buffer) { + discriminator = buffer.getOctet(); + buffer.getLongString(value); + validate(); +} + +size_t Content::size() const { + return 1/*discriminator*/ + 4/*for recording size of long string*/ + value.size(); +} + +std::ostream& operator<<(std::ostream& out, const Content& content) { + if (content.discriminator == REFERENCE) { + out << "{REF:" << content.value << "}"; + } else if (content.discriminator == INLINE) { + out << "{INLINE:" << content.value.size() << " bytes}"; + } + return out; +} + +}} // namespace framing::qpid diff --git a/cpp/src/qpid/framing/FramingContent.h b/cpp/src/qpid/framing/FramingContent.h new file mode 100644 index 0000000000..876e90c905 --- /dev/null +++ b/cpp/src/qpid/framing/FramingContent.h @@ -0,0 +1,41 @@ +#ifndef _framing_FramingContent_h +#define _framing_FramingContent_h + +#include + +namespace qpid { +namespace framing { + +enum discriminator_types { INLINE = 0, REFERENCE = 1 }; + +/** + * A representation of the AMQP 'content' data type (used for message + * bodies) which can hold inline data or a reference. + */ +class Content +{ + uint8_t discriminator; + string value; + + void validate(); + + public: + Content(); + Content(uint8_t _discriminator, const string& _value); + ~Content(); + + void encode(Buffer& buffer) const; + void decode(Buffer& buffer); + size_t size() const; + bool isInline() const { return discriminator == INLINE; } + bool isReference() const { return discriminator == REFERENCE; } + const string& getValue() const { return value; } + void setValue(const string& newValue) { value = newValue; } + + friend std::ostream& operator<<(std::ostream&, const Content&); +}; + +}} // namespace qpid::framing + + +#endif /*!_framing_FramingContent_h*/ diff --git a/cpp/src/qpid/framing/HeaderProperties.h b/cpp/src/qpid/framing/HeaderProperties.h new file mode 100644 index 0000000000..ae8b796aa9 --- /dev/null +++ b/cpp/src/qpid/framing/HeaderProperties.h @@ -0,0 +1,46 @@ +/* + * + * 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 "amqp_types.h" +#include "Buffer.h" + +#ifndef _HeaderProperties_ +#define _HeaderProperties_ + +namespace qpid { +namespace framing { + + enum header_classes{BASIC = 60}; + + class HeaderProperties + { + + public: + inline virtual ~HeaderProperties(){} + virtual uint8_t classId() = 0; + virtual uint32_t size() const = 0; + virtual void encode(Buffer& buffer) const = 0; + virtual void decode(Buffer& buffer, uint32_t size) = 0; + }; +} +} + + +#endif diff --git a/cpp/src/qpid/framing/InitiationHandler.cpp b/cpp/src/qpid/framing/InitiationHandler.cpp new file mode 100644 index 0000000000..eceeaf4abc --- /dev/null +++ b/cpp/src/qpid/framing/InitiationHandler.cpp @@ -0,0 +1,24 @@ +/* + * + * 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 "InitiationHandler.h" + +qpid::framing::InitiationHandler::~InitiationHandler() {} diff --git a/cpp/src/qpid/framing/InitiationHandler.h b/cpp/src/qpid/framing/InitiationHandler.h new file mode 100644 index 0000000000..16a6b502e8 --- /dev/null +++ b/cpp/src/qpid/framing/InitiationHandler.h @@ -0,0 +1,41 @@ +/* + * + * 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 + +#ifndef _InitiationHandler_ +#define _InitiationHandler_ + +#include "ProtocolInitiation.h" + +namespace qpid { +namespace framing { + + class InitiationHandler{ + public: + virtual ~InitiationHandler(); + virtual void initiated(const ProtocolInitiation&) = 0; + }; + +} +} + + +#endif diff --git a/cpp/src/qpid/framing/InputHandler.h b/cpp/src/qpid/framing/InputHandler.h new file mode 100644 index 0000000000..dc6814b849 --- /dev/null +++ b/cpp/src/qpid/framing/InputHandler.h @@ -0,0 +1,39 @@ +#ifndef _InputHandler_ +#define _InputHandler_ +/* + * + * 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 "AMQFrame.h" +#include + +namespace qpid { +namespace framing { + +class InputHandler : private boost::noncopyable { + public: + virtual ~InputHandler() {} + virtual void received(AMQFrame* frame) = 0; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/framing/MethodContext.cpp b/cpp/src/qpid/framing/MethodContext.cpp new file mode 100644 index 0000000000..73af73f8e5 --- /dev/null +++ b/cpp/src/qpid/framing/MethodContext.cpp @@ -0,0 +1,31 @@ +/* + * + * 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 "MethodContext.h" +#include "amqp_types.h" +#include "AMQRequestBody.h" + +namespace qpid { +namespace framing { + +RequestId MethodContext::getRequestId() const { + return boost::shared_polymorphic_downcast(methodBody) + ->getRequestId(); +} + +}} // namespace qpid::framing diff --git a/cpp/src/qpid/framing/MethodContext.h b/cpp/src/qpid/framing/MethodContext.h new file mode 100644 index 0000000000..80e4c55d7e --- /dev/null +++ b/cpp/src/qpid/framing/MethodContext.h @@ -0,0 +1,75 @@ +#ifndef _framing_MethodContext_h +#define _framing_MethodContext_h + +/* + * + * 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 + +#include "OutputHandler.h" +#include "ProtocolVersion.h" + +namespace qpid { +namespace framing { + +class BodyHandler; +class AMQMethodBody; +class ChannelAdapter; + +/** + * Invocation context for an AMQP method. + * + * It provides the method being processed and the channel on which + * it arrived. + * + * All Handler functions take a MethodContext as the last parameter. + */ +struct MethodContext +{ + typedef boost::shared_ptr BodyPtr; + + MethodContext(ChannelAdapter* ch=0, BodyPtr method=BodyPtr()) + : channel(ch), methodBody(method) {} + + /** + * Channel on which the method being processed arrived. + * 0 if the method was constructed by the caller + * rather than received from a channel. + */ + ChannelAdapter* channel; + + /** + * Body of the method being processed. + * It's useful for passing around instead of unpacking all its parameters. + * It's also provides the request ID when constructing a response. + */ + BodyPtr methodBody; + + /** + * Return methodBody's request ID. + * It is an error to call this if methodBody is not a request. + */ + RequestId getRequestId() const; +}; + + +}} // namespace qpid::framing + + + +#endif /*!_framing_MethodContext_h*/ diff --git a/cpp/src/qpid/framing/OutputHandler.h b/cpp/src/qpid/framing/OutputHandler.h new file mode 100644 index 0000000000..9ffd4227d8 --- /dev/null +++ b/cpp/src/qpid/framing/OutputHandler.h @@ -0,0 +1,39 @@ +#ifndef _OutputHandler_ +#define _OutputHandler_ + +/* + * + * 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 + +namespace qpid { +namespace framing { +class AMQFrame; + +class OutputHandler : private boost::noncopyable { + public: + virtual ~OutputHandler() {} + virtual void send(AMQFrame* frame) = 0; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/framing/ProtocolInitiation.cpp b/cpp/src/qpid/framing/ProtocolInitiation.cpp new file mode 100644 index 0000000000..a6d1b17f6e --- /dev/null +++ b/cpp/src/qpid/framing/ProtocolInitiation.cpp @@ -0,0 +1,63 @@ +/* + * + * 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 "ProtocolInitiation.h" + +namespace qpid { +namespace framing { + +ProtocolInitiation::ProtocolInitiation(){} + +ProtocolInitiation::ProtocolInitiation(uint8_t _major, uint8_t _minor) : version(_major, _minor) {} + +ProtocolInitiation::ProtocolInitiation(ProtocolVersion p) : version(p) {} + +ProtocolInitiation::~ProtocolInitiation(){} + +void ProtocolInitiation::encode(Buffer& buffer){ + buffer.putOctet('A'); + buffer.putOctet('M'); + buffer.putOctet('Q'); + buffer.putOctet('P'); + buffer.putOctet(1);//class + buffer.putOctet(1);//instance + buffer.putOctet(version.getMajor()); + buffer.putOctet(version.getMinor()); +} + +bool ProtocolInitiation::decode(Buffer& buffer){ + if(buffer.available() >= 8){ + buffer.getOctet();//A + buffer.getOctet();//M + buffer.getOctet();//Q + buffer.getOctet();//P + buffer.getOctet();//class + buffer.getOctet();//instance + version.setMajor(buffer.getOctet()); + version.setMinor(buffer.getOctet()); + return true; + }else{ + return false; + } +} + +//TODO: this should prbably be generated from the spec at some point to keep the version numbers up to date + +}} // namespace qpid::framing diff --git a/cpp/src/qpid/framing/ProtocolInitiation.h b/cpp/src/qpid/framing/ProtocolInitiation.h new file mode 100644 index 0000000000..adfdc8215d --- /dev/null +++ b/cpp/src/qpid/framing/ProtocolInitiation.h @@ -0,0 +1,54 @@ +/* + * + * 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 "amqp_types.h" +#include "Buffer.h" +#include "AMQDataBlock.h" +#include "ProtocolVersion.h" + +#ifndef _ProtocolInitiation_ +#define _ProtocolInitiation_ + +namespace qpid { +namespace framing { + +class ProtocolInitiation : public AMQDataBlock +{ +private: + ProtocolVersion version; + +public: + ProtocolInitiation(); + ProtocolInitiation(uint8_t major, uint8_t minor); + ProtocolInitiation(ProtocolVersion p); + virtual ~ProtocolInitiation(); + virtual void encode(Buffer& buffer); + virtual bool decode(Buffer& buffer); + inline virtual uint32_t size() const { return 8; } + inline uint8_t getMajor() const { return version.getMajor(); } + inline uint8_t getMinor() const { return version.getMinor(); } + inline ProtocolVersion getVersion() const { return version; } +}; + +} +} + + +#endif diff --git a/cpp/src/qpid/framing/ProtocolVersion.cpp b/cpp/src/qpid/framing/ProtocolVersion.cpp new file mode 100644 index 0000000000..7a96bfa925 --- /dev/null +++ b/cpp/src/qpid/framing/ProtocolVersion.cpp @@ -0,0 +1,44 @@ +/* + * + * 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 "ProtocolVersion.h" +#include + +using namespace qpid::framing; + +const std::string ProtocolVersion::toString() const +{ + std::stringstream ss; + ss << major_ << "-" << minor_; + return ss.str(); +} + +ProtocolVersion& ProtocolVersion::operator=(ProtocolVersion p) +{ + major_ = p.major_; + minor_ = p.minor_; + return *this; +} + +bool ProtocolVersion::operator==(ProtocolVersion p) const +{ + return major_ == p.major_ && minor_ == p.minor_; +} + diff --git a/cpp/src/qpid/framing/ProtocolVersion.h b/cpp/src/qpid/framing/ProtocolVersion.h new file mode 100644 index 0000000000..a2a755397b --- /dev/null +++ b/cpp/src/qpid/framing/ProtocolVersion.h @@ -0,0 +1,57 @@ +/* + * + * 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 _ProtocolVersion_ +#define _ProtocolVersion_ + +#include "amqp_types.h" + +namespace qpid +{ +namespace framing +{ + +class ProtocolVersion +{ +private: + uint8_t major_; + uint8_t minor_; + +public: + ProtocolVersion(uint8_t _major=0, uint8_t _minor=0) + : major_(_major), minor_(_minor) {} + + uint8_t getMajor() const { return major_; } + void setMajor(uint8_t major) { major_ = major; } + uint8_t getMinor() const { return minor_; } + void setMinor(uint8_t minor) { minor_ = minor; } + const std::string toString() const; + + ProtocolVersion& operator=(ProtocolVersion p); + + bool operator==(ProtocolVersion p) const; + bool operator!=(ProtocolVersion p) const { return ! (*this == p); } +}; + +} // namespace framing +} // namespace qpid + + +#endif // ifndef _ProtocolVersion_ diff --git a/cpp/src/qpid/framing/ProtocolVersionException.cpp b/cpp/src/qpid/framing/ProtocolVersionException.cpp new file mode 100644 index 0000000000..b68b3af1f9 --- /dev/null +++ b/cpp/src/qpid/framing/ProtocolVersionException.cpp @@ -0,0 +1,33 @@ +/* + * + * 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 +#include "ProtocolVersionException.h" + + +using namespace qpid::framing; + +void ProtocolVersionException::init(const std::string& msg) +{ + whatStr = boost::str( + boost::format("ProtocolVersionException: %s found: %s") + % versionFound.toString() % msg); +} + diff --git a/cpp/src/qpid/framing/ProtocolVersionException.h b/cpp/src/qpid/framing/ProtocolVersionException.h new file mode 100644 index 0000000000..bd16804470 --- /dev/null +++ b/cpp/src/qpid/framing/ProtocolVersionException.h @@ -0,0 +1,56 @@ +/* + * + * 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 _ProtocolVersionException_ +#define _ProtocolVersionException_ + +#include "qpid/Exception.h" +#include "ProtocolVersion.h" +#include +#include + +namespace qpid { +namespace framing { + +class ProtocolVersionException : public qpid::Exception +{ +protected: + ProtocolVersion versionFound; + +public: + ~ProtocolVersionException() throw() {} + + template + ProtocolVersionException( + ProtocolVersion ver, const T& msg) throw () : versionFound(ver) + { init(boost::lexical_cast(msg)); } + + template + ProtocolVersionException(const T& msg) throw () + { init(boost::lexical_cast(msg)); } + + private: + void init(const std::string& msg); +}; + +}} // namespace qpid::framing + +#endif //ifndef _ProtocolVersionException_ diff --git a/cpp/src/qpid/framing/Proxy.cpp b/cpp/src/qpid/framing/Proxy.cpp new file mode 100644 index 0000000000..0b2a882a49 --- /dev/null +++ b/cpp/src/qpid/framing/Proxy.cpp @@ -0,0 +1,32 @@ +/* + * + * 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 "Proxy.h" +#include "ChannelAdapter.h" +#include "ProtocolVersion.h" + +namespace qpid { +namespace framing { + +Proxy::~Proxy() {} + +ProtocolVersion Proxy::getProtocolVersion() const { + return channel.getVersion(); +} + +}} // namespace qpid::framing diff --git a/cpp/src/qpid/framing/Proxy.h b/cpp/src/qpid/framing/Proxy.h new file mode 100644 index 0000000000..8ed46ed748 --- /dev/null +++ b/cpp/src/qpid/framing/Proxy.h @@ -0,0 +1,51 @@ +#ifndef _framing_Proxy_h +#define _framing_Proxy_h + +/* + * + * 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 "ProtocolVersion.h" + +namespace qpid { +namespace framing { + +class ChannelAdapter; +class FieldTable; +class Content; + +/** + * Base class for proxies. + */ +class Proxy +{ + + public: + Proxy(ChannelAdapter& ch) : channel(ch) {} + virtual ~Proxy(); + + ProtocolVersion getProtocolVersion() const; + + protected: + ChannelAdapter& channel; +}; + +}} // namespace qpid::framing + + + +#endif /*!_framing_Proxy_h*/ diff --git a/cpp/src/qpid/framing/Requester.cpp b/cpp/src/qpid/framing/Requester.cpp new file mode 100644 index 0000000000..a675f0a61b --- /dev/null +++ b/cpp/src/qpid/framing/Requester.cpp @@ -0,0 +1,40 @@ +/* + * + * 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 + +#include "Requester.h" +#include "qpid/QpidError.h" + +namespace qpid { +namespace framing { + +Requester::Requester() : lastId(0), responseMark(0) {} + +void Requester::sending(AMQRequestBody::Data& request) { + request.requestId = ++lastId; + request.responseMark = responseMark; +} + +void Requester::processed(const AMQResponseBody::Data& response) { + responseMark = response.responseId; + firstAckRequest = response.requestId; + lastAckRequest = firstAckRequest + response.batchOffset; +} + +}} // namespace qpid::framing diff --git a/cpp/src/qpid/framing/Requester.h b/cpp/src/qpid/framing/Requester.h new file mode 100644 index 0000000000..65bdc9a5a1 --- /dev/null +++ b/cpp/src/qpid/framing/Requester.h @@ -0,0 +1,68 @@ +#ifndef _framing_Requester_h +#define _framing_Requester_h + +/* + * + * 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 +#include "AMQRequestBody.h" +#include "AMQResponseBody.h" + +namespace qpid { +namespace framing { + +class AMQRequestBody; +class AMQResponseBody; + +/** + * Manage request IDs and the response mark for locally initiated requests. + * + * THREAD UNSAFE: must be locked externally. + */ +class Requester +{ + public: + Requester(); + + /** Called before sending a request to set request data. */ + void sending(AMQRequestBody::Data&); + + /** Called after processing a response. */ + void processed(const AMQResponseBody::Data&); + + /** Get the next request id to be used. */ + RequestId getNextId() { return lastId + 1; } + + /** Get the first request acked by last response */ + RequestId getFirstAckRequest() { return firstAckRequest; } + + /** Get the last request acked by last response */ + RequestId getLastAckRequest() { return lastAckRequest; } + + private: + RequestId lastId; + ResponseId responseMark; + ResponseId firstAckRequest; + ResponseId lastAckRequest; +}; + +}} // namespace qpid::framing + + + +#endif /*!_framing_Requester_h*/ diff --git a/cpp/src/qpid/framing/Responder.cpp b/cpp/src/qpid/framing/Responder.cpp new file mode 100644 index 0000000000..1b9c8a6c59 --- /dev/null +++ b/cpp/src/qpid/framing/Responder.cpp @@ -0,0 +1,43 @@ +/* + * + * 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 + +#include "Responder.h" +#include "qpid/QpidError.h" + +namespace qpid { +namespace framing { + +Responder::Responder() : lastId(0), responseMark(0) {} + +void Responder::received(const AMQRequestBody::Data& request) { + if (request.responseMark < responseMark || request.responseMark > lastId) + THROW_QPID_ERROR( + PROTOCOL_ERROR, boost::format("Invalid response mark %d.") + %request.responseMark); + responseMark = request.responseMark; +} + +void Responder::sending(AMQResponseBody::Data& response) { + response.responseId = ++lastId; + assert(response.requestId); // Should be already set. + response.batchOffset = 0; +} + +}} // namespace qpid::framing + diff --git a/cpp/src/qpid/framing/Responder.h b/cpp/src/qpid/framing/Responder.h new file mode 100644 index 0000000000..0e1785256b --- /dev/null +++ b/cpp/src/qpid/framing/Responder.h @@ -0,0 +1,61 @@ +#ifndef _framing_Responder_h +#define _framing_Responder_h + +/* + * + * 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 "AMQRequestBody.h" +#include "AMQResponseBody.h" + +namespace qpid { +namespace framing { + +/** + * Manage response ids and response mark remotely initianted requests. + * + * THREAD UNSAFE: This class is called as frames are sent or received + * sequentially on a connection, so it does not need to be thread safe. + */ +class Responder +{ + public: + Responder(); + + /** Called after receiving a request. */ + void received(const AMQRequestBody::Data& request); + + /** Called before sending a response to set respose data. */ + void sending(AMQResponseBody::Data& response); + + /** Get the ID of the highest response acknowledged by the peer. */ + ResponseId getResponseMark() { return responseMark; } + + // TODO aconway 2007-01-14: Batching support - store unsent + // Response for equality comparison with subsequent responses. + // + + private: + ResponseId lastId; + ResponseId responseMark; +}; + +}} // namespace qpid::framing + + + +#endif /*!_framing_Responder_h*/ diff --git a/cpp/src/qpid/framing/Value.cpp b/cpp/src/qpid/framing/Value.cpp new file mode 100644 index 0000000000..3e3cc4cbd5 --- /dev/null +++ b/cpp/src/qpid/framing/Value.cpp @@ -0,0 +1,122 @@ +/* + * + * 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 "Value.h" +#include "Buffer.h" +#include "FieldTable.h" +#include "qpid/QpidError.h" +#include + +namespace qpid { +namespace framing { + +Value::~Value() {} + +void StringValue::encode(Buffer& buffer){ + buffer.putLongString(value); +} +void StringValue::decode(Buffer& buffer){ + buffer.getLongString(value); +} + +void IntegerValue::encode(Buffer& buffer){ + buffer.putLong((uint32_t) value); +} +void IntegerValue::decode(Buffer& buffer){ + value = buffer.getLong(); +} + +void TimeValue::encode(Buffer& buffer){ + buffer.putLongLong(value); +} +void TimeValue::decode(Buffer& buffer){ + value = buffer.getLongLong(); +} + +void DecimalValue::encode(Buffer& buffer){ + buffer.putOctet(value.decimals); + buffer.putLong(value.value); +} +void DecimalValue::decode(Buffer& buffer){ + value = Decimal(buffer.getLong(), buffer.getOctet()); +} + +void FieldTableValue::encode(Buffer& buffer){ + buffer.putFieldTable(value); +} +void FieldTableValue::decode(Buffer& buffer){ + buffer.getFieldTable(value); +} + +std::auto_ptr Value::decode_value(Buffer& buffer) +{ + std::auto_ptr value; + uint8_t type = buffer.getOctet(); + switch(type){ + case 'S': + value.reset(new StringValue()); + break; + case 'I': + value.reset(new IntegerValue()); + break; + case 'D': + value.reset(new DecimalValue()); + break; + case 'T': + value.reset(new TimeValue()); + break; + case 'F': + value.reset(new FieldTableValue()); + break; + + //non-standard types, introduced in java client for JMS compliance + case 'x': + value.reset(new BinaryValue()); + break; + default: + std::stringstream out; + out << "Unknown field table value type: " << type; + THROW_QPID_ERROR(FRAMING_ERROR, out.str()); + } + value->decode(buffer); + return value; +} + +EmptyValue::~EmptyValue() {} + +void EmptyValue::print(std::ostream& out) const +{ + out << ""; +} + +std::ostream& operator<<(std::ostream& out, const Value& v) { + v.print(out); + return out; +} + +std::ostream& operator<<(std::ostream& out, const Decimal& d) +{ + return out << "Decimal(" << d.value << "," << d.decimals << ")"; +} + +}} + + + diff --git a/cpp/src/qpid/framing/Value.h b/cpp/src/qpid/framing/Value.h new file mode 100644 index 0000000000..a6cff8ec88 --- /dev/null +++ b/cpp/src/qpid/framing/Value.h @@ -0,0 +1,171 @@ +/* + * + * 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 +#include +#include "amqp_types.h" +#include "FieldTable.h" + +#ifndef _Value_ +#define _Value_ + +namespace qpid { +namespace framing { + +class Buffer; + +/** + * Represents a decimal value. + * No arithmetic functionality for now, we only care about encoding/decoding. + */ +struct Decimal { + uint32_t value; + uint8_t decimals; + + Decimal(uint32_t value_=0, uint8_t decimals_=0) : value(value_), decimals(decimals_) {} + bool operator==(const Decimal& d) const { + return decimals == d.decimals && value == d.value; + } + bool operator!=(const Decimal& d) const { return !(*this == d); } +}; + +std::ostream& operator<<(std::ostream& out, const Decimal& d); + +/** + * Polymorpic base class for values. + */ +class Value { + public: + virtual ~Value(); + virtual uint32_t size() const = 0; + virtual char getType() const = 0; + virtual void encode(Buffer& buffer) = 0; + virtual void decode(Buffer& buffer) = 0; + virtual bool operator==(const Value&) const = 0; + bool operator!=(const Value& v) const { return !(*this == v); } + virtual void print(std::ostream& out) const = 0; + + /** Create a new value by decoding from the buffer */ + static std::auto_ptr decode_value(Buffer& buffer); +}; + +std::ostream& operator<<(std::ostream& out, const Value& d); + + +/** + * Template for common operations on Value sub-classes. + */ +template +class ValueOps : public Value +{ + protected: + T value; + public: + ValueOps() {} + ValueOps(const T& v) : value(v) {} + const T& getValue() const { return value; } + T& getValue() { return value; } + + virtual bool operator==(const Value& v) const { + const ValueOps* vo = dynamic_cast*>(&v); + if (vo == 0) return false; + else return value == vo->value; + } + + void print(std::ostream& out) const { out << value; } +}; + + +class StringValue : public ValueOps { + public: + StringValue(const std::string& v) : ValueOps(v) {} + StringValue() {} + virtual uint32_t size() const { return 4 + value.length(); } + virtual char getType() const { return 'S'; } + virtual void encode(Buffer& buffer); + virtual void decode(Buffer& buffer); +}; + +class IntegerValue : public ValueOps { + public: + IntegerValue(int v) : ValueOps(v) {} + IntegerValue(){} + virtual uint32_t size() const { return 4; } + virtual char getType() const { return 'I'; } + virtual void encode(Buffer& buffer); + virtual void decode(Buffer& buffer); +}; + +class TimeValue : public ValueOps { + public: + TimeValue(uint64_t v) : ValueOps(v){} + TimeValue(){} + virtual uint32_t size() const { return 8; } + virtual char getType() const { return 'T'; } + virtual void encode(Buffer& buffer); + virtual void decode(Buffer& buffer); +}; + +class DecimalValue : public ValueOps { + public: + DecimalValue(const Decimal& d) : ValueOps(d) {} + DecimalValue(uint32_t value_=0, uint8_t decimals_=0) : + ValueOps(Decimal(value_, decimals_)){} + virtual uint32_t size() const { return 5; } + virtual char getType() const { return 'D'; } + virtual void encode(Buffer& buffer); + virtual void decode(Buffer& buffer); +}; + + +class FieldTableValue : public ValueOps { + public: + FieldTableValue(const FieldTable& v) : ValueOps(v){} + FieldTableValue(){} + virtual uint32_t size() const { return 4 + value.size(); } + virtual char getType() const { return 'F'; } + virtual void encode(Buffer& buffer); + virtual void decode(Buffer& buffer); +}; + +class EmptyValue : public Value { + public: + ~EmptyValue(); + virtual uint32_t size() const { return 0; } + virtual char getType() const { return 0; } + virtual void encode(Buffer& ) {} + virtual void decode(Buffer& ) {} + virtual bool operator==(const Value& v) const { + return dynamic_cast(&v); + } + virtual void print(std::ostream& out) const; +}; + +//non-standard types, introduced in java client for JMS compliance +class BinaryValue : public StringValue { + public: + BinaryValue(const std::string& v) : StringValue(v) {} + BinaryValue() {} + virtual char getType() const { return 'x'; } +}; + +}} // qpid::framing + +#endif diff --git a/cpp/src/qpid/framing/amqp_framing.h b/cpp/src/qpid/framing/amqp_framing.h new file mode 100644 index 0000000000..888c6a7382 --- /dev/null +++ b/cpp/src/qpid/framing/amqp_framing.h @@ -0,0 +1,36 @@ +/* + * + * 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 "amqp_types.h" +#include "AMQFrame.h" +#include "AMQBody.h" +#include "BodyHandler.h" +#include "AMQMethodBody.h" +#include "AMQHeaderBody.h" +#include "AMQContentBody.h" +#include "AMQHeartbeatBody.h" +#include "qpid/framing/AMQP_MethodVersionMap.h" +#include "InputHandler.h" +#include "OutputHandler.h" +#include "InitiationHandler.h" +#include "ProtocolInitiation.h" +#include "BasicHeaderProperties.h" +#include "ProtocolVersion.h" +#include "ProtocolVersionException.h" diff --git a/cpp/src/qpid/framing/amqp_types.h b/cpp/src/qpid/framing/amqp_types.h new file mode 100644 index 0000000000..f0bd0ce427 --- /dev/null +++ b/cpp/src/qpid/framing/amqp_types.h @@ -0,0 +1,57 @@ +#ifndef AMQP_TYPES_H +#define AMQP_TYPES_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. + * + */ + +/** \file + * Type definitions and forward declarations of all types used to + * in AMQP messages. + */ + +#include +#ifdef _WINDOWS +#include "windows.h" +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; +typedef unsigned __int64 uint64_t; +#endif +#ifndef _WINDOWS +#include +#endif + +namespace qpid { +namespace framing { + +using std::string; +typedef uint16_t ChannelId; +typedef uint64_t RequestId; +typedef uint64_t ResponseId; +typedef uint32_t BatchOffset; +typedef uint16_t ClassId; +typedef uint16_t MethodId; +typedef uint16_t ReplyCode; + +// Types represented by classes. +class Content; +class FieldTable; +}} // namespace qpid::framing +#endif diff --git a/cpp/src/qpid/framing/amqp_types_full.h b/cpp/src/qpid/framing/amqp_types_full.h new file mode 100644 index 0000000000..6a24a99d38 --- /dev/null +++ b/cpp/src/qpid/framing/amqp_types_full.h @@ -0,0 +1,36 @@ +#ifndef _framing_amqp_types_decl_h +#define _framing_amqp_types_decl_h + +/* + * + * 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. + * + */ + +/** \file + * Type definitions and full declarations of all types used to + * in AMQP messages. + * + * Its better to include amqp_types.h in another header instead of this file + * unless the header actually needs the full declarations. Including + * full declarations when forward declarations would do increases compile + * times. + */ + +#include "amqp_types.h" +#include "FramingContent.h" +#include "FieldTable.h" + +#endif /*!_framing_amqp_types_decl_h*/ diff --git a/cpp/src/qpid/shared_ptr.h b/cpp/src/qpid/shared_ptr.h new file mode 100644 index 0000000000..eb5f3f906a --- /dev/null +++ b/cpp/src/qpid/shared_ptr.h @@ -0,0 +1,49 @@ +#ifndef _common_shared_ptr_h +#define _common_shared_ptr_h + +/* + * + * 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 +#include + +namespace qpid { + +// Import shared_ptr definitions into qpid namespace and define some +// useful shared_ptr templates for convenience. + +using boost::shared_ptr; +using boost::dynamic_pointer_cast; +using boost::static_pointer_cast; +using boost::const_pointer_cast; +using boost::shared_polymorphic_downcast; + +template shared_ptr make_shared_ptr(T* ptr) { + return shared_ptr(ptr); +} + +template +shared_ptr make_shared_ptr(T* ptr, D deleter) { + return shared_ptr(ptr, deleter); +} + +} // namespace qpid + + + +#endif /*!_common_shared_ptr_h*/ diff --git a/cpp/src/qpid/sys/Acceptor.h b/cpp/src/qpid/sys/Acceptor.h new file mode 100644 index 0000000000..5e624a956e --- /dev/null +++ b/cpp/src/qpid/sys/Acceptor.h @@ -0,0 +1,47 @@ +#ifndef _sys_Acceptor_h +#define _sys_Acceptor_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 +#include "qpid/SharedObject.h" + +namespace qpid { +namespace sys { + +class ConnectionInputHandlerFactory; + +class Acceptor : public qpid::SharedObject +{ + public: + static Acceptor::shared_ptr create(int16_t port, int backlog, int threads, bool trace = false); + virtual ~Acceptor() = 0; + virtual uint16_t getPort() const = 0; + virtual void run(qpid::sys::ConnectionInputHandlerFactory* factory) = 0; + virtual void shutdown() = 0; +}; + +}} + + + +#endif /*!_sys_Acceptor_h*/ diff --git a/cpp/src/qpid/sys/AtomicCount.h b/cpp/src/qpid/sys/AtomicCount.h new file mode 100644 index 0000000000..63670cbf00 --- /dev/null +++ b/cpp/src/qpid/sys/AtomicCount.h @@ -0,0 +1,53 @@ +#ifndef _posix_AtomicCount_h +#define _posix_AtomicCount_h + +/* + * + * 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 +#include "ScopedIncrement.h" + +namespace qpid { +namespace sys { + +/** + * Atomic counter. + */ +class AtomicCount : boost::noncopyable { + public: + typedef ScopedDecrement ScopedDecrement; + typedef ScopedIncrement ScopedIncrement; + + AtomicCount(long value = 0) : count(value) {} + + void operator++() { ++count ; } + + long operator--() { return --count; } + + operator long() const { return count; } + + + private: + boost::detail::atomic_count count; +}; + + +}} + + +#endif // _posix_AtomicCount_h diff --git a/cpp/src/qpid/sys/Condition.h b/cpp/src/qpid/sys/Condition.h new file mode 100644 index 0000000000..455b179683 --- /dev/null +++ b/cpp/src/qpid/sys/Condition.h @@ -0,0 +1,128 @@ +#ifndef _sys_Condition_h +#define _sys_Condition_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 +#include +#include "Mutex.h" +#include "Time.h" + +#ifdef USE_APR +#include +#endif + +namespace qpid { +namespace sys { + +/** + * A condition variable for thread synchronization. + */ +class Condition +{ + public: + inline Condition(); + inline ~Condition(); + inline void wait(Mutex&); + inline bool wait(Mutex&, const Time& absoluteTime); + inline void notify(); + inline void notifyAll(); + + private: +#ifdef USE_APR + apr_thread_cond_t* condition; +#else + pthread_cond_t condition; +#endif +}; + + +// APR ================================================================ +#ifdef USE_APR + +Condition::Condition() { + CHECK_APR_SUCCESS(apr_thread_cond_create(&condition, APRPool::get())); +} + +Condition::~Condition() { + CHECK_APR_SUCCESS(apr_thread_cond_destroy(condition)); +} + +void Condition::wait(Mutex& mutex) { + CHECK_APR_SUCCESS(apr_thread_cond_wait(condition, mutex.mutex)); +} + +bool Condition::wait(Mutex& mutex, const Time& absoluteTime){ + // APR uses microseconds. + apr_status_t status = + apr_thread_cond_timedwait( + condition, mutex.mutex, absoluteTime/TIME_USEC); + if(status != APR_TIMEUP) CHECK_APR_SUCCESS(status); + return status == 0; +} + +void Condition::notify(){ + CHECK_APR_SUCCESS(apr_thread_cond_signal(condition)); +} + +void Condition::notifyAll(){ + CHECK_APR_SUCCESS(apr_thread_cond_broadcast(condition)); +} + +#else +// POSIX ================================================================ + +Condition::Condition() { + QPID_POSIX_THROW_IF(pthread_cond_init(&condition, 0)); +} + +Condition::~Condition() { + QPID_POSIX_THROW_IF(pthread_cond_destroy(&condition)); +} + +void Condition::wait(Mutex& mutex) { + QPID_POSIX_THROW_IF(pthread_cond_wait(&condition, &mutex.mutex)); +} + +bool Condition::wait(Mutex& mutex, const Time& absoluteTime){ + struct timespec ts; + toTimespec(ts, absoluteTime); + int status = pthread_cond_timedwait(&condition, &mutex.mutex, &ts); + if (status != 0) { + if (status == ETIMEDOUT) return false; + throw QPID_POSIX_ERROR(status); + } + return true; +} + +void Condition::notify(){ + QPID_POSIX_THROW_IF(pthread_cond_signal(&condition)); +} + +void Condition::notifyAll(){ + QPID_POSIX_THROW_IF(pthread_cond_broadcast(&condition)); +} +#endif /*USE_APR*/ + + +}} +#endif /*!_sys_Condition_h*/ diff --git a/cpp/src/qpid/sys/ConnectionInputHandler.h b/cpp/src/qpid/sys/ConnectionInputHandler.h new file mode 100644 index 0000000000..2bf3f66ec2 --- /dev/null +++ b/cpp/src/qpid/sys/ConnectionInputHandler.h @@ -0,0 +1,45 @@ +/* + * + * 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 _ConnectionInputHandler_ +#define _ConnectionInputHandler_ + +#include "qpid/framing/InputHandler.h" +#include "qpid/framing/InitiationHandler.h" +#include "qpid/framing/ProtocolInitiation.h" +#include "TimeoutHandler.h" + +namespace qpid { +namespace sys { + + class ConnectionInputHandler : + public qpid::framing::InitiationHandler, + public qpid::framing::InputHandler, + public TimeoutHandler + { + public: + virtual void closed() = 0; + }; + +} +} + + +#endif diff --git a/cpp/src/qpid/sys/ConnectionInputHandlerFactory.h b/cpp/src/qpid/sys/ConnectionInputHandlerFactory.h new file mode 100644 index 0000000000..af7d411928 --- /dev/null +++ b/cpp/src/qpid/sys/ConnectionInputHandlerFactory.h @@ -0,0 +1,46 @@ +/* + * + * 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 _ConnectionInputHandlerFactory_ +#define _ConnectionInputHandlerFactory_ + +#include + +namespace qpid { +namespace sys { + +class ConnectionOutputHandler; +class ConnectionInputHandler; + +/** + * Callback interface used by the Acceptor to + * create a ConnectionInputHandler for each new connection. + */ +class ConnectionInputHandlerFactory : private boost::noncopyable +{ + public: + virtual ConnectionInputHandler* create(ConnectionOutputHandler* ctxt) = 0; + virtual ~ConnectionInputHandlerFactory(){} +}; + +}} + + +#endif diff --git a/cpp/src/qpid/sys/ConnectionOutputHandler.h b/cpp/src/qpid/sys/ConnectionOutputHandler.h new file mode 100644 index 0000000000..8436bea599 --- /dev/null +++ b/cpp/src/qpid/sys/ConnectionOutputHandler.h @@ -0,0 +1,41 @@ +/* + * + * 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 _ConnectionOutputHandler_ +#define _ConnectionOutputHandler_ + +#include "qpid/framing/OutputHandler.h" + +namespace qpid { +namespace sys { + +/** + * Provides the output handler associated with a connection. + */ +class ConnectionOutputHandler : public virtual qpid::framing::OutputHandler +{ + public: + virtual void close() = 0; +}; + +}} + + +#endif diff --git a/cpp/src/qpid/sys/Module.h b/cpp/src/qpid/sys/Module.h new file mode 100644 index 0000000000..cb31018fdc --- /dev/null +++ b/cpp/src/qpid/sys/Module.h @@ -0,0 +1,161 @@ +#ifndef _sys_Module_h +#define _sys_Module_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 +#include +#include "qpid/QpidError.h" + +namespace qpid { +namespace sys { +#if USE_APR +#include + typedef apr_dso_handle_t* dso_handle_t; +#else + typedef void* dso_handle_t; +#endif + + template class Module : private boost::noncopyable + { + typedef T* create_t(); + typedef void destroy_t(T*); + + dso_handle_t handle; + destroy_t* destroy; + T* ptr; + + void load(const std::string& name); + void unload(); + void* getSymbol(const std::string& name); + + public: + Module(const std::string& name); + T* operator->(); + T* get(); + ~Module() throw(); + }; + +} +} + +using namespace qpid::sys; + +template Module::Module(const std::string& module) : destroy(0), ptr(0) +{ + load(module); + //TODO: need a better strategy for symbol names to allow multiple + //modules to be loaded without clashes... + + //Note: need the double cast to avoid errors in casting from void* to function pointer with -pedantic + create_t* create = reinterpret_cast(reinterpret_cast(getSymbol("create"))); + destroy = reinterpret_cast(reinterpret_cast(getSymbol("destroy"))); + ptr = create(); +} + +template T* Module::operator->() +{ + return ptr; +} + +template T* Module::get() +{ + return ptr; +} + +template Module::~Module() throw() +{ + try { + if (handle && ptr) { + destroy(ptr); + } + if (handle) unload(); + } catch (std::exception& e) { + std::cout << "Error while destroying module: " << e.what() << std::endl; + } + destroy = 0; + handle = 0; + ptr = 0; +} + +// APR ================================================================ +#if USE_APR + +#include "apr/APRBase.h" +#include "apr/APRPool.h" + +template void Module::load(const std::string& name) +{ + CHECK_APR_SUCCESS(apr_dso_load(&handle, name.c_str(), APRPool::get())); +} + +template void Module::unload() +{ + CHECK_APR_SUCCESS(apr_dso_unload(handle)); +} + +template void* Module::getSymbol(const std::string& name) +{ + apr_dso_handle_sym_t symbol; + CHECK_APR_SUCCESS(apr_dso_sym(&symbol, handle, name.c_str())); + return (void*) symbol; +} + +// POSIX================================================================ +#else + +#include + +template void Module::load(const std::string& name) +{ + dlerror(); + handle = dlopen(name.c_str(), RTLD_NOW); + const char* error = dlerror(); + if (error) { + THROW_QPID_ERROR(INTERNAL_ERROR, error); + } +} + +template void Module::unload() +{ + dlerror(); + dlclose(handle); + const char* error = dlerror(); + if (error) { + THROW_QPID_ERROR(INTERNAL_ERROR, error); + } +} + +template void* Module::getSymbol(const std::string& name) +{ + dlerror(); + void* sym = dlsym(handle, name.c_str()); + const char* error = dlerror(); + if (error) { + THROW_QPID_ERROR(INTERNAL_ERROR, error); + } + return sym; +} + +#endif //if USE_APR + +#endif //ifndef _sys_Module_h + diff --git a/cpp/src/qpid/sys/Monitor.h b/cpp/src/qpid/sys/Monitor.h new file mode 100644 index 0000000000..1b8ae1a527 --- /dev/null +++ b/cpp/src/qpid/sys/Monitor.h @@ -0,0 +1,55 @@ +#ifndef _sys_Monitor_h +#define _sys_Monitor_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 +#include "Condition.h" + +#ifdef USE_APR +#include +#endif + +namespace qpid { +namespace sys { + +/** + * A monitor is a condition variable and a mutex + */ +class Monitor : public Mutex, public Condition { + public: + using Condition::wait; + inline void wait(); + inline bool wait(const Time& absoluteTime); +}; + + +void Monitor::wait() { + Condition::wait(*this); +} + +bool Monitor::wait(const Time& absoluteTime) { + return Condition::wait(*this, absoluteTime); +} + +}} +#endif /*!_sys_Monitor_h*/ diff --git a/cpp/src/qpid/sys/Mutex.h b/cpp/src/qpid/sys/Mutex.h new file mode 100644 index 0000000000..825b519039 --- /dev/null +++ b/cpp/src/qpid/sys/Mutex.h @@ -0,0 +1,165 @@ +#ifndef _sys_Mutex_h +#define _sys_Mutex_h + +/* + * + * 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. + * + */ + +#ifdef USE_APR +#include +#include "apr/APRBase.h" +#include "apr/APRPool.h" +#else +#include +#include "posix/check.h" +#endif +#include + +namespace qpid { +namespace sys { + +class Condition; + +/** + * Scoped lock template: calls lock() in ctor, unlock() in dtor. + * L can be any class with lock() and unlock() functions. + */ +template +class ScopedLock +{ + public: + ScopedLock(L& l) : mutex(l) { l.lock(); } + ~ScopedLock() { mutex.unlock(); } + private: + L& mutex; +}; + +template +class ScopedUnlock +{ + public: + ScopedUnlock(L& l) : mutex(l) { l.unlock(); } + ~ScopedUnlock() { mutex.lock(); } + private: + L& mutex; +}; + +/** + * Mutex lock. + */ +class Mutex : private boost::noncopyable { + public: + typedef ScopedLock ScopedLock; + typedef ScopedUnlock ScopedUnlock; + + inline Mutex(); + inline ~Mutex(); + inline void lock(); + inline void unlock(); + inline void trylock(); + + protected: +#ifdef USE_APR + apr_thread_mutex_t* mutex; +#else + pthread_mutex_t mutex; +#endif + friend class Condition; +}; + +#ifdef USE_APR +// APR ================================================================ + +Mutex::Mutex() { + CHECK_APR_SUCCESS(apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_NESTED, APRPool::get())); +} + +Mutex::~Mutex(){ + CHECK_APR_SUCCESS(apr_thread_mutex_destroy(mutex)); +} + +void Mutex::lock() { + CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex)); +} +void Mutex::unlock() { + CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex)); +} + +void Mutex::trylock() { + CHECK_APR_SUCCESS(apr_thread_mutex_trylock(mutex)); +} + +#else +// POSIX ================================================================ + +/** + * PODMutex is a POD, can be static-initialized with + * PODMutex m = QPID_PODMUTEX_INITIALIZER + */ +struct PODMutex +{ + typedef ScopedLock ScopedLock; + + inline void lock(); + inline void unlock(); + inline void trylock(); + + // Must be public to be a POD: + pthread_mutex_t mutex; +}; + +#define QPID_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER } + + +void PODMutex::lock() { + QPID_POSIX_THROW_IF(pthread_mutex_lock(&mutex)); +} +void PODMutex::unlock() { + QPID_POSIX_THROW_IF(pthread_mutex_unlock(&mutex)); +} + +void PODMutex::trylock() { + QPID_POSIX_THROW_IF(pthread_mutex_trylock(&mutex)); +} + + +Mutex::Mutex() { + QPID_POSIX_THROW_IF(pthread_mutex_init(&mutex, 0)); +} + +Mutex::~Mutex(){ + QPID_POSIX_THROW_IF(pthread_mutex_destroy(&mutex)); +} + +void Mutex::lock() { + QPID_POSIX_THROW_IF(pthread_mutex_lock(&mutex)); +} +void Mutex::unlock() { + QPID_POSIX_THROW_IF(pthread_mutex_unlock(&mutex)); +} + +void Mutex::trylock() { + QPID_POSIX_THROW_IF(pthread_mutex_trylock(&mutex)); +} + +#endif // USE_APR + +}} + + + +#endif /*!_sys_Mutex_h*/ diff --git a/cpp/src/qpid/sys/ProducerConsumer.cpp b/cpp/src/qpid/sys/ProducerConsumer.cpp new file mode 100644 index 0000000000..56b4c0cdb9 --- /dev/null +++ b/cpp/src/qpid/sys/ProducerConsumer.cpp @@ -0,0 +1,141 @@ +/* + * + * 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/QpidError.h" +#include "ScopedIncrement.h" +#include "ProducerConsumer.h" + +namespace qpid { +namespace sys { + +// // ================ ProducerConsumer + +ProducerConsumer::ProducerConsumer(size_t init_items) + : items(init_items), waiters(0), shutdownFlag(false) +{} + +void ProducerConsumer::shutdown() { + Mutex::ScopedLock l(monitor); + shutdownFlag = true; + monitor.notifyAll(); + // Wait for waiting consumers to wake up. + while (waiters > 0) + monitor.wait(); +} + +size_t ProducerConsumer::available() const { + Mutex::ScopedLock l(monitor); + return items; +} + +size_t ProducerConsumer::consumers() const { + Mutex::ScopedLock l(monitor); + return waiters; +} + +// ================ Lock + +ProducerConsumer::Lock::Lock(ProducerConsumer& p) + : pc(p), lock(p.monitor), status(INCOMPLETE) {} + +bool ProducerConsumer::Lock::isOk() const { + return !pc.isShutdown() && status==INCOMPLETE; +} + +void ProducerConsumer::Lock::checkOk() const { + assert(!pc.isShutdown()); + assert(status == INCOMPLETE); +} + +ProducerConsumer::Lock::~Lock() { + assert(status != INCOMPLETE || pc.isShutdown()); +} + +void ProducerConsumer::Lock::confirm() { + checkOk(); + status = CONFIRMED; +} + +void ProducerConsumer::Lock::cancel() { + checkOk(); + status = CANCELLED; +} + +// ================ ProducerLock + +ProducerConsumer::ProducerLock::ProducerLock(ProducerConsumer& p) : Lock(p) +{} + + +ProducerConsumer::ProducerLock::~ProducerLock() { + if (status == CONFIRMED) { + pc.items++; + pc.monitor.notify(); // Notify a consumer. + } +} + +// ================ ConsumerLock + +ProducerConsumer::ConsumerLock::ConsumerLock(ProducerConsumer& p) : Lock(p) +{ + if (isOk()) { + ScopedIncrement inc(pc.waiters); + while (pc.items == 0 && !pc.shutdownFlag) { + pc.monitor.wait(); + } + } +} + +ProducerConsumer::ConsumerLock::ConsumerLock( + ProducerConsumer& p, const Time& timeout) : Lock(p) +{ + if (isOk()) { + // Don't wait if timeout==0 + if (timeout == 0) { + if (pc.items == 0) + status = TIMEOUT; + return; + } + else { + Time deadline = now() + timeout; + ScopedIncrement inc(pc.waiters); + while (pc.items == 0 && !pc.shutdownFlag) { + if (!pc.monitor.wait(deadline)) { + status = TIMEOUT; + return; + } + } + } + } +} + +ProducerConsumer::ConsumerLock::~ConsumerLock() { + if (pc.isShutdown()) { + if (pc.waiters == 0) + pc.monitor.notifyAll(); // Notify shutdown thread(s) + } + else if (status==CONFIRMED) { + pc.items--; + if (pc.items > 0) + pc.monitor.notify(); // Notify another consumer. + } +} + + +}} // namespace qpid::sys diff --git a/cpp/src/qpid/sys/ProducerConsumer.h b/cpp/src/qpid/sys/ProducerConsumer.h new file mode 100644 index 0000000000..fc6434ef48 --- /dev/null +++ b/cpp/src/qpid/sys/ProducerConsumer.h @@ -0,0 +1,165 @@ +#ifndef _sys_ProducerConsumer_h +#define _sys_ProducerConsumer_h + +/* + * + * 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 +#include "qpid/Exception.h" +#include "Monitor.h" + +namespace qpid { +namespace sys { + +/** + * Producer-consumer synchronisation. + * + * Producers increase the number of available items, consumers reduce it. + * Consumers wait till an item is available. Waiting threads can be + * woken for shutdown using shutdown(). + * + * Note: Currently implements unbounded producer-consumer, i.e. no limit + * to available items, producers never block. Can be extended to support + * bounded PC if required. + * + // TODO aconway 2007-02-13: example, from tests. +*/ +class ProducerConsumer +{ + public: + ProducerConsumer(size_t init_items=0); + + ~ProducerConsumer() { shutdown(); } + + /** + * Wake any threads waiting for ProducerLock or ConsumerLock. + *@post No threads are waiting in Producer or Consumer locks. + */ + void shutdown(); + + /** True if queue is shutdown */ + bool isShutdown() { return shutdownFlag; } + + /** Number of items available for consumers */ + size_t available() const; + + /** Number of consumers waiting for items */ + size_t consumers() const; + + /** True if available == 0 */ + bool empty() const { return available() == 0; } + + /** + * Base class for producer and consumer locks. + */ + class Lock : private boost::noncopyable { + public: + + /** + * You must call isOk() after creating a lock to verify its state. + * + *@return true means the lock succeeded. You MUST call either + *confirm() or cancel() before the lock goes out of scope. + * + * false means the lock failed - timed out or the + * ProducerConsumer is shutdown. You should not do anything in + * the scope of the lock. + */ + bool isOk() const; + + /** + * Confirm that an item was produced/consumed. + *@pre isOk() + */ + void confirm(); + + /** + * Cancel the lock to indicate nothing was produced/consumed. + * Note that locks are not actually released until destroyed. + * + *@pre isOk() + */ + void cancel(); + + /** True if this lock experienced a timeout */ + bool isTimedOut() const { return status == TIMEOUT; } + + /** True if we have been shutdown */ + bool isShutdown() const { return pc.isShutdown(); } + + ProducerConsumer& pc; + + protected: + /** Lock status */ + enum Status { INCOMPLETE, CONFIRMED, CANCELLED, TIMEOUT }; + + Lock(ProducerConsumer& p); + ~Lock(); + void checkOk() const; + Mutex::ScopedLock lock; + Status status; + }; + + /** Lock for code that produces items. */ + struct ProducerLock : public Lock { + /** + * Acquire locks to produce an item. + *@post If isOk() the calling thread has exclusive access + * to produce an item. + */ + ProducerLock(ProducerConsumer& p); + + /** Release locks, signal waiting consumers if confirm() was called. */ + ~ProducerLock(); + }; + + /** Lock for code that consumes items */ + struct ConsumerLock : public Lock { + /** + * Wait for an item to consume and acquire locks. + * + *@post If isOk() there is at least one item available and the + *calling thread has exclusive access to consume it. + */ + ConsumerLock(ProducerConsumer& p); + + /** + * Wait up to timeout to acquire lock. + *@post If isOk() caller has a producer lock. + * If isTimedOut() there was a timeout. + * If neither then we were shutdown. + */ + ConsumerLock(ProducerConsumer& p, const Time& timeout); + + /** Release locks */ + ~ConsumerLock(); + }; + + private: + mutable Monitor monitor; + size_t items; + size_t waiters; + bool shutdownFlag; + + friend class Lock; + friend class ProducerLock; + friend class ConsumerLock; +}; + +}} // namespace qpid::sys + +#endif /*!_sys_ProducerConsumer_h*/ diff --git a/cpp/src/qpid/sys/Runnable.cpp b/cpp/src/qpid/sys/Runnable.cpp new file mode 100644 index 0000000000..30122c682f --- /dev/null +++ b/cpp/src/qpid/sys/Runnable.cpp @@ -0,0 +1,32 @@ +/* + * + * 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 "Runnable.h" +#include + +namespace qpid { +namespace sys { + +Runnable::~Runnable() {} + +Runnable::Functor Runnable::functor() +{ + return boost::bind(&Runnable::run, this); +} + +}} diff --git a/cpp/src/qpid/sys/Runnable.h b/cpp/src/qpid/sys/Runnable.h new file mode 100644 index 0000000000..fb3927c612 --- /dev/null +++ b/cpp/src/qpid/sys/Runnable.h @@ -0,0 +1,50 @@ +#ifndef _Runnable_ +#define _Runnable_ +/* + * + * 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 + +namespace qpid { +namespace sys { + +/** + * Interface for objects that can be run, e.g. in a thread. + */ +class Runnable +{ + public: + /** Type to represent a runnable as a Functor */ + typedef boost::function0 Functor; + + virtual ~Runnable(); + + /** Derived classes override run(). */ + virtual void run() = 0; + + /** Create a functor object that will call this->run(). */ + Functor functor(); +}; + +}} + + +#endif diff --git a/cpp/src/qpid/sys/ScopedIncrement.h b/cpp/src/qpid/sys/ScopedIncrement.h new file mode 100644 index 0000000000..f14461ddaf --- /dev/null +++ b/cpp/src/qpid/sys/ScopedIncrement.h @@ -0,0 +1,59 @@ +#ifndef _posix_ScopedIncrement_h +#define _posix_ScopedIncrement_h + +/* + * + * 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 + +namespace qpid { +namespace sys { + +/** Increment counter in constructor and decrement in destructor. */ +template +class ScopedIncrement : boost::noncopyable +{ + public: + ScopedIncrement(T& c) : count(c) { ++count; } + ~ScopedIncrement() { --count; } + private: + T& count; +}; + + +/** Decrement counter in constructor and increment in destructor. */ +template +class ScopedDecrement : boost::noncopyable +{ + public: + ScopedDecrement(T& c) : count(c) { value = --count; } + ~ScopedDecrement() { ++count; } + + /** Return the value after the decrement. */ + operator long() { return value; } + + private: + T& count; + long value; +}; + + +}} + + +#endif // _posix_ScopedIncrement_h diff --git a/cpp/src/qpid/sys/ShutdownHandler.h b/cpp/src/qpid/sys/ShutdownHandler.h new file mode 100644 index 0000000000..88baecb5b6 --- /dev/null +++ b/cpp/src/qpid/sys/ShutdownHandler.h @@ -0,0 +1,37 @@ +/* + * + * 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 _ShutdownHandler_ +#define _ShutdownHandler_ + +namespace qpid { +namespace sys { + + class ShutdownHandler + { + public: + virtual void shutdown() = 0; + virtual ~ShutdownHandler(){} + }; + +} +} + +#endif diff --git a/cpp/src/qpid/sys/Socket.h b/cpp/src/qpid/sys/Socket.h new file mode 100644 index 0000000000..ea03222aed --- /dev/null +++ b/cpp/src/qpid/sys/Socket.h @@ -0,0 +1,88 @@ +#ifndef _sys_Socket_h +#define _sys_Socket_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 +#include "Time.h" + +#ifdef USE_APR +#include +#endif + +namespace qpid { +namespace sys { + +class Socket +{ + public: + /** Create an initialized TCP socket */ + static Socket createTcp(); + + /** Create a socket wrapper for descriptor. */ +#ifdef USE_APR + Socket(apr_socket_t* descriptor = 0); +#else + Socket(int descriptor = 0); +#endif + + /** Set timeout for read and write */ + void setTimeout(Time interval); + + void connect(const std::string& host, int port); + + void close(); + + enum { SOCKET_TIMEOUT=-2, SOCKET_EOF=-3 } ErrorCode; + + /** Returns bytes sent or an ErrorCode value < 0. */ + ssize_t send(const void* data, size_t size); + + /** + * Returns bytes received, an ErrorCode value < 0 or 0 + * if the connection closed in an orderly manner. + */ + ssize_t recv(void* data, size_t size); + + /** Bind to a port and start listening. + *@param port 0 means choose an available port. + *@param backlog maximum number of pending connections. + *@return The bound port. + */ + int listen(int port = 0, int backlog = 10); + + /** Get file descriptor */ + int fd(); + + private: +#ifdef USE_APR + apr_socket_t* socket; +#else + void init() const; + mutable int socket; // Initialized on demand. +#endif +}; + +}} + + +#endif /*!_sys_Socket_h*/ diff --git a/cpp/src/qpid/sys/Thread.h b/cpp/src/qpid/sys/Thread.h new file mode 100644 index 0000000000..e52f2a1b3e --- /dev/null +++ b/cpp/src/qpid/sys/Thread.h @@ -0,0 +1,142 @@ +#ifndef _sys_Thread_h +#define _sys_Thread_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 "Runnable.h" + +#ifdef USE_APR +#include +#include +#include "apr/APRPool.h" +#include "apr/APRBase.h" +#else +#include "posix/check.h" +#include +#endif + +namespace qpid { +namespace sys { + +class Thread +{ + public: + inline static Thread current(); + inline static void yield(); + + inline Thread(); + inline explicit Thread(qpid::sys::Runnable*); + inline explicit Thread(qpid::sys::Runnable&); + + inline void join(); + + inline long id(); + + private: +#ifdef USE_APR + static void* APR_THREAD_FUNC runRunnable(apr_thread_t* thread, void *data); + inline Thread(apr_thread_t* t); + apr_thread_t* thread; +#else + static void* runRunnable(void* runnable); + inline Thread(pthread_t); + pthread_t thread; +#endif +}; + + +Thread::Thread() : thread(0) {} + +// APR ================================================================ +#ifdef USE_APR + +Thread::Thread(Runnable* runnable) { + CHECK_APR_SUCCESS( + apr_thread_create(&thread, 0, runRunnable, runnable, APRPool::get())); +} + +Thread::Thread(Runnable& runnable) { + CHECK_APR_SUCCESS( + apr_thread_create(&thread, 0, runRunnable, &runnable, APRPool::get())); +} + +void Thread::join(){ + apr_status_t status; + if (thread != 0) + CHECK_APR_SUCCESS(apr_thread_join(&status, thread)); +} + +long Thread::id() { + return long(thread); +} + +Thread::Thread(apr_thread_t* t) : thread(t) {} + +Thread Thread::current(){ + apr_thread_t* thr; + apr_os_thread_t osthr = apr_os_thread_current(); + CHECK_APR_SUCCESS(apr_os_thread_put(&thr, &osthr, APRPool::get())); + return Thread(thr); +} + +void Thread::yield() +{ + apr_thread_yield(); +} + + +// POSIX ================================================================ +#else + +Thread::Thread(Runnable* runnable) { + QPID_POSIX_THROW_IF(pthread_create(&thread, NULL, runRunnable, runnable)); +} + +Thread::Thread(Runnable& runnable) { + QPID_POSIX_THROW_IF(pthread_create(&thread, NULL, runRunnable, &runnable)); +} + +void Thread::join(){ + QPID_POSIX_THROW_IF(pthread_join(thread, 0)); +} + +long Thread::id() { + return long(thread); +} + +Thread::Thread(pthread_t thr) : thread(thr) {} + +Thread Thread::current() { + return Thread(pthread_self()); +} + +void Thread::yield() +{ + QPID_POSIX_THROW_IF(pthread_yield()); +} + + +#endif + +}} + +#endif /*!_sys_Thread_h*/ diff --git a/cpp/src/qpid/sys/ThreadSafeQueue.h b/cpp/src/qpid/sys/ThreadSafeQueue.h new file mode 100644 index 0000000000..8f11c42051 --- /dev/null +++ b/cpp/src/qpid/sys/ThreadSafeQueue.h @@ -0,0 +1,98 @@ +#ifndef _sys_ThreadSafeQueue_h +#define _sys_ThreadSafeQueue_h + +/* + * + * 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 +#include "ProducerConsumer.h" +#include "qpid/Exception.h" + +namespace qpid { +namespace sys { + +/** + * A thread safe queue template. + */ +template > +class ThreadSafeQueue +{ + public: + + ThreadSafeQueue() {} + + /** Push a value onto the back of the queue */ + void push(const T& value) { + ProducerConsumer::ProducerLock producer(pc); + if (producer.isOk()) { + producer.confirm(); + container.push_back(value); + } + } + + /** Pop a value from the front of the queue. Waits till value is available. + *@throw ShutdownException if queue is shutdown while waiting. + */ + T pop() { + ProducerConsumer::ConsumerLock consumer(pc); + if (consumer.isOk()) { + consumer.confirm(); + T value(container.front()); + container.pop_front(); + return value; + } + throw ShutdownException(); + } + + /** + * If a value becomes available within the timeout, set outValue + * and return true. Otherwise return false; + */ + bool pop(T& outValue, const Time& timeout) { + ProducerConsumer::ConsumerLock consumer(pc, timeout); + if (consumer.isOk()) { + consumer.confirm(); + outValue = container.front(); + container.pop_front(); + return true; + } + return false; + } + + /** Interrupt threads waiting in pop() */ + void shutdown() { pc.shutdown(); } + + /** True if queue is shutdown */ + bool isShutdown() { return pc.isShutdown(); } + + /** Size of the queue */ + size_t size() { ProducerConsumer::Lock l(pc); return container.size(); } + + /** True if queue is empty */ + bool empty() { ProducerConsumer::Lock l(pc); return container.empty(); } + + private: + ProducerConsumer pc; + ContainerType container; +}; + +}} // namespace qpid::sys + + + +#endif /*!_sys_ThreadSafeQueue_h*/ diff --git a/cpp/src/qpid/sys/Time.cpp b/cpp/src/qpid/sys/Time.cpp new file mode 100644 index 0000000000..ad6185b966 --- /dev/null +++ b/cpp/src/qpid/sys/Time.cpp @@ -0,0 +1,60 @@ +/* + * + * 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 "Time.h" + +namespace qpid { +namespace sys { + +// APR ================================================================ +#if USE_APR + +Time now() { return apr_time_now() * TIME_USEC; } + +// POSIX================================================================ +#else + +Time now() { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + return toTime(ts); +} + +struct timespec toTimespec(const Time& t) { + struct timespec ts; + toTimespec(ts, t); + return ts; +} + +struct timespec& toTimespec(struct timespec& ts, const Time& t) { + ts.tv_sec = t / TIME_SEC; + ts.tv_nsec = t % TIME_SEC; + return ts; +} + +Time toTime(const struct timespec& ts) { + return ts.tv_sec*TIME_SEC + ts.tv_nsec; +} + + +#endif +}} + diff --git a/cpp/src/qpid/sys/Time.h b/cpp/src/qpid/sys/Time.h new file mode 100644 index 0000000000..47609d51df --- /dev/null +++ b/cpp/src/qpid/sys/Time.h @@ -0,0 +1,58 @@ +#ifndef _sys_Time_h +#define _sys_Time_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 + +#ifdef USE_APR +#include +#else +#include +#endif + +namespace qpid { +namespace sys { + +/** Time in nanoseconds */ +typedef int64_t Time; + +Time now(); + +/** Nanoseconds per second. */ +const Time TIME_SEC = 1000*1000*1000; +/** Nanoseconds per millisecond */ +const Time TIME_MSEC = 1000*1000; +/** Nanoseconds per microseconds. */ +const Time TIME_USEC = 1000; +/** Nanoseconds per nanosecond. */ +const Time TIME_NSEC = 1; + +#ifndef USE_APR +struct timespec toTimespec(const Time& t); +struct timespec& toTimespec(struct timespec& ts, const Time& t); +Time toTime(const struct timespec& ts); +#endif + +}} + +#endif /*!_sys_Time_h*/ diff --git a/cpp/src/qpid/sys/TimeoutHandler.h b/cpp/src/qpid/sys/TimeoutHandler.h new file mode 100644 index 0000000000..0c10709bbf --- /dev/null +++ b/cpp/src/qpid/sys/TimeoutHandler.h @@ -0,0 +1,39 @@ +/* + * + * 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 _TimeoutHandler_ +#define _TimeoutHandler_ + +namespace qpid { +namespace sys { + + class TimeoutHandler + { + public: + virtual void idleOut() = 0; + virtual void idleIn() = 0; + virtual ~TimeoutHandler(){} + }; + +} +} + + +#endif diff --git a/cpp/src/qpid/sys/apr/APRAcceptor.cpp b/cpp/src/qpid/sys/apr/APRAcceptor.cpp new file mode 100644 index 0000000000..dc021d2a3f --- /dev/null +++ b/cpp/src/qpid/sys/apr/APRAcceptor.cpp @@ -0,0 +1,120 @@ +/* + * + * 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/sys/Acceptor.h" +#include "qpid/sys/ConnectionInputHandlerFactory.h" +#include "LFProcessor.h" +#include "LFSessionContext.h" +#include "APRBase.h" +#include "APRPool.h" + +namespace qpid { +namespace sys { + +class APRAcceptor : public Acceptor +{ + public: + APRAcceptor(int16_t port, int backlog, int threads, bool trace); + virtual uint16_t getPort() const; + virtual void run(qpid::sys::ConnectionInputHandlerFactory* factory); + virtual void shutdown(); + + private: + void shutdownImpl(); + + private: + int16_t port; + bool trace; + LFProcessor processor; + apr_socket_t* socket; + volatile bool running; + Mutex shutdownLock; +}; + +// Define generic Acceptor::create() to return APRAcceptor. +Acceptor::shared_ptr Acceptor::create(int16_t port, int backlog, int threads, bool trace) +{ + return Acceptor::shared_ptr(new APRAcceptor(port, backlog, threads, trace)); +} +// Must define Acceptor virtual dtor. +Acceptor::~Acceptor() {} + +APRAcceptor::APRAcceptor(int16_t port_, int backlog, int threads, bool trace_) : + port(port_), + trace(trace_), + processor(APRPool::get(), threads, 1000, 5000000), + running(false) +{ + apr_sockaddr_t* address; + CHECK_APR_SUCCESS(apr_sockaddr_info_get(&address, APR_ANYADDR, APR_UNSPEC, port, APR_IPV4_ADDR_OK, APRPool::get())); + CHECK_APR_SUCCESS(apr_socket_create(&socket, APR_INET, SOCK_STREAM, APR_PROTO_TCP, APRPool::get())); + CHECK_APR_SUCCESS(apr_socket_opt_set(socket, APR_SO_REUSEADDR, 1)); + CHECK_APR_SUCCESS(apr_socket_bind(socket, address)); + CHECK_APR_SUCCESS(apr_socket_listen(socket, backlog)); +} + +uint16_t APRAcceptor::getPort() const { + apr_sockaddr_t* address; + CHECK_APR_SUCCESS(apr_socket_addr_get(&address, APR_LOCAL, socket)); + return address->port; +} + +void APRAcceptor::run(ConnectionInputHandlerFactory* factory) { + running = true; + processor.start(); + std::cout << "Listening on port " << getPort() << "..." << std::endl; + while(running) { + apr_socket_t* client; + apr_status_t status = apr_socket_accept(&client, socket, APRPool::get()); + if(status == APR_SUCCESS){ + //make this socket non-blocking: + CHECK_APR_SUCCESS(apr_socket_timeout_set(client, 0)); + CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_SO_NONBLOCK, 1)); + CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_TCP_NODELAY, 1)); + CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_SO_SNDBUF, 32768)); + CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_SO_RCVBUF, 32768)); + LFSessionContext* session = new LFSessionContext(APRPool::get(), client, &processor, trace); + session->init(factory->create(session)); + }else{ + Mutex::ScopedLock locker(shutdownLock); + if(running) { + if(status != APR_EINTR){ + std::cout << "ERROR: " << get_desc(status) << std::endl; + } + shutdownImpl(); + } + } + } +} + +void APRAcceptor::shutdown() { + Mutex::ScopedLock locker(shutdownLock); + if (running) + shutdownImpl(); +} + +void APRAcceptor::shutdownImpl() { + running = false; + processor.stop(); + CHECK_APR_SUCCESS(apr_socket_close(socket)); +} + + +}} diff --git a/cpp/src/qpid/sys/apr/APRBase.cpp b/cpp/src/qpid/sys/apr/APRBase.cpp new file mode 100644 index 0000000000..ba7dfc08d4 --- /dev/null +++ b/cpp/src/qpid/sys/apr/APRBase.cpp @@ -0,0 +1,90 @@ +/* + * + * 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 +#include "qpid/QpidError.h" +#include "APRBase.h" + +using namespace qpid::sys; + +APRBase* APRBase::instance = 0; + +APRBase* APRBase::getInstance(){ + if(instance == 0){ + instance = new APRBase(); + } + return instance; +} + + +APRBase::APRBase() : count(0){ + apr_initialize(); + CHECK_APR_SUCCESS(apr_pool_create(&pool, 0)); + CHECK_APR_SUCCESS(apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_NESTED, pool)); +} + +APRBase::~APRBase(){ + CHECK_APR_SUCCESS(apr_thread_mutex_destroy(mutex)); + apr_pool_destroy(pool); + apr_terminate(); +} + +bool APRBase::_increment(){ + bool deleted(false); + CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex)); + if(this == instance){ + count++; + }else{ + deleted = true; + } + CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex)); + return !deleted; +} + +void APRBase::_decrement(){ + APRBase* copy = 0; + CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex)); + if(--count == 0){ + copy = instance; + instance = 0; + } + CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex)); + if(copy != 0){ + delete copy; + } +} + +void APRBase::increment(){ + int count = 0; + while(count++ < 2 && !getInstance()->_increment()){ + std::cout << "WARNING: APR initialization triggered concurrently with termination." << std::endl; + } +} + +void APRBase::decrement(){ + getInstance()->_decrement(); +} + +std::string qpid::sys::get_desc(apr_status_t status){ + const int size = 50; + char tmp[size]; + return std::string(apr_strerror(status, tmp, size)); +} + diff --git a/cpp/src/qpid/sys/apr/APRBase.h b/cpp/src/qpid/sys/apr/APRBase.h new file mode 100644 index 0000000000..c6b1854fb1 --- /dev/null +++ b/cpp/src/qpid/sys/apr/APRBase.h @@ -0,0 +1,78 @@ +/* + * + * 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 _APRBase_ +#define _APRBase_ + +#include +#include +#include +#include "qpid/QpidError.h" + +namespace qpid { +namespace sys { + + /** + * Use of APR libraries necessitates explicit init and terminate + * calls. Any class using APR libs should obtain the reference to + * this singleton and increment on construction, decrement on + * destruction. This class can then correctly initialise apr + * before the first use and terminate after the last use. + */ + class APRBase{ + static APRBase* instance; + apr_pool_t* pool; + apr_thread_mutex_t* mutex; + int count; + + APRBase(); + ~APRBase(); + static APRBase* getInstance(); + bool _increment(); + void _decrement(); + public: + static void increment(); + static void decrement(); + }; + + //this is also a convenient place for a helper function for error checking: + void check(apr_status_t status, const char* file, const int line); + std::string get_desc(apr_status_t status); + +#define CHECK_APR_SUCCESS(A) qpid::sys::check(A, __FILE__, __LINE__); + +} +} + +// Inlined as it is called *a lot* +void inline qpid::sys::check(apr_status_t status, const char* file, const int line){ + if (status != APR_SUCCESS){ + const int size = 50; + char tmp[size]; + std::string msg(apr_strerror(status, tmp, size)); + throw qpid::QpidError(APR_ERROR + ((int) status), msg, + qpid::SrcLine(file, line)); + } +} + + + + +#endif diff --git a/cpp/src/qpid/sys/apr/APRPool.cpp b/cpp/src/qpid/sys/apr/APRPool.cpp new file mode 100644 index 0000000000..e8b71f6e8a --- /dev/null +++ b/cpp/src/qpid/sys/apr/APRPool.cpp @@ -0,0 +1,41 @@ +/* + * + * 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 "APRPool.h" +#include "APRBase.h" +#include + +using namespace qpid::sys; + +APRPool::APRPool(){ + APRBase::increment(); + CHECK_APR_SUCCESS(apr_pool_create(&pool, NULL)); +} + +APRPool::~APRPool(){ + apr_pool_destroy(pool); + APRBase::decrement(); +} + +apr_pool_t* APRPool::get() { + return boost::details::pool::singleton_default::instance().pool; +} + diff --git a/cpp/src/qpid/sys/apr/APRPool.h b/cpp/src/qpid/sys/apr/APRPool.h new file mode 100644 index 0000000000..da7661fcfa --- /dev/null +++ b/cpp/src/qpid/sys/apr/APRPool.h @@ -0,0 +1,50 @@ +#ifndef _APRPool_ +#define _APRPool_ + +/* + * + * 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 +#include + +namespace qpid { +namespace sys { +/** + * Singleton APR memory pool. + */ +class APRPool : private boost::noncopyable { + public: + APRPool(); + ~APRPool(); + + /** Get singleton instance */ + static apr_pool_t* get(); + + private: + apr_pool_t* pool; +}; + +}} + + + + + +#endif /*!_APRPool_*/ diff --git a/cpp/src/qpid/sys/apr/APRSocket.cpp b/cpp/src/qpid/sys/apr/APRSocket.cpp new file mode 100644 index 0000000000..96dbd132a1 --- /dev/null +++ b/cpp/src/qpid/sys/apr/APRSocket.cpp @@ -0,0 +1,78 @@ +/* + * + * 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 "APRBase.h" +#include "APRSocket.h" +#include +#include + +using namespace qpid::sys; +using namespace qpid::framing; + +APRSocket::APRSocket(apr_socket_t* _socket) : socket(_socket), closed(false){ + +} + +void APRSocket::read(qpid::framing::Buffer& buffer){ + apr_size_t bytes; + bytes = buffer.available(); + apr_status_t s = apr_socket_recv(socket, buffer.start(), &bytes); + buffer.move(bytes); + if(APR_STATUS_IS_TIMEUP(s)){ + //timed out + }else if(APR_STATUS_IS_EOF(s)){ + close(); + } +} + +void APRSocket::write(qpid::framing::Buffer& buffer){ + apr_size_t bytes; + do{ + bytes = buffer.available(); + apr_socket_send(socket, buffer.start(), &bytes); + buffer.move(bytes); + }while(bytes > 0); +} + +void APRSocket::close(){ + if(!closed){ + std::cout << "Closing socket " << socket << "@" << this << std::endl; + CHECK_APR_SUCCESS(apr_socket_close(socket)); + closed = true; + } +} + +bool APRSocket::isOpen() const { + return !closed; +} + +uint8_t APRSocket::read(){ + char data[1]; + apr_size_t bytes = 1; + apr_status_t s = apr_socket_recv(socket, data, &bytes); + if(APR_STATUS_IS_EOF(s) || bytes == 0){ + return 0; + }else{ + return *data; + } +} + +APRSocket::~APRSocket(){ +} diff --git a/cpp/src/qpid/sys/apr/APRSocket.h b/cpp/src/qpid/sys/apr/APRSocket.h new file mode 100644 index 0000000000..30a3be2746 --- /dev/null +++ b/cpp/src/qpid/sys/apr/APRSocket.h @@ -0,0 +1,48 @@ +/* + * + * 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 _APRSocket_ +#define _APRSocket_ + +#include +#include "qpid/framing/Buffer.h" + +namespace qpid { +namespace sys { + + class APRSocket + { + apr_socket_t* const socket; + volatile bool closed; + public: + APRSocket(apr_socket_t* socket); + void read(qpid::framing::Buffer& b); + void write(qpid::framing::Buffer& b); + void close(); + bool isOpen() const; + uint8_t read(); + ~APRSocket(); + }; + +} +} + + +#endif diff --git a/cpp/src/qpid/sys/apr/LFProcessor.cpp b/cpp/src/qpid/sys/apr/LFProcessor.cpp new file mode 100644 index 0000000000..0d8ac425fe --- /dev/null +++ b/cpp/src/qpid/sys/apr/LFProcessor.cpp @@ -0,0 +1,179 @@ +/* + * + * 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 +#include "qpid/QpidError.h" +#include "LFProcessor.h" +#include "APRBase.h" +#include "LFSessionContext.h" + +using namespace qpid::sys; +using qpid::QpidError; + +// TODO aconway 2006-10-12: stopped is read outside locks. +// + +LFProcessor::LFProcessor(apr_pool_t* pool, int _workers, int _size, int _timeout) : + size(_size), + timeout(_timeout), + signalledCount(0), + current(0), + count(0), + workerCount(_workers), + hasLeader(false), + workers(new Thread[_workers]), + stopped(false) +{ + + CHECK_APR_SUCCESS(apr_pollset_create(&pollset, size, pool, APR_POLLSET_THREADSAFE)); +} + + +LFProcessor::~LFProcessor(){ + if (!stopped) stop(); + delete[] workers; + CHECK_APR_SUCCESS(apr_pollset_destroy(pollset)); +} + +void LFProcessor::start(){ + for(int i = 0; i < workerCount; i++){ + workers[i] = Thread(this); + } +} + +void LFProcessor::add(const apr_pollfd_t* const fd){ + CHECK_APR_SUCCESS(apr_pollset_add(pollset, fd)); + Monitor::ScopedLock l(countLock); + sessions.push_back(reinterpret_cast(fd->client_data)); + count++; +} + +void LFProcessor::remove(const apr_pollfd_t* const fd){ + CHECK_APR_SUCCESS(apr_pollset_remove(pollset, fd)); + Monitor::ScopedLock l(countLock); + sessions.erase(find(sessions.begin(), sessions.end(), reinterpret_cast(fd->client_data))); + count--; +} + +void LFProcessor::reactivate(const apr_pollfd_t* const fd){ + CHECK_APR_SUCCESS(apr_pollset_add(pollset, fd)); +} + +void LFProcessor::deactivate(const apr_pollfd_t* const fd){ + CHECK_APR_SUCCESS(apr_pollset_remove(pollset, fd)); +} + +void LFProcessor::update(const apr_pollfd_t* const fd){ + CHECK_APR_SUCCESS(apr_pollset_remove(pollset, fd)); + CHECK_APR_SUCCESS(apr_pollset_add(pollset, fd)); +} + +bool LFProcessor::full(){ + Mutex::ScopedLock locker(countLock); + return count == size; +} + +bool LFProcessor::empty(){ + Mutex::ScopedLock locker(countLock); + return count == 0; +} + +void LFProcessor::poll() { + apr_status_t status = APR_EGENERAL; + do{ + current = 0; + if(!stopped){ + status = apr_pollset_poll(pollset, timeout, &signalledCount, &signalledFDs); + } + }while(status != APR_SUCCESS && !stopped); +} + +void LFProcessor::run(){ + try{ + while(!stopped){ + const apr_pollfd_t* event = 0; + LFSessionContext* session = 0; + { + Monitor::ScopedLock l(leadLock); + waitToLead(); + event = getNextEvent(); + if(!event) return; + session = reinterpret_cast( + event->client_data); + session->startProcessing(); + relinquishLead(); + } + + //process event: + if(event->rtnevents & APR_POLLIN) session->read(); + if(event->rtnevents & APR_POLLOUT) session->write(); + + if(session->isClosed()){ + session->handleClose(); + Monitor::ScopedLock l(countLock); + sessions.erase(find(sessions.begin(),sessions.end(), session)); + count--; + }else{ + session->stopProcessing(); + } + } + }catch(std::exception e){ + std::cout << e.what() << std::endl; + } +} + +void LFProcessor::waitToLead(){ + while(hasLeader && !stopped) leadLock.wait(); + hasLeader = !stopped; +} + +void LFProcessor::relinquishLead(){ + hasLeader = false; + leadLock.notify(); +} + +const apr_pollfd_t* LFProcessor::getNextEvent(){ + while(true){ + if(stopped){ + return 0; + }else if(current < signalledCount){ + //use result of previous poll if one is available + return signalledFDs + (current++); + }else{ + //else poll to get new events + poll(); + } + } +} + +void LFProcessor::stop(){ + stopped = true; + { + Monitor::ScopedLock l(leadLock); + leadLock.notifyAll(); + } + for(int i = 0; i < workerCount; i++){ + workers[i].join(); + } + for(iterator i = sessions.begin(); i < sessions.end(); i++){ + (*i)->shutdown(); + } +} + diff --git a/cpp/src/qpid/sys/apr/LFProcessor.h b/cpp/src/qpid/sys/apr/LFProcessor.h new file mode 100644 index 0000000000..38d0b2787f --- /dev/null +++ b/cpp/src/qpid/sys/apr/LFProcessor.h @@ -0,0 +1,121 @@ +/* + * + * 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 _LFProcessor_ +#define _LFProcessor_ + +#include +#include +#include +#include "qpid/sys/Monitor.h" +#include "qpid/sys/Runnable.h" +#include "qpid/sys/Thread.h" + +namespace qpid { +namespace sys { + + class LFSessionContext; + + /** + * This class processes a poll set using the leaders-followers + * pattern for thread synchronization: the leader will poll and on + * the poll returning, it will remove a session, promote a + * follower to leadership, then process the session. + */ + class LFProcessor : private virtual qpid::sys::Runnable + { + typedef std::vector::iterator iterator; + + const int size; + const apr_interval_time_t timeout; + apr_pollset_t* pollset; + int signalledCount; + int current; + const apr_pollfd_t* signalledFDs; + int count; + const int workerCount; + bool hasLeader; + qpid::sys::Thread* workers; + qpid::sys::Monitor leadLock; + qpid::sys::Mutex countLock; + std::vector sessions; + volatile bool stopped; + + const apr_pollfd_t* getNextEvent(); + void waitToLead(); + void relinquishLead(); + void poll(); + virtual void run(); + + public: + LFProcessor(apr_pool_t* pool, int workers, int size, int timeout); + /** + * Add the fd to the poll set. Relies on the client_data being + * an instance of LFSessionContext. + */ + void add(const apr_pollfd_t* const fd); + /** + * Remove the fd from the poll set. + */ + void remove(const apr_pollfd_t* const fd); + /** + * Signal that the fd passed in, already part of the pollset, + * has had its flags altered. + */ + void update(const apr_pollfd_t* const fd); + /** + * Add an fd back to the poll set after deactivation. + */ + void reactivate(const apr_pollfd_t* const fd); + /** + * Temporarily remove the fd from the poll set. Called when processing + * is about to begin. + */ + void deactivate(const apr_pollfd_t* const fd); + /** + * Indicates whether the capacity of this processor has been + * reached (or whether it can still handle further fd's). + */ + bool full(); + /** + * Indicates whether there are any fd's registered. + */ + bool empty(); + /** + * Stop processing. + */ + void stop(); + /** + * Start processing. + */ + void start(); + /** + * Is processing stopped? + */ + bool isStopped(); + + ~LFProcessor(); + }; + +} +} + + +#endif diff --git a/cpp/src/qpid/sys/apr/LFSessionContext.cpp b/cpp/src/qpid/sys/apr/LFSessionContext.cpp new file mode 100644 index 0000000000..4f5fcb269e --- /dev/null +++ b/cpp/src/qpid/sys/apr/LFSessionContext.cpp @@ -0,0 +1,179 @@ +/* + * + * 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 "LFSessionContext.h" +#include "APRBase.h" +#include "qpid/QpidError.h" +#include + +using namespace qpid::sys; +using namespace qpid::sys; +using namespace qpid::framing; + +LFSessionContext::LFSessionContext(apr_pool_t* _pool, apr_socket_t* _socket, + LFProcessor* const _processor, + bool _debug) : + debug(_debug), + socket(_socket), + initiated(false), + in(65536), + out(65536), + processor(_processor), + processing(false), + closing(false) +{ + + fd.p = _pool; + fd.desc_type = APR_POLL_SOCKET; + fd.reqevents = APR_POLLIN; + fd.client_data = this; + fd.desc.s = _socket; + + out.flip(); +} + +LFSessionContext::~LFSessionContext(){ + +} + +void LFSessionContext::read(){ + socket.read(in); + in.flip(); + if(initiated){ + AMQFrame frame; + try{ + while(frame.decode(in)){ + if(debug) log("RECV", &frame); + handler->received(&frame); + } + }catch(QpidError error){ + std::cout << "Error [" << error.code << "] " << error.msg + << " (" << error.loc.file << ":" << error.loc.line + << ")" << std::endl; + } + }else{ + ProtocolInitiation protocolInit; + if(protocolInit.decode(in)){ + handler->initiated(protocolInit); + initiated = true; + if(debug) std::cout << "INIT [" << &socket << "]" << std::endl; + } + } + in.compact(); +} + +void LFSessionContext::write(){ + bool done = isClosed(); + while(!done){ + if(out.available() > 0){ + socket.write(out); + if(out.available() > 0){ + + //incomplete write, leave flags to receive notification of readiness to write + done = true;//finished processing for now, but write is still in progress + } + }else{ + //do we have any frames to write? + Mutex::ScopedLock l(writeLock); + if(!framesToWrite.empty()){ + out.clear(); + bool encoded(false); + AMQFrame* frame = framesToWrite.front(); + while(frame && out.available() >= frame->size()){ + encoded = true; + frame->encode(out); + if(debug) log("SENT", frame); + delete frame; + framesToWrite.pop(); + frame = framesToWrite.empty() ? 0 : framesToWrite.front(); + } + if(!encoded) THROW_QPID_ERROR(FRAMING_ERROR, "Could not write frame, too large for buffer."); + out.flip(); + }else{ + //reset flags, don't care about writability anymore + fd.reqevents = APR_POLLIN; + done = true; + + if(closing){ + socket.close(); + } + } + } + } +} + +void LFSessionContext::send(AMQFrame* frame){ + Mutex::ScopedLock l(writeLock); + if(!closing){ + framesToWrite.push(frame); + if(!(fd.reqevents & APR_POLLOUT)){ + fd.reqevents |= APR_POLLOUT; + if(!processing){ + processor->update(&fd); + } + } + } +} + +void LFSessionContext::startProcessing(){ + Mutex::ScopedLock l(writeLock); + processing = true; + processor->deactivate(&fd); +} + +void LFSessionContext::stopProcessing(){ + Mutex::ScopedLock l(writeLock); + processor->reactivate(&fd); + processing = false; +} + +void LFSessionContext::close(){ + Mutex::ScopedLock l(writeLock); + closing = true; + if(!processing){ + //allow pending frames to be written to socket + fd.reqevents = APR_POLLOUT; + processor->update(&fd); + } +} + +void LFSessionContext::handleClose(){ + handler->closed(); + std::cout << "Session closed [" << &socket << "]" << std::endl; + delete handler; + delete this; +} + +void LFSessionContext::shutdown(){ + socket.close(); + handleClose(); +} + +void LFSessionContext::init(ConnectionInputHandler* _handler){ + handler = _handler; + processor->add(&fd); +} + +void LFSessionContext::log(const std::string& desc, AMQFrame* const frame){ + Mutex::ScopedLock l(logLock); + std::cout << desc << " [" << &socket << "]: " << *frame << std::endl; +} + +Mutex LFSessionContext::logLock; diff --git a/cpp/src/qpid/sys/apr/LFSessionContext.h b/cpp/src/qpid/sys/apr/LFSessionContext.h new file mode 100644 index 0000000000..ed97b23645 --- /dev/null +++ b/cpp/src/qpid/sys/apr/LFSessionContext.h @@ -0,0 +1,90 @@ +/* + * + * 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 _LFSessionContext_ +#define _LFSessionContext_ + +#include + +#include +#include +#include + +#include "qpid/framing/AMQFrame.h" +#include "qpid/framing/Buffer.h" +#include "qpid/sys/Monitor.h" +#include "qpid/sys/ConnectionOutputHandler.h" +#include "qpid/sys/ConnectionInputHandler.h" + +#include "APRSocket.h" +#include "LFProcessor.h" + +namespace qpid { +namespace sys { + + +class LFSessionContext : public virtual qpid::sys::ConnectionOutputHandler +{ + const bool debug; + APRSocket socket; + bool initiated; + + qpid::framing::Buffer in; + qpid::framing::Buffer out; + + qpid::sys::ConnectionInputHandler* handler; + LFProcessor* const processor; + + apr_pollfd_t fd; + + std::queue framesToWrite; + qpid::sys::Mutex writeLock; + + bool processing; + bool closing; + + static qpid::sys::Mutex logLock; + void log(const std::string& desc, + qpid::framing::AMQFrame* const frame); + + + public: + LFSessionContext(apr_pool_t* pool, apr_socket_t* socket, + LFProcessor* const processor, + bool debug = false); + virtual ~LFSessionContext(); + virtual void send(qpid::framing::AMQFrame* frame); + virtual void close(); + void read(); + void write(); + void init(qpid::sys::ConnectionInputHandler* handler); + void startProcessing(); + void stopProcessing(); + void handleClose(); + void shutdown(); + inline apr_pollfd_t* const getFd(){ return &fd; } + inline bool isClosed(){ return !socket.isOpen(); } +}; + +} +} + + +#endif diff --git a/cpp/src/qpid/sys/apr/Socket.cpp b/cpp/src/qpid/sys/apr/Socket.cpp new file mode 100644 index 0000000000..8cd98161d2 --- /dev/null +++ b/cpp/src/qpid/sys/apr/Socket.cpp @@ -0,0 +1,86 @@ +/* + * + * 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/sys/Socket.h" +#include "APRBase.h" +#include "APRPool.h" + + +using namespace qpid::sys; + +Socket Socket::createTcp() { + Socket s; + CHECK_APR_SUCCESS( + apr_socket_create( + &s.socket, APR_INET, SOCK_STREAM, APR_PROTO_TCP, + APRPool::get())); + return s; +} + +Socket::Socket(apr_socket_t* s) { + socket = s; +} + +void Socket::setTimeout(Time interval) { + apr_socket_timeout_set(socket, interval/TIME_USEC); +} + +void Socket::connect(const std::string& host, int port) { + apr_sockaddr_t* address; + CHECK_APR_SUCCESS( + apr_sockaddr_info_get( + &address, host.c_str(), APR_UNSPEC, port, APR_IPV4_ADDR_OK, + APRPool::get())); + CHECK_APR_SUCCESS(apr_socket_connect(socket, address)); +} + +void Socket::close() { + if (socket == 0) return; + CHECK_APR_SUCCESS(apr_socket_close(socket)); + socket = 0; +} + +ssize_t Socket::send(const void* data, size_t size) +{ + apr_size_t sent = size; + apr_status_t status = + apr_socket_send(socket, reinterpret_cast(data), &sent); + if (APR_STATUS_IS_TIMEUP(status)) return SOCKET_TIMEOUT; + if (APR_STATUS_IS_EOF(status)) return SOCKET_EOF; + CHECK_APR_SUCCESS(status); + return sent; +} + +ssize_t Socket::recv(void* data, size_t size) +{ + apr_size_t received = size; + apr_status_t status = + apr_socket_recv(socket, reinterpret_cast(data), &received); + if (APR_STATUS_IS_TIMEUP(status)) + return SOCKET_TIMEOUT; + if (APR_STATUS_IS_EOF(status)) + return SOCKET_EOF; + CHECK_APR_SUCCESS(status); + return received; +} + + diff --git a/cpp/src/qpid/sys/apr/Thread.cpp b/cpp/src/qpid/sys/apr/Thread.cpp new file mode 100644 index 0000000000..0f9aeca186 --- /dev/null +++ b/cpp/src/qpid/sys/apr/Thread.cpp @@ -0,0 +1,33 @@ +/* + * + * 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/sys/Thread.h" + +using namespace qpid::sys; +using qpid::sys::Runnable; + +void* APR_THREAD_FUNC Thread::runRunnable(apr_thread_t* thread, void *data) { + reinterpret_cast(data)->run(); + CHECK_APR_SUCCESS(apr_thread_exit(thread, APR_SUCCESS)); + return NULL; +} + + diff --git a/cpp/src/qpid/sys/posix/EventChannel.cpp b/cpp/src/qpid/sys/posix/EventChannel.cpp new file mode 100644 index 0000000000..8dc736ed0b --- /dev/null +++ b/cpp/src/qpid/sys/posix/EventChannel.cpp @@ -0,0 +1,325 @@ +/* + * + * 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 +#include +#include + +#include +#include +#include + +#include +#include +#include + +#include +#include + +#include "qpid/QpidError.h" +#include "qpid/sys/Monitor.h" + +#include "check.h" +#include "EventChannel.h" + +using namespace std; + + +// Convenience template to zero out a struct. +template struct ZeroStruct : public S { + ZeroStruct() { memset(this, 0, sizeof(*this)); } +}; + +namespace qpid { +namespace sys { + + +/** + * EventHandler wraps an epoll file descriptor. Acts as private + * interface between EventChannel and subclasses. + * + * Also implements Event interface for events that are not associated + * with a file descriptor and are passed via the message queue. + */ +class EventHandler : public Event, private Monitor +{ + public: + EventHandler(int epollSize = 256); + ~EventHandler(); + + int getEpollFd() { return epollFd; } + void epollAdd(int fd, uint32_t epollEvents, Event* event); + void epollMod(int fd, uint32_t epollEvents, Event* event); + void epollDel(int fd); + + void mqPut(Event* event); + Event* mqGet(); + + protected: + // Should never be called, only complete. + void prepare(EventHandler&) { assert(0); } + Event* complete(EventHandler& eh); + + private: + int epollFd; + std::string mqName; + int mqFd; + std::queue mqEvents; +}; + +EventHandler::EventHandler(int epollSize) +{ + epollFd = epoll_create(epollSize); + if (epollFd < 0) throw QPID_POSIX_ERROR(errno); + + // Create a POSIX message queue for non-fd events. + // We write one byte and never read it is always ready for read + // when we add it to epoll. + // + ZeroStruct attr; + attr.mq_maxmsg = 1; + attr.mq_msgsize = 1; + do { + char tmpnam[L_tmpnam]; + tmpnam_r(tmpnam); + mqName = tmpnam + 4; // Skip "tmp/" + mqFd = mq_open( + mqName.c_str(), O_CREAT|O_EXCL|O_RDWR|O_NONBLOCK, S_IRWXU, &attr); + if (mqFd < 0) throw QPID_POSIX_ERROR(errno); + } while (mqFd == EEXIST); // Name already taken, try again. + + static char zero = '\0'; + mq_send(mqFd, &zero, 1, 0); + epollAdd(mqFd, 0, this); +} + +EventHandler::~EventHandler() { + mq_close(mqFd); + mq_unlink(mqName.c_str()); +} + +void EventHandler::mqPut(Event* event) { + ScopedLock l(*this); + assert(event != 0); + mqEvents.push(event); + epollMod(mqFd, EPOLLIN|EPOLLONESHOT, this); +} + +Event* EventHandler::mqGet() { + ScopedLock l(*this); + if (mqEvents.empty()) + return 0; + Event* event = mqEvents.front(); + mqEvents.pop(); + if(!mqEvents.empty()) + epollMod(mqFd, EPOLLIN|EPOLLONESHOT, this); + return event; +} + +void EventHandler::epollAdd(int fd, uint32_t epollEvents, Event* event) +{ + ZeroStruct ee; + ee.data.ptr = event; + ee.events = epollEvents; + if (epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &ee) < 0) + throw QPID_POSIX_ERROR(errno); +} + +void EventHandler::epollMod(int fd, uint32_t epollEvents, Event* event) +{ + ZeroStruct ee; + ee.data.ptr = event; + ee.events = epollEvents; + if (epoll_ctl(epollFd, EPOLL_CTL_MOD, fd, &ee) < 0) + throw QPID_POSIX_ERROR(errno); +} + +void EventHandler::epollDel(int fd) { + if (epoll_ctl(epollFd, EPOLL_CTL_DEL, fd, 0) < 0) + throw QPID_POSIX_ERROR(errno); +} + +Event* EventHandler::complete(EventHandler& eh) +{ + assert(&eh == this); + Event* event = mqGet(); + return event==0 ? 0 : event->complete(eh); +} + +// ================================================================ +// EventChannel + +EventChannel::shared_ptr EventChannel::create() { + return shared_ptr(new EventChannel()); +} + +EventChannel::EventChannel() : handler(new EventHandler()) {} + +EventChannel::~EventChannel() {} + +void EventChannel::postEvent(Event& e) +{ + e.prepare(*handler); +} + +Event* EventChannel::getEvent() +{ + static const int infiniteTimeout = -1; + ZeroStruct epollEvent; + + // Loop until we can complete the event. Some events may re-post + // themselves and return 0 from complete, e.g. partial reads. // + Event* event = 0; + while (event == 0) { + int eventCount = epoll_wait(handler->getEpollFd(), + &epollEvent, 1, infiniteTimeout); + if (eventCount < 0) { + if (errno != EINTR) { + // TODO aconway 2006-11-28: Proper handling/logging of errors. + cerr << BOOST_CURRENT_FUNCTION << " ignoring error " + << PosixError::getMessage(errno) << endl; + assert(0); + } + } + else if (eventCount == 1) { + event = reinterpret_cast(epollEvent.data.ptr); + assert(event != 0); + try { + event = event->complete(*handler); + } + catch (const Exception& e) { + if (event) + event->setError(e); + } + catch (const std::exception& e) { + if (event) + event->setError(e); + } + } + } + return event; +} + +Event::~Event() {} + +void Event::prepare(EventHandler& handler) +{ + handler.mqPut(this); +} + +bool Event::hasError() const { + return error; +} + +void Event::throwIfError() throw (Exception) { + if (hasError()) + error.throwSelf(); +} + +Event* Event::complete(EventHandler&) +{ + return this; +} + +void Event::dispatch() +{ + try { + if (!callback.empty()) + callback(); + } catch (const std::exception&) { + throw; + } catch (...) { + throw QPID_ERROR(INTERNAL_ERROR, "Unknown exception."); + } +} + +void Event::setError(const ExceptionHolder& e) { + error = e; +} + +void ReadEvent::prepare(EventHandler& handler) +{ + handler.epollAdd(descriptor, EPOLLIN | EPOLLONESHOT, this); +} + +ssize_t ReadEvent::doRead() { + ssize_t n = ::read(descriptor, static_cast(buffer) + received, + size - received); + if (n > 0) received += n; + return n; +} + +Event* ReadEvent::complete(EventHandler& handler) +{ + // Read as much as possible without blocking. + ssize_t n = doRead(); + while (n > 0 && received < size) doRead(); + + if (received == size) { + handler.epollDel(descriptor); + received = 0; // Reset for re-use. + return this; + } + else if (n <0 && (errno == EAGAIN)) { + // Keep polling for more. + handler.epollMod(descriptor, EPOLLIN | EPOLLONESHOT, this); + return 0; + } + else { + // Unexpected EOF or error. Throw ENODATA for EOF. + handler.epollDel(descriptor); + received = 0; // Reset for re-use. + throw QPID_POSIX_ERROR((n < 0) ? errno : ENODATA); + } +} + +void WriteEvent::prepare(EventHandler& handler) +{ + handler.epollAdd(descriptor, EPOLLOUT | EPOLLONESHOT, this); +} + +Event* WriteEvent::complete(EventHandler& handler) +{ + ssize_t n = write(descriptor, static_cast(buffer) + written, + size - written); + if (n < 0) throw QPID_POSIX_ERROR(errno); + written += n; + if(written < size) { + // Keep polling. + handler.epollMod(descriptor, EPOLLOUT | EPOLLONESHOT, this); + return 0; + } + written = 0; // Reset for re-use. + handler.epollDel(descriptor); + return this; +} + +void AcceptEvent::prepare(EventHandler& handler) +{ + handler.epollAdd(descriptor, EPOLLIN | EPOLLONESHOT, this); +} + +Event* AcceptEvent::complete(EventHandler& handler) +{ + handler.epollDel(descriptor); + accepted = ::accept(descriptor, 0, 0); + if (accepted < 0) throw QPID_POSIX_ERROR(errno); + return this; +} + +}} diff --git a/cpp/src/qpid/sys/posix/EventChannel.h b/cpp/src/qpid/sys/posix/EventChannel.h new file mode 100644 index 0000000000..f465580996 --- /dev/null +++ b/cpp/src/qpid/sys/posix/EventChannel.h @@ -0,0 +1,176 @@ +#ifndef _sys_EventChannel_h +#define _sys_EventChannel_h + +/* + * + * 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/SharedObject.h" +#include "qpid/ExceptionHolder.h" +#include +#include + +namespace qpid { +namespace sys { + +class Event; +class EventHandler; +class EventChannel; + +/** + * Base class for all Events. + */ +class Event +{ + public: + /** Type for callback when event is dispatched */ + typedef boost::function0 Callback; + + /** + * Create an event with optional callback. + * Instances of Event are sent directly through the channel. + * Derived classes define additional waiting behaviour. + *@param cb A callback functor that is invoked when dispatch() is called. + */ + Event(Callback cb = 0) : callback(cb) {} + + virtual ~Event(); + + /** Call the callback provided to the constructor, if any. */ + void dispatch(); + + /** True if there was an error processing this event */ + bool hasError() const; + + /** If hasError() throw the corresponding exception. */ + void throwIfError() throw(Exception); + + protected: + virtual void prepare(EventHandler&); + virtual Event* complete(EventHandler&); + void setError(const ExceptionHolder& e); + + Callback callback; + ExceptionHolder error; + + friend class EventChannel; + friend class EventHandler; +}; + +template +class IOEvent : public Event { + public: + void getDescriptor() const { return descriptor; } + size_t getSize() const { return size; } + BufT getBuffer() const { return buffer; } + + protected: + IOEvent(int fd, Callback cb, size_t sz, BufT buf) : + Event(cb), descriptor(fd), buffer(buf), size(sz) {} + + int descriptor; + BufT buffer; + size_t size; +}; + +/** Asynchronous read event */ +class ReadEvent : public IOEvent +{ + public: + explicit ReadEvent(int fd=-1, void* buf=0, size_t sz=0, Callback cb=0) : + IOEvent(fd, cb, sz, buf), received(0) {} + + private: + void prepare(EventHandler&); + Event* complete(EventHandler&); + ssize_t doRead(); + + size_t received; +}; + +/** Asynchronous write event */ +class WriteEvent : public IOEvent +{ + public: + explicit WriteEvent(int fd=-1, const void* buf=0, size_t sz=0, + Callback cb=0) : + IOEvent(fd, cb, sz, buf), written(0) {} + + protected: + void prepare(EventHandler&); + Event* complete(EventHandler&); + + private: + ssize_t doWrite(); + size_t written; +}; + +/** Asynchronous socket accept event */ +class AcceptEvent : public Event +{ + public: + /** Accept a connection on fd. */ + explicit AcceptEvent(int fd=-1, Callback cb=0) : + Event(cb), descriptor(fd), accepted(0) {} + + /** Get descriptor for server socket */ + int getAcceptedDesscriptor() const { return accepted; } + + private: + void prepare(EventHandler&); + Event* complete(EventHandler&); + + int descriptor; + int accepted; +}; + + +class QueueSet; + +/** + * Channel to post and wait for events. + */ +class EventChannel : public qpid::SharedObject +{ + public: + static shared_ptr create(); + + ~EventChannel(); + + /** Post an event to the channel. */ + void postEvent(Event& event); + + /** Post an event to the channel. Must not be 0. */ + void postEvent(Event* event) { postEvent(*event); } + + /** + * Wait for the next complete event. + *@return Pointer to event. Will never return 0. + */ + Event* getEvent(); + + private: + EventChannel(); + boost::shared_ptr handler; +}; + + +}} + + + +#endif /*!_sys_EventChannel_h*/ diff --git a/cpp/src/qpid/sys/posix/EventChannelAcceptor.cpp b/cpp/src/qpid/sys/posix/EventChannelAcceptor.cpp new file mode 100644 index 0000000000..2676985cc6 --- /dev/null +++ b/cpp/src/qpid/sys/posix/EventChannelAcceptor.cpp @@ -0,0 +1,149 @@ +/* + * + * 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 + +#include +#include +#include +#include +#include + +#include "qpid/sys/ConnectionOutputHandler.h" +#include "qpid/sys/ConnectionInputHandler.h" +#include "qpid/sys/ConnectionInputHandlerFactory.h" +#include "qpid/sys/Acceptor.h" +#include "qpid/sys/Socket.h" +#include "qpid/framing/Buffer.h" +#include "qpid/framing/AMQFrame.h" +#include "qpid/Exception.h" + +#include "EventChannelConnection.h" + +namespace qpid { +namespace sys { + +using namespace qpid::framing; +using namespace std; + +class EventChannelAcceptor : public Acceptor { + public: + + + EventChannelAcceptor( + int16_t port_, int backlog, int nThreads, bool trace_ + ); + + int getPort() const; + + void run(ConnectionInputHandlerFactory& factory); + + void shutdown(); + + private: + + void accept(); + + Mutex lock; + Socket listener; + const int port; + const bool isTrace; + bool isRunning; + boost::ptr_vector connections; + AcceptEvent acceptEvent; + ConnectionInputHandlerFactory* factory; + bool isShutdown; + EventChannelThreads::shared_ptr threads; +}; + +Acceptor::shared_ptr Acceptor::create( + int16_t port, int backlog, int threads, bool trace) +{ + return Acceptor::shared_ptr( + new EventChannelAcceptor(port, backlog, threads, trace)); +} + +// Must define Acceptor virtual dtor. +Acceptor::~Acceptor() {} + +EventChannelAcceptor::EventChannelAcceptor( + int16_t port_, int backlog, int nThreads, bool trace_ +) : listener(Socket::createTcp()), + port(listener.listen(int(port_), backlog)), + isTrace(trace_), + isRunning(false), + acceptEvent(listener.fd(), + boost::bind(&EventChannelAcceptor::accept, this)), + factory(0), + isShutdown(false), + threads(EventChannelThreads::create(EventChannel::create(), nThreads)) +{ } + +int EventChannelAcceptor::getPort() const { + return port; // Immutable no need for lock. +} + +void EventChannelAcceptor::run(ConnectionInputHandlerFactory& f) { + { + Mutex::ScopedLock l(lock); + if (!isRunning && !isShutdown) { + isRunning = true; + factory = &f; + threads->post(acceptEvent); + } + } + threads->join(); // Wait for shutdown. +} + +void EventChannelAcceptor::shutdown() { + bool doShutdown = false; + { + Mutex::ScopedLock l(lock); + doShutdown = !isShutdown; // I'm the shutdown thread. + isShutdown = true; + } + if (doShutdown) { + ::close(acceptEvent.getDescriptor()); + threads->shutdown(); + for_each(connections.begin(), connections.end(), + boost::bind(&EventChannelConnection::close, _1)); + } + threads->join(); +} + +void EventChannelAcceptor::accept() +{ + // No lock, we only post one accept event at a time. + if (isShutdown) + return; + if (acceptEvent.getException()) { + Exception::log(*acceptEvent.getException(), + "EventChannelAcceptor::accept"); + shutdown(); + return; + } + // TODO aconway 2006-11-29: Need to reap closed connections also. + int fd = acceptEvent.getAcceptedDesscriptor(); + connections.push_back( + new EventChannelConnection(threads, *factory, fd, fd, isTrace)); + threads->post(acceptEvent); // Keep accepting. +} + +}} // namespace qpid::sys diff --git a/cpp/src/qpid/sys/posix/EventChannelConnection.cpp b/cpp/src/qpid/sys/posix/EventChannelConnection.cpp new file mode 100644 index 0000000000..9a7d53fa8a --- /dev/null +++ b/cpp/src/qpid/sys/posix/EventChannelConnection.cpp @@ -0,0 +1,229 @@ +/* + * + * 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 + +#include +#include + +#include "EventChannelConnection.h" +#include "qpid/sys/ConnectionInputHandlerFactory.h" +#include "qpid/QpidError.h" + +using namespace std; +using namespace qpid; +using namespace qpid::framing; + +namespace qpid { +namespace sys { + +const size_t EventChannelConnection::bufferSize = 65536; + +EventChannelConnection::EventChannelConnection( + EventChannelThreads::shared_ptr threads_, + ConnectionInputHandlerFactory& factory_, + int rfd, + int wfd, + bool isTrace_ +) : + readFd(rfd), + writeFd(wfd ? wfd : rfd), + readCallback(boost::bind(&EventChannelConnection::closeOnException, + this, &EventChannelConnection::endInitRead)), + + isWriting(false), + isClosed(false), + threads(threads_), + handler(factory_.create(this)), + in(bufferSize), + out(bufferSize), + isTrace(isTrace_) +{ + BOOST_ASSERT(readFd > 0); + BOOST_ASSERT(writeFd > 0); + closeOnException(&EventChannelConnection::startRead); +} + + +void EventChannelConnection::send(std::auto_ptr frame) { + { + Monitor::ScopedLock lock(monitor); + assert(frame.get()); + writeFrames.push_back(frame.release()); + } + closeOnException(&EventChannelConnection::startWrite); +} + +void EventChannelConnection::close() { + { + Monitor::ScopedLock lock(monitor); + if (isClosed) + return; + isClosed = true; + } + ::close(readFd); + ::close(writeFd); + { + Monitor::ScopedLock lock(monitor); + while (busyThreads > 0) + monitor.wait(); + } + handler->closed(); +} + +void EventChannelConnection::closeNoThrow() { + Exception::tryCatchLog( + boost::bind(&EventChannelConnection::close, this), + false, + "Exception closing channel" + ); +} + +/** + * Call f in a try/catch block and close the connection if + * an exception is thrown. + */ +void EventChannelConnection::closeOnException(MemberFnPtr f) +{ + try { + Exception::tryCatchLog( + boost::bind(f, this), + "Closing connection due to exception" + ); + return; + } catch (...) { + // Exception was already logged by tryCatchLog + closeNoThrow(); + } +} + +// Post the write event. +// Always called inside closeOnException. +// Called by endWrite and send, but only one thread writes at a time. +// +void EventChannelConnection::startWrite() { + FrameQueue::auto_type frame; + { + Monitor::ScopedLock lock(monitor); + // Stop if closed or a write event is already in progress. + if (isClosed || isWriting) + return; + if (writeFrames.empty()) { + isWriting = false; + return; + } + isWriting = true; + frame = writeFrames.pop_front(); + } + // No need to lock here - only one thread can be writing at a time. + out.clear(); + if (isTrace) + cout << "Send on socket " << writeFd << ": " << *frame << endl; + frame->encode(out); + out.flip(); + writeEvent = WriteEvent( + writeFd, out.start(), out.available(), + boost::bind(&EventChannelConnection::closeOnException, + this, &EventChannelConnection::endWrite)); + threads->post(writeEvent); +} + +// ScopedBusy ctor increments busyThreads. +// dtor decrements and calls monitor.notifyAll if it reaches 0. +// +struct EventChannelConnection::ScopedBusy : public AtomicCount::ScopedIncrement +{ + ScopedBusy(EventChannelConnection& ecc) + : AtomicCount::ScopedIncrement( + ecc.busyThreads, boost::bind(&Monitor::notifyAll, &ecc.monitor)) + {} +}; + +// Write event completed. +// Always called by a channel thread inside closeOnException. +// +void EventChannelConnection::endWrite() { + ScopedBusy(*this); + { + Monitor::ScopedLock lock(monitor); + isWriting = false; + if (isClosed) + return; + writeEvent.throwIfException(); + } + // Check if there's more in to write in the write queue. + startWrite(); +} + + +// Post the read event. +// Always called inside closeOnException. +// Called from ctor and end[Init]Read, so only one call at a time +// is possible since we only post one read event at a time. +// +void EventChannelConnection::startRead() { + // Non blocking read, as much as we can swallow. + readEvent = ReadEvent( + readFd, in.start(), in.available(), readCallback,true); + threads->post(readEvent); +} + +// Completion of initial read, expect protocolInit. +// Always called inside closeOnException in channel thread. +// Only called by one thread at a time. +void EventChannelConnection::endInitRead() { + ScopedBusy(*this); + if (!isClosed) { + readEvent.throwIfException(); + in.move(readEvent.getBytesRead()); + in.flip(); + ProtocolInitiation protocolInit; + if(protocolInit.decode(in)){ + handler->initiated(&protocolInit); + readCallback = boost::bind( + &EventChannelConnection::closeOnException, + this, &EventChannelConnection::endRead); + } + in.compact(); + // Continue reading. + startRead(); + } +} + +// Normal reads, expect a frame. +// Always called inside closeOnException in channel thread. +void EventChannelConnection::endRead() { + ScopedBusy(*this); + if (!isClosed) { + readEvent.throwIfException(); + in.move(readEvent.getBytesRead()); + in.flip(); + AMQFrame frame; + while (frame.decode(in)) { + // TODO aconway 2006-11-30: received should take Frame& + if (isTrace) + cout << "Received on socket " << readFd + << ": " << frame << endl; + handler->received(&frame); + } + in.compact(); + startRead(); + } +} + +}} // namespace qpid::sys diff --git a/cpp/src/qpid/sys/posix/EventChannelConnection.h b/cpp/src/qpid/sys/posix/EventChannelConnection.h new file mode 100644 index 0000000000..bd010a4240 --- /dev/null +++ b/cpp/src/qpid/sys/posix/EventChannelConnection.h @@ -0,0 +1,102 @@ +#ifndef _posix_EventChannelConnection_h +#define _posix_EventChannelConnection_h + +/* + * + * 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 + +#include "EventChannelThreads.h" +#include "qpid/sys/Monitor.h" +#include "qpid/sys/ConnectionOutputHandler.h" +#include "qpid/sys/ConnectionInputHandler.h" +#include "qpid/sys/AtomicCount.h" +#include "qpid/framing/AMQFrame.h" + +namespace qpid { +namespace sys { + +class ConnectionInputHandlerFactory; + +/** + * Implements ConnectionOutputHandler and delegates to a ConnectionInputHandler + * for a connection via the EventChannel. + *@param readDescriptor file descriptor for reading. + *@param writeDescriptor file descriptor for writing, + * by default same as readDescriptor + */ +class EventChannelConnection : public ConnectionOutputHandler { + public: + EventChannelConnection( + EventChannelThreads::shared_ptr threads, + ConnectionInputHandlerFactory& factory, + int readDescriptor, + int writeDescriptor = 0, + bool isTrace = false + ); + + // TODO aconway 2006-11-30: ConnectionOutputHandler::send should take auto_ptr + virtual void send(qpid::framing::AMQFrame* frame) { + send(std::auto_ptr(frame)); + } + + virtual void send(std::auto_ptr frame); + + virtual void close(); + + private: + typedef boost::ptr_deque FrameQueue; + typedef void (EventChannelConnection::*MemberFnPtr)(); + struct ScopedBusy; + + void startWrite(); + void endWrite(); + void startRead(); + void endInitRead(); + void endRead(); + void closeNoThrow(); + void closeOnException(MemberFnPtr); + bool shouldContinue(bool& flag); + + static const size_t bufferSize; + + Monitor monitor; + + int readFd, writeFd; + ReadEvent readEvent; + WriteEvent writeEvent; + Event::Callback readCallback; + bool isWriting; + bool isClosed; + AtomicCount busyThreads; + + EventChannelThreads::shared_ptr threads; + std::auto_ptr handler; + qpid::framing::Buffer in, out; + FrameQueue writeFrames; + bool isTrace; + + friend struct ScopedBusy; +}; + + +}} // namespace qpid::sys + + + +#endif /*!_posix_EventChannelConnection_h*/ diff --git a/cpp/src/qpid/sys/posix/EventChannelThreads.cpp b/cpp/src/qpid/sys/posix/EventChannelThreads.cpp new file mode 100644 index 0000000000..ec58a03671 --- /dev/null +++ b/cpp/src/qpid/sys/posix/EventChannelThreads.cpp @@ -0,0 +1,119 @@ +/* + * + * 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 "EventChannelThreads.h" +#include "qpid/sys/Runnable.h" +#include +using namespace std; +#include + +namespace qpid { +namespace sys { + +EventChannelThreads::shared_ptr EventChannelThreads::create( + EventChannel::shared_ptr ec) +{ + return EventChannelThreads::shared_ptr(new EventChannelThreads(ec)); +} + +EventChannelThreads::EventChannelThreads(EventChannel::shared_ptr ec) : + channel(ec), nWaiting(0), state(RUNNING) +{ + // TODO aconway 2006-11-15: Estimate initial threads based on CPUs. + addThread(); +} + +EventChannelThreads::~EventChannelThreads() { + shutdown(); + join(); +} + +void EventChannelThreads::shutdown() +{ + ScopedLock lock(*this); + if (state != RUNNING) // Already shutting down. + return; + for (size_t i = 0; i < workers.size(); ++i) { + channel->postEvent(terminate); + } + state = TERMINATE_SENT; + notify(); // Wake up one join() thread. +} + +void EventChannelThreads::join() +{ + { + ScopedLock lock(*this); + while (state == RUNNING) // Wait for shutdown to start. + wait(); + if (state == SHUTDOWN) // Shutdown is complete + return; + if (state == JOINING) { + // Someone else is doing the join. + while (state != SHUTDOWN) + wait(); + return; + } + // I'm the joining thread + assert(state == TERMINATE_SENT); + state = JOINING; + } // Drop the lock. + + for (size_t i = 0; i < workers.size(); ++i) { + assert(state == JOINING); // Only this thread can change JOINING. + workers[i].join(); + } + state = SHUTDOWN; + notifyAll(); // Notify other join() threaeds. +} + +void EventChannelThreads::addThread() { + ScopedLock l(*this); + workers.push_back(Thread(*this)); +} + +void EventChannelThreads::run() +{ + // Start life waiting. Decrement on exit. + AtomicCount::ScopedIncrement inc(nWaiting); + try { + while (true) { + Event* e = channel->getEvent(); + assert(e != 0); + if (e == &terminate) { + return; + } + AtomicCount::ScopedDecrement dec(nWaiting); + // I'm no longer waiting, make sure someone is. + if (dec == 0) + addThread(); + e->dispatch(); + } + } + catch (const std::exception& e) { + // TODO aconway 2006-11-15: need better logging across the board. + std::cerr << "EventChannelThreads::run() caught: " << e.what() + << std::endl; + } + catch (...) { + std::cerr << "EventChannelThreads::run() caught unknown exception." + << std::endl; + } +} + +}} diff --git a/cpp/src/qpid/sys/posix/EventChannelThreads.h b/cpp/src/qpid/sys/posix/EventChannelThreads.h new file mode 100644 index 0000000000..245cefe585 --- /dev/null +++ b/cpp/src/qpid/sys/posix/EventChannelThreads.h @@ -0,0 +1,92 @@ +#ifndef _posix_EventChannelThreads_h +#define _sys_EventChannelThreads_h + +/* + * + * 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 + +#include "qpid/Exception.h" +#include "qpid/sys/Time.h" +#include "qpid/sys/Monitor.h" +#include "qpid/sys/Thread.h" +#include "qpid/sys/AtomicCount.h" +#include "EventChannel.h" + +namespace qpid { +namespace sys { + +/** + Dynamic thread pool serving an EventChannel. + + Threads run a loop { e = getEvent(); e->dispatch(); } + The size of the thread pool is automatically adjusted to optimal size. +*/ +class EventChannelThreads : + public qpid::SharedObject, + public sys::Monitor, private sys::Runnable +{ + public: + /** Create the thread pool and start initial threads. */ + static EventChannelThreads::shared_ptr create( + EventChannel::shared_ptr channel + ); + + ~EventChannelThreads(); + + /** Post event to the underlying channel */ + void postEvent(Event& event) { channel->postEvent(event); } + + /** Post event to the underlying channel Must not be 0. */ + void postEvent(Event* event) { channel->postEvent(event); } + + /** + * Terminate all threads. + * + * Returns immediately, use join() to wait till all threads are + * shut down. + */ + void shutdown(); + + /** Wait for all threads to terminate. */ + void join(); + + private: + typedef std::vector Threads; + typedef enum { + RUNNING, TERMINATE_SENT, JOINING, SHUTDOWN + } State; + + EventChannelThreads(EventChannel::shared_ptr underlyingChannel); + void addThread(); + + void run(); + bool keepRunning(); + void adjustThreads(); + + EventChannel::shared_ptr channel; + Threads workers; + sys::AtomicCount nWaiting; + State state; + Event terminate; +}; + + +}} + + +#endif /*!_sys_EventChannelThreads_h*/ diff --git a/cpp/src/qpid/sys/posix/PosixAcceptor.cpp b/cpp/src/qpid/sys/posix/PosixAcceptor.cpp new file mode 100644 index 0000000000..b5f6c6ee83 --- /dev/null +++ b/cpp/src/qpid/sys/posix/PosixAcceptor.cpp @@ -0,0 +1,48 @@ +/* + * + * 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/sys/Acceptor.h" +#include "qpid/Exception.h" + +namespace qpid { +namespace sys { + +namespace { +void fail() { throw qpid::Exception("PosixAcceptor not implemented"); } +} + +class PosixAcceptor : public Acceptor { + public: + virtual int16_t getPort() const { fail(); return 0; } + virtual void run(qpid::sys::ConnectionInputHandlerFactory* ) { fail(); } + virtual void shutdown() { fail(); } +}; + +// Define generic Acceptor::create() to return APRAcceptor. + Acceptor::shared_ptr Acceptor::create(int16_t , int, int, bool) +{ + return Acceptor::shared_ptr(new PosixAcceptor()); +} + +// Must define Acceptor virtual dtor. +Acceptor::~Acceptor() {} + +}} diff --git a/cpp/src/qpid/sys/posix/Socket.cpp b/cpp/src/qpid/sys/posix/Socket.cpp new file mode 100644 index 0000000000..e9bd2eeb6b --- /dev/null +++ b/cpp/src/qpid/sys/posix/Socket.cpp @@ -0,0 +1,118 @@ +/* + * + * 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 +#include +#include +#include + +#include + +#include "qpid/QpidError.h" +#include "check.h" +#include "qpid/sys/Socket.h" + +using namespace qpid::sys; + +Socket Socket::createTcp() +{ + int s = ::socket (PF_INET, SOCK_STREAM, 0); + if (s < 0) throw QPID_POSIX_ERROR(errno); + return s; +} + +Socket::Socket(int descriptor) : socket(descriptor) {} + +void Socket::setTimeout(Time interval) +{ + struct timeval tv; + tv.tv_sec = interval/TIME_SEC; + tv.tv_usec = (interval%TIME_SEC)/TIME_USEC; + setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); + setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); +} + +void Socket::connect(const std::string& host, int port) +{ + struct sockaddr_in name; + name.sin_family = AF_INET; + name.sin_port = htons(port); + struct hostent* hp = gethostbyname ( host.c_str() ); + if (hp == 0) throw QPID_POSIX_ERROR(errno); + memcpy(&name.sin_addr.s_addr, hp->h_addr_list[0], hp->h_length); + if (::connect(socket, (struct sockaddr*)(&name), sizeof(name)) < 0) + throw QPID_POSIX_ERROR(errno); +} + +void +Socket::close() +{ + if (socket == 0) return; + if (::close(socket) < 0) throw QPID_POSIX_ERROR(errno); + socket = 0; +} + +ssize_t +Socket::send(const void* data, size_t size) +{ + ssize_t sent = ::send(socket, data, size, 0); + if (sent < 0) { + if (errno == ECONNRESET) return SOCKET_EOF; + if (errno == ETIMEDOUT) return SOCKET_TIMEOUT; + throw QPID_POSIX_ERROR(errno); + } + return sent; +} + +ssize_t +Socket::recv(void* data, size_t size) +{ + ssize_t received = ::recv(socket, data, size, 0); + if (received < 0) { + if (errno == ETIMEDOUT) return SOCKET_TIMEOUT; + throw QPID_POSIX_ERROR(errno); + } + return received; +} + +int Socket::listen(int port, int backlog) +{ + struct sockaddr_in name; + name.sin_family = AF_INET; + name.sin_port = htons(port); + name.sin_addr.s_addr = 0; + if (::bind(socket, (struct sockaddr*)&name, sizeof(name)) < 0) + throw QPID_POSIX_ERROR(errno); + if (::listen(socket, backlog) < 0) + throw QPID_POSIX_ERROR(errno); + + socklen_t namelen = sizeof(name); + if (::getsockname(socket, (struct sockaddr*)&name, &namelen) < 0) + throw QPID_POSIX_ERROR(errno); + + return ntohs(name.sin_port); +} + + +int Socket::fd() +{ + return socket; +} diff --git a/cpp/src/qpid/sys/posix/Thread.cpp b/cpp/src/qpid/sys/posix/Thread.cpp new file mode 100644 index 0000000000..fc85e35028 --- /dev/null +++ b/cpp/src/qpid/sys/posix/Thread.cpp @@ -0,0 +1,28 @@ +/* + * + * 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/sys/Thread.h" + +void* qpid::sys::Thread::runRunnable(void* p) +{ + static_cast(p)->run(); + return 0; +} diff --git a/cpp/src/qpid/sys/posix/check.cpp b/cpp/src/qpid/sys/posix/check.cpp new file mode 100644 index 0000000000..408679caa8 --- /dev/null +++ b/cpp/src/qpid/sys/posix/check.cpp @@ -0,0 +1,39 @@ +/* + * + * 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 +#include "check.h" + +namespace qpid { +namespace sys { + +std::string +PosixError::getMessage(int errNo) +{ + char buf[512]; + return std::string(strerror_r(errNo, buf, sizeof(buf))); +} + +PosixError::PosixError(int errNo, const qpid::SrcLine& loc) throw() + : qpid::QpidError(INTERNAL_ERROR + errNo, getMessage(errNo), loc) +{ } + +}} diff --git a/cpp/src/qpid/sys/posix/check.h b/cpp/src/qpid/sys/posix/check.h new file mode 100644 index 0000000000..fe53321e27 --- /dev/null +++ b/cpp/src/qpid/sys/posix/check.h @@ -0,0 +1,62 @@ +#ifndef _posix_check_h +#define _posix_check_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 +#include +#include "qpid/QpidError.h" + +namespace qpid { +namespace sys { + +/** + * Exception with message from errno. + */ +class PosixError : public qpid::QpidError +{ + public: + static std::string getMessage(int errNo); + + PosixError(int errNo, const qpid::SrcLine& location) throw(); + + ~PosixError() throw() {} + + int getErrNo() { return errNo; } + + Exception* clone() const throw() { return new PosixError(*this); } + + void throwSelf() const { throw *this; } + + private: + int errNo; +}; + +}} + +/** Create a PosixError for the current file/line and errno. */ +#define QPID_POSIX_ERROR(errNo) ::qpid::sys::PosixError(errNo, SRCLINE) + +/** Throw a posix error if errNo is non-zero */ +#define QPID_POSIX_THROW_IF(ERRNO) \ + if ((ERRNO) != 0) throw QPID_POSIX_ERROR((ERRNO)) +#endif /*!_posix_check_h*/ diff --git a/cpp/src/shared_ptr.h b/cpp/src/shared_ptr.h deleted file mode 100644 index eb5f3f906a..0000000000 --- a/cpp/src/shared_ptr.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef _common_shared_ptr_h -#define _common_shared_ptr_h - -/* - * - * 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 -#include - -namespace qpid { - -// Import shared_ptr definitions into qpid namespace and define some -// useful shared_ptr templates for convenience. - -using boost::shared_ptr; -using boost::dynamic_pointer_cast; -using boost::static_pointer_cast; -using boost::const_pointer_cast; -using boost::shared_polymorphic_downcast; - -template shared_ptr make_shared_ptr(T* ptr) { - return shared_ptr(ptr); -} - -template -shared_ptr make_shared_ptr(T* ptr, D deleter) { - return shared_ptr(ptr, deleter); -} - -} // namespace qpid - - - -#endif /*!_common_shared_ptr_h*/ diff --git a/cpp/src/sys/Acceptor.h b/cpp/src/sys/Acceptor.h deleted file mode 100644 index f8edb3cef7..0000000000 --- a/cpp/src/sys/Acceptor.h +++ /dev/null @@ -1,47 +0,0 @@ -#ifndef _sys_Acceptor_h -#define _sys_Acceptor_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 -#include "../SharedObject.h" - -namespace qpid { -namespace sys { - -class ConnectionInputHandlerFactory; - -class Acceptor : public qpid::SharedObject -{ - public: - static Acceptor::shared_ptr create(int16_t port, int backlog, int threads, bool trace = false); - virtual ~Acceptor() = 0; - virtual uint16_t getPort() const = 0; - virtual void run(qpid::sys::ConnectionInputHandlerFactory* factory) = 0; - virtual void shutdown() = 0; -}; - -}} - - - -#endif /*!_sys_Acceptor_h*/ diff --git a/cpp/src/sys/AtomicCount.h b/cpp/src/sys/AtomicCount.h deleted file mode 100644 index 63670cbf00..0000000000 --- a/cpp/src/sys/AtomicCount.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef _posix_AtomicCount_h -#define _posix_AtomicCount_h - -/* - * - * 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 -#include "ScopedIncrement.h" - -namespace qpid { -namespace sys { - -/** - * Atomic counter. - */ -class AtomicCount : boost::noncopyable { - public: - typedef ScopedDecrement ScopedDecrement; - typedef ScopedIncrement ScopedIncrement; - - AtomicCount(long value = 0) : count(value) {} - - void operator++() { ++count ; } - - long operator--() { return --count; } - - operator long() const { return count; } - - - private: - boost::detail::atomic_count count; -}; - - -}} - - -#endif // _posix_AtomicCount_h diff --git a/cpp/src/sys/Condition.h b/cpp/src/sys/Condition.h deleted file mode 100644 index 455b179683..0000000000 --- a/cpp/src/sys/Condition.h +++ /dev/null @@ -1,128 +0,0 @@ -#ifndef _sys_Condition_h -#define _sys_Condition_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 -#include -#include "Mutex.h" -#include "Time.h" - -#ifdef USE_APR -#include -#endif - -namespace qpid { -namespace sys { - -/** - * A condition variable for thread synchronization. - */ -class Condition -{ - public: - inline Condition(); - inline ~Condition(); - inline void wait(Mutex&); - inline bool wait(Mutex&, const Time& absoluteTime); - inline void notify(); - inline void notifyAll(); - - private: -#ifdef USE_APR - apr_thread_cond_t* condition; -#else - pthread_cond_t condition; -#endif -}; - - -// APR ================================================================ -#ifdef USE_APR - -Condition::Condition() { - CHECK_APR_SUCCESS(apr_thread_cond_create(&condition, APRPool::get())); -} - -Condition::~Condition() { - CHECK_APR_SUCCESS(apr_thread_cond_destroy(condition)); -} - -void Condition::wait(Mutex& mutex) { - CHECK_APR_SUCCESS(apr_thread_cond_wait(condition, mutex.mutex)); -} - -bool Condition::wait(Mutex& mutex, const Time& absoluteTime){ - // APR uses microseconds. - apr_status_t status = - apr_thread_cond_timedwait( - condition, mutex.mutex, absoluteTime/TIME_USEC); - if(status != APR_TIMEUP) CHECK_APR_SUCCESS(status); - return status == 0; -} - -void Condition::notify(){ - CHECK_APR_SUCCESS(apr_thread_cond_signal(condition)); -} - -void Condition::notifyAll(){ - CHECK_APR_SUCCESS(apr_thread_cond_broadcast(condition)); -} - -#else -// POSIX ================================================================ - -Condition::Condition() { - QPID_POSIX_THROW_IF(pthread_cond_init(&condition, 0)); -} - -Condition::~Condition() { - QPID_POSIX_THROW_IF(pthread_cond_destroy(&condition)); -} - -void Condition::wait(Mutex& mutex) { - QPID_POSIX_THROW_IF(pthread_cond_wait(&condition, &mutex.mutex)); -} - -bool Condition::wait(Mutex& mutex, const Time& absoluteTime){ - struct timespec ts; - toTimespec(ts, absoluteTime); - int status = pthread_cond_timedwait(&condition, &mutex.mutex, &ts); - if (status != 0) { - if (status == ETIMEDOUT) return false; - throw QPID_POSIX_ERROR(status); - } - return true; -} - -void Condition::notify(){ - QPID_POSIX_THROW_IF(pthread_cond_signal(&condition)); -} - -void Condition::notifyAll(){ - QPID_POSIX_THROW_IF(pthread_cond_broadcast(&condition)); -} -#endif /*USE_APR*/ - - -}} -#endif /*!_sys_Condition_h*/ diff --git a/cpp/src/sys/ConnectionInputHandler.h b/cpp/src/sys/ConnectionInputHandler.h deleted file mode 100644 index df9bf55f89..0000000000 --- a/cpp/src/sys/ConnectionInputHandler.h +++ /dev/null @@ -1,45 +0,0 @@ -/* - * - * 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 _ConnectionInputHandler_ -#define _ConnectionInputHandler_ - -#include "../framing/InputHandler.h" -#include "../framing/InitiationHandler.h" -#include "../framing/ProtocolInitiation.h" -#include "TimeoutHandler.h" - -namespace qpid { -namespace sys { - - class ConnectionInputHandler : - public qpid::framing::InitiationHandler, - public qpid::framing::InputHandler, - public TimeoutHandler - { - public: - virtual void closed() = 0; - }; - -} -} - - -#endif diff --git a/cpp/src/sys/ConnectionInputHandlerFactory.h b/cpp/src/sys/ConnectionInputHandlerFactory.h deleted file mode 100644 index af7d411928..0000000000 --- a/cpp/src/sys/ConnectionInputHandlerFactory.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * - * 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 _ConnectionInputHandlerFactory_ -#define _ConnectionInputHandlerFactory_ - -#include - -namespace qpid { -namespace sys { - -class ConnectionOutputHandler; -class ConnectionInputHandler; - -/** - * Callback interface used by the Acceptor to - * create a ConnectionInputHandler for each new connection. - */ -class ConnectionInputHandlerFactory : private boost::noncopyable -{ - public: - virtual ConnectionInputHandler* create(ConnectionOutputHandler* ctxt) = 0; - virtual ~ConnectionInputHandlerFactory(){} -}; - -}} - - -#endif diff --git a/cpp/src/sys/ConnectionOutputHandler.h b/cpp/src/sys/ConnectionOutputHandler.h deleted file mode 100644 index 81bbbd1a17..0000000000 --- a/cpp/src/sys/ConnectionOutputHandler.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * - * 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 _ConnectionOutputHandler_ -#define _ConnectionOutputHandler_ - -#include "../framing/OutputHandler.h" - -namespace qpid { -namespace sys { - -/** - * Provides the output handler associated with a connection. - */ -class ConnectionOutputHandler : public virtual qpid::framing::OutputHandler -{ - public: - virtual void close() = 0; -}; - -}} - - -#endif diff --git a/cpp/src/sys/Module.h b/cpp/src/sys/Module.h deleted file mode 100644 index 224e1b9c1b..0000000000 --- a/cpp/src/sys/Module.h +++ /dev/null @@ -1,161 +0,0 @@ -#ifndef _sys_Module_h -#define _sys_Module_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 -#include -#include "../QpidError.h" - -namespace qpid { -namespace sys { -#if USE_APR -#include - typedef apr_dso_handle_t* dso_handle_t; -#else - typedef void* dso_handle_t; -#endif - - template class Module : private boost::noncopyable - { - typedef T* create_t(); - typedef void destroy_t(T*); - - dso_handle_t handle; - destroy_t* destroy; - T* ptr; - - void load(const std::string& name); - void unload(); - void* getSymbol(const std::string& name); - - public: - Module(const std::string& name); - T* operator->(); - T* get(); - ~Module() throw(); - }; - -} -} - -using namespace qpid::sys; - -template Module::Module(const std::string& module) : destroy(0), ptr(0) -{ - load(module); - //TODO: need a better strategy for symbol names to allow multiple - //modules to be loaded without clashes... - - //Note: need the double cast to avoid errors in casting from void* to function pointer with -pedantic - create_t* create = reinterpret_cast(reinterpret_cast(getSymbol("create"))); - destroy = reinterpret_cast(reinterpret_cast(getSymbol("destroy"))); - ptr = create(); -} - -template T* Module::operator->() -{ - return ptr; -} - -template T* Module::get() -{ - return ptr; -} - -template Module::~Module() throw() -{ - try { - if (handle && ptr) { - destroy(ptr); - } - if (handle) unload(); - } catch (std::exception& e) { - std::cout << "Error while destroying module: " << e.what() << std::endl; - } - destroy = 0; - handle = 0; - ptr = 0; -} - -// APR ================================================================ -#if USE_APR - -#include "apr/APRBase.h" -#include "apr/APRPool.h" - -template void Module::load(const std::string& name) -{ - CHECK_APR_SUCCESS(apr_dso_load(&handle, name.c_str(), APRPool::get())); -} - -template void Module::unload() -{ - CHECK_APR_SUCCESS(apr_dso_unload(handle)); -} - -template void* Module::getSymbol(const std::string& name) -{ - apr_dso_handle_sym_t symbol; - CHECK_APR_SUCCESS(apr_dso_sym(&symbol, handle, name.c_str())); - return (void*) symbol; -} - -// POSIX================================================================ -#else - -#include - -template void Module::load(const std::string& name) -{ - dlerror(); - handle = dlopen(name.c_str(), RTLD_NOW); - const char* error = dlerror(); - if (error) { - THROW_QPID_ERROR(INTERNAL_ERROR, error); - } -} - -template void Module::unload() -{ - dlerror(); - dlclose(handle); - const char* error = dlerror(); - if (error) { - THROW_QPID_ERROR(INTERNAL_ERROR, error); - } -} - -template void* Module::getSymbol(const std::string& name) -{ - dlerror(); - void* sym = dlsym(handle, name.c_str()); - const char* error = dlerror(); - if (error) { - THROW_QPID_ERROR(INTERNAL_ERROR, error); - } - return sym; -} - -#endif //if USE_APR - -#endif //ifndef _sys_Module_h - diff --git a/cpp/src/sys/Monitor.h b/cpp/src/sys/Monitor.h deleted file mode 100644 index 1b8ae1a527..0000000000 --- a/cpp/src/sys/Monitor.h +++ /dev/null @@ -1,55 +0,0 @@ -#ifndef _sys_Monitor_h -#define _sys_Monitor_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 -#include "Condition.h" - -#ifdef USE_APR -#include -#endif - -namespace qpid { -namespace sys { - -/** - * A monitor is a condition variable and a mutex - */ -class Monitor : public Mutex, public Condition { - public: - using Condition::wait; - inline void wait(); - inline bool wait(const Time& absoluteTime); -}; - - -void Monitor::wait() { - Condition::wait(*this); -} - -bool Monitor::wait(const Time& absoluteTime) { - return Condition::wait(*this, absoluteTime); -} - -}} -#endif /*!_sys_Monitor_h*/ diff --git a/cpp/src/sys/Mutex.h b/cpp/src/sys/Mutex.h deleted file mode 100644 index 825b519039..0000000000 --- a/cpp/src/sys/Mutex.h +++ /dev/null @@ -1,165 +0,0 @@ -#ifndef _sys_Mutex_h -#define _sys_Mutex_h - -/* - * - * 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. - * - */ - -#ifdef USE_APR -#include -#include "apr/APRBase.h" -#include "apr/APRPool.h" -#else -#include -#include "posix/check.h" -#endif -#include - -namespace qpid { -namespace sys { - -class Condition; - -/** - * Scoped lock template: calls lock() in ctor, unlock() in dtor. - * L can be any class with lock() and unlock() functions. - */ -template -class ScopedLock -{ - public: - ScopedLock(L& l) : mutex(l) { l.lock(); } - ~ScopedLock() { mutex.unlock(); } - private: - L& mutex; -}; - -template -class ScopedUnlock -{ - public: - ScopedUnlock(L& l) : mutex(l) { l.unlock(); } - ~ScopedUnlock() { mutex.lock(); } - private: - L& mutex; -}; - -/** - * Mutex lock. - */ -class Mutex : private boost::noncopyable { - public: - typedef ScopedLock ScopedLock; - typedef ScopedUnlock ScopedUnlock; - - inline Mutex(); - inline ~Mutex(); - inline void lock(); - inline void unlock(); - inline void trylock(); - - protected: -#ifdef USE_APR - apr_thread_mutex_t* mutex; -#else - pthread_mutex_t mutex; -#endif - friend class Condition; -}; - -#ifdef USE_APR -// APR ================================================================ - -Mutex::Mutex() { - CHECK_APR_SUCCESS(apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_NESTED, APRPool::get())); -} - -Mutex::~Mutex(){ - CHECK_APR_SUCCESS(apr_thread_mutex_destroy(mutex)); -} - -void Mutex::lock() { - CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex)); -} -void Mutex::unlock() { - CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex)); -} - -void Mutex::trylock() { - CHECK_APR_SUCCESS(apr_thread_mutex_trylock(mutex)); -} - -#else -// POSIX ================================================================ - -/** - * PODMutex is a POD, can be static-initialized with - * PODMutex m = QPID_PODMUTEX_INITIALIZER - */ -struct PODMutex -{ - typedef ScopedLock ScopedLock; - - inline void lock(); - inline void unlock(); - inline void trylock(); - - // Must be public to be a POD: - pthread_mutex_t mutex; -}; - -#define QPID_MUTEX_INITIALIZER { PTHREAD_MUTEX_INITIALIZER } - - -void PODMutex::lock() { - QPID_POSIX_THROW_IF(pthread_mutex_lock(&mutex)); -} -void PODMutex::unlock() { - QPID_POSIX_THROW_IF(pthread_mutex_unlock(&mutex)); -} - -void PODMutex::trylock() { - QPID_POSIX_THROW_IF(pthread_mutex_trylock(&mutex)); -} - - -Mutex::Mutex() { - QPID_POSIX_THROW_IF(pthread_mutex_init(&mutex, 0)); -} - -Mutex::~Mutex(){ - QPID_POSIX_THROW_IF(pthread_mutex_destroy(&mutex)); -} - -void Mutex::lock() { - QPID_POSIX_THROW_IF(pthread_mutex_lock(&mutex)); -} -void Mutex::unlock() { - QPID_POSIX_THROW_IF(pthread_mutex_unlock(&mutex)); -} - -void Mutex::trylock() { - QPID_POSIX_THROW_IF(pthread_mutex_trylock(&mutex)); -} - -#endif // USE_APR - -}} - - - -#endif /*!_sys_Mutex_h*/ diff --git a/cpp/src/sys/ProducerConsumer.cpp b/cpp/src/sys/ProducerConsumer.cpp deleted file mode 100644 index 1cf2a79784..0000000000 --- a/cpp/src/sys/ProducerConsumer.cpp +++ /dev/null @@ -1,141 +0,0 @@ -/* - * - * 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 "../QpidError.h" -#include "ScopedIncrement.h" -#include "ProducerConsumer.h" - -namespace qpid { -namespace sys { - -// // ================ ProducerConsumer - -ProducerConsumer::ProducerConsumer(size_t init_items) - : items(init_items), waiters(0), shutdownFlag(false) -{} - -void ProducerConsumer::shutdown() { - Mutex::ScopedLock l(monitor); - shutdownFlag = true; - monitor.notifyAll(); - // Wait for waiting consumers to wake up. - while (waiters > 0) - monitor.wait(); -} - -size_t ProducerConsumer::available() const { - Mutex::ScopedLock l(monitor); - return items; -} - -size_t ProducerConsumer::consumers() const { - Mutex::ScopedLock l(monitor); - return waiters; -} - -// ================ Lock - -ProducerConsumer::Lock::Lock(ProducerConsumer& p) - : pc(p), lock(p.monitor), status(INCOMPLETE) {} - -bool ProducerConsumer::Lock::isOk() const { - return !pc.isShutdown() && status==INCOMPLETE; -} - -void ProducerConsumer::Lock::checkOk() const { - assert(!pc.isShutdown()); - assert(status == INCOMPLETE); -} - -ProducerConsumer::Lock::~Lock() { - assert(status != INCOMPLETE || pc.isShutdown()); -} - -void ProducerConsumer::Lock::confirm() { - checkOk(); - status = CONFIRMED; -} - -void ProducerConsumer::Lock::cancel() { - checkOk(); - status = CANCELLED; -} - -// ================ ProducerLock - -ProducerConsumer::ProducerLock::ProducerLock(ProducerConsumer& p) : Lock(p) -{} - - -ProducerConsumer::ProducerLock::~ProducerLock() { - if (status == CONFIRMED) { - pc.items++; - pc.monitor.notify(); // Notify a consumer. - } -} - -// ================ ConsumerLock - -ProducerConsumer::ConsumerLock::ConsumerLock(ProducerConsumer& p) : Lock(p) -{ - if (isOk()) { - ScopedIncrement inc(pc.waiters); - while (pc.items == 0 && !pc.shutdownFlag) { - pc.monitor.wait(); - } - } -} - -ProducerConsumer::ConsumerLock::ConsumerLock( - ProducerConsumer& p, const Time& timeout) : Lock(p) -{ - if (isOk()) { - // Don't wait if timeout==0 - if (timeout == 0) { - if (pc.items == 0) - status = TIMEOUT; - return; - } - else { - Time deadline = now() + timeout; - ScopedIncrement inc(pc.waiters); - while (pc.items == 0 && !pc.shutdownFlag) { - if (!pc.monitor.wait(deadline)) { - status = TIMEOUT; - return; - } - } - } - } -} - -ProducerConsumer::ConsumerLock::~ConsumerLock() { - if (pc.isShutdown()) { - if (pc.waiters == 0) - pc.monitor.notifyAll(); // Notify shutdown thread(s) - } - else if (status==CONFIRMED) { - pc.items--; - if (pc.items > 0) - pc.monitor.notify(); // Notify another consumer. - } -} - - -}} // namespace qpid::sys diff --git a/cpp/src/sys/ProducerConsumer.h b/cpp/src/sys/ProducerConsumer.h deleted file mode 100644 index 144f4ed600..0000000000 --- a/cpp/src/sys/ProducerConsumer.h +++ /dev/null @@ -1,165 +0,0 @@ -#ifndef _sys_ProducerConsumer_h -#define _sys_ProducerConsumer_h - -/* - * - * 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 -#include "../Exception.h" -#include "Monitor.h" - -namespace qpid { -namespace sys { - -/** - * Producer-consumer synchronisation. - * - * Producers increase the number of available items, consumers reduce it. - * Consumers wait till an item is available. Waiting threads can be - * woken for shutdown using shutdown(). - * - * Note: Currently implements unbounded producer-consumer, i.e. no limit - * to available items, producers never block. Can be extended to support - * bounded PC if required. - * - // TODO aconway 2007-02-13: example, from tests. -*/ -class ProducerConsumer -{ - public: - ProducerConsumer(size_t init_items=0); - - ~ProducerConsumer() { shutdown(); } - - /** - * Wake any threads waiting for ProducerLock or ConsumerLock. - *@post No threads are waiting in Producer or Consumer locks. - */ - void shutdown(); - - /** True if queue is shutdown */ - bool isShutdown() { return shutdownFlag; } - - /** Number of items available for consumers */ - size_t available() const; - - /** Number of consumers waiting for items */ - size_t consumers() const; - - /** True if available == 0 */ - bool empty() const { return available() == 0; } - - /** - * Base class for producer and consumer locks. - */ - class Lock : private boost::noncopyable { - public: - - /** - * You must call isOk() after creating a lock to verify its state. - * - *@return true means the lock succeeded. You MUST call either - *confirm() or cancel() before the lock goes out of scope. - * - * false means the lock failed - timed out or the - * ProducerConsumer is shutdown. You should not do anything in - * the scope of the lock. - */ - bool isOk() const; - - /** - * Confirm that an item was produced/consumed. - *@pre isOk() - */ - void confirm(); - - /** - * Cancel the lock to indicate nothing was produced/consumed. - * Note that locks are not actually released until destroyed. - * - *@pre isOk() - */ - void cancel(); - - /** True if this lock experienced a timeout */ - bool isTimedOut() const { return status == TIMEOUT; } - - /** True if we have been shutdown */ - bool isShutdown() const { return pc.isShutdown(); } - - ProducerConsumer& pc; - - protected: - /** Lock status */ - enum Status { INCOMPLETE, CONFIRMED, CANCELLED, TIMEOUT }; - - Lock(ProducerConsumer& p); - ~Lock(); - void checkOk() const; - Mutex::ScopedLock lock; - Status status; - }; - - /** Lock for code that produces items. */ - struct ProducerLock : public Lock { - /** - * Acquire locks to produce an item. - *@post If isOk() the calling thread has exclusive access - * to produce an item. - */ - ProducerLock(ProducerConsumer& p); - - /** Release locks, signal waiting consumers if confirm() was called. */ - ~ProducerLock(); - }; - - /** Lock for code that consumes items */ - struct ConsumerLock : public Lock { - /** - * Wait for an item to consume and acquire locks. - * - *@post If isOk() there is at least one item available and the - *calling thread has exclusive access to consume it. - */ - ConsumerLock(ProducerConsumer& p); - - /** - * Wait up to timeout to acquire lock. - *@post If isOk() caller has a producer lock. - * If isTimedOut() there was a timeout. - * If neither then we were shutdown. - */ - ConsumerLock(ProducerConsumer& p, const Time& timeout); - - /** Release locks */ - ~ConsumerLock(); - }; - - private: - mutable Monitor monitor; - size_t items; - size_t waiters; - bool shutdownFlag; - - friend class Lock; - friend class ProducerLock; - friend class ConsumerLock; -}; - -}} // namespace qpid::sys - -#endif /*!_sys_ProducerConsumer_h*/ diff --git a/cpp/src/sys/Runnable.cpp b/cpp/src/sys/Runnable.cpp deleted file mode 100644 index 30122c682f..0000000000 --- a/cpp/src/sys/Runnable.cpp +++ /dev/null @@ -1,32 +0,0 @@ -/* - * - * 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 "Runnable.h" -#include - -namespace qpid { -namespace sys { - -Runnable::~Runnable() {} - -Runnable::Functor Runnable::functor() -{ - return boost::bind(&Runnable::run, this); -} - -}} diff --git a/cpp/src/sys/Runnable.h b/cpp/src/sys/Runnable.h deleted file mode 100644 index fb3927c612..0000000000 --- a/cpp/src/sys/Runnable.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef _Runnable_ -#define _Runnable_ -/* - * - * 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 - -namespace qpid { -namespace sys { - -/** - * Interface for objects that can be run, e.g. in a thread. - */ -class Runnable -{ - public: - /** Type to represent a runnable as a Functor */ - typedef boost::function0 Functor; - - virtual ~Runnable(); - - /** Derived classes override run(). */ - virtual void run() = 0; - - /** Create a functor object that will call this->run(). */ - Functor functor(); -}; - -}} - - -#endif diff --git a/cpp/src/sys/ScopedIncrement.h b/cpp/src/sys/ScopedIncrement.h deleted file mode 100644 index f14461ddaf..0000000000 --- a/cpp/src/sys/ScopedIncrement.h +++ /dev/null @@ -1,59 +0,0 @@ -#ifndef _posix_ScopedIncrement_h -#define _posix_ScopedIncrement_h - -/* - * - * 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 - -namespace qpid { -namespace sys { - -/** Increment counter in constructor and decrement in destructor. */ -template -class ScopedIncrement : boost::noncopyable -{ - public: - ScopedIncrement(T& c) : count(c) { ++count; } - ~ScopedIncrement() { --count; } - private: - T& count; -}; - - -/** Decrement counter in constructor and increment in destructor. */ -template -class ScopedDecrement : boost::noncopyable -{ - public: - ScopedDecrement(T& c) : count(c) { value = --count; } - ~ScopedDecrement() { ++count; } - - /** Return the value after the decrement. */ - operator long() { return value; } - - private: - T& count; - long value; -}; - - -}} - - -#endif // _posix_ScopedIncrement_h diff --git a/cpp/src/sys/ShutdownHandler.h b/cpp/src/sys/ShutdownHandler.h deleted file mode 100644 index 88baecb5b6..0000000000 --- a/cpp/src/sys/ShutdownHandler.h +++ /dev/null @@ -1,37 +0,0 @@ -/* - * - * 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 _ShutdownHandler_ -#define _ShutdownHandler_ - -namespace qpid { -namespace sys { - - class ShutdownHandler - { - public: - virtual void shutdown() = 0; - virtual ~ShutdownHandler(){} - }; - -} -} - -#endif diff --git a/cpp/src/sys/Socket.h b/cpp/src/sys/Socket.h deleted file mode 100644 index ea03222aed..0000000000 --- a/cpp/src/sys/Socket.h +++ /dev/null @@ -1,88 +0,0 @@ -#ifndef _sys_Socket_h -#define _sys_Socket_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 -#include "Time.h" - -#ifdef USE_APR -#include -#endif - -namespace qpid { -namespace sys { - -class Socket -{ - public: - /** Create an initialized TCP socket */ - static Socket createTcp(); - - /** Create a socket wrapper for descriptor. */ -#ifdef USE_APR - Socket(apr_socket_t* descriptor = 0); -#else - Socket(int descriptor = 0); -#endif - - /** Set timeout for read and write */ - void setTimeout(Time interval); - - void connect(const std::string& host, int port); - - void close(); - - enum { SOCKET_TIMEOUT=-2, SOCKET_EOF=-3 } ErrorCode; - - /** Returns bytes sent or an ErrorCode value < 0. */ - ssize_t send(const void* data, size_t size); - - /** - * Returns bytes received, an ErrorCode value < 0 or 0 - * if the connection closed in an orderly manner. - */ - ssize_t recv(void* data, size_t size); - - /** Bind to a port and start listening. - *@param port 0 means choose an available port. - *@param backlog maximum number of pending connections. - *@return The bound port. - */ - int listen(int port = 0, int backlog = 10); - - /** Get file descriptor */ - int fd(); - - private: -#ifdef USE_APR - apr_socket_t* socket; -#else - void init() const; - mutable int socket; // Initialized on demand. -#endif -}; - -}} - - -#endif /*!_sys_Socket_h*/ diff --git a/cpp/src/sys/Thread.h b/cpp/src/sys/Thread.h deleted file mode 100644 index e52f2a1b3e..0000000000 --- a/cpp/src/sys/Thread.h +++ /dev/null @@ -1,142 +0,0 @@ -#ifndef _sys_Thread_h -#define _sys_Thread_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 "Runnable.h" - -#ifdef USE_APR -#include -#include -#include "apr/APRPool.h" -#include "apr/APRBase.h" -#else -#include "posix/check.h" -#include -#endif - -namespace qpid { -namespace sys { - -class Thread -{ - public: - inline static Thread current(); - inline static void yield(); - - inline Thread(); - inline explicit Thread(qpid::sys::Runnable*); - inline explicit Thread(qpid::sys::Runnable&); - - inline void join(); - - inline long id(); - - private: -#ifdef USE_APR - static void* APR_THREAD_FUNC runRunnable(apr_thread_t* thread, void *data); - inline Thread(apr_thread_t* t); - apr_thread_t* thread; -#else - static void* runRunnable(void* runnable); - inline Thread(pthread_t); - pthread_t thread; -#endif -}; - - -Thread::Thread() : thread(0) {} - -// APR ================================================================ -#ifdef USE_APR - -Thread::Thread(Runnable* runnable) { - CHECK_APR_SUCCESS( - apr_thread_create(&thread, 0, runRunnable, runnable, APRPool::get())); -} - -Thread::Thread(Runnable& runnable) { - CHECK_APR_SUCCESS( - apr_thread_create(&thread, 0, runRunnable, &runnable, APRPool::get())); -} - -void Thread::join(){ - apr_status_t status; - if (thread != 0) - CHECK_APR_SUCCESS(apr_thread_join(&status, thread)); -} - -long Thread::id() { - return long(thread); -} - -Thread::Thread(apr_thread_t* t) : thread(t) {} - -Thread Thread::current(){ - apr_thread_t* thr; - apr_os_thread_t osthr = apr_os_thread_current(); - CHECK_APR_SUCCESS(apr_os_thread_put(&thr, &osthr, APRPool::get())); - return Thread(thr); -} - -void Thread::yield() -{ - apr_thread_yield(); -} - - -// POSIX ================================================================ -#else - -Thread::Thread(Runnable* runnable) { - QPID_POSIX_THROW_IF(pthread_create(&thread, NULL, runRunnable, runnable)); -} - -Thread::Thread(Runnable& runnable) { - QPID_POSIX_THROW_IF(pthread_create(&thread, NULL, runRunnable, &runnable)); -} - -void Thread::join(){ - QPID_POSIX_THROW_IF(pthread_join(thread, 0)); -} - -long Thread::id() { - return long(thread); -} - -Thread::Thread(pthread_t thr) : thread(thr) {} - -Thread Thread::current() { - return Thread(pthread_self()); -} - -void Thread::yield() -{ - QPID_POSIX_THROW_IF(pthread_yield()); -} - - -#endif - -}} - -#endif /*!_sys_Thread_h*/ diff --git a/cpp/src/sys/ThreadSafeQueue.h b/cpp/src/sys/ThreadSafeQueue.h deleted file mode 100644 index ce8b0b1bd9..0000000000 --- a/cpp/src/sys/ThreadSafeQueue.h +++ /dev/null @@ -1,98 +0,0 @@ -#ifndef _sys_ThreadSafeQueue_h -#define _sys_ThreadSafeQueue_h - -/* - * - * 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 -#include "ProducerConsumer.h" -#include "../Exception.h" - -namespace qpid { -namespace sys { - -/** - * A thread safe queue template. - */ -template > -class ThreadSafeQueue -{ - public: - - ThreadSafeQueue() {} - - /** Push a value onto the back of the queue */ - void push(const T& value) { - ProducerConsumer::ProducerLock producer(pc); - if (producer.isOk()) { - producer.confirm(); - container.push_back(value); - } - } - - /** Pop a value from the front of the queue. Waits till value is available. - *@throw ShutdownException if queue is shutdown while waiting. - */ - T pop() { - ProducerConsumer::ConsumerLock consumer(pc); - if (consumer.isOk()) { - consumer.confirm(); - T value(container.front()); - container.pop_front(); - return value; - } - throw ShutdownException(); - } - - /** - * If a value becomes available within the timeout, set outValue - * and return true. Otherwise return false; - */ - bool pop(T& outValue, const Time& timeout) { - ProducerConsumer::ConsumerLock consumer(pc, timeout); - if (consumer.isOk()) { - consumer.confirm(); - outValue = container.front(); - container.pop_front(); - return true; - } - return false; - } - - /** Interrupt threads waiting in pop() */ - void shutdown() { pc.shutdown(); } - - /** True if queue is shutdown */ - bool isShutdown() { return pc.isShutdown(); } - - /** Size of the queue */ - size_t size() { ProducerConsumer::Lock l(pc); return container.size(); } - - /** True if queue is empty */ - bool empty() { ProducerConsumer::Lock l(pc); return container.empty(); } - - private: - ProducerConsumer pc; - ContainerType container; -}; - -}} // namespace qpid::sys - - - -#endif /*!_sys_ThreadSafeQueue_h*/ diff --git a/cpp/src/sys/Time.cpp b/cpp/src/sys/Time.cpp deleted file mode 100644 index ad6185b966..0000000000 --- a/cpp/src/sys/Time.cpp +++ /dev/null @@ -1,60 +0,0 @@ -/* - * - * 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 "Time.h" - -namespace qpid { -namespace sys { - -// APR ================================================================ -#if USE_APR - -Time now() { return apr_time_now() * TIME_USEC; } - -// POSIX================================================================ -#else - -Time now() { - struct timespec ts; - clock_gettime(CLOCK_REALTIME, &ts); - return toTime(ts); -} - -struct timespec toTimespec(const Time& t) { - struct timespec ts; - toTimespec(ts, t); - return ts; -} - -struct timespec& toTimespec(struct timespec& ts, const Time& t) { - ts.tv_sec = t / TIME_SEC; - ts.tv_nsec = t % TIME_SEC; - return ts; -} - -Time toTime(const struct timespec& ts) { - return ts.tv_sec*TIME_SEC + ts.tv_nsec; -} - - -#endif -}} - diff --git a/cpp/src/sys/Time.h b/cpp/src/sys/Time.h deleted file mode 100644 index 47609d51df..0000000000 --- a/cpp/src/sys/Time.h +++ /dev/null @@ -1,58 +0,0 @@ -#ifndef _sys_Time_h -#define _sys_Time_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 - -#ifdef USE_APR -#include -#else -#include -#endif - -namespace qpid { -namespace sys { - -/** Time in nanoseconds */ -typedef int64_t Time; - -Time now(); - -/** Nanoseconds per second. */ -const Time TIME_SEC = 1000*1000*1000; -/** Nanoseconds per millisecond */ -const Time TIME_MSEC = 1000*1000; -/** Nanoseconds per microseconds. */ -const Time TIME_USEC = 1000; -/** Nanoseconds per nanosecond. */ -const Time TIME_NSEC = 1; - -#ifndef USE_APR -struct timespec toTimespec(const Time& t); -struct timespec& toTimespec(struct timespec& ts, const Time& t); -Time toTime(const struct timespec& ts); -#endif - -}} - -#endif /*!_sys_Time_h*/ diff --git a/cpp/src/sys/TimeoutHandler.h b/cpp/src/sys/TimeoutHandler.h deleted file mode 100644 index 0c10709bbf..0000000000 --- a/cpp/src/sys/TimeoutHandler.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * - * 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 _TimeoutHandler_ -#define _TimeoutHandler_ - -namespace qpid { -namespace sys { - - class TimeoutHandler - { - public: - virtual void idleOut() = 0; - virtual void idleIn() = 0; - virtual ~TimeoutHandler(){} - }; - -} -} - - -#endif diff --git a/cpp/src/sys/apr/APRAcceptor.cpp b/cpp/src/sys/apr/APRAcceptor.cpp deleted file mode 100644 index b2535d9e30..0000000000 --- a/cpp/src/sys/apr/APRAcceptor.cpp +++ /dev/null @@ -1,120 +0,0 @@ -/* - * - * 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 "../Acceptor.h" -#include "../ConnectionInputHandlerFactory.h" -#include "LFProcessor.h" -#include "LFSessionContext.h" -#include "APRBase.h" -#include "APRPool.h" - -namespace qpid { -namespace sys { - -class APRAcceptor : public Acceptor -{ - public: - APRAcceptor(int16_t port, int backlog, int threads, bool trace); - virtual uint16_t getPort() const; - virtual void run(qpid::sys::ConnectionInputHandlerFactory* factory); - virtual void shutdown(); - - private: - void shutdownImpl(); - - private: - int16_t port; - bool trace; - LFProcessor processor; - apr_socket_t* socket; - volatile bool running; - Mutex shutdownLock; -}; - -// Define generic Acceptor::create() to return APRAcceptor. -Acceptor::shared_ptr Acceptor::create(int16_t port, int backlog, int threads, bool trace) -{ - return Acceptor::shared_ptr(new APRAcceptor(port, backlog, threads, trace)); -} -// Must define Acceptor virtual dtor. -Acceptor::~Acceptor() {} - -APRAcceptor::APRAcceptor(int16_t port_, int backlog, int threads, bool trace_) : - port(port_), - trace(trace_), - processor(APRPool::get(), threads, 1000, 5000000), - running(false) -{ - apr_sockaddr_t* address; - CHECK_APR_SUCCESS(apr_sockaddr_info_get(&address, APR_ANYADDR, APR_UNSPEC, port, APR_IPV4_ADDR_OK, APRPool::get())); - CHECK_APR_SUCCESS(apr_socket_create(&socket, APR_INET, SOCK_STREAM, APR_PROTO_TCP, APRPool::get())); - CHECK_APR_SUCCESS(apr_socket_opt_set(socket, APR_SO_REUSEADDR, 1)); - CHECK_APR_SUCCESS(apr_socket_bind(socket, address)); - CHECK_APR_SUCCESS(apr_socket_listen(socket, backlog)); -} - -uint16_t APRAcceptor::getPort() const { - apr_sockaddr_t* address; - CHECK_APR_SUCCESS(apr_socket_addr_get(&address, APR_LOCAL, socket)); - return address->port; -} - -void APRAcceptor::run(ConnectionInputHandlerFactory* factory) { - running = true; - processor.start(); - std::cout << "Listening on port " << getPort() << "..." << std::endl; - while(running) { - apr_socket_t* client; - apr_status_t status = apr_socket_accept(&client, socket, APRPool::get()); - if(status == APR_SUCCESS){ - //make this socket non-blocking: - CHECK_APR_SUCCESS(apr_socket_timeout_set(client, 0)); - CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_SO_NONBLOCK, 1)); - CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_TCP_NODELAY, 1)); - CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_SO_SNDBUF, 32768)); - CHECK_APR_SUCCESS(apr_socket_opt_set(client, APR_SO_RCVBUF, 32768)); - LFSessionContext* session = new LFSessionContext(APRPool::get(), client, &processor, trace); - session->init(factory->create(session)); - }else{ - Mutex::ScopedLock locker(shutdownLock); - if(running) { - if(status != APR_EINTR){ - std::cout << "ERROR: " << get_desc(status) << std::endl; - } - shutdownImpl(); - } - } - } -} - -void APRAcceptor::shutdown() { - Mutex::ScopedLock locker(shutdownLock); - if (running) - shutdownImpl(); -} - -void APRAcceptor::shutdownImpl() { - running = false; - processor.stop(); - CHECK_APR_SUCCESS(apr_socket_close(socket)); -} - - -}} diff --git a/cpp/src/sys/apr/APRBase.cpp b/cpp/src/sys/apr/APRBase.cpp deleted file mode 100644 index 01c8246c91..0000000000 --- a/cpp/src/sys/apr/APRBase.cpp +++ /dev/null @@ -1,90 +0,0 @@ -/* - * - * 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 -#include "../../QpidError.h" -#include "APRBase.h" - -using namespace qpid::sys; - -APRBase* APRBase::instance = 0; - -APRBase* APRBase::getInstance(){ - if(instance == 0){ - instance = new APRBase(); - } - return instance; -} - - -APRBase::APRBase() : count(0){ - apr_initialize(); - CHECK_APR_SUCCESS(apr_pool_create(&pool, 0)); - CHECK_APR_SUCCESS(apr_thread_mutex_create(&mutex, APR_THREAD_MUTEX_NESTED, pool)); -} - -APRBase::~APRBase(){ - CHECK_APR_SUCCESS(apr_thread_mutex_destroy(mutex)); - apr_pool_destroy(pool); - apr_terminate(); -} - -bool APRBase::_increment(){ - bool deleted(false); - CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex)); - if(this == instance){ - count++; - }else{ - deleted = true; - } - CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex)); - return !deleted; -} - -void APRBase::_decrement(){ - APRBase* copy = 0; - CHECK_APR_SUCCESS(apr_thread_mutex_lock(mutex)); - if(--count == 0){ - copy = instance; - instance = 0; - } - CHECK_APR_SUCCESS(apr_thread_mutex_unlock(mutex)); - if(copy != 0){ - delete copy; - } -} - -void APRBase::increment(){ - int count = 0; - while(count++ < 2 && !getInstance()->_increment()){ - std::cout << "WARNING: APR initialization triggered concurrently with termination." << std::endl; - } -} - -void APRBase::decrement(){ - getInstance()->_decrement(); -} - -std::string qpid::sys::get_desc(apr_status_t status){ - const int size = 50; - char tmp[size]; - return std::string(apr_strerror(status, tmp, size)); -} - diff --git a/cpp/src/sys/apr/APRBase.h b/cpp/src/sys/apr/APRBase.h deleted file mode 100644 index e9dc085c3b..0000000000 --- a/cpp/src/sys/apr/APRBase.h +++ /dev/null @@ -1,78 +0,0 @@ -/* - * - * 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 _APRBase_ -#define _APRBase_ - -#include -#include -#include -#include "../../QpidError.h" - -namespace qpid { -namespace sys { - - /** - * Use of APR libraries necessitates explicit init and terminate - * calls. Any class using APR libs should obtain the reference to - * this singleton and increment on construction, decrement on - * destruction. This class can then correctly initialise apr - * before the first use and terminate after the last use. - */ - class APRBase{ - static APRBase* instance; - apr_pool_t* pool; - apr_thread_mutex_t* mutex; - int count; - - APRBase(); - ~APRBase(); - static APRBase* getInstance(); - bool _increment(); - void _decrement(); - public: - static void increment(); - static void decrement(); - }; - - //this is also a convenient place for a helper function for error checking: - void check(apr_status_t status, const char* file, const int line); - std::string get_desc(apr_status_t status); - -#define CHECK_APR_SUCCESS(A) qpid::sys::check(A, __FILE__, __LINE__); - -} -} - -// Inlined as it is called *a lot* -void inline qpid::sys::check(apr_status_t status, const char* file, const int line){ - if (status != APR_SUCCESS){ - const int size = 50; - char tmp[size]; - std::string msg(apr_strerror(status, tmp, size)); - throw qpid::QpidError(APR_ERROR + ((int) status), msg, - qpid::SrcLine(file, line)); - } -} - - - - -#endif diff --git a/cpp/src/sys/apr/APRPool.cpp b/cpp/src/sys/apr/APRPool.cpp deleted file mode 100644 index e8b71f6e8a..0000000000 --- a/cpp/src/sys/apr/APRPool.cpp +++ /dev/null @@ -1,41 +0,0 @@ -/* - * - * 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 "APRPool.h" -#include "APRBase.h" -#include - -using namespace qpid::sys; - -APRPool::APRPool(){ - APRBase::increment(); - CHECK_APR_SUCCESS(apr_pool_create(&pool, NULL)); -} - -APRPool::~APRPool(){ - apr_pool_destroy(pool); - APRBase::decrement(); -} - -apr_pool_t* APRPool::get() { - return boost::details::pool::singleton_default::instance().pool; -} - diff --git a/cpp/src/sys/apr/APRPool.h b/cpp/src/sys/apr/APRPool.h deleted file mode 100644 index da7661fcfa..0000000000 --- a/cpp/src/sys/apr/APRPool.h +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef _APRPool_ -#define _APRPool_ - -/* - * - * 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 -#include - -namespace qpid { -namespace sys { -/** - * Singleton APR memory pool. - */ -class APRPool : private boost::noncopyable { - public: - APRPool(); - ~APRPool(); - - /** Get singleton instance */ - static apr_pool_t* get(); - - private: - apr_pool_t* pool; -}; - -}} - - - - - -#endif /*!_APRPool_*/ diff --git a/cpp/src/sys/apr/APRSocket.cpp b/cpp/src/sys/apr/APRSocket.cpp deleted file mode 100644 index 96dbd132a1..0000000000 --- a/cpp/src/sys/apr/APRSocket.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - * - * 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 "APRBase.h" -#include "APRSocket.h" -#include -#include - -using namespace qpid::sys; -using namespace qpid::framing; - -APRSocket::APRSocket(apr_socket_t* _socket) : socket(_socket), closed(false){ - -} - -void APRSocket::read(qpid::framing::Buffer& buffer){ - apr_size_t bytes; - bytes = buffer.available(); - apr_status_t s = apr_socket_recv(socket, buffer.start(), &bytes); - buffer.move(bytes); - if(APR_STATUS_IS_TIMEUP(s)){ - //timed out - }else if(APR_STATUS_IS_EOF(s)){ - close(); - } -} - -void APRSocket::write(qpid::framing::Buffer& buffer){ - apr_size_t bytes; - do{ - bytes = buffer.available(); - apr_socket_send(socket, buffer.start(), &bytes); - buffer.move(bytes); - }while(bytes > 0); -} - -void APRSocket::close(){ - if(!closed){ - std::cout << "Closing socket " << socket << "@" << this << std::endl; - CHECK_APR_SUCCESS(apr_socket_close(socket)); - closed = true; - } -} - -bool APRSocket::isOpen() const { - return !closed; -} - -uint8_t APRSocket::read(){ - char data[1]; - apr_size_t bytes = 1; - apr_status_t s = apr_socket_recv(socket, data, &bytes); - if(APR_STATUS_IS_EOF(s) || bytes == 0){ - return 0; - }else{ - return *data; - } -} - -APRSocket::~APRSocket(){ -} diff --git a/cpp/src/sys/apr/APRSocket.h b/cpp/src/sys/apr/APRSocket.h deleted file mode 100644 index 323500e61a..0000000000 --- a/cpp/src/sys/apr/APRSocket.h +++ /dev/null @@ -1,48 +0,0 @@ -/* - * - * 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 _APRSocket_ -#define _APRSocket_ - -#include -#include "../../framing/Buffer.h" - -namespace qpid { -namespace sys { - - class APRSocket - { - apr_socket_t* const socket; - volatile bool closed; - public: - APRSocket(apr_socket_t* socket); - void read(qpid::framing::Buffer& b); - void write(qpid::framing::Buffer& b); - void close(); - bool isOpen() const; - uint8_t read(); - ~APRSocket(); - }; - -} -} - - -#endif diff --git a/cpp/src/sys/apr/LFProcessor.cpp b/cpp/src/sys/apr/LFProcessor.cpp deleted file mode 100644 index 691af97c70..0000000000 --- a/cpp/src/sys/apr/LFProcessor.cpp +++ /dev/null @@ -1,179 +0,0 @@ -/* - * - * 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 -#include "../../QpidError.h" -#include "LFProcessor.h" -#include "APRBase.h" -#include "LFSessionContext.h" - -using namespace qpid::sys; -using qpid::QpidError; - -// TODO aconway 2006-10-12: stopped is read outside locks. -// - -LFProcessor::LFProcessor(apr_pool_t* pool, int _workers, int _size, int _timeout) : - size(_size), - timeout(_timeout), - signalledCount(0), - current(0), - count(0), - workerCount(_workers), - hasLeader(false), - workers(new Thread[_workers]), - stopped(false) -{ - - CHECK_APR_SUCCESS(apr_pollset_create(&pollset, size, pool, APR_POLLSET_THREADSAFE)); -} - - -LFProcessor::~LFProcessor(){ - if (!stopped) stop(); - delete[] workers; - CHECK_APR_SUCCESS(apr_pollset_destroy(pollset)); -} - -void LFProcessor::start(){ - for(int i = 0; i < workerCount; i++){ - workers[i] = Thread(this); - } -} - -void LFProcessor::add(const apr_pollfd_t* const fd){ - CHECK_APR_SUCCESS(apr_pollset_add(pollset, fd)); - Monitor::ScopedLock l(countLock); - sessions.push_back(reinterpret_cast(fd->client_data)); - count++; -} - -void LFProcessor::remove(const apr_pollfd_t* const fd){ - CHECK_APR_SUCCESS(apr_pollset_remove(pollset, fd)); - Monitor::ScopedLock l(countLock); - sessions.erase(find(sessions.begin(), sessions.end(), reinterpret_cast(fd->client_data))); - count--; -} - -void LFProcessor::reactivate(const apr_pollfd_t* const fd){ - CHECK_APR_SUCCESS(apr_pollset_add(pollset, fd)); -} - -void LFProcessor::deactivate(const apr_pollfd_t* const fd){ - CHECK_APR_SUCCESS(apr_pollset_remove(pollset, fd)); -} - -void LFProcessor::update(const apr_pollfd_t* const fd){ - CHECK_APR_SUCCESS(apr_pollset_remove(pollset, fd)); - CHECK_APR_SUCCESS(apr_pollset_add(pollset, fd)); -} - -bool LFProcessor::full(){ - Mutex::ScopedLock locker(countLock); - return count == size; -} - -bool LFProcessor::empty(){ - Mutex::ScopedLock locker(countLock); - return count == 0; -} - -void LFProcessor::poll() { - apr_status_t status = APR_EGENERAL; - do{ - current = 0; - if(!stopped){ - status = apr_pollset_poll(pollset, timeout, &signalledCount, &signalledFDs); - } - }while(status != APR_SUCCESS && !stopped); -} - -void LFProcessor::run(){ - try{ - while(!stopped){ - const apr_pollfd_t* event = 0; - LFSessionContext* session = 0; - { - Monitor::ScopedLock l(leadLock); - waitToLead(); - event = getNextEvent(); - if(!event) return; - session = reinterpret_cast( - event->client_data); - session->startProcessing(); - relinquishLead(); - } - - //process event: - if(event->rtnevents & APR_POLLIN) session->read(); - if(event->rtnevents & APR_POLLOUT) session->write(); - - if(session->isClosed()){ - session->handleClose(); - Monitor::ScopedLock l(countLock); - sessions.erase(find(sessions.begin(),sessions.end(), session)); - count--; - }else{ - session->stopProcessing(); - } - } - }catch(std::exception e){ - std::cout << e.what() << std::endl; - } -} - -void LFProcessor::waitToLead(){ - while(hasLeader && !stopped) leadLock.wait(); - hasLeader = !stopped; -} - -void LFProcessor::relinquishLead(){ - hasLeader = false; - leadLock.notify(); -} - -const apr_pollfd_t* LFProcessor::getNextEvent(){ - while(true){ - if(stopped){ - return 0; - }else if(current < signalledCount){ - //use result of previous poll if one is available - return signalledFDs + (current++); - }else{ - //else poll to get new events - poll(); - } - } -} - -void LFProcessor::stop(){ - stopped = true; - { - Monitor::ScopedLock l(leadLock); - leadLock.notifyAll(); - } - for(int i = 0; i < workerCount; i++){ - workers[i].join(); - } - for(iterator i = sessions.begin(); i < sessions.end(); i++){ - (*i)->shutdown(); - } -} - diff --git a/cpp/src/sys/apr/LFProcessor.h b/cpp/src/sys/apr/LFProcessor.h deleted file mode 100644 index 62f6536bde..0000000000 --- a/cpp/src/sys/apr/LFProcessor.h +++ /dev/null @@ -1,121 +0,0 @@ -/* - * - * 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 _LFProcessor_ -#define _LFProcessor_ - -#include -#include -#include -#include "../Monitor.h" -#include "../Runnable.h" -#include "../Thread.h" - -namespace qpid { -namespace sys { - - class LFSessionContext; - - /** - * This class processes a poll set using the leaders-followers - * pattern for thread synchronization: the leader will poll and on - * the poll returning, it will remove a session, promote a - * follower to leadership, then process the session. - */ - class LFProcessor : private virtual qpid::sys::Runnable - { - typedef std::vector::iterator iterator; - - const int size; - const apr_interval_time_t timeout; - apr_pollset_t* pollset; - int signalledCount; - int current; - const apr_pollfd_t* signalledFDs; - int count; - const int workerCount; - bool hasLeader; - qpid::sys::Thread* workers; - qpid::sys::Monitor leadLock; - qpid::sys::Mutex countLock; - std::vector sessions; - volatile bool stopped; - - const apr_pollfd_t* getNextEvent(); - void waitToLead(); - void relinquishLead(); - void poll(); - virtual void run(); - - public: - LFProcessor(apr_pool_t* pool, int workers, int size, int timeout); - /** - * Add the fd to the poll set. Relies on the client_data being - * an instance of LFSessionContext. - */ - void add(const apr_pollfd_t* const fd); - /** - * Remove the fd from the poll set. - */ - void remove(const apr_pollfd_t* const fd); - /** - * Signal that the fd passed in, already part of the pollset, - * has had its flags altered. - */ - void update(const apr_pollfd_t* const fd); - /** - * Add an fd back to the poll set after deactivation. - */ - void reactivate(const apr_pollfd_t* const fd); - /** - * Temporarily remove the fd from the poll set. Called when processing - * is about to begin. - */ - void deactivate(const apr_pollfd_t* const fd); - /** - * Indicates whether the capacity of this processor has been - * reached (or whether it can still handle further fd's). - */ - bool full(); - /** - * Indicates whether there are any fd's registered. - */ - bool empty(); - /** - * Stop processing. - */ - void stop(); - /** - * Start processing. - */ - void start(); - /** - * Is processing stopped? - */ - bool isStopped(); - - ~LFProcessor(); - }; - -} -} - - -#endif diff --git a/cpp/src/sys/apr/LFSessionContext.cpp b/cpp/src/sys/apr/LFSessionContext.cpp deleted file mode 100644 index a3616d80a4..0000000000 --- a/cpp/src/sys/apr/LFSessionContext.cpp +++ /dev/null @@ -1,179 +0,0 @@ -/* - * - * 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 "LFSessionContext.h" -#include "APRBase.h" -#include "../../QpidError.h" -#include - -using namespace qpid::sys; -using namespace qpid::sys; -using namespace qpid::framing; - -LFSessionContext::LFSessionContext(apr_pool_t* _pool, apr_socket_t* _socket, - LFProcessor* const _processor, - bool _debug) : - debug(_debug), - socket(_socket), - initiated(false), - in(65536), - out(65536), - processor(_processor), - processing(false), - closing(false) -{ - - fd.p = _pool; - fd.desc_type = APR_POLL_SOCKET; - fd.reqevents = APR_POLLIN; - fd.client_data = this; - fd.desc.s = _socket; - - out.flip(); -} - -LFSessionContext::~LFSessionContext(){ - -} - -void LFSessionContext::read(){ - socket.read(in); - in.flip(); - if(initiated){ - AMQFrame frame; - try{ - while(frame.decode(in)){ - if(debug) log("RECV", &frame); - handler->received(&frame); - } - }catch(QpidError error){ - std::cout << "Error [" << error.code << "] " << error.msg - << " (" << error.loc.file << ":" << error.loc.line - << ")" << std::endl; - } - }else{ - ProtocolInitiation protocolInit; - if(protocolInit.decode(in)){ - handler->initiated(protocolInit); - initiated = true; - if(debug) std::cout << "INIT [" << &socket << "]" << std::endl; - } - } - in.compact(); -} - -void LFSessionContext::write(){ - bool done = isClosed(); - while(!done){ - if(out.available() > 0){ - socket.write(out); - if(out.available() > 0){ - - //incomplete write, leave flags to receive notification of readiness to write - done = true;//finished processing for now, but write is still in progress - } - }else{ - //do we have any frames to write? - Mutex::ScopedLock l(writeLock); - if(!framesToWrite.empty()){ - out.clear(); - bool encoded(false); - AMQFrame* frame = framesToWrite.front(); - while(frame && out.available() >= frame->size()){ - encoded = true; - frame->encode(out); - if(debug) log("SENT", frame); - delete frame; - framesToWrite.pop(); - frame = framesToWrite.empty() ? 0 : framesToWrite.front(); - } - if(!encoded) THROW_QPID_ERROR(FRAMING_ERROR, "Could not write frame, too large for buffer."); - out.flip(); - }else{ - //reset flags, don't care about writability anymore - fd.reqevents = APR_POLLIN; - done = true; - - if(closing){ - socket.close(); - } - } - } - } -} - -void LFSessionContext::send(AMQFrame* frame){ - Mutex::ScopedLock l(writeLock); - if(!closing){ - framesToWrite.push(frame); - if(!(fd.reqevents & APR_POLLOUT)){ - fd.reqevents |= APR_POLLOUT; - if(!processing){ - processor->update(&fd); - } - } - } -} - -void LFSessionContext::startProcessing(){ - Mutex::ScopedLock l(writeLock); - processing = true; - processor->deactivate(&fd); -} - -void LFSessionContext::stopProcessing(){ - Mutex::ScopedLock l(writeLock); - processor->reactivate(&fd); - processing = false; -} - -void LFSessionContext::close(){ - Mutex::ScopedLock l(writeLock); - closing = true; - if(!processing){ - //allow pending frames to be written to socket - fd.reqevents = APR_POLLOUT; - processor->update(&fd); - } -} - -void LFSessionContext::handleClose(){ - handler->closed(); - std::cout << "Session closed [" << &socket << "]" << std::endl; - delete handler; - delete this; -} - -void LFSessionContext::shutdown(){ - socket.close(); - handleClose(); -} - -void LFSessionContext::init(ConnectionInputHandler* _handler){ - handler = _handler; - processor->add(&fd); -} - -void LFSessionContext::log(const std::string& desc, AMQFrame* const frame){ - Mutex::ScopedLock l(logLock); - std::cout << desc << " [" << &socket << "]: " << *frame << std::endl; -} - -Mutex LFSessionContext::logLock; diff --git a/cpp/src/sys/apr/LFSessionContext.h b/cpp/src/sys/apr/LFSessionContext.h deleted file mode 100644 index 99aa39bd6e..0000000000 --- a/cpp/src/sys/apr/LFSessionContext.h +++ /dev/null @@ -1,90 +0,0 @@ -/* - * - * 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 _LFSessionContext_ -#define _LFSessionContext_ - -#include - -#include -#include -#include - -#include "../../framing/AMQFrame.h" -#include "../../framing/Buffer.h" -#include "../Monitor.h" -#include "../ConnectionOutputHandler.h" -#include "../ConnectionInputHandler.h" - -#include "APRSocket.h" -#include "LFProcessor.h" - -namespace qpid { -namespace sys { - - -class LFSessionContext : public virtual qpid::sys::ConnectionOutputHandler -{ - const bool debug; - APRSocket socket; - bool initiated; - - qpid::framing::Buffer in; - qpid::framing::Buffer out; - - qpid::sys::ConnectionInputHandler* handler; - LFProcessor* const processor; - - apr_pollfd_t fd; - - std::queue framesToWrite; - qpid::sys::Mutex writeLock; - - bool processing; - bool closing; - - static qpid::sys::Mutex logLock; - void log(const std::string& desc, - qpid::framing::AMQFrame* const frame); - - - public: - LFSessionContext(apr_pool_t* pool, apr_socket_t* socket, - LFProcessor* const processor, - bool debug = false); - virtual ~LFSessionContext(); - virtual void send(qpid::framing::AMQFrame* frame); - virtual void close(); - void read(); - void write(); - void init(qpid::sys::ConnectionInputHandler* handler); - void startProcessing(); - void stopProcessing(); - void handleClose(); - void shutdown(); - inline apr_pollfd_t* const getFd(){ return &fd; } - inline bool isClosed(){ return !socket.isOpen(); } -}; - -} -} - - -#endif diff --git a/cpp/src/sys/apr/Socket.cpp b/cpp/src/sys/apr/Socket.cpp deleted file mode 100644 index 1b9c980441..0000000000 --- a/cpp/src/sys/apr/Socket.cpp +++ /dev/null @@ -1,86 +0,0 @@ -/* - * - * 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 "../Socket.h" -#include "APRBase.h" -#include "APRPool.h" - - -using namespace qpid::sys; - -Socket Socket::createTcp() { - Socket s; - CHECK_APR_SUCCESS( - apr_socket_create( - &s.socket, APR_INET, SOCK_STREAM, APR_PROTO_TCP, - APRPool::get())); - return s; -} - -Socket::Socket(apr_socket_t* s) { - socket = s; -} - -void Socket::setTimeout(Time interval) { - apr_socket_timeout_set(socket, interval/TIME_USEC); -} - -void Socket::connect(const std::string& host, int port) { - apr_sockaddr_t* address; - CHECK_APR_SUCCESS( - apr_sockaddr_info_get( - &address, host.c_str(), APR_UNSPEC, port, APR_IPV4_ADDR_OK, - APRPool::get())); - CHECK_APR_SUCCESS(apr_socket_connect(socket, address)); -} - -void Socket::close() { - if (socket == 0) return; - CHECK_APR_SUCCESS(apr_socket_close(socket)); - socket = 0; -} - -ssize_t Socket::send(const void* data, size_t size) -{ - apr_size_t sent = size; - apr_status_t status = - apr_socket_send(socket, reinterpret_cast(data), &sent); - if (APR_STATUS_IS_TIMEUP(status)) return SOCKET_TIMEOUT; - if (APR_STATUS_IS_EOF(status)) return SOCKET_EOF; - CHECK_APR_SUCCESS(status); - return sent; -} - -ssize_t Socket::recv(void* data, size_t size) -{ - apr_size_t received = size; - apr_status_t status = - apr_socket_recv(socket, reinterpret_cast(data), &received); - if (APR_STATUS_IS_TIMEUP(status)) - return SOCKET_TIMEOUT; - if (APR_STATUS_IS_EOF(status)) - return SOCKET_EOF; - CHECK_APR_SUCCESS(status); - return received; -} - - diff --git a/cpp/src/sys/apr/Thread.cpp b/cpp/src/sys/apr/Thread.cpp deleted file mode 100644 index 67d2853d9f..0000000000 --- a/cpp/src/sys/apr/Thread.cpp +++ /dev/null @@ -1,33 +0,0 @@ -/* - * - * 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 "../Thread.h" - -using namespace qpid::sys; -using qpid::sys::Runnable; - -void* APR_THREAD_FUNC Thread::runRunnable(apr_thread_t* thread, void *data) { - reinterpret_cast(data)->run(); - CHECK_APR_SUCCESS(apr_thread_exit(thread, APR_SUCCESS)); - return NULL; -} - - diff --git a/cpp/src/sys/posix/EventChannel.cpp b/cpp/src/sys/posix/EventChannel.cpp deleted file mode 100644 index 1d584d6318..0000000000 --- a/cpp/src/sys/posix/EventChannel.cpp +++ /dev/null @@ -1,325 +0,0 @@ -/* - * - * 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 -#include -#include - -#include -#include -#include - -#include -#include -#include - -#include -#include - -#include "../../QpidError.h" -#include "../Monitor.h" - -#include "check.h" -#include "EventChannel.h" - -using namespace std; - - -// Convenience template to zero out a struct. -template struct ZeroStruct : public S { - ZeroStruct() { memset(this, 0, sizeof(*this)); } -}; - -namespace qpid { -namespace sys { - - -/** - * EventHandler wraps an epoll file descriptor. Acts as private - * interface between EventChannel and subclasses. - * - * Also implements Event interface for events that are not associated - * with a file descriptor and are passed via the message queue. - */ -class EventHandler : public Event, private Monitor -{ - public: - EventHandler(int epollSize = 256); - ~EventHandler(); - - int getEpollFd() { return epollFd; } - void epollAdd(int fd, uint32_t epollEvents, Event* event); - void epollMod(int fd, uint32_t epollEvents, Event* event); - void epollDel(int fd); - - void mqPut(Event* event); - Event* mqGet(); - - protected: - // Should never be called, only complete. - void prepare(EventHandler&) { assert(0); } - Event* complete(EventHandler& eh); - - private: - int epollFd; - std::string mqName; - int mqFd; - std::queue mqEvents; -}; - -EventHandler::EventHandler(int epollSize) -{ - epollFd = epoll_create(epollSize); - if (epollFd < 0) throw QPID_POSIX_ERROR(errno); - - // Create a POSIX message queue for non-fd events. - // We write one byte and never read it is always ready for read - // when we add it to epoll. - // - ZeroStruct attr; - attr.mq_maxmsg = 1; - attr.mq_msgsize = 1; - do { - char tmpnam[L_tmpnam]; - tmpnam_r(tmpnam); - mqName = tmpnam + 4; // Skip "tmp/" - mqFd = mq_open( - mqName.c_str(), O_CREAT|O_EXCL|O_RDWR|O_NONBLOCK, S_IRWXU, &attr); - if (mqFd < 0) throw QPID_POSIX_ERROR(errno); - } while (mqFd == EEXIST); // Name already taken, try again. - - static char zero = '\0'; - mq_send(mqFd, &zero, 1, 0); - epollAdd(mqFd, 0, this); -} - -EventHandler::~EventHandler() { - mq_close(mqFd); - mq_unlink(mqName.c_str()); -} - -void EventHandler::mqPut(Event* event) { - ScopedLock l(*this); - assert(event != 0); - mqEvents.push(event); - epollMod(mqFd, EPOLLIN|EPOLLONESHOT, this); -} - -Event* EventHandler::mqGet() { - ScopedLock l(*this); - if (mqEvents.empty()) - return 0; - Event* event = mqEvents.front(); - mqEvents.pop(); - if(!mqEvents.empty()) - epollMod(mqFd, EPOLLIN|EPOLLONESHOT, this); - return event; -} - -void EventHandler::epollAdd(int fd, uint32_t epollEvents, Event* event) -{ - ZeroStruct ee; - ee.data.ptr = event; - ee.events = epollEvents; - if (epoll_ctl(epollFd, EPOLL_CTL_ADD, fd, &ee) < 0) - throw QPID_POSIX_ERROR(errno); -} - -void EventHandler::epollMod(int fd, uint32_t epollEvents, Event* event) -{ - ZeroStruct ee; - ee.data.ptr = event; - ee.events = epollEvents; - if (epoll_ctl(epollFd, EPOLL_CTL_MOD, fd, &ee) < 0) - throw QPID_POSIX_ERROR(errno); -} - -void EventHandler::epollDel(int fd) { - if (epoll_ctl(epollFd, EPOLL_CTL_DEL, fd, 0) < 0) - throw QPID_POSIX_ERROR(errno); -} - -Event* EventHandler::complete(EventHandler& eh) -{ - assert(&eh == this); - Event* event = mqGet(); - return event==0 ? 0 : event->complete(eh); -} - -// ================================================================ -// EventChannel - -EventChannel::shared_ptr EventChannel::create() { - return shared_ptr(new EventChannel()); -} - -EventChannel::EventChannel() : handler(new EventHandler()) {} - -EventChannel::~EventChannel() {} - -void EventChannel::postEvent(Event& e) -{ - e.prepare(*handler); -} - -Event* EventChannel::getEvent() -{ - static const int infiniteTimeout = -1; - ZeroStruct epollEvent; - - // Loop until we can complete the event. Some events may re-post - // themselves and return 0 from complete, e.g. partial reads. // - Event* event = 0; - while (event == 0) { - int eventCount = epoll_wait(handler->getEpollFd(), - &epollEvent, 1, infiniteTimeout); - if (eventCount < 0) { - if (errno != EINTR) { - // TODO aconway 2006-11-28: Proper handling/logging of errors. - cerr << BOOST_CURRENT_FUNCTION << " ignoring error " - << PosixError::getMessage(errno) << endl; - assert(0); - } - } - else if (eventCount == 1) { - event = reinterpret_cast(epollEvent.data.ptr); - assert(event != 0); - try { - event = event->complete(*handler); - } - catch (const Exception& e) { - if (event) - event->setError(e); - } - catch (const std::exception& e) { - if (event) - event->setError(e); - } - } - } - return event; -} - -Event::~Event() {} - -void Event::prepare(EventHandler& handler) -{ - handler.mqPut(this); -} - -bool Event::hasError() const { - return error; -} - -void Event::throwIfError() throw (Exception) { - if (hasError()) - error.throwSelf(); -} - -Event* Event::complete(EventHandler&) -{ - return this; -} - -void Event::dispatch() -{ - try { - if (!callback.empty()) - callback(); - } catch (const std::exception&) { - throw; - } catch (...) { - throw QPID_ERROR(INTERNAL_ERROR, "Unknown exception."); - } -} - -void Event::setError(const ExceptionHolder& e) { - error = e; -} - -void ReadEvent::prepare(EventHandler& handler) -{ - handler.epollAdd(descriptor, EPOLLIN | EPOLLONESHOT, this); -} - -ssize_t ReadEvent::doRead() { - ssize_t n = ::read(descriptor, static_cast(buffer) + received, - size - received); - if (n > 0) received += n; - return n; -} - -Event* ReadEvent::complete(EventHandler& handler) -{ - // Read as much as possible without blocking. - ssize_t n = doRead(); - while (n > 0 && received < size) doRead(); - - if (received == size) { - handler.epollDel(descriptor); - received = 0; // Reset for re-use. - return this; - } - else if (n <0 && (errno == EAGAIN)) { - // Keep polling for more. - handler.epollMod(descriptor, EPOLLIN | EPOLLONESHOT, this); - return 0; - } - else { - // Unexpected EOF or error. Throw ENODATA for EOF. - handler.epollDel(descriptor); - received = 0; // Reset for re-use. - throw QPID_POSIX_ERROR((n < 0) ? errno : ENODATA); - } -} - -void WriteEvent::prepare(EventHandler& handler) -{ - handler.epollAdd(descriptor, EPOLLOUT | EPOLLONESHOT, this); -} - -Event* WriteEvent::complete(EventHandler& handler) -{ - ssize_t n = write(descriptor, static_cast(buffer) + written, - size - written); - if (n < 0) throw QPID_POSIX_ERROR(errno); - written += n; - if(written < size) { - // Keep polling. - handler.epollMod(descriptor, EPOLLOUT | EPOLLONESHOT, this); - return 0; - } - written = 0; // Reset for re-use. - handler.epollDel(descriptor); - return this; -} - -void AcceptEvent::prepare(EventHandler& handler) -{ - handler.epollAdd(descriptor, EPOLLIN | EPOLLONESHOT, this); -} - -Event* AcceptEvent::complete(EventHandler& handler) -{ - handler.epollDel(descriptor); - accepted = ::accept(descriptor, 0, 0); - if (accepted < 0) throw QPID_POSIX_ERROR(errno); - return this; -} - -}} diff --git a/cpp/src/sys/posix/EventChannel.h b/cpp/src/sys/posix/EventChannel.h deleted file mode 100644 index 4177839d00..0000000000 --- a/cpp/src/sys/posix/EventChannel.h +++ /dev/null @@ -1,176 +0,0 @@ -#ifndef _sys_EventChannel_h -#define _sys_EventChannel_h - -/* - * - * 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 "../../SharedObject.h" -#include "../../ExceptionHolder.h" -#include -#include - -namespace qpid { -namespace sys { - -class Event; -class EventHandler; -class EventChannel; - -/** - * Base class for all Events. - */ -class Event -{ - public: - /** Type for callback when event is dispatched */ - typedef boost::function0 Callback; - - /** - * Create an event with optional callback. - * Instances of Event are sent directly through the channel. - * Derived classes define additional waiting behaviour. - *@param cb A callback functor that is invoked when dispatch() is called. - */ - Event(Callback cb = 0) : callback(cb) {} - - virtual ~Event(); - - /** Call the callback provided to the constructor, if any. */ - void dispatch(); - - /** True if there was an error processing this event */ - bool hasError() const; - - /** If hasError() throw the corresponding exception. */ - void throwIfError() throw(Exception); - - protected: - virtual void prepare(EventHandler&); - virtual Event* complete(EventHandler&); - void setError(const ExceptionHolder& e); - - Callback callback; - ExceptionHolder error; - - friend class EventChannel; - friend class EventHandler; -}; - -template -class IOEvent : public Event { - public: - void getDescriptor() const { return descriptor; } - size_t getSize() const { return size; } - BufT getBuffer() const { return buffer; } - - protected: - IOEvent(int fd, Callback cb, size_t sz, BufT buf) : - Event(cb), descriptor(fd), buffer(buf), size(sz) {} - - int descriptor; - BufT buffer; - size_t size; -}; - -/** Asynchronous read event */ -class ReadEvent : public IOEvent -{ - public: - explicit ReadEvent(int fd=-1, void* buf=0, size_t sz=0, Callback cb=0) : - IOEvent(fd, cb, sz, buf), received(0) {} - - private: - void prepare(EventHandler&); - Event* complete(EventHandler&); - ssize_t doRead(); - - size_t received; -}; - -/** Asynchronous write event */ -class WriteEvent : public IOEvent -{ - public: - explicit WriteEvent(int fd=-1, const void* buf=0, size_t sz=0, - Callback cb=0) : - IOEvent(fd, cb, sz, buf), written(0) {} - - protected: - void prepare(EventHandler&); - Event* complete(EventHandler&); - - private: - ssize_t doWrite(); - size_t written; -}; - -/** Asynchronous socket accept event */ -class AcceptEvent : public Event -{ - public: - /** Accept a connection on fd. */ - explicit AcceptEvent(int fd=-1, Callback cb=0) : - Event(cb), descriptor(fd), accepted(0) {} - - /** Get descriptor for server socket */ - int getAcceptedDesscriptor() const { return accepted; } - - private: - void prepare(EventHandler&); - Event* complete(EventHandler&); - - int descriptor; - int accepted; -}; - - -class QueueSet; - -/** - * Channel to post and wait for events. - */ -class EventChannel : public qpid::SharedObject -{ - public: - static shared_ptr create(); - - ~EventChannel(); - - /** Post an event to the channel. */ - void postEvent(Event& event); - - /** Post an event to the channel. Must not be 0. */ - void postEvent(Event* event) { postEvent(*event); } - - /** - * Wait for the next complete event. - *@return Pointer to event. Will never return 0. - */ - Event* getEvent(); - - private: - EventChannel(); - boost::shared_ptr handler; -}; - - -}} - - - -#endif /*!_sys_EventChannel_h*/ diff --git a/cpp/src/sys/posix/EventChannelAcceptor.cpp b/cpp/src/sys/posix/EventChannelAcceptor.cpp deleted file mode 100644 index 94dda99d78..0000000000 --- a/cpp/src/sys/posix/EventChannelAcceptor.cpp +++ /dev/null @@ -1,149 +0,0 @@ -/* - * - * 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 - -#include -#include -#include -#include -#include - -#include "../ConnectionOutputHandler.h" -#include "../ConnectionInputHandler.h" -#include "../ConnectionInputHandlerFactory.h" -#include "../Acceptor.h" -#include "../Socket.h" -#include "../../framing/Buffer.h" -#include "../../framing/AMQFrame.h" -#include "../../Exception.h" - -#include "EventChannelConnection.h" - -namespace qpid { -namespace sys { - -using namespace qpid::framing; -using namespace std; - -class EventChannelAcceptor : public Acceptor { - public: - - - EventChannelAcceptor( - int16_t port_, int backlog, int nThreads, bool trace_ - ); - - int getPort() const; - - void run(ConnectionInputHandlerFactory& factory); - - void shutdown(); - - private: - - void accept(); - - Mutex lock; - Socket listener; - const int port; - const bool isTrace; - bool isRunning; - boost::ptr_vector connections; - AcceptEvent acceptEvent; - ConnectionInputHandlerFactory* factory; - bool isShutdown; - EventChannelThreads::shared_ptr threads; -}; - -Acceptor::shared_ptr Acceptor::create( - int16_t port, int backlog, int threads, bool trace) -{ - return Acceptor::shared_ptr( - new EventChannelAcceptor(port, backlog, threads, trace)); -} - -// Must define Acceptor virtual dtor. -Acceptor::~Acceptor() {} - -EventChannelAcceptor::EventChannelAcceptor( - int16_t port_, int backlog, int nThreads, bool trace_ -) : listener(Socket::createTcp()), - port(listener.listen(int(port_), backlog)), - isTrace(trace_), - isRunning(false), - acceptEvent(listener.fd(), - boost::bind(&EventChannelAcceptor::accept, this)), - factory(0), - isShutdown(false), - threads(EventChannelThreads::create(EventChannel::create(), nThreads)) -{ } - -int EventChannelAcceptor::getPort() const { - return port; // Immutable no need for lock. -} - -void EventChannelAcceptor::run(ConnectionInputHandlerFactory& f) { - { - Mutex::ScopedLock l(lock); - if (!isRunning && !isShutdown) { - isRunning = true; - factory = &f; - threads->post(acceptEvent); - } - } - threads->join(); // Wait for shutdown. -} - -void EventChannelAcceptor::shutdown() { - bool doShutdown = false; - { - Mutex::ScopedLock l(lock); - doShutdown = !isShutdown; // I'm the shutdown thread. - isShutdown = true; - } - if (doShutdown) { - ::close(acceptEvent.getDescriptor()); - threads->shutdown(); - for_each(connections.begin(), connections.end(), - boost::bind(&EventChannelConnection::close, _1)); - } - threads->join(); -} - -void EventChannelAcceptor::accept() -{ - // No lock, we only post one accept event at a time. - if (isShutdown) - return; - if (acceptEvent.getException()) { - Exception::log(*acceptEvent.getException(), - "EventChannelAcceptor::accept"); - shutdown(); - return; - } - // TODO aconway 2006-11-29: Need to reap closed connections also. - int fd = acceptEvent.getAcceptedDesscriptor(); - connections.push_back( - new EventChannelConnection(threads, *factory, fd, fd, isTrace)); - threads->post(acceptEvent); // Keep accepting. -} - -}} // namespace qpid::sys diff --git a/cpp/src/sys/posix/EventChannelConnection.cpp b/cpp/src/sys/posix/EventChannelConnection.cpp deleted file mode 100644 index cb6506bdde..0000000000 --- a/cpp/src/sys/posix/EventChannelConnection.cpp +++ /dev/null @@ -1,229 +0,0 @@ -/* - * - * 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 - -#include -#include - -#include "EventChannelConnection.h" -#include "../ConnectionInputHandlerFactory.h" -#include "../../QpidError.h" - -using namespace std; -using namespace qpid; -using namespace qpid::framing; - -namespace qpid { -namespace sys { - -const size_t EventChannelConnection::bufferSize = 65536; - -EventChannelConnection::EventChannelConnection( - EventChannelThreads::shared_ptr threads_, - ConnectionInputHandlerFactory& factory_, - int rfd, - int wfd, - bool isTrace_ -) : - readFd(rfd), - writeFd(wfd ? wfd : rfd), - readCallback(boost::bind(&EventChannelConnection::closeOnException, - this, &EventChannelConnection::endInitRead)), - - isWriting(false), - isClosed(false), - threads(threads_), - handler(factory_.create(this)), - in(bufferSize), - out(bufferSize), - isTrace(isTrace_) -{ - BOOST_ASSERT(readFd > 0); - BOOST_ASSERT(writeFd > 0); - closeOnException(&EventChannelConnection::startRead); -} - - -void EventChannelConnection::send(std::auto_ptr frame) { - { - Monitor::ScopedLock lock(monitor); - assert(frame.get()); - writeFrames.push_back(frame.release()); - } - closeOnException(&EventChannelConnection::startWrite); -} - -void EventChannelConnection::close() { - { - Monitor::ScopedLock lock(monitor); - if (isClosed) - return; - isClosed = true; - } - ::close(readFd); - ::close(writeFd); - { - Monitor::ScopedLock lock(monitor); - while (busyThreads > 0) - monitor.wait(); - } - handler->closed(); -} - -void EventChannelConnection::closeNoThrow() { - Exception::tryCatchLog( - boost::bind(&EventChannelConnection::close, this), - false, - "Exception closing channel" - ); -} - -/** - * Call f in a try/catch block and close the connection if - * an exception is thrown. - */ -void EventChannelConnection::closeOnException(MemberFnPtr f) -{ - try { - Exception::tryCatchLog( - boost::bind(f, this), - "Closing connection due to exception" - ); - return; - } catch (...) { - // Exception was already logged by tryCatchLog - closeNoThrow(); - } -} - -// Post the write event. -// Always called inside closeOnException. -// Called by endWrite and send, but only one thread writes at a time. -// -void EventChannelConnection::startWrite() { - FrameQueue::auto_type frame; - { - Monitor::ScopedLock lock(monitor); - // Stop if closed or a write event is already in progress. - if (isClosed || isWriting) - return; - if (writeFrames.empty()) { - isWriting = false; - return; - } - isWriting = true; - frame = writeFrames.pop_front(); - } - // No need to lock here - only one thread can be writing at a time. - out.clear(); - if (isTrace) - cout << "Send on socket " << writeFd << ": " << *frame << endl; - frame->encode(out); - out.flip(); - writeEvent = WriteEvent( - writeFd, out.start(), out.available(), - boost::bind(&EventChannelConnection::closeOnException, - this, &EventChannelConnection::endWrite)); - threads->post(writeEvent); -} - -// ScopedBusy ctor increments busyThreads. -// dtor decrements and calls monitor.notifyAll if it reaches 0. -// -struct EventChannelConnection::ScopedBusy : public AtomicCount::ScopedIncrement -{ - ScopedBusy(EventChannelConnection& ecc) - : AtomicCount::ScopedIncrement( - ecc.busyThreads, boost::bind(&Monitor::notifyAll, &ecc.monitor)) - {} -}; - -// Write event completed. -// Always called by a channel thread inside closeOnException. -// -void EventChannelConnection::endWrite() { - ScopedBusy(*this); - { - Monitor::ScopedLock lock(monitor); - isWriting = false; - if (isClosed) - return; - writeEvent.throwIfException(); - } - // Check if there's more in to write in the write queue. - startWrite(); -} - - -// Post the read event. -// Always called inside closeOnException. -// Called from ctor and end[Init]Read, so only one call at a time -// is possible since we only post one read event at a time. -// -void EventChannelConnection::startRead() { - // Non blocking read, as much as we can swallow. - readEvent = ReadEvent( - readFd, in.start(), in.available(), readCallback,true); - threads->post(readEvent); -} - -// Completion of initial read, expect protocolInit. -// Always called inside closeOnException in channel thread. -// Only called by one thread at a time. -void EventChannelConnection::endInitRead() { - ScopedBusy(*this); - if (!isClosed) { - readEvent.throwIfException(); - in.move(readEvent.getBytesRead()); - in.flip(); - ProtocolInitiation protocolInit; - if(protocolInit.decode(in)){ - handler->initiated(&protocolInit); - readCallback = boost::bind( - &EventChannelConnection::closeOnException, - this, &EventChannelConnection::endRead); - } - in.compact(); - // Continue reading. - startRead(); - } -} - -// Normal reads, expect a frame. -// Always called inside closeOnException in channel thread. -void EventChannelConnection::endRead() { - ScopedBusy(*this); - if (!isClosed) { - readEvent.throwIfException(); - in.move(readEvent.getBytesRead()); - in.flip(); - AMQFrame frame; - while (frame.decode(in)) { - // TODO aconway 2006-11-30: received should take Frame& - if (isTrace) - cout << "Received on socket " << readFd - << ": " << frame << endl; - handler->received(&frame); - } - in.compact(); - startRead(); - } -} - -}} // namespace qpid::sys diff --git a/cpp/src/sys/posix/EventChannelConnection.h b/cpp/src/sys/posix/EventChannelConnection.h deleted file mode 100644 index 0c1eeff087..0000000000 --- a/cpp/src/sys/posix/EventChannelConnection.h +++ /dev/null @@ -1,102 +0,0 @@ -#ifndef _posix_EventChannelConnection_h -#define _posix_EventChannelConnection_h - -/* - * - * 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 - -#include "EventChannelThreads.h" -#include "../Monitor.h" -#include "../ConnectionOutputHandler.h" -#include "../ConnectionInputHandler.h" -#include "../AtomicCount.h" -#include "../../framing/AMQFrame.h" - -namespace qpid { -namespace sys { - -class ConnectionInputHandlerFactory; - -/** - * Implements ConnectionOutputHandler and delegates to a ConnectionInputHandler - * for a connection via the EventChannel. - *@param readDescriptor file descriptor for reading. - *@param writeDescriptor file descriptor for writing, - * by default same as readDescriptor - */ -class EventChannelConnection : public ConnectionOutputHandler { - public: - EventChannelConnection( - EventChannelThreads::shared_ptr threads, - ConnectionInputHandlerFactory& factory, - int readDescriptor, - int writeDescriptor = 0, - bool isTrace = false - ); - - // TODO aconway 2006-11-30: ConnectionOutputHandler::send should take auto_ptr - virtual void send(qpid::framing::AMQFrame* frame) { - send(std::auto_ptr(frame)); - } - - virtual void send(std::auto_ptr frame); - - virtual void close(); - - private: - typedef boost::ptr_deque FrameQueue; - typedef void (EventChannelConnection::*MemberFnPtr)(); - struct ScopedBusy; - - void startWrite(); - void endWrite(); - void startRead(); - void endInitRead(); - void endRead(); - void closeNoThrow(); - void closeOnException(MemberFnPtr); - bool shouldContinue(bool& flag); - - static const size_t bufferSize; - - Monitor monitor; - - int readFd, writeFd; - ReadEvent readEvent; - WriteEvent writeEvent; - Event::Callback readCallback; - bool isWriting; - bool isClosed; - AtomicCount busyThreads; - - EventChannelThreads::shared_ptr threads; - std::auto_ptr handler; - qpid::framing::Buffer in, out; - FrameQueue writeFrames; - bool isTrace; - - friend struct ScopedBusy; -}; - - -}} // namespace qpid::sys - - - -#endif /*!_posix_EventChannelConnection_h*/ diff --git a/cpp/src/sys/posix/EventChannelThreads.cpp b/cpp/src/sys/posix/EventChannelThreads.cpp deleted file mode 100644 index 90caf901b1..0000000000 --- a/cpp/src/sys/posix/EventChannelThreads.cpp +++ /dev/null @@ -1,119 +0,0 @@ -/* - * - * 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 "EventChannelThreads.h" -#include "../Runnable.h" -#include -using namespace std; -#include - -namespace qpid { -namespace sys { - -EventChannelThreads::shared_ptr EventChannelThreads::create( - EventChannel::shared_ptr ec) -{ - return EventChannelThreads::shared_ptr(new EventChannelThreads(ec)); -} - -EventChannelThreads::EventChannelThreads(EventChannel::shared_ptr ec) : - channel(ec), nWaiting(0), state(RUNNING) -{ - // TODO aconway 2006-11-15: Estimate initial threads based on CPUs. - addThread(); -} - -EventChannelThreads::~EventChannelThreads() { - shutdown(); - join(); -} - -void EventChannelThreads::shutdown() -{ - ScopedLock lock(*this); - if (state != RUNNING) // Already shutting down. - return; - for (size_t i = 0; i < workers.size(); ++i) { - channel->postEvent(terminate); - } - state = TERMINATE_SENT; - notify(); // Wake up one join() thread. -} - -void EventChannelThreads::join() -{ - { - ScopedLock lock(*this); - while (state == RUNNING) // Wait for shutdown to start. - wait(); - if (state == SHUTDOWN) // Shutdown is complete - return; - if (state == JOINING) { - // Someone else is doing the join. - while (state != SHUTDOWN) - wait(); - return; - } - // I'm the joining thread - assert(state == TERMINATE_SENT); - state = JOINING; - } // Drop the lock. - - for (size_t i = 0; i < workers.size(); ++i) { - assert(state == JOINING); // Only this thread can change JOINING. - workers[i].join(); - } - state = SHUTDOWN; - notifyAll(); // Notify other join() threaeds. -} - -void EventChannelThreads::addThread() { - ScopedLock l(*this); - workers.push_back(Thread(*this)); -} - -void EventChannelThreads::run() -{ - // Start life waiting. Decrement on exit. - AtomicCount::ScopedIncrement inc(nWaiting); - try { - while (true) { - Event* e = channel->getEvent(); - assert(e != 0); - if (e == &terminate) { - return; - } - AtomicCount::ScopedDecrement dec(nWaiting); - // I'm no longer waiting, make sure someone is. - if (dec == 0) - addThread(); - e->dispatch(); - } - } - catch (const std::exception& e) { - // TODO aconway 2006-11-15: need better logging across the board. - std::cerr << "EventChannelThreads::run() caught: " << e.what() - << std::endl; - } - catch (...) { - std::cerr << "EventChannelThreads::run() caught unknown exception." - << std::endl; - } -} - -}} diff --git a/cpp/src/sys/posix/EventChannelThreads.h b/cpp/src/sys/posix/EventChannelThreads.h deleted file mode 100644 index eb8b7cf287..0000000000 --- a/cpp/src/sys/posix/EventChannelThreads.h +++ /dev/null @@ -1,92 +0,0 @@ -#ifndef _posix_EventChannelThreads_h -#define _sys_EventChannelThreads_h - -/* - * - * 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 - -#include "../../Exception.h" -#include "../Time.h" -#include "../Monitor.h" -#include "../Thread.h" -#include "../AtomicCount.h" -#include "EventChannel.h" - -namespace qpid { -namespace sys { - -/** - Dynamic thread pool serving an EventChannel. - - Threads run a loop { e = getEvent(); e->dispatch(); } - The size of the thread pool is automatically adjusted to optimal size. -*/ -class EventChannelThreads : - public qpid::SharedObject, - public sys::Monitor, private sys::Runnable -{ - public: - /** Create the thread pool and start initial threads. */ - static EventChannelThreads::shared_ptr create( - EventChannel::shared_ptr channel - ); - - ~EventChannelThreads(); - - /** Post event to the underlying channel */ - void postEvent(Event& event) { channel->postEvent(event); } - - /** Post event to the underlying channel Must not be 0. */ - void postEvent(Event* event) { channel->postEvent(event); } - - /** - * Terminate all threads. - * - * Returns immediately, use join() to wait till all threads are - * shut down. - */ - void shutdown(); - - /** Wait for all threads to terminate. */ - void join(); - - private: - typedef std::vector Threads; - typedef enum { - RUNNING, TERMINATE_SENT, JOINING, SHUTDOWN - } State; - - EventChannelThreads(EventChannel::shared_ptr underlyingChannel); - void addThread(); - - void run(); - bool keepRunning(); - void adjustThreads(); - - EventChannel::shared_ptr channel; - Threads workers; - sys::AtomicCount nWaiting; - State state; - Event terminate; -}; - - -}} - - -#endif /*!_sys_EventChannelThreads_h*/ diff --git a/cpp/src/sys/posix/PosixAcceptor.cpp b/cpp/src/sys/posix/PosixAcceptor.cpp deleted file mode 100644 index 6ac0c2b11b..0000000000 --- a/cpp/src/sys/posix/PosixAcceptor.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * - * 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 "../Acceptor.h" -#include "../../Exception.h" - -namespace qpid { -namespace sys { - -namespace { -void fail() { throw qpid::Exception("PosixAcceptor not implemented"); } -} - -class PosixAcceptor : public Acceptor { - public: - virtual int16_t getPort() const { fail(); return 0; } - virtual void run(qpid::sys::ConnectionInputHandlerFactory* ) { fail(); } - virtual void shutdown() { fail(); } -}; - -// Define generic Acceptor::create() to return APRAcceptor. - Acceptor::shared_ptr Acceptor::create(int16_t , int, int, bool) -{ - return Acceptor::shared_ptr(new PosixAcceptor()); -} - -// Must define Acceptor virtual dtor. -Acceptor::~Acceptor() {} - -}} diff --git a/cpp/src/sys/posix/Socket.cpp b/cpp/src/sys/posix/Socket.cpp deleted file mode 100644 index 6d6ac7f49c..0000000000 --- a/cpp/src/sys/posix/Socket.cpp +++ /dev/null @@ -1,118 +0,0 @@ -/* - * - * 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 -#include -#include -#include - -#include - -#include "../../QpidError.h" -#include "check.h" -#include "../Socket.h" - -using namespace qpid::sys; - -Socket Socket::createTcp() -{ - int s = ::socket (PF_INET, SOCK_STREAM, 0); - if (s < 0) throw QPID_POSIX_ERROR(errno); - return s; -} - -Socket::Socket(int descriptor) : socket(descriptor) {} - -void Socket::setTimeout(Time interval) -{ - struct timeval tv; - tv.tv_sec = interval/TIME_SEC; - tv.tv_usec = (interval%TIME_SEC)/TIME_USEC; - setsockopt(socket, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); - setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)); -} - -void Socket::connect(const std::string& host, int port) -{ - struct sockaddr_in name; - name.sin_family = AF_INET; - name.sin_port = htons(port); - struct hostent* hp = gethostbyname ( host.c_str() ); - if (hp == 0) throw QPID_POSIX_ERROR(errno); - memcpy(&name.sin_addr.s_addr, hp->h_addr_list[0], hp->h_length); - if (::connect(socket, (struct sockaddr*)(&name), sizeof(name)) < 0) - throw QPID_POSIX_ERROR(errno); -} - -void -Socket::close() -{ - if (socket == 0) return; - if (::close(socket) < 0) throw QPID_POSIX_ERROR(errno); - socket = 0; -} - -ssize_t -Socket::send(const void* data, size_t size) -{ - ssize_t sent = ::send(socket, data, size, 0); - if (sent < 0) { - if (errno == ECONNRESET) return SOCKET_EOF; - if (errno == ETIMEDOUT) return SOCKET_TIMEOUT; - throw QPID_POSIX_ERROR(errno); - } - return sent; -} - -ssize_t -Socket::recv(void* data, size_t size) -{ - ssize_t received = ::recv(socket, data, size, 0); - if (received < 0) { - if (errno == ETIMEDOUT) return SOCKET_TIMEOUT; - throw QPID_POSIX_ERROR(errno); - } - return received; -} - -int Socket::listen(int port, int backlog) -{ - struct sockaddr_in name; - name.sin_family = AF_INET; - name.sin_port = htons(port); - name.sin_addr.s_addr = 0; - if (::bind(socket, (struct sockaddr*)&name, sizeof(name)) < 0) - throw QPID_POSIX_ERROR(errno); - if (::listen(socket, backlog) < 0) - throw QPID_POSIX_ERROR(errno); - - socklen_t namelen = sizeof(name); - if (::getsockname(socket, (struct sockaddr*)&name, &namelen) < 0) - throw QPID_POSIX_ERROR(errno); - - return ntohs(name.sin_port); -} - - -int Socket::fd() -{ - return socket; -} diff --git a/cpp/src/sys/posix/Thread.cpp b/cpp/src/sys/posix/Thread.cpp deleted file mode 100644 index 5b8d9e2180..0000000000 --- a/cpp/src/sys/posix/Thread.cpp +++ /dev/null @@ -1,28 +0,0 @@ -/* - * - * 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 "../Thread.h" - -void* qpid::sys::Thread::runRunnable(void* p) -{ - static_cast(p)->run(); - return 0; -} diff --git a/cpp/src/sys/posix/check.cpp b/cpp/src/sys/posix/check.cpp deleted file mode 100644 index 408679caa8..0000000000 --- a/cpp/src/sys/posix/check.cpp +++ /dev/null @@ -1,39 +0,0 @@ -/* - * - * 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 -#include "check.h" - -namespace qpid { -namespace sys { - -std::string -PosixError::getMessage(int errNo) -{ - char buf[512]; - return std::string(strerror_r(errNo, buf, sizeof(buf))); -} - -PosixError::PosixError(int errNo, const qpid::SrcLine& loc) throw() - : qpid::QpidError(INTERNAL_ERROR + errNo, getMessage(errNo), loc) -{ } - -}} diff --git a/cpp/src/sys/posix/check.h b/cpp/src/sys/posix/check.h deleted file mode 100644 index 099044afa3..0000000000 --- a/cpp/src/sys/posix/check.h +++ /dev/null @@ -1,62 +0,0 @@ -#ifndef _posix_check_h -#define _posix_check_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 -#include -#include "../../QpidError.h" - -namespace qpid { -namespace sys { - -/** - * Exception with message from errno. - */ -class PosixError : public qpid::QpidError -{ - public: - static std::string getMessage(int errNo); - - PosixError(int errNo, const qpid::SrcLine& location) throw(); - - ~PosixError() throw() {} - - int getErrNo() { return errNo; } - - Exception* clone() const throw() { return new PosixError(*this); } - - void throwSelf() const { throw *this; } - - private: - int errNo; -}; - -}} - -/** Create a PosixError for the current file/line and errno. */ -#define QPID_POSIX_ERROR(errNo) ::qpid::sys::PosixError(errNo, SRCLINE) - -/** Throw a posix error if errNo is non-zero */ -#define QPID_POSIX_THROW_IF(ERRNO) \ - if ((ERRNO) != 0) throw QPID_POSIX_ERROR((ERRNO)) -#endif /*!_posix_check_h*/ diff --git a/cpp/src/tests/APRBaseTest.cpp b/cpp/src/tests/APRBaseTest.cpp index 5ed8bf1918..3ec18d658e 100644 --- a/cpp/src/tests/APRBaseTest.cpp +++ b/cpp/src/tests/APRBaseTest.cpp @@ -18,7 +18,7 @@ * under the License. * */ -#include "../sys/apr/APRBase.h" +#include "qpid/sys/apr/APRBase.h" #include "qpid_test_plugin.h" #include diff --git a/cpp/src/tests/AccumulatedAckTest.cpp b/cpp/src/tests/AccumulatedAckTest.cpp index 56870209fe..aa90de5b42 100644 --- a/cpp/src/tests/AccumulatedAckTest.cpp +++ b/cpp/src/tests/AccumulatedAckTest.cpp @@ -18,7 +18,7 @@ * under the License. * */ -#include "../broker/AccumulatedAck.h" +#include "qpid/broker/AccumulatedAck.h" #include "qpid_test_plugin.h" #include #include diff --git a/cpp/src/tests/BrokerChannelTest.cpp b/cpp/src/tests/BrokerChannelTest.cpp index 7d8bbf5e49..3b2119be13 100644 --- a/cpp/src/tests/BrokerChannelTest.cpp +++ b/cpp/src/tests/BrokerChannelTest.cpp @@ -18,19 +18,19 @@ * under the License. * */ -#include "../broker/BrokerChannel.h" -#include "../broker/BrokerMessage.h" -#include "../broker/BrokerQueue.h" -#include "../broker/FanOutExchange.h" -#include "../broker/NullMessageStore.h" +#include "qpid/broker/BrokerChannel.h" +#include "qpid/broker/BrokerMessage.h" +#include "qpid/broker/BrokerQueue.h" +#include "qpid/broker/FanOutExchange.h" +#include "qpid/broker/NullMessageStore.h" #include "qpid_test_plugin.h" #include #include -#include "../gen/AMQP_HighestVersion.h" -#include "../framing/AMQFrame.h" +#include "qpid/framing/AMQP_HighestVersion.h" +#include "qpid/framing/AMQFrame.h" #include "MockChannel.h" -#include "../broker/Connection.h" -#include "../framing/ProtocolInitiation.h" +#include "qpid/broker/Connection.h" +#include "qpid/framing/ProtocolInitiation.h" #include using namespace boost; diff --git a/cpp/src/tests/ClientChannelTest.cpp b/cpp/src/tests/ClientChannelTest.cpp index 18c71527b6..252b419299 100644 --- a/cpp/src/tests/ClientChannelTest.cpp +++ b/cpp/src/tests/ClientChannelTest.cpp @@ -21,13 +21,13 @@ #include #include "qpid_test_plugin.h" #include "InProcessBroker.h" -#include "../client/ClientChannel.h" -#include "../client/ClientMessage.h" -#include "../client/ClientQueue.h" -#include "../client/ClientExchange.h" -#include "../client/MessageListener.h" -#include "../client/BasicMessageChannel.h" -#include "../client/MessageMessageChannel.h" +#include "qpid/client/ClientChannel.h" +#include "qpid/client/ClientMessage.h" +#include "qpid/client/ClientQueue.h" +#include "qpid/client/ClientExchange.h" +#include "qpid/client/MessageListener.h" +#include "qpid/client/BasicMessageChannel.h" +#include "qpid/client/MessageMessageChannel.h" using namespace std; using namespace boost; diff --git a/cpp/src/tests/ConfigurationTest.cpp b/cpp/src/tests/ConfigurationTest.cpp index ecaa2865ce..1f133b6d75 100644 --- a/cpp/src/tests/ConfigurationTest.cpp +++ b/cpp/src/tests/ConfigurationTest.cpp @@ -18,7 +18,7 @@ * under the License. * */ -#include "../broker/Configuration.h" +#include "qpid/broker/Configuration.h" #include "qpid_test_plugin.h" #include diff --git a/cpp/src/tests/EventChannelConnectionTest.cpp b/cpp/src/tests/EventChannelConnectionTest.cpp index 24cd492441..28eea2fd20 100644 --- a/cpp/src/tests/EventChannelConnectionTest.cpp +++ b/cpp/src/tests/EventChannelConnectionTest.cpp @@ -21,12 +21,12 @@ #include #include -#include "../framing/AMQHeartbeatBody.h" -#include "../framing/AMQFrame.h" -#include "../sys/posix/EventChannelConnection.h" -#include "../sys/ConnectionInputHandler.h" -#include "../sys/ConnectionInputHandlerFactory.h" -#include "../sys/Socket.h" +#include "qpid/framing/AMQHeartbeatBody.h" +#include "qpid/framing/AMQFrame.h" +#include "qpid/sys/posix/EventChannelConnection.h" +#include "qpid/sys/ConnectionInputHandler.h" +#include "qpid/sys/ConnectionInputHandlerFactory.h" +#include "qpid/sys/Socket.h" #include "qpid_test_plugin.h" #include "MockConnectionInputHandler.h" diff --git a/cpp/src/tests/EventChannelTest.cpp b/cpp/src/tests/EventChannelTest.cpp index 45229ce20f..3ba54def86 100644 --- a/cpp/src/tests/EventChannelTest.cpp +++ b/cpp/src/tests/EventChannelTest.cpp @@ -18,11 +18,11 @@ * under the License. * */ -#include "../sys/posix/EventChannel.h" -#include "../sys/posix/check.h" -#include "../sys/Runnable.h" -#include "../sys/Socket.h" -#include "../sys/Thread.h" +#include "qpid/sys/posix/EventChannel.h" +#include "qpid/sys/posix/check.h" +#include "qpid/sys/Runnable.h" +#include "qpid/sys/Socket.h" +#include "qpid/sys/Thread.h" #include "qpid_test_plugin.h" #include diff --git a/cpp/src/tests/EventChannelThreadsTest.cpp b/cpp/src/tests/EventChannelThreadsTest.cpp index ee1e2859c4..22ea57d675 100644 --- a/cpp/src/tests/EventChannelThreadsTest.cpp +++ b/cpp/src/tests/EventChannelThreadsTest.cpp @@ -21,8 +21,8 @@ #include #include -#include "../sys/Socket.h" -#include "../sys/posix/EventChannelThreads.h" +#include "qpid/sys/Socket.h" +#include "qpid/sys/posix/EventChannelThreads.h" #include "qpid_test_plugin.h" diff --git a/cpp/src/tests/ExchangeTest.cpp b/cpp/src/tests/ExchangeTest.cpp index ff0e6b4ae2..16a0da0746 100644 --- a/cpp/src/tests/ExchangeTest.cpp +++ b/cpp/src/tests/ExchangeTest.cpp @@ -19,14 +19,14 @@ * */ -#include "../broker/DeliverableMessage.h" -#include "../broker/DirectExchange.h" -#include "../broker/BrokerExchange.h" -#include "../broker/BrokerQueue.h" -#include "../broker/TopicExchange.h" +#include "qpid/broker/DeliverableMessage.h" +#include "qpid/broker/DirectExchange.h" +#include "qpid/broker/BrokerExchange.h" +#include "qpid/broker/BrokerQueue.h" +#include "qpid/broker/TopicExchange.h" #include "qpid_test_plugin.h" #include -#include "../gen/BasicGetBody.h" +#include "qpid/framing/BasicGetBody.h" using namespace qpid::broker; using namespace qpid::framing; diff --git a/cpp/src/tests/FieldTableTest.cpp b/cpp/src/tests/FieldTableTest.cpp index a7a03cc574..dcab96fb08 100644 --- a/cpp/src/tests/FieldTableTest.cpp +++ b/cpp/src/tests/FieldTableTest.cpp @@ -19,7 +19,7 @@ * */ #include -#include "../framing/amqp_framing.h" +#include "qpid/framing/amqp_framing.h" #include "qpid_test_plugin.h" using namespace qpid::framing; diff --git a/cpp/src/tests/FramingTest.cpp b/cpp/src/tests/FramingTest.cpp index 24e609e9b9..528919de48 100644 --- a/cpp/src/tests/FramingTest.cpp +++ b/cpp/src/tests/FramingTest.cpp @@ -18,25 +18,25 @@ * under the License. * */ -#include "../gen/ConnectionRedirectBody.h" -#include "../framing/ProtocolVersion.h" -#include "../framing/amqp_framing.h" +#include "qpid/framing/ConnectionRedirectBody.h" +#include "qpid/framing/ProtocolVersion.h" +#include "qpid/framing/amqp_framing.h" #include #include "qpid_test_plugin.h" #include #include -#include "../QpidError.h" -#include "../gen/AMQP_HighestVersion.h" -#include "../framing/AMQRequestBody.h" -#include "../framing/AMQResponseBody.h" -#include "../framing/Requester.h" -#include "../framing/Responder.h" +#include "qpid/QpidError.h" +#include "qpid/framing/AMQP_HighestVersion.h" +#include "qpid/framing/AMQRequestBody.h" +#include "qpid/framing/AMQResponseBody.h" +#include "qpid/framing/Requester.h" +#include "qpid/framing/Responder.h" #include "InProcessBroker.h" -#include "../client/Connection.h" -#include "../client/ClientExchange.h" -#include "../client/ClientQueue.h" -#include "../framing/Correlator.h" -#include "../gen/BasicGetOkBody.h" +#include "qpid/client/Connection.h" +#include "qpid/client/ClientExchange.h" +#include "qpid/client/ClientQueue.h" +#include "qpid/framing/Correlator.h" +#include "qpid/framing/BasicGetOkBody.h" #include #include #include diff --git a/cpp/src/tests/HeaderTest.cpp b/cpp/src/tests/HeaderTest.cpp index 29e2ddee3d..17381cc868 100644 --- a/cpp/src/tests/HeaderTest.cpp +++ b/cpp/src/tests/HeaderTest.cpp @@ -19,7 +19,7 @@ * */ #include -#include "../framing/amqp_framing.h" +#include "qpid/framing/amqp_framing.h" #include "qpid_test_plugin.h" using namespace qpid::framing; diff --git a/cpp/src/tests/HeadersExchangeTest.cpp b/cpp/src/tests/HeadersExchangeTest.cpp index 64125f4a0a..a7a1001198 100644 --- a/cpp/src/tests/HeadersExchangeTest.cpp +++ b/cpp/src/tests/HeadersExchangeTest.cpp @@ -19,9 +19,9 @@ * */ -#include "../broker/HeadersExchange.h" -#include "../framing/FieldTable.h" -#include "../framing/Value.h" +#include "qpid/broker/HeadersExchange.h" +#include "qpid/framing/FieldTable.h" +#include "qpid/framing/Value.h" #include "qpid_test_plugin.h" using namespace qpid::broker; diff --git a/cpp/src/tests/InMemoryContentTest.cpp b/cpp/src/tests/InMemoryContentTest.cpp index 3dbe31e072..cb729f9930 100644 --- a/cpp/src/tests/InMemoryContentTest.cpp +++ b/cpp/src/tests/InMemoryContentTest.cpp @@ -18,12 +18,12 @@ * under the License. * */ -#include "../broker/InMemoryContent.h" +#include "qpid/broker/InMemoryContent.h" #include "qpid_test_plugin.h" -#include "../gen/AMQP_HighestVersion.h" +#include "qpid/framing/AMQP_HighestVersion.h" #include #include -#include "../framing/AMQFrame.h" +#include "qpid/framing/AMQFrame.h" #include "MockChannel.h" using std::list; diff --git a/cpp/src/tests/InProcessBroker.h b/cpp/src/tests/InProcessBroker.h index 22af62879a..96f8ce72cf 100644 --- a/cpp/src/tests/InProcessBroker.h +++ b/cpp/src/tests/InProcessBroker.h @@ -18,12 +18,12 @@ * limitations under the License. * */ -#include "../gen/AMQP_HighestVersion.h" -#include "../framing/AMQFrame.h" -#include "../broker/Broker.h" -#include "../broker/Connection.h" -#include "../client/Connector.h" -#include "../client/Connection.h" +#include "qpid/framing/AMQP_HighestVersion.h" +#include "qpid/framing/AMQFrame.h" +#include "qpid/broker/Broker.h" +#include "qpid/broker/Connection.h" +#include "qpid/client/Connector.h" +#include "qpid/client/Connection.h" #include #include diff --git a/cpp/src/tests/LazyLoadedContentTest.cpp b/cpp/src/tests/LazyLoadedContentTest.cpp index f366a25cf9..7bac3613ad 100644 --- a/cpp/src/tests/LazyLoadedContentTest.cpp +++ b/cpp/src/tests/LazyLoadedContentTest.cpp @@ -18,14 +18,14 @@ * under the License. * */ -#include "../broker/LazyLoadedContent.h" -#include "../gen/AMQP_HighestVersion.h" -#include "../broker/NullMessageStore.h" +#include "qpid/broker/LazyLoadedContent.h" +#include "qpid/framing/AMQP_HighestVersion.h" +#include "qpid/broker/NullMessageStore.h" #include "qpid_test_plugin.h" #include #include #include -#include "../framing/AMQFrame.h" +#include "qpid/framing/AMQFrame.h" #include "MockChannel.h" using std::list; using std::string; diff --git a/cpp/src/tests/MessageBuilderTest.cpp b/cpp/src/tests/MessageBuilderTest.cpp index b660987708..526db85c31 100644 --- a/cpp/src/tests/MessageBuilderTest.cpp +++ b/cpp/src/tests/MessageBuilderTest.cpp @@ -18,11 +18,11 @@ * under the License. * */ -#include "../Exception.h" -#include "../broker/BrokerMessage.h" -#include "../broker/MessageBuilder.h" -#include "../broker/NullMessageStore.h" -#include "../framing/Buffer.h" +#include "qpid/Exception.h" +#include "qpid/broker/BrokerMessage.h" +#include "qpid/broker/MessageBuilder.h" +#include "qpid/broker/NullMessageStore.h" +#include "qpid/framing/Buffer.h" #include "qpid_test_plugin.h" #include #include diff --git a/cpp/src/tests/MessageHandlerTest.cpp b/cpp/src/tests/MessageHandlerTest.cpp index 4c4321d19d..2650475f79 100644 --- a/cpp/src/tests/MessageHandlerTest.cpp +++ b/cpp/src/tests/MessageHandlerTest.cpp @@ -19,11 +19,11 @@ * */ //#include -//#include "../gen/AMQP_HighestVersion.h> -#include "../framing/amqp_framing.h" +//#include "qpid/framing/AMQP_HighestVersion.h> +#include "qpid/framing/amqp_framing.h" #include "qpid_test_plugin.h" -#include "../broker/BrokerAdapter.h" +#include "qpid/broker/BrokerAdapter.h" using namespace qpid::framing; using namespace qpid::broker; diff --git a/cpp/src/tests/MessageTest.cpp b/cpp/src/tests/MessageTest.cpp index dd6d66c3cd..d3869b552e 100644 --- a/cpp/src/tests/MessageTest.cpp +++ b/cpp/src/tests/MessageTest.cpp @@ -18,11 +18,11 @@ * under the License. * */ -#include "../broker/BrokerMessage.h" +#include "qpid/broker/BrokerMessage.h" #include "qpid_test_plugin.h" #include -#include "../gen/AMQP_HighestVersion.h" -#include "../framing/AMQFrame.h" +#include "qpid/framing/AMQP_HighestVersion.h" +#include "qpid/framing/AMQFrame.h" #include "MockChannel.h" using namespace boost; diff --git a/cpp/src/tests/MockChannel.h b/cpp/src/tests/MockChannel.h index dfadb3a3cc..cadfa83b56 100644 --- a/cpp/src/tests/MockChannel.h +++ b/cpp/src/tests/MockChannel.h @@ -19,11 +19,11 @@ * */ -#include "../framing/MethodContext.h" -#include "../framing/ChannelAdapter.h" -#include "../framing/OutputHandler.h" -#include "../framing/AMQFrame.h" -#include "../gen/BasicGetBody.h" +#include "qpid/framing/MethodContext.h" +#include "qpid/framing/ChannelAdapter.h" +#include "qpid/framing/OutputHandler.h" +#include "qpid/framing/AMQFrame.h" +#include "qpid/framing/BasicGetBody.h" #include #include diff --git a/cpp/src/tests/MockConnectionInputHandler.h b/cpp/src/tests/MockConnectionInputHandler.h index 4503ac33a5..d104e7d934 100644 --- a/cpp/src/tests/MockConnectionInputHandler.h +++ b/cpp/src/tests/MockConnectionInputHandler.h @@ -19,10 +19,10 @@ * */ -#include "../sys/ConnectionInputHandler.h" -#include "../sys/ConnectionInputHandlerFactory.h" -#include "../sys/Monitor.h" -#include "../framing/ProtocolInitiation.h" +#include "qpid/sys/ConnectionInputHandler.h" +#include "qpid/sys/ConnectionInputHandlerFactory.h" +#include "qpid/sys/Monitor.h" +#include "qpid/framing/ProtocolInitiation.h" struct MockConnectionInputHandler : public qpid::sys::ConnectionInputHandler { diff --git a/cpp/src/tests/ProducerConsumerTest.cpp b/cpp/src/tests/ProducerConsumerTest.cpp index 0aa812e33e..410a846b8b 100644 --- a/cpp/src/tests/ProducerConsumerTest.cpp +++ b/cpp/src/tests/ProducerConsumerTest.cpp @@ -25,10 +25,10 @@ #include "qpid_test_plugin.h" #include "InProcessBroker.h" -#include "../sys/ProducerConsumer.h" -#include "../sys/Thread.h" -#include "../gen/AMQP_HighestVersion.h" -#include "../sys/AtomicCount.h" +#include "qpid/sys/ProducerConsumer.h" +#include "qpid/sys/Thread.h" +#include "qpid/framing/AMQP_HighestVersion.h" +#include "qpid/sys/AtomicCount.h" using namespace qpid; using namespace sys; diff --git a/cpp/src/tests/QueuePolicyTest.cpp b/cpp/src/tests/QueuePolicyTest.cpp index 5ccc9417cd..467f43638f 100644 --- a/cpp/src/tests/QueuePolicyTest.cpp +++ b/cpp/src/tests/QueuePolicyTest.cpp @@ -18,7 +18,7 @@ * under the License. * */ -#include "../broker/QueuePolicy.h" +#include "qpid/broker/QueuePolicy.h" #include "qpid_test_plugin.h" using namespace qpid::broker; diff --git a/cpp/src/tests/QueueRegistryTest.cpp b/cpp/src/tests/QueueRegistryTest.cpp index d01fbd0ad4..5fd861d6be 100644 --- a/cpp/src/tests/QueueRegistryTest.cpp +++ b/cpp/src/tests/QueueRegistryTest.cpp @@ -17,7 +17,7 @@ * under the License. */ -#include "../broker/QueueRegistry.h" +#include "qpid/broker/QueueRegistry.h" #include "qpid_test_plugin.h" #include diff --git a/cpp/src/tests/QueueTest.cpp b/cpp/src/tests/QueueTest.cpp index bb2b424375..dd63cbaa05 100644 --- a/cpp/src/tests/QueueTest.cpp +++ b/cpp/src/tests/QueueTest.cpp @@ -18,8 +18,8 @@ * under the License. * */ -#include "../broker/BrokerQueue.h" -#include "../broker/QueueRegistry.h" +#include "qpid/broker/BrokerQueue.h" +#include "qpid/broker/QueueRegistry.h" #include "qpid_test_plugin.h" #include #include "MockChannel.h" diff --git a/cpp/src/tests/ReferenceTest.cpp b/cpp/src/tests/ReferenceTest.cpp index 3f23d1072f..e8b759264b 100644 --- a/cpp/src/tests/ReferenceTest.cpp +++ b/cpp/src/tests/ReferenceTest.cpp @@ -21,11 +21,11 @@ #include #include #include "qpid_test_plugin.h" -#include "../broker/Reference.h" -#include "../broker/BrokerMessageMessage.h" -#include "../gen/MessageTransferBody.h" -#include "../gen/MessageAppendBody.h" -#include "../broker/CompletionHandler.h" +#include "qpid/broker/Reference.h" +#include "qpid/broker/BrokerMessageMessage.h" +#include "qpid/framing/MessageTransferBody.h" +#include "qpid/framing/MessageAppendBody.h" +#include "qpid/broker/CompletionHandler.h" using namespace boost; using namespace qpid; diff --git a/cpp/src/tests/TopicExchangeTest.cpp b/cpp/src/tests/TopicExchangeTest.cpp index 39035c776f..9e320b76f9 100644 --- a/cpp/src/tests/TopicExchangeTest.cpp +++ b/cpp/src/tests/TopicExchangeTest.cpp @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -#include "../broker/TopicExchange.h" +#include "qpid/broker/TopicExchange.h" #include "qpid_test_plugin.h" using namespace qpid::broker; diff --git a/cpp/src/tests/TxAckTest.cpp b/cpp/src/tests/TxAckTest.cpp index 91e07a3faa..3b9b1db308 100644 --- a/cpp/src/tests/TxAckTest.cpp +++ b/cpp/src/tests/TxAckTest.cpp @@ -18,9 +18,9 @@ * under the License. * */ -#include "../broker/NullMessageStore.h" -#include "../broker/RecoveryManager.h" -#include "../broker/TxAck.h" +#include "qpid/broker/NullMessageStore.h" +#include "qpid/broker/RecoveryManager.h" +#include "qpid/broker/TxAck.h" #include "qpid_test_plugin.h" #include #include diff --git a/cpp/src/tests/TxBufferTest.cpp b/cpp/src/tests/TxBufferTest.cpp index f87c33d817..bd43691235 100644 --- a/cpp/src/tests/TxBufferTest.cpp +++ b/cpp/src/tests/TxBufferTest.cpp @@ -18,7 +18,7 @@ * under the License. * */ -#include "../broker/TxBuffer.h" +#include "qpid/broker/TxBuffer.h" #include "qpid_test_plugin.h" #include #include diff --git a/cpp/src/tests/TxPublishTest.cpp b/cpp/src/tests/TxPublishTest.cpp index 84d2666b6c..8c3c7afe31 100644 --- a/cpp/src/tests/TxPublishTest.cpp +++ b/cpp/src/tests/TxPublishTest.cpp @@ -18,9 +18,9 @@ * under the License. * */ -#include "../broker/NullMessageStore.h" -#include "../broker/RecoveryManager.h" -#include "../broker/TxPublish.h" +#include "qpid/broker/NullMessageStore.h" +#include "qpid/broker/RecoveryManager.h" +#include "qpid/broker/TxPublish.h" #include "qpid_test_plugin.h" #include #include diff --git a/cpp/src/tests/ValueTest.cpp b/cpp/src/tests/ValueTest.cpp index 2d1fc45461..7e767b8559 100644 --- a/cpp/src/tests/ValueTest.cpp +++ b/cpp/src/tests/ValueTest.cpp @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -#include "../framing/Value.h" +#include "qpid/framing/Value.h" #include "qpid_test_plugin.h" using namespace qpid::framing; diff --git a/cpp/src/tests/client_test.cpp b/cpp/src/tests/client_test.cpp index 5c084302d8..d1fc261931 100644 --- a/cpp/src/tests/client_test.cpp +++ b/cpp/src/tests/client_test.cpp @@ -28,13 +28,13 @@ #include -#include "../QpidError.h" -#include "../client/ClientChannel.h" -#include "../client/Connection.h" -#include "../client/ClientMessage.h" -#include "../client/MessageListener.h" -#include "../sys/Monitor.h" -#include "../framing/FieldTable.h" +#include "qpid/QpidError.h" +#include "qpid/client/ClientChannel.h" +#include "qpid/client/Connection.h" +#include "qpid/client/ClientMessage.h" +#include "qpid/client/MessageListener.h" +#include "qpid/sys/Monitor.h" +#include "qpid/framing/FieldTable.h" using namespace qpid::client; using namespace qpid::sys; diff --git a/cpp/src/tests/echo_service.cpp b/cpp/src/tests/echo_service.cpp index 14224eeee5..09cf1e01bb 100644 --- a/cpp/src/tests/echo_service.cpp +++ b/cpp/src/tests/echo_service.cpp @@ -27,13 +27,13 @@ * sender-specified private queue. */ -#include "../QpidError.h" -#include "../client/ClientChannel.h" -#include "../client/Connection.h" -#include "../client/ClientExchange.h" -#include "../client/MessageListener.h" -#include "../client/ClientQueue.h" -#include "../sys/Time.h" +#include "qpid/QpidError.h" +#include "qpid/client/ClientChannel.h" +#include "qpid/client/Connection.h" +#include "qpid/client/ClientExchange.h" +#include "qpid/client/MessageListener.h" +#include "qpid/client/ClientQueue.h" +#include "qpid/sys/Time.h" #include #include diff --git a/cpp/src/tests/topic_listener.cpp b/cpp/src/tests/topic_listener.cpp index 0b7d6c4e86..aa51dd27d4 100644 --- a/cpp/src/tests/topic_listener.cpp +++ b/cpp/src/tests/topic_listener.cpp @@ -32,13 +32,13 @@ * listening). */ -#include "../QpidError.h" -#include "../client/ClientChannel.h" -#include "../client/Connection.h" -#include "../client/ClientExchange.h" -#include "../client/MessageListener.h" -#include "../client/ClientQueue.h" -#include "../sys/Time.h" +#include "qpid/QpidError.h" +#include "qpid/client/ClientChannel.h" +#include "qpid/client/Connection.h" +#include "qpid/client/ClientExchange.h" +#include "qpid/client/MessageListener.h" +#include "qpid/client/ClientQueue.h" +#include "qpid/sys/Time.h" #include #include diff --git a/cpp/src/tests/topic_publisher.cpp b/cpp/src/tests/topic_publisher.cpp index e4b39a2966..1c92f75cc8 100644 --- a/cpp/src/tests/topic_publisher.cpp +++ b/cpp/src/tests/topic_publisher.cpp @@ -34,15 +34,15 @@ * subscriber shutdown. */ -#include "../QpidError.h" -#include "../client/ClientChannel.h" -#include "../client/Connection.h" -#include "../client/ClientExchange.h" -#include "../client/MessageListener.h" -#include "../client/ClientQueue.h" -#include "../sys/Monitor.h" +#include "qpid/QpidError.h" +#include "qpid/client/ClientChannel.h" +#include "qpid/client/Connection.h" +#include "qpid/client/ClientExchange.h" +#include "qpid/client/MessageListener.h" +#include "qpid/client/ClientQueue.h" +#include "qpid/sys/Monitor.h" #include -#include "../sys/Time.h" +#include "qpid/sys/Time.h" #include #include -- cgit v1.2.1