Author: nmittler Date: Mon Dec 11 17:11:54 2006 New Revision: 485973 URL: http://svn.apache.org/viewvc?view=rev&rev=485973 Log: [AMQCPP-16] - changing StompFrame to use a std::vector rather than a direct pointer. This is to support the updated interface on the BytesMessage.
Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandReader.cpp incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.cpp incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompFrame.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/AbstractCommand.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/BytesMessageCommand.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/ErrorCommand.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/StompMessage.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/TextMessageCommand.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompConnectorTest.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompFrameTest.h incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/core/ActiveMQSessionTest.h Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandReader.cpp URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandReader.cpp?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandReader.cpp (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandReader.cpp Mon Dec 11 17:11:54 2006 @@ -282,11 +282,8 @@ if( content_length != 0 ) { - char* cpyBody = new char[content_length]; - memcpy( cpyBody, &buffer[0], content_length ); - // Set the body contents in the frame - copy the memory - frame.setBody( cpyBody, content_length ); + frame.getBody() = buffer; } } AMQ_CATCH_EXCEPTION_CONVERT( IOException, CommandIOException ) Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.cpp URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.cpp?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.cpp (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompCommandWriter.cpp Mon Dec 11 17:11:54 2006 @@ -78,10 +78,10 @@ writeByte( '\n' ); // Write the body. - const char* body = frame.getBody(); - if( body != NULL ) + const std::vector<unsigned char>& body = frame.getBody(); + if( body.size() > 0 ) { - write( body, frame.getBodyLength() ); + write( &body[0], body.size() ); } if( ( frame.getBodyLength() == 0 ) || Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompFrame.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompFrame.h?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompFrame.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/StompFrame.h Mon Dec 11 17:11:54 2006 @@ -37,28 +37,23 @@ /** * Default constructor. */ - StompFrame(void){ - body = NULL; - bodyLength = 0; - } + StompFrame(){} /** - * Destruction - frees the memory pool. + * Destruction. */ - virtual ~StompFrame(void) { delete body; } + virtual ~StompFrame() {} /** * Clonse this message exactly, returns a new instance that the * caller is required to delete. * @return new copy of this message */ - virtual StompFrame* clone(void) const { + virtual StompFrame* clone() const { StompFrame* frame = new StompFrame(); frame->command = command; frame->properties = properties; - char* cpyBody = new char[bodyLength]; - memcpy(cpyBody, body, bodyLength); - frame->setBody(cpyBody, bodyLength); + frame->body = body; return frame; } @@ -73,7 +68,7 @@ /** * Accessor for this frame's command field. */ - const std::string& getCommand(void) const{ + const std::string& getCommand() const{ return command; } @@ -81,8 +76,8 @@ * Gets access to the header properties for this frame. * @return the Properties object owned by this Frame */ - util::Properties& getProperties(void){ return properties; } - const util::Properties& getProperties(void) const { + util::Properties& getProperties(){ return properties; } + const util::Properties& getProperties() const { return properties; } @@ -90,7 +85,14 @@ * Accessor for the body data of this frame. * @return char pointer to body data */ - const char* getBody(void) const{ + const std::vector<unsigned char>& getBody() const{ + return body; + } + + /** + * Non-const version of the body accessor. + */ + std::vector<unsigned char>& getBody(){ return body; } @@ -98,16 +100,23 @@ * Return the number of bytes contained in this frames body * @return Body bytes length. */ - long long getBodyLength(void) const{ return bodyLength; } + unsigned long long getBodyLength() const{ return (unsigned long long)body.size(); } /** * Sets the body data of this frame as a byte sequence. * @param bytes The byte buffer to be set in the body. * @param numBytes The number of bytes in the buffer. */ - void setBody( const char* bytes, const long long numBytes ){ - body = bytes; - bodyLength = numBytes; + void setBody( const unsigned char* bytes, unsigned long long numBytes ){ + + // Remove old data + body.clear(); + + // Copy data to internal buffer. + for( long long ix = 0; ix < numBytes; ++ix ) + { + body.push_back(bytes[ix]); + } } private: @@ -119,9 +128,7 @@ util::SimpleProperties properties; // Byte data of Body. - const char* body; - long long bodyLength; - + std::vector<unsigned char> body; }; }}} Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/AbstractCommand.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/AbstractCommand.h?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/AbstractCommand.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/AbstractCommand.h Mon Dec 11 17:11:54 2006 @@ -257,7 +257,11 @@ * Returns a char array of bytes that are contained in the message * @return pointer to array of bytes. */ - virtual const char* getBytes(void) const{ + virtual const std::vector<unsigned char>& getBytes() const{ + return getFrame().getBody(); + } + + virtual std::vector<unsigned char>& getBytes(){ return getFrame().getBody(); } @@ -270,19 +274,30 @@ * @param setContentLength true if the content length header should * be set */ - virtual void setBytes( const char* bytes, + virtual void setBytes( const unsigned char* bytes, const unsigned long long numBytes, const bool setContentLength = true ) { - char* copy = new char[numBytes]; - memcpy( copy, bytes, numBytes ); - getFrame().setBody( copy, numBytes ); + getFrame().setBody( bytes, numBytes ); if( setContentLength ) { setPropertyValue( CommandConstants::toString( CommandConstants::HEADER_CONTENTLENGTH), util::Long::toString( numBytes ) ); + } + } + + virtual void setBytes( const std::vector<unsigned char>& bytes, + const bool setContentLength = true ) + { + getFrame().getBody() = bytes; + if( setContentLength ) + { + setPropertyValue( + CommandConstants::toString( + CommandConstants::HEADER_CONTENTLENGTH), + util::Long::toString( bytes.size() ) ); } } }; Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/BytesMessageCommand.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/BytesMessageCommand.h?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/BytesMessageCommand.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/BytesMessageCommand.h Mon Dec 11 17:11:54 2006 @@ -21,6 +21,7 @@ #include <cms/BytesMessage.h> #include <activemq/connector/stomp/commands/StompMessage.h> #include <activemq/connector/stomp/commands/CommandConstants.h> +#include <activemq/exceptions/IllegalStateException.h> namespace activemq{ namespace connector{ @@ -34,24 +35,59 @@ */ class BytesMessageCommand : public StompMessage< cms::BytesMessage > { + private: + + /** + * Flag that indicates what state the stream is in. If true, the + * message may only be read from. If false, the message may only be + * written to. + */ + bool readOnly; + public: - BytesMessageCommand(void) : - StompMessage< cms::BytesMessage >() { - initialize( getFrame() ); + BytesMessageCommand() : + StompMessage< cms::BytesMessage >() { + initialize( getFrame() ); + readOnly = false; } + BytesMessageCommand( StompFrame* frame ) : - StompMessage< cms::BytesMessage >( frame ) { - validate( getFrame() ); + StompMessage< cms::BytesMessage >( frame ) { + validate( getFrame() ); + readOnly = false; } - virtual ~BytesMessageCommand(void) {} + + virtual ~BytesMessageCommand() {} /** + * Clears out the body of the message. This does not clear the + * headers or properties. + */ + virtual void clearBody(){ + + // Invoke base class's version. + StompMessage< cms::BytesMessage >::clearBody(); + + // Set the stream in write only mode. + readOnly = false; + } + + /** + * Puts the message body in read-only mode and repositions the stream + * of bytes to the beginning. + * @throws CMSException + */ + virtual void reset() throw ( cms::CMSException ){ + readOnly = true; + } + + /** * Clonse this message exactly, returns a new instance that the * caller is required to delete. * @return new copy of this message */ - virtual cms::Message* clone(void) const { + virtual cms::Message* clone() const { StompFrame* frame = getFrame().clone(); return new BytesMessageCommand( frame ); @@ -66,8 +102,13 @@ virtual void setBodyBytes( const unsigned char* buffer, const unsigned long long numBytes ) throw( cms::CMSException ) { - this->setBytes( - reinterpret_cast<const char*>( buffer ), numBytes ); + + if( readOnly ){ + throw exceptions::IllegalStateException( __FILE__, __LINE__, + "message is in read-only mode and cannot be written to" ); + } + + this->setBytes( buffer, numBytes ); } /** @@ -77,19 +118,23 @@ * to expect. * @return const pointer to a byte buffer */ - virtual const unsigned char* getBodyBytes(void) const { - return reinterpret_cast<const unsigned char*>( - this->getBytes() ); + virtual const unsigned char* getBodyBytes() const { + const std::vector<unsigned char>& bytes = getBytes(); + if( bytes.size() == 0 ){ + return NULL; + } + + return &this->getBytes()[0]; } /** * Returns the number of bytes contained in the body of this message. * @return number of bytes. */ - virtual unsigned long long getBodyLength(void) const { + virtual unsigned long long getBodyLength() const { return this->getNumBytes(); - } - + } + }; }}}} Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/ErrorCommand.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/ErrorCommand.h?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/ErrorCommand.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/ErrorCommand.h Mon Dec 11 17:11:54 2006 @@ -35,21 +35,23 @@ { public: - ErrorCommand(void) : + ErrorCommand() : AbstractCommand<transport::Command>() { initialize( getFrame() ); } + ErrorCommand( StompFrame* frame ) : AbstractCommand<transport::Command>( frame ) { validate( getFrame() ); } - virtual ~ErrorCommand(void) {}; + + virtual ~ErrorCommand() {}; /** * Get the error message * @return the error message string */ - virtual std::string getErrorMessage(void) const { + virtual std::string getErrorMessage() const { return getPropertyValue( CommandConstants::toString( CommandConstants::HEADER_MESSAGE ), "" ); @@ -71,15 +73,21 @@ * @param text Detailed Error Message */ virtual void setErrorDetails( const std::string& text ) { - setBytes( text.c_str(), text.length() + 1 ); + setBytes( (unsigned char*)text.c_str(), text.length() + 1 ); } /** * Get the Text associated with this Error * @return Error Message String */ - virtual std::string getErrorDetails(void) const { - return getBytes() != NULL ? getBytes() : ""; + virtual std::string getErrorDetails() const { + + const std::vector<unsigned char>& bytes = getBytes(); + if( bytes.size() == 0 ){ + return ""; + } + + return std::string((char*)&getBytes()[0]); } protected: Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/StompMessage.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/StompMessage.h?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/StompMessage.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/StompMessage.h Mon Dec 11 17:11:54 2006 @@ -72,7 +72,9 @@ AbstractCommand< transport::Command >(), ackHandler( NULL ), dest( NULL ), - replyTo( NULL) {} + replyTo( NULL) { + } + StompMessage( StompFrame* frame ) : AbstractCommand< transport::Command >( frame ), ackHandler( NULL ), @@ -93,14 +95,23 @@ } } - virtual ~StompMessage() { delete dest; } + virtual ~StompMessage() { + + if( dest != NULL ){ + delete dest; + } + + if( replyTo != NULL ){ + delete replyTo; + } + } /** * Clears out the body of the message. This does not clear the * headers or properties. */ virtual void clearBody(){ - getFrame().setBody( NULL, 0 ); + getFrame().getBody().clear(); } /** Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/TextMessageCommand.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/TextMessageCommand.h?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/TextMessageCommand.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/connector/stomp/commands/TextMessageCommand.h Mon Dec 11 17:11:54 2006 @@ -32,22 +32,24 @@ { public: - TextMessageCommand(void) : + TextMessageCommand() : StompMessage< cms::TextMessage >() { initialize( getFrame() ); } + TextMessageCommand( StompFrame* frame ) : StompMessage< cms::TextMessage >( frame ) { validate( getFrame() ); } - virtual ~TextMessageCommand(void) {} + + virtual ~TextMessageCommand() {} /** * Clonse this message exactly, returns a new instance that the * caller is required to delete. * @return new copy of this message */ - virtual cms::Message* clone(void) const { + virtual cms::Message* clone() const { StompFrame* frame = getFrame().clone(); return new TextMessageCommand( frame ); @@ -57,8 +59,14 @@ * Gets the message character buffer. * @return The message character buffer. */ - virtual std::string getText(void) const throw( cms::CMSException ) { - return getBytes() != NULL ? getBytes() : ""; + virtual std::string getText() const throw( cms::CMSException ) { + + const std::vector<unsigned char>& bytes = getBytes(); + if( bytes.size() == 0 ){ + return ""; + } + + return std::string( (char*)&bytes[0] ); } /** @@ -66,7 +74,7 @@ * @param msg The message buffer. */ virtual void setText( const char* msg ) throw( cms::CMSException ) { - setBytes( msg, strlen(msg) + 1, false ); + setBytes( (unsigned char*)msg, strlen(msg) + 1, false ); } /** @@ -74,7 +82,7 @@ * @param msg The message buffer. */ virtual void setText( const std::string& msg ) throw( cms::CMSException ) { - setBytes( msg.c_str(), msg.length() + 1, false ); + setBytes( (unsigned char*)msg.c_str(), msg.length() + 1, false ); } }; Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompConnectorTest.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompConnectorTest.h?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompConnectorTest.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompConnectorTest.h Mon Dec 11 17:11:54 2006 @@ -247,7 +247,7 @@ frame->getProperties().setProperty( "destination", dest1.toProviderString() ); const char* buffer = strdup("hello world"); - frame->setBody( buffer, 12 ); + frame->setBody( (const unsigned char*)buffer, 12 ); commands::TextMessageCommand* msg = new commands::TextMessageCommand( frame ); @@ -267,7 +267,7 @@ frame->getProperties().setProperty( "destination", dest2.toProviderString() ); buffer = strdup("hello world"); - frame->setBody( buffer, 12 ); + frame->setBody( (unsigned char*)buffer, 12 ); msg = new commands::TextMessageCommand( frame ); transport.fireCommand( msg ); Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompFrameTest.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompFrameTest.h?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompFrameTest.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/connector/stomp/StompFrameTest.h Mon Dec 11 17:11:54 2006 @@ -52,14 +52,14 @@ CPPUNIT_ASSERT( result == "value" ); - CPPUNIT_ASSERT( frame.getBody() == NULL ); + CPPUNIT_ASSERT( frame.getBody().size() == 0 ); CPPUNIT_ASSERT( frame.getBodyLength() == 0 ); - frame.setBody( strdup("ABC"), 4 ); + frame.setBody( (unsigned char*)strdup("ABC"), 4 ); - CPPUNIT_ASSERT( frame.getBody() != NULL ); + CPPUNIT_ASSERT( frame.getBody().size() == 4 ); CPPUNIT_ASSERT( frame.getBodyLength() == 4 ); - CPPUNIT_ASSERT( std::string(frame.getBody()) == "ABC" ); + CPPUNIT_ASSERT( std::string((char*)&frame.getBody()[0]) == "ABC" ); } }; Modified: incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/core/ActiveMQSessionTest.h URL: http://svn.apache.org/viewvc/incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/core/ActiveMQSessionTest.h?view=diff&rev=485973&r1=485972&r2=485973 ============================================================================== --- incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/core/ActiveMQSessionTest.h (original) +++ incubator/activemq/activemq-cpp/trunk/activemq-cpp/src/test/activemq/core/ActiveMQSessionTest.h Mon Dec 11 17:11:54 2006 @@ -200,7 +200,7 @@ frame->getProperties().setProperty( "destination", destination.toProviderString() ); const char* buffer = strdup( message.c_str() ); - frame->setBody( buffer, 12 ); + frame->setBody( (unsigned char*)buffer, 12 ); connector::stomp::commands::TextMessageCommand* msg = new connector::stomp::commands::TextMessageCommand( frame );