activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject svn commit: r545883 - in /activemq/activemq-cpp/trunk/src/decaf/src: main/ main/decaf/net/ test/ test/decaf/net/
Date Sun, 10 Jun 2007 14:57:36 GMT
Author: tabish
Date: Sun Jun 10 07:57:34 2007
New Revision: 545883

URL: http://svn.apache.org/viewvc?view=rev&rev=545883
Log:
https://issues.apache.org/activemq/browse/AMQCPP-103

Building up the Decaf Library

Added:
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.cpp
    activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.h
    activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketFactoryTest.cpp
    activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketFactoryTest.h
    activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketTest.cpp
    activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketTest.h
Modified:
    activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am
    activemq/activemq-cpp/trunk/src/decaf/src/test/Makefile.am

Modified: activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am?view=diff&rev=545883&r1=545882&r2=545883
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/Makefile.am Sun Jun 10 07:57:34 2007
@@ -22,6 +22,7 @@
    decaf/net/SocketInputStream.cpp \
    decaf/net/SocketError.cpp \
    decaf/net/TcpSocket.cpp \
+   decaf/net/SocketFactory.cpp \
    decaf/lang/Exception.cpp \
    decaf/lang/Thread.cpp \
    decaf/io/BufferedOutputStream.cpp \
@@ -53,6 +54,7 @@
    decaf/net/Socket.h \
    decaf/net/SocketInputStream.h \
    decaf/net/ServerSocket.h \
+   decaf/net/SocketFactory.h \
    decaf/lang/Boolean.h \
    decaf/lang/Throwable.h \
    decaf/lang/Exception.h \

Added: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.cpp?view=auto&rev=545883
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.cpp (added)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.cpp Sun Jun 10
07:57:34 2007
@@ -0,0 +1,137 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <decaf/net/SocketFactory.h>
+#include <decaf/net/BufferedSocket.h>
+#include <decaf/net/TcpSocket.h>
+#include <decaf/util/Properties.h>
+#include <stdio.h>
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::util;
+using namespace decaf::net;
+using namespace decaf::lang;
+using namespace decaf::lang::exceptions;
+
+////////////////////////////////////////////////////////////////////////////////
+Socket* SocketFactory::createSocket(
+    const std::string& uri,
+    const Properties& properties)
+        throw ( SocketException )
+{
+    try
+    {
+        // Ensure something is actually passed in for the URI
+        if( uri == "" )
+        {
+            throw SocketException( __FILE__, __LINE__,
+                "SocketTransport::start() - uri not provided" );
+        }
+
+        string dummy = uri;
+
+        // Extract the port.
+        std::size_t portIx = dummy.find( ':' );
+        if( portIx == string::npos )
+        {
+            throw SocketException( __FILE__, __LINE__,
+                "SocketTransport::start() - uri malformed - port not specified: %s", uri.c_str()
);
+        }
+        string host = dummy.substr( 0, portIx );
+        string portString = dummy.substr( portIx + 1 );
+        int port;
+        if( sscanf( portString.c_str(), "%d", &port) != 1 )
+        {
+            throw SocketException( __FILE__, __LINE__,
+               "SocketTransport::start() - unable to extract port from uri: %s", uri.c_str()
);
+        }
+
+        // Get the read buffer size.
+        int inputBufferSize = 10000;
+        dummy = properties.getProperty( "inputBufferSize", "10000" );
+        sscanf( dummy.c_str(), "%d", &inputBufferSize );
+
+        // Get the write buffer size.
+        int outputBufferSize = 10000;
+        dummy = properties.getProperty( "outputBufferSize", "10000" );
+        sscanf( dummy.c_str(), "%d", &outputBufferSize );
+
+        // Get the linger flag.
+        int soLinger = 0;
+        dummy = properties.getProperty( "soLinger", "0" );
+        sscanf( dummy.c_str(), "%d", &soLinger );
+
+        // Get the keepAlive flag.
+        bool soKeepAlive =
+            properties.getProperty( "soKeepAlive", "false" ) == "true";
+
+        // Get the socket receive buffer size.
+        int soReceiveBufferSize = -1;
+        dummy = properties.getProperty( "soReceiveBufferSize", "-1" );
+        sscanf( dummy.c_str(), "%d", &soReceiveBufferSize );
+
+        // Get the socket send buffer size.
+        int soSendBufferSize = -1;
+        dummy = properties.getProperty( "soSendBufferSize", "-1" );
+        sscanf( dummy.c_str(), "%d", &soSendBufferSize );
+
+        // Get the socket TCP_NODELAY flag.
+        bool tcpNoDelay =
+            properties.getProperty( "tcpNoDelay", "true" ) == "true";
+
+        // Now that we have all the elements that we wanted - let's do it!
+        // Create a TCP Socket and then Wrap it in a buffered socket
+        // so that users get the benefit of buffered reads and writes.
+        // The buffered socket will own the TcpSocket instance, and will
+        // clean it up when it is cleaned up.
+        TcpSocket* tcpSocket = new TcpSocket();
+
+        try
+        {
+            // Connect the socket.
+            tcpSocket->connect( host.c_str(), port );
+
+            // Set the socket options.
+            tcpSocket->setSoLinger( soLinger );
+            tcpSocket->setKeepAlive( soKeepAlive );
+            tcpSocket->setTcpNoDelay( tcpNoDelay );
+
+            if( soReceiveBufferSize > 0 ){
+                tcpSocket->setReceiveBufferSize( soReceiveBufferSize );
+            }
+
+            if( soSendBufferSize > 0 ){
+                tcpSocket->setSendBufferSize( soSendBufferSize );
+            }
+
+        }
+        catch ( SocketException& ex )
+        {
+            ex.setMark( __FILE__, __LINE__ );
+            try{
+                delete tcpSocket;
+            } catch( SocketException& ex2 ){ /* Absorb */ }
+
+            throw ex;
+        }
+
+        return tcpSocket;
+    }
+    DECAF_CATCH_RETHROW( SocketException )
+    DECAF_CATCH_EXCEPTION_CONVERT( Exception, SocketException )
+    DECAF_CATCHALL_THROW( SocketException )
+}

