You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2010/04/28 22:40:49 UTC

svn commit: r939081 - in /activemq/activemq-cpp/trunk/activemq-cpp/src: main/decaf/internal/net/tcp/TcpSocket.cpp main/decaf/net/Socket.cpp test/decaf/net/SocketTest.cpp test/decaf/net/SocketTest.h

Author: tabish
Date: Wed Apr 28 20:40:49 2010
New Revision: 939081

URL: http://svn.apache.org/viewvc?rev=939081&view=rev
Log:
Adds new Socket tests, fixes some socket methods for getting the ports on the local and remote end of the connection.

Modified:
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketTest.cpp
    activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketTest.h

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.cpp?rev=939081&r1=939080&r2=939081&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.cpp Wed Apr 28 20:40:49 2010
@@ -294,6 +294,9 @@ void TcpSocket::connect( const std::stri
         apr_socket_opt_set( socketHandle, APR_SO_NONBLOCK, oldNonblockSetting );
         apr_socket_timeout_set( socketHandle, oldTimeoutSetting );
 
+        // Now that we connected, cache the port value for later lookups.
+        this->port = port;
+
     } catch( IOException& ex ) {
         ex.setMark( __FILE__, __LINE__);
         try{ close(); } catch( lang::Exception& cx){ /* Absorb */ }
@@ -441,6 +444,8 @@ void TcpSocket::close() throw( decaf::io
             apr_socket_shutdown( socketHandle, APR_SHUTDOWN_READWRITE );
             apr_socket_close( socketHandle );
             socketHandle = NULL;
+            port = 0;
+            localPort = 0;
         }
     }
     DECAF_CATCH_RETHROW( decaf::io::IOException )

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.cpp?rev=939081&r1=939080&r2=939081&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.cpp Wed Apr 28 20:40:49 2010
@@ -74,7 +74,7 @@ Socket::Socket( const std::string& host,
             this->impl = new TcpSocket();
         }
 
-        this->initSocketImpl( host, port, "", 0 );
+        this->initSocketImpl( host, port, "0.0.0.0", 0 );
     }
     DECAF_CATCH_RETHROW( UnknownHostException )
     DECAF_CATCH_RETHROW( IOException )
@@ -127,7 +127,7 @@ void Socket::initSocketImpl( const std::
         ensureCreated();
 
         try {
-            //this->impl->bind( localAddress, localPort );
+            this->impl->bind( localAddress, localPort );
             this->bound = true;
             this->impl->connect( host, port, -1 );
             this->connected = true;

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketTest.cpp?rev=939081&r1=939080&r2=939081&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketTest.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketTest.cpp Wed Apr 28 20:40:49 2010
@@ -84,6 +84,19 @@ void SocketTest::testClose() {
 }
 
 ////////////////////////////////////////////////////////////////////////////////
+void SocketTest::testGetPort() {
+
+    ServerSocket server(0);
+    int serverPort = server.getLocalPort();
+    Socket client( "localhost", serverPort );
+
+    CPPUNIT_ASSERT_EQUAL_MESSAGE( "Returned incorrect port", serverPort, client.getPort() );
+
+    client.close();
+    server.close();
+}
+
+////////////////////////////////////////////////////////////////////////////////
 void SocketTest::testGetInputStream() {
 
     ServerSocket ss(0);
@@ -307,7 +320,7 @@ namespace {
         bool done;
         int numClients;
         std::string lastMessage;
-        int port;
+        std::auto_ptr<ServerSocket> server;
 
     public:
 
@@ -315,13 +328,22 @@ namespace {
 
     public:
 
-        MyServerThread( int port ) : Thread(), done( false ), numClients( 0 ), lastMessage(), port( port ) {
+        MyServerThread() : Thread(), done( false ), numClients( 0 ), lastMessage() {
+            server.reset( new ServerSocket(0) );
         }
 
         virtual ~MyServerThread(){
             stop();
         }
 
+        int getLocalPort() {
+            if( this->server.get() != NULL ) {
+                return server->getLocalPort();
+            }
+
+            return 0;
+        }
+
         std::string getLastMessage(){
             return lastMessage;
         }
@@ -338,11 +360,8 @@ namespace {
             try{
                 unsigned char buf[1000];
 
-                ServerSocket server;
-                server.bind( "127.0.0.1", port );
-
-                Socket* socket = server.accept();
-                server.close();
+                std::auto_ptr<Socket> socket( server->accept() );
+                server->close();
 
                 //socket->setSoTimeout( 10 );
                 socket->setSoLinger( false, 0 );
@@ -353,7 +372,7 @@ namespace {
                    mutex.notifyAll();
                 }
 
-                while( !done && socket != NULL ){
+                while( !done && socket.get() != NULL ){
 
                     io::InputStream* stream = socket->getInputStream();
 
@@ -382,7 +401,6 @@ namespace {
                 }
 
                 socket->close();
-                delete socket;
 
                 numClients--;
 
@@ -407,23 +425,22 @@ void SocketTest::testConnect() {
 
     try{
 
-        MyServerThread serverThread( port );
+        MyServerThread serverThread;
         serverThread.start();
 
-        Thread::sleep( 40 );
+        Thread::sleep( 100 );
 
         std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
         std::auto_ptr<Socket> client( factory->createSocket() );
 
-        client->connect( "127.0.0.1", port );
+        client->connect( "127.0.0.1", serverThread.getLocalPort() );
         client->setSoLinger( false, 0 );
 
-        synchronized(&serverThread.mutex)
-        {
-           if(serverThread.getNumClients() != 1)
-           {
-              serverThread.mutex.wait(1000);
-           }
+        synchronized( &serverThread.mutex ) {
+
+            if( serverThread.getNumClients() != 1 ) {
+                serverThread.mutex.wait(1000);
+            }
         }
 
         CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
@@ -453,15 +470,15 @@ void SocketTest::testTx() {
 
     try{
 
-        MyServerThread serverThread( port );
+        MyServerThread serverThread;
         serverThread.start();
 
-        Thread::sleep( 10 );
+        Thread::sleep( 100 );
 
         std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
         std::auto_ptr<Socket> client( factory->createSocket() );
 
-        client->connect("127.0.0.1", port);
+        client->connect("127.0.0.1", serverThread.getLocalPort() );
         client->setSoLinger( false, 0 );
         client->setTcpNoDelay( true );
 
@@ -509,15 +526,15 @@ void SocketTest::testTrx() {
 
     try{
 
-        MyServerThread serverThread( port );
+        MyServerThread serverThread;
         serverThread.start();
 
-        Thread::sleep( 10 );
+        Thread::sleep( 100 );
 
         std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
         std::auto_ptr<Socket> client( factory->createSocket() );
 
-        client->connect("127.0.0.1", port);
+        client->connect( "127.0.0.1", serverThread.getLocalPort() );
         client->setSoLinger( false, 0 );
 
         synchronized(&serverThread.mutex)
@@ -563,15 +580,15 @@ void SocketTest::testRxFail() {
 
     try{
 
-        MyServerThread serverThread( port );
+        MyServerThread serverThread;
         serverThread.start();
 
-        Thread::sleep( 10 );
+        Thread::sleep( 100 );
 
         std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
         std::auto_ptr<Socket> client( factory->createSocket() );
 
-        client->connect("127.0.0.1", port);
+        client->connect("127.0.0.1", serverThread.getLocalPort() );
         client->setSoLinger( false, 0 );
 
         synchronized(&serverThread.mutex)
@@ -612,7 +629,7 @@ void SocketTest::testTrxNoDelay() {
 
     try{
 
-        MyServerThread serverThread( port );
+        MyServerThread serverThread;
         serverThread.start();
 
         Thread::sleep( 10 );
@@ -620,7 +637,7 @@ void SocketTest::testTrxNoDelay() {
         std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
         std::auto_ptr<Socket> client( factory->createSocket() );
 
-        client->connect("127.0.0.1", port);
+        client->connect("127.0.0.1", serverThread.getLocalPort() );
         client->setSoLinger( false, 0 );
         client->setTcpNoDelay(true);
 

Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketTest.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketTest.h?rev=939081&r1=939080&r2=939081&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketTest.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketTest.h Wed Apr 28 20:40:49 2010
@@ -31,6 +31,7 @@ namespace net{
         CPPUNIT_TEST( testConstructor );
         CPPUNIT_TEST( testGetReuseAddress );
         CPPUNIT_TEST( testClose );
+        CPPUNIT_TEST( testGetPort );
         CPPUNIT_TEST( testGetInputStream );
         CPPUNIT_TEST( testGetKeepAlive );
         CPPUNIT_TEST( testGetLocalPort );
@@ -50,16 +51,13 @@ namespace net{
 
     public:
 
-        static const int port = 23232;
-
-    public:
-
         virtual ~SocketTest() {}
 
         void testConnectUnknownHost();
         void testConstructor();
         void testGetReuseAddress();
         void testClose();
+        void testGetPort();
         void testGetInputStream();
         void testGetKeepAlive();
         void testGetLocalPort();