Return-Path: Delivered-To: apmail-activemq-commits-archive@www.apache.org Received: (qmail 8920 invoked from network); 8 Dec 2008 21:08:12 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 8 Dec 2008 21:08:12 -0000 Received: (qmail 37978 invoked by uid 500); 8 Dec 2008 21:08:25 -0000 Delivered-To: apmail-activemq-commits-archive@activemq.apache.org Received: (qmail 37918 invoked by uid 500); 8 Dec 2008 21:08:24 -0000 Mailing-List: contact commits-help@activemq.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@activemq.apache.org Delivered-To: mailing list commits@activemq.apache.org Received: (qmail 37909 invoked by uid 99); 8 Dec 2008 21:08:24 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Dec 2008 13:08:24 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Dec 2008 21:06:51 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id BBC7F238889E; Mon, 8 Dec 2008 13:07:40 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r724483 - in /activemq/activemq-cpp/trunk/src/examples: Makefile.am topics/ topics/chat/ topics/chat/Chat.cpp topics/chat/Chat.h Date: Mon, 08 Dec 2008 21:07:40 -0000 To: commits@activemq.apache.org From: tabish@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081208210740.BBC7F238889E@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: tabish Date: Mon Dec 8 13:07:40 2008 New Revision: 724483 URL: http://svn.apache.org/viewvc?rev=724483&view=rev Log: https://issues.apache.org/activemq/browse/AMQCPP-212 Added the Chat sample. Added: activemq/activemq-cpp/trunk/src/examples/topics/ activemq/activemq-cpp/trunk/src/examples/topics/chat/ activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.cpp (with props) activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.h (with props) Modified: activemq/activemq-cpp/trunk/src/examples/Makefile.am Modified: activemq/activemq-cpp/trunk/src/examples/Makefile.am URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/examples/Makefile.am?rev=724483&r1=724482&r2=724483&view=diff ============================================================================== --- activemq/activemq-cpp/trunk/src/examples/Makefile.am (original) +++ activemq/activemq-cpp/trunk/src/examples/Makefile.am Mon Dec 8 13:07:40 2008 @@ -29,14 +29,21 @@ ## Simple Async Consumer simple_async_consumer_sources = ./consumers/SimpleAsyncConsumer.cpp -bin_PROGRAMS += simple_async_consumer +noinst_PROGRAMS = simple_async_consumer simple_async_consumer_SOURCES = $(simple_async_consumer_sources) simple_async_consumer_LDADD= $(AMQ_TEST_LIBS) simple_async_consumer_CXXFLAGS = $(AMQ_TEST_CXXFLAGS) -I$(srcdir)/../main ## Simple Producer simple_producer_sources = ./producers/SimpleProducer.cpp -bin_PROGRAMS += simple_producer +noinst_PROGRAMS += simple_producer simple_producer_SOURCES = $(simple_producer_sources) simple_producer_LDADD= $(AMQ_TEST_LIBS) simple_producer_CXXFLAGS = $(AMQ_TEST_CXXFLAGS) -I$(srcdir)/../main + +## Chat Sample +chat_sources = ./topics/chat/Chat.cpp +noinst_PROGRAMS += chat +chat_SOURCES = $(chat_sources) +chat_LDADD= $(AMQ_TEST_LIBS) +chat_CXXFLAGS = $(AMQ_TEST_CXXFLAGS) -I$(srcdir)/../main Added: activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.cpp URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.cpp?rev=724483&view=auto ============================================================================== --- activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.cpp (added) +++ activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.cpp Mon Dec 8 13:07:40 2008 @@ -0,0 +1,263 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "Chat.h" + +#include +#include +#include +#include +#include +#include + +#include +#include + +using namespace std; +using namespace cms; +using namespace example; +using namespace example::topics; +using namespace example::topics::chat; + +//////////////////////////////////////////////////////////////////////////////// +const char* Chat::DEFAULT_BROKER_URI = "tcp://127.0.0.1:61616"; +const char* Chat::DEFAULT_TOPIC_NAME = "cms.sample.chat"; + +//////////////////////////////////////////////////////////////////////////////// +Chat::Chat() { + + this->setBrokerURI( DEFAULT_BROKER_URI ); + this->setTopicName( DEFAULT_TOPIC_NAME ); +} + +//////////////////////////////////////////////////////////////////////////////// +Chat::~Chat() { + + try { + + if( this->connection.get() != NULL ) { + this->connection->close(); + } + + } catch( CMSException& ex ) { + onException( ex ); + } +} + +//////////////////////////////////////////////////////////////////////////////// +void Chat::onException( const cms::CMSException& ex ) { + ex.printStackTrace(); +} + +//////////////////////////////////////////////////////////////////////////////// +void Chat::run() { + + auto_ptr connectionFactory; + + // Create a connection. + try { + + connectionFactory.reset( + cms::ConnectionFactory::createCMSConnectionFactory( this->getBrokerURI() ) ); + + this->connection.reset( + connectionFactory->createConnection( this->getUsername(), this->getPassword() ) ); + + this->session.reset( this->connection->createSession( cms::Session::AUTO_ACKNOWLEDGE ) ); + this->topic.reset( this->session->createTopic( this->getTopicName() ) ); + this->producer.reset( this->session->createProducer( this->topic.get() ) ); + this->producer->setDeliveryMode( cms::DeliveryMode::NON_PERSISTENT ); + this->consumer.reset( this->session->createConsumer( this->topic.get() ) ); + this->consumer->setMessageListener( this ); + + // Now that setup is complete, start the Connection + this->connection->start(); + + } catch( cms::CMSException& ex ) { + onException( ex ); + return; + } + + try { + + std::cout << "\n"; + std::cout << "Enter text messages to clients that subscribe to the "; + std::cout << this->getTopicName() << " topic." << "\n"; + std::cout << "Press Enter to publish each message." << "\n"; + + while( true ) { + + char s[120]; +#ifdef AIX + if( fgets( s, 120, stdin ) == NULL ) { + break; + } + else if ( feof(stdin) || (strlen(s) == 0) || (s[0] == L'\n') ) { + break; + } +#else + + std::cin.getline(s, 120, '\n'); + + // If there was an error reading input, or + // the line was empty, exit the program. + if( std::cin.fail() || (strlen(s) == 0) ) { + break; + } + +#endif + else if( strlen(s) > 0 ) { + + int cch = this->username.length() + strlen(s) + strlen(": ") + 1; + char *text = new char[cch]; + + if( text != NULL ) { + + sprintf( text, "%s: %s", this->username.c_str(), s ); + + try { + + auto_ptr message( + this->session->createTextMessage( text ) ); + this->producer->send( message.get() ); + + } catch( cms::CMSException& ex ) { + delete [] text; + onException( ex ); + exit(1); + } + + delete [] text; + } + } + } + + } catch( cms::CMSException& ex ) { + onException( ex ); + } +} + +//////////////////////////////////////////////////////////////////////////////// +void Chat::onMessage( const cms::Message* message ) { + + // Cast the message as a TextMessage if possible. + try { + + const cms::TextMessage* textMessage = + dynamic_cast( message ); + + if( textMessage != NULL ) { + std::cout << textMessage->getText() << std::endl; + } else { + std::cerr << "Unsupported message type received: " + << typeid( *message ).name() + << std::endl; + } + + } catch( cms::CMSException& ex ) { + onException( ex ); + } +} + +//////////////////////////////////////////////////////////////////////////////// +static void printUsage() { + + std::cout << "usage: Chat (options) ..." << "\n" << "\n"; + std::cout << "options:" << "\n"; + std::cout << " -b name:port Specify name:port of broker." << "\n"; + std::cout << " Default broker: " << Chat::DEFAULT_BROKER_URI << "\n"; + std::cout << " -u name Specify a user name. (Required)" << "\n"; + std::cout << " Default user name: \n"; + std::cout << " -p password Specify password for user." << "\n"; + std::cout << " Default password: \n"; + std::cout << " -h This help screen." << "\n"; +} + +//////////////////////////////////////////////////////////////////////////////// +int main( int argc, char *argv[] ) { + + Chat chat; + + // Is there anything to do? + if( argc < 2 ) { + printUsage(); + exit(1); + } + + // Check parameters + for( int i = 1; i < argc; i++ ) { + + char *arg = argv[i]; + + if( apr_strnatcmp(arg,"-b" ) == 0 ) { + + if( i == argc - 1 || strncmp( argv[i+1], "-", 1 ) == 0 ) { + std::cout << "error: missing broker name:port" << "\n"; + exit(1); + } + + chat.setBrokerURI( argv[++i] ); + continue; + } + + if( apr_strnatcmp(arg, "-u") == 0 ) { + if (i == argc - 1 || strncmp( argv[i+1], "-", 1) == 0 ) { + std::cout << "error: missing user name" << "\n"; + exit(1); + } + + chat.setUsername( argv[++i] ); + continue; + } + + if( apr_strnatcmp(arg, "-p") == 0 ) { + if( i == argc - 1 || strncmp( argv[i+1], "-", 1 ) == 0 ) { + std::cout << "error: missing password" << "\n"; + exit(1); + } + + chat.setPassword( argv[++i] ); + continue; + } + + if( apr_strnatcmp(arg, "-h") == 0 ) { + printUsage(); + exit(1); + } + + // Invalid argument + std::cout << "error: unexpected argument: " << arg << "\n"; + printUsage(); + exit(1); + } + + // Check values read in. + if( chat.getUsername() == "" ) { + std::cout << "error: user name must be supplied" << "\n"; + printUsage(); + exit(1); + } + + // Start the CMS client for the "chat". + chat.run(); + + return 0; +} + + + + Propchange: activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.cpp ------------------------------------------------------------------------------ svn:eol-style = native Added: activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.h URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.h?rev=724483&view=auto ============================================================================== --- activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.h (added) +++ activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.h Mon Dec 8 13:07:40 2008 @@ -0,0 +1,130 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT 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 _EXAMPLE_TOPICS_CHAT_CHAT_H_ +#define _EXAMPLE_TOPICS_CHAT_CHAT_H_ + +#include +#include +#include +#include +#include +#include +#include +#include + +namespace example{ +namespace topics{ +namespace chat{ + + /** + * Simple Chat Client that uses a Topic + */ + class Chat : public cms::MessageListener, + public cms::ExceptionListener { + private: + + std::auto_ptr connection; + std::auto_ptr session; + std::auto_ptr consumer; + std::auto_ptr producer; + std::auto_ptr topic; + + std::string brokerURI; + std::string username; + std::string password; + std::string topicName; + + public: + + static const char* DEFAULT_BROKER_URI; + static const char* DEFAULT_TOPIC_NAME; + + public: + + Chat(); + virtual ~Chat(); + + virtual void onMessage( const cms::Message* message ); + virtual void onException( const cms::CMSException& ex ); + + /** + * Sets the BrokerURI + */ + void setBrokerURI( const std::string& uri ) { + this->brokerURI = uri; + } + + /** + * Gets the BrokerURI + */ + std::string getBrokerURI() const { + return this->brokerURI; + } + + /** + * Sets the Topic Name + */ + void setTopicName( const std::string& topicName ) { + this->topicName = topicName; + } + + /** + * Gets the Topic Name + */ + std::string getTopicName() const { + return this->topicName; + } + + /** + * Sets the User Name + */ + void setUsername( const std::string& username ) { + this->username = username; + } + + /** + * Gets the User Name + */ + std::string getUsername() const { + return this->username; + } + + /** + * Sets the Password + */ + void setPassword( const std::string& password ) { + this->password = password; + } + + /** + * Gets the Password + */ + std::string getPassword() const { + return this->password; + } + + /** + * Run the Chat Application. + */ + void run(); + + }; + +}}} + +#endif // _EXAMPLE_TOPICS_CHAT_CHAT_H_ Propchange: activemq/activemq-cpp/trunk/src/examples/topics/chat/Chat.h ------------------------------------------------------------------------------ svn:eol-style = native