Added: activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.h?view=auto&rev=545883
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/src/main/decaf/net/SocketFactory.h Sun Jun 10 07:57:34
2007
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT 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 _DECAF_NET_SOCKETFACTORY_H_
+#define _DECAF_NET_SOCKETFACTORY_H_
+
+#include <decaf/net/SocketException.h>
+#include <decaf/util/Properties.h>
+#include <decaf/util/Config.h>
+
+namespace decaf{
+namespace net{
+
+    class Socket;
+
+    /**
+     * Socket Factory implementation for use in Creating Sockets
+     * <p>
+     * <p>
+     * Property Options: <p>
+     * Name                  Value <p>
+     * ------------------------------------- <p>
+     * inputBufferSize       size in bytes of the buffered input stream buffer.  Defaults
to 10000.<p>
+     * outputBufferSize      size in bytes of the buffered output stream buffer. Defaults
to 10000.<p>
+     * soLinger              linger time for the socket (in microseconds). Defaults to 0.<p>
+     * soKeepAlive           keep alive flag for the socket (true/false). Defaults to false.<p>
+     * soReceiveBufferSize   The size of the socket receive buffer (in bytes). Defaults to
2MB.<p>
+     * soSendBufferSize      The size of the socket send buffer (in bytes). Defaults to 2MB.<p>
+     * soTimeout             The timeout of socket IO operations (in microseconds). Defaults
to 10000<p>
+     *
+     * @see <code>Socket</code>
+     */
+    class DECAF_API SocketFactory
+    {
+    public:
+
+        virtual ~SocketFactory();
+
+        /**
+         * Creates and returns a Socket dervied Object based on the values
+         * defined in the Properties Object that is passed in.
+         * @param the URI for the Socket Connection.
+         * @param properties a IProperties pointer.
+         * @throws SocketException.
+         */
+        static Socket* createSocket( const std::string& uri,
+                                     const util::Properties& properties )
+            throw ( SocketException );
+
+    };
+
+}}
+
+#endif /*_DECAF_NET_SOCKETFACTORY_H_*/

Modified: activemq/activemq-cpp/trunk/src/decaf/src/test/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/test/Makefile.am?view=diff&rev=545883&r1=545882&r2=545883
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/test/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/src/decaf/src/test/Makefile.am Sun Jun 10 07:57:34 2007
@@ -26,6 +26,8 @@
   decaf/io/ByteArrayOutputStreamTest.cpp \
   decaf/io/DataInputStreamTest.cpp \
   decaf/io/DataOutputStreamTest.cpp \
+  decaf/net/SocketFactoryTest.cpp \
+  decaf/net/SocketTest.cpp \
   decaf/util/StringTokenizerTest.cpp \
   decaf/util/Endian.cpp \
   decaf/util/DateTest.cpp \
@@ -46,6 +48,8 @@
   decaf/io/ByteArrayOutputStreamTest.h \
   decaf/io/DataInputStreamTest.h \
   decaf/io/DataOutputStreamTest.h \
+  decaf/net/SocketFactoryTest.h \
+  decaf/net/SocketTest.h \
   decaf/util/StringTokenizerTest.h \
   decaf/util/Endian.h \
   decaf/util/DateTest.h \

Added: activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketFactoryTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketFactoryTest.cpp?view=auto&rev=545883
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketFactoryTest.cpp (added)
+++ activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketFactoryTest.cpp Sun Jun
10 07:57:34 2007
@@ -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 "SocketFactoryTest.h"
+
+CPPUNIT_TEST_SUITE_REGISTRATION( decaf::net::SocketFactoryTest );
+
+#include <decaf/util/Properties.h>
+#include <decaf/net/SocketFactory.h>
+#include <decaf/net/TcpSocket.h>
+
+using namespace decaf;
+using namespace decaf::net;
+using namespace decaf::util;
+using namespace decaf::lang;
+using namespace decaf::util::concurrent;
+
+////////////////////////////////////////////////////////////////////////////////
+void SocketFactoryTest::test()
+{
+    try
+    {
+        MyServerThread serverThread;
+        serverThread.start();
+
+        Thread::sleep( 500 );
+
+        util::Properties properties;
+
+        std::ostringstream ostream;
+
+        ostream << "127.0.0.1:" << port;
+
+        properties.setProperty("soLinger", "false");
+
+        Socket* client = SocketFactory::createSocket(
+            ostream.str(), properties );
+
+        synchronized(&serverThread.mutex)
+        {
+            if(serverThread.getNumClients() != 1)
+            {
+                serverThread.mutex.wait(1000);
+            }
+        }
+
+        CPPUNIT_ASSERT( client->isConnected() );
+
+        CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
+
+        client->close();
+
+        synchronized(&serverThread.mutex)
+        {
+            if(serverThread.getNumClients() != 0)
+            {
+                serverThread.mutex.wait(1000);
+            }
+        }
+
+        CPPUNIT_ASSERT( serverThread.getNumClients() == 0 );
+
+        serverThread.stop();
+        serverThread.join();
+
+        delete client;
+    }
+    catch(lang::Exception ex)
+    {
+        std::cout << "SocketFactoryTest::test - Caught Exception." << std::endl;
+        ex.printStackTrace();
+        CPPUNIT_ASSERT( false );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SocketFactoryTest::testNoDelay()
+{
+    try
+    {
+        MyServerThread serverThread;
+        serverThread.start();
+
+        Thread::sleep( 40 );
+
+        util::Properties properties;
+
+        std::ostringstream ostream;
+
+        ostream << "127.0.0.1:" << port;
+
+        properties.setProperty( "soLinger", "false" );
+        properties.setProperty( "tcpNoDelay", "true" );
+
+        Socket* client = SocketFactory::createSocket(
+            ostream.str(), properties );
+
+        TcpSocket* tcpSock = dynamic_cast<TcpSocket*>( client );
+        CPPUNIT_ASSERT( tcpSock != NULL );
+        CPPUNIT_ASSERT( tcpSock->getTcpNoDelay() == true );
+
+        synchronized(&serverThread.mutex)
+        {
+            if(serverThread.getNumClients() != 1)
+            {
+                serverThread.mutex.wait(1000);
+            }
+        }
+
+        CPPUNIT_ASSERT( client->isConnected() );
+
+        CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
+
+        client->close();
+
+        synchronized(&serverThread.mutex)
+        {
+            if(serverThread.getNumClients() != 0)
+            {
+                serverThread.mutex.wait(1000);
+            }
+        }
+
+        CPPUNIT_ASSERT( serverThread.getNumClients() == 0 );
+
+        serverThread.stop();
+        serverThread.join();
+
+        delete client;
+    }
+    catch(lang::Exception ex)
+    {
+        CPPUNIT_ASSERT( false );
+    }
+}

Added: activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketFactoryTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketFactoryTest.h?view=auto&rev=545883
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketFactoryTest.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketFactoryTest.h Sun Jun 10
07:57:34 2007
@@ -0,0 +1,145 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT 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 _DECAF_NET_SOCKETFACTORYTEST_H_
+#define _DECAF_NET_SOCKETFACTORYTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <decaf/net/Socket.h>
+#include <decaf/net/ServerSocket.h>
+#include <decaf/util/concurrent/Concurrent.h>
+#include <decaf/util/concurrent/Mutex.h>
+#include <decaf/lang/Thread.h>
+
+#include <sstream>
+
+namespace decaf{
+namespace net{
+
+    class SocketFactoryTest : public CppUnit::TestFixture
+    {
+        CPPUNIT_TEST_SUITE( SocketFactoryTest );
+        CPPUNIT_TEST( test );
+        CPPUNIT_TEST( testNoDelay );
+        CPPUNIT_TEST_SUITE_END();
+
+        static const int port = 23232;
+
+        class MyServerThread : public lang::Thread{
+        private:
+
+            bool done;
+            int numClients;
+            std::string lastMessage;
+
+        public:
+
+            util::concurrent::Mutex mutex;
+
+        public:
+
+            MyServerThread(){
+                done = false;
+                numClients = 0;
+            }
+            virtual ~MyServerThread(){
+                stop();
+            }
+
+            std::string getLastMessage(){
+                return lastMessage;
+            }
+
+            int getNumClients(){
+                return numClients;
+            }
+
+            virtual void stop(){
+                done = true;
+            }
+
+            virtual void run(){
+                try{
+                    unsigned char buf[1000];
+
+                    ServerSocket server;
+                    server.bind( "127.0.0.1", port );
+
+                    net::Socket* socket = server.accept();
+                    server.close();
+
+                    socket->setSoLinger( false );
+
+                    synchronized(&mutex)
+                    {
+                        numClients++;
+                        mutex.notifyAll();
+                    }
+
+                    while( !done && socket != NULL ){
+
+                        io::InputStream* stream = socket->getInputStream();
+                        memset( buf, 0, 1000 );
+                        try{
+                            stream->read( buf, 1000 );
+
+                            lastMessage = (char*)buf;
+
+                            if( strcmp( (char*)buf, "reply" ) == 0 ){
+                                io::OutputStream* output = socket->getOutputStream();
+                                output->write( (unsigned char*)"hello", strlen("hello"
) );
+                            }
+
+                        }catch( io::IOException& ex ){
+                            done = true;
+                        }
+                    }
+
+                    socket->close();
+                    delete socket;
+
+                    numClients--;
+
+                    synchronized(&mutex)
+                    {
+                        mutex.notifyAll();
+                    }
+
+                }catch( io::IOException& ex ){
+                    printf("%s\n", ex.getMessage().c_str() );
+                    CPPUNIT_ASSERT( false );
+                }catch( ... ){
+                    CPPUNIT_ASSERT( false );
+                }
+            }
+        };
+
+    public:
+
+        SocketFactoryTest() {}
+        virtual ~SocketFactoryTest() {}
+
+        void test();
+        void testNoDelay();
+
+    };
+
+}}
+
+#endif /*_DECAF_NET_SOCKETFACTORYTEST_H_*/

Added: activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketTest.cpp?view=auto&rev=545883
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketTest.cpp (added)
+++ activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketTest.cpp Sun Jun 10 07:57:34
2007
@@ -0,0 +1,234 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "SocketTest.h"
+
+CPPUNIT_TEST_SUITE_REGISTRATION( decaf::net::SocketTest );
+
+#include <decaf/net/TcpSocket.h>
+
+using namespace std;
+using namespace decaf;
+using namespace decaf::net;
+using namespace decaf::util;
+using namespace decaf::lang;
+
+////////////////////////////////////////////////////////////////////////////////
+void SocketTest::testConnect() {
+
+    try{
+
+        MyServerThread serverThread;
+        serverThread.start();
+
+        Thread::sleep( 40 );
+
+        TcpSocket client;
+
+        client.connect("127.0.0.1", port);
+        client.setSoLinger( false );
+
+        synchronized(&serverThread.mutex)
+        {
+           if(serverThread.getNumClients() != 1)
+           {
+              serverThread.mutex.wait(1000);
+           }
+        }
+
+        CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
+
+        client.close();
+
+        synchronized(&serverThread.mutex)
+        {
+           if(serverThread.getNumClients() != 0)
+           {
+              serverThread.mutex.wait(1000);
+           }
+        }
+
+        CPPUNIT_ASSERT( serverThread.getNumClients() == 0 );
+
+        serverThread.stop();
+        serverThread.join();
+
+    }catch( io::IOException& ex ){
+        printf( "%s\n", ex.getMessage().c_str() );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SocketTest::testTx() {
+
+    try{
+
+        MyServerThread serverThread;
+        serverThread.start();
+
+        Thread::sleep( 10 );
+
+        TcpSocket client;
+
+        client.connect("127.0.0.1", port);
+        client.setSoLinger( false );
+
+        synchronized(&serverThread.mutex)
+        {
+           if(serverThread.getNumClients() != 1)
+           {
+              serverThread.mutex.wait(1000);
+           }
+        }
+
+        CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
+
+        io::OutputStream* stream = client.getOutputStream();
+
+        std::string msg = "don't reply";
+        stream->write( (unsigned char*)msg.c_str(), msg.length() );
+
+        Thread::sleep( 10 );
+
+        CPPUNIT_ASSERT( serverThread.getLastMessage() == msg );
+
+        client.close();
+
+        synchronized(&serverThread.mutex)
+        {
+           if(serverThread.getNumClients() != 0)
+           {
+              serverThread.mutex.wait(1000);
+           }
+        }
+
+        CPPUNIT_ASSERT( serverThread.getNumClients() == 0 );
+
+        serverThread.stop();
+        serverThread.join();
+
+    }catch( io::IOException& ex ){
+        printf( "%s\n", ex.getMessage().c_str() );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SocketTest::testTrx() {
+
+    try{
+
+        MyServerThread serverThread;
+        serverThread.start();
+
+        Thread::sleep( 10 );
+
+        TcpSocket client;
+
+        client.connect("127.0.0.1", port);
+        client.setSoLinger(false);
+
+        synchronized(&serverThread.mutex)
+        {
+           if(serverThread.getNumClients() != 1)
+           {
+              serverThread.mutex.wait(1000);
+           }
+        }
+
+        CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
+
+        io::OutputStream* stream = client.getOutputStream();
+
+        std::string msg = "reply";
+        stream->write( (unsigned char*)msg.c_str(), msg.length() );
+
+        synchronized(&serverThread.mutex)
+        {
+           serverThread.mutex.wait(300);
+        }
+
+        unsigned char buf[500];
+        memset( buf, 0, 500 );
+        io::InputStream* istream = client.getInputStream();
+        std::size_t numRead = istream->read( buf, 500 );
+        CPPUNIT_ASSERT( numRead == 5 );
+        CPPUNIT_ASSERT( strcmp( (char*)buf, "hello" ) == 0 );
+
+        client.close();
+
+        serverThread.stop();
+        serverThread.join();
+
+    }catch( io::IOException& ex ){
+        printf( "%s\n", ex.getMessage().c_str() );
+    }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+void SocketTest::testTrxNoDelay() {
+
+    try{
+
+        MyServerThread serverThread;
+        serverThread.start();
+
+        Thread::sleep( 10 );
+
+        TcpSocket client;
+
+        client.connect("127.0.0.1", port);
+        client.setSoLinger(false);
+        client.setTcpNoDelay(true);
+
+        CPPUNIT_ASSERT( client.getTcpNoDelay() == true );
+
+        synchronized(&serverThread.mutex)
+        {
+           if(serverThread.getNumClients() != 1)
+           {
+              serverThread.mutex.wait(1000);
+           }
+        }
+
+        CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
+
+        io::OutputStream* stream = client.getOutputStream();
+
+        std::string msg = "reply";
+        stream->write( (unsigned char*)msg.c_str(), msg.length() );
+
+        synchronized(&serverThread.mutex)
+        {
+           serverThread.mutex.wait(300);
+        }
+
+        unsigned char buf[500];
+        memset( buf, 0, 500 );
+        io::InputStream* istream = client.getInputStream();
+        std::size_t numRead = istream->read( buf, 500 );
+        CPPUNIT_ASSERT( numRead == 5 );
+        CPPUNIT_ASSERT( strcmp( (char*)buf, "hello" ) == 0 );
+
+        client.close();
+
+        serverThread.stop();
+        serverThread.join();
+
+    }catch( io::IOException& ex ){
+        printf( "%s\n", ex.getMessage().c_str() );
+    }
+}

Added: activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketTest.h?view=auto&rev=545883
==============================================================================
--- activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketTest.h (added)
+++ activemq/activemq-cpp/trunk/src/decaf/src/test/decaf/net/SocketTest.h Sun Jun 10 07:57:34
2007
@@ -0,0 +1,158 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT 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 _DECAF_IO_SOCKETTEST_H_
+#define _DECAF_IO_SOCKETTEST_H_
+
+#include <cppunit/TestFixture.h>
+#include <cppunit/extensions/HelperMacros.h>
+
+#include <decaf/net/ServerSocket.h>
+#include <decaf/util/concurrent/Concurrent.h>
+#include <decaf/util/concurrent/Mutex.h>
+#include <decaf/lang/Thread.h>
+#include <list>
+#include <string.h>
+
+namespace decaf{
+namespace net{
+
+    class SocketTest : public CppUnit::TestFixture {
+
+        CPPUNIT_TEST_SUITE( SocketTest );
+        CPPUNIT_TEST( testConnect );
+        CPPUNIT_TEST( testTx );
+        CPPUNIT_TEST( testTrx );
+        CPPUNIT_TEST( testTrxNoDelay );
+        CPPUNIT_TEST_SUITE_END();
+
+    public:
+
+        static const int port = 23232;
+
+        class MyServerThread : public lang::Thread{
+        private:
+
+            bool done;
+            int numClients;
+            std::string lastMessage;
+
+        public:
+
+            util::concurrent::Mutex mutex;
+
+        public:
+
+            MyServerThread(){
+                done = false;
+                numClients = 0;
+            }
+
+            virtual ~MyServerThread(){
+                stop();
+            }
+
+            std::string getLastMessage(){
+                return lastMessage;
+            }
+
+            int getNumClients(){
+                return numClients;
+            }
+
+            virtual void stop(){
+                done = true;
+            }
+
+            virtual void run(){
+                try{
+                    unsigned char buf[1000];
+
+                    ServerSocket server;
+                    server.bind( "127.0.0.1", port );
+
+                    Socket* socket = server.accept();
+                    server.close();
+
+                    //socket->setSoTimeout( 10 );
+                    socket->setSoLinger( false );
+                    numClients++;
+
+                    synchronized(&mutex)
+                    {
+                       mutex.notifyAll();
+                    }
+
+                    while( !done && socket != NULL ){
+
+                        io::InputStream* stream = socket->getInputStream();
+
+                        memset( buf, 0, 1000 );
+                        try{
+                            stream->read( buf, 1000 );
+
+                            lastMessage = (char*)buf;
+
+                            if( strcmp( (char*)buf, "reply" ) == 0 ){
+                                io::OutputStream* output = socket->getOutputStream();
+                                output->write( (unsigned char*)"hello", strlen("hello"
) );
+
+                                  synchronized(&mutex)
+                                  {
+                                     mutex.notifyAll();
+                                  }
+                            }
+
+                        }catch( io::IOException& ex ){
+                            done = true;
+                        }
+                    }
+
+                    socket->close();
+                    delete socket;
+
+                    numClients--;
+
+                    synchronized(&mutex)
+                    {
+                        mutex.notifyAll();
+                    }
+
+                }catch( io::IOException& ex ){
+                    printf("%s\n", ex.getMessage().c_str() );
+                    CPPUNIT_ASSERT( false );
+                }catch( ... ){
+                    CPPUNIT_ASSERT( false );
+                }
+            }
+
+        };
+
+    public:
+
+        virtual ~SocketTest() {}
+
+        void testConnect();
+        void testTx();
+        void testTrx();
+        void testTrxNoDelay();
+
+    };
+
+}}
+
+#endif /*_DECAF_IO_SOCKETTEST_H_*/



Mime
View raw message