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/03/26 00:24:39 UTC
svn commit: r927638 - in /activemq/activemq-cpp/trunk/activemq-cpp/src:
main/ main/activemq/transport/tcp/ main/decaf/internal/net/ssl/
main/decaf/internal/net/tcp/ main/decaf/net/ main/decaf/net/ssl/
test/decaf/net/
Author: tabish
Date: Thu Mar 25 23:24:38 2010
New Revision: 927638
URL: http://svn.apache.org/viewvc?rev=927638&view=rev
Log:
Some initial refactoring to make adding in SSL Sockets and a Transport simpler.
Added:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/ssl/
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.cpp (with props)
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.h (with props)
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.cpp
- copied, changed from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.h
- copied, changed from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketInputStream.cpp
- copied, changed from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketInputStream.h
- copied, changed from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketOutputStream.cpp
- copied, changed from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketOutputStream.h
- copied, changed from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ssl/
Removed:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.h
Modified:
activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am
activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.h
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketFactory.cpp
activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketFactory.h
activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketFactoryTest.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/Makefile.am
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am?rev=927638&r1=927637&r2=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/Makefile.am Thu Mar 25 23:24:38 2010
@@ -484,6 +484,10 @@ cc_sources = \
decaf/internal/io/StandardOutputStream.cpp \
decaf/internal/net/URIEncoderDecoder.cpp \
decaf/internal/net/URIHelper.cpp \
+ decaf/internal/net/tcp/DefaultSocketFactory.cpp \
+ decaf/internal/net/tcp/TcpSocket.cpp \
+ decaf/internal/net/tcp/TcpSocketInputStream.cpp \
+ decaf/internal/net/tcp/TcpSocketOutputStream.cpp \
decaf/internal/nio/BufferFactory.cpp \
decaf/internal/nio/ByteArrayBuffer.cpp \
decaf/internal/nio/CharArrayBuffer.cpp \
@@ -545,9 +549,6 @@ cc_sources = \
decaf/net/ServerSocket.cpp \
decaf/net/SocketError.cpp \
decaf/net/SocketFactory.cpp \
- decaf/net/SocketInputStream.cpp \
- decaf/net/SocketOutputStream.cpp \
- decaf/net/TcpSocket.cpp \
decaf/net/URI.cpp \
decaf/net/URL.cpp \
decaf/net/URLDecoder.cpp \
@@ -1136,6 +1137,10 @@ h_sources = \
decaf/internal/net/URIEncoderDecoder.h \
decaf/internal/net/URIHelper.h \
decaf/internal/net/URIType.h \
+ decaf/internal/net/tcp/DefaultSocketFactory.h \
+ decaf/internal/net/tcp/TcpSocket.h \
+ decaf/internal/net/tcp/TcpSocketInputStream.h \
+ decaf/internal/net/tcp/TcpSocketOutputStream.h \
decaf/internal/nio/BufferFactory.h \
decaf/internal/nio/ByteArrayBuffer.h \
decaf/internal/nio/CharArrayBuffer.h \
@@ -1244,10 +1249,7 @@ h_sources = \
decaf/net/SocketError.h \
decaf/net/SocketException.h \
decaf/net/SocketFactory.h \
- decaf/net/SocketInputStream.h \
- decaf/net/SocketOutputStream.h \
decaf/net/SocketTimeoutException.h \
- decaf/net/TcpSocket.h \
decaf/net/URI.h \
decaf/net/URISyntaxException.h \
decaf/net/URL.h \
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.cpp?rev=927638&r1=927637&r2=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.cpp Thu Mar 25 23:24:38 2010
@@ -22,6 +22,11 @@
#include <activemq/transport/IOTransport.h>
#include <activemq/transport/TransportFactory.h>
+#include <decaf/lang/Integer.h>
+#include <decaf/lang/Boolean.h>
+
+#include <memory>
+
using namespace std;
using namespace activemq;
using namespace activemq::io;
@@ -30,6 +35,7 @@ using namespace activemq::transport::tcp
using namespace activemq::exceptions;
using namespace decaf;
using namespace decaf::net;
+using namespace decaf::util;
using namespace decaf::io;
using namespace decaf::lang;
@@ -94,10 +100,11 @@ void TcpTransport::initialize( const dec
try {
- // Create the IO device we will be communicating over the
- // wire with. This may need to change if we add more types
- // of sockets, such as SSL.
- socket.reset( SocketFactory::createSocket( uri.getAuthority(), properties ) );
+ std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
+
+ socket.reset( factory->createSocket() );
+
+ this->configureSocket( *socket, uri, properties );
// Cast it to an IO transport so we can wire up the socket
// input and output streams.
@@ -109,6 +116,14 @@ void TcpTransport::initialize( const dec
"transport must be of type IOTransport");
}
+ // Get the read buffer size.
+ int inputBufferSize = Integer::parseInt(
+ properties.getProperty( "inputBufferSize", "8192" ) );
+
+ // Get the write buffer size.
+ int outputBufferSize = Integer::parseInt(
+ properties.getProperty( "outputBufferSize", "8192" ) );
+
InputStream* inputStream = socket->getInputStream();
OutputStream* outputStream = socket->getOutputStream();
@@ -120,14 +135,14 @@ void TcpTransport::initialize( const dec
outputStream = new LoggingOutputStream( outputStream );
// Now wrap with the Buffered streams, we own the source streams
- inputStream = new BufferedInputStream( inputStream, true );
- outputStream = new BufferedOutputStream( outputStream, true );
+ inputStream = new BufferedInputStream( inputStream, inputBufferSize, true );
+ outputStream = new BufferedOutputStream( outputStream, outputBufferSize, true );
} else {
// Wrap with the Buffered streams, we don't own the source streams
- inputStream = new BufferedInputStream( inputStream );
- outputStream = new BufferedOutputStream( outputStream );
+ inputStream = new BufferedInputStream( inputStream, inputBufferSize );
+ outputStream = new BufferedOutputStream( outputStream, outputBufferSize );
}
// Now wrap the Buffered Streams with DataInput based streams. We own
@@ -144,3 +159,62 @@ void TcpTransport::initialize( const dec
AMQ_CATCH_EXCEPTION_CONVERT( Exception, ActiveMQException )
AMQ_CATCHALL_THROW( ActiveMQException )
}
+
+////////////////////////////////////////////////////////////////////////////////
+void TcpTransport::configureSocket( Socket& socket, const URI& uri, const Properties& properties ) {
+
+ try {
+
+ // Ensure something is actually passed in for the URI
+ if( uri.getAuthority() == "" ) {
+ throw SocketException( __FILE__, __LINE__,
+ "SocketTransport::start() - uri not provided" );
+ }
+
+ string host = uri.getHost();
+ int port = uri.getPort();
+
+ // Get the linger flag.
+ int soLinger = Integer::parseInt(
+ properties.getProperty( "soLinger", "0" ) );
+
+ // Get the keepAlive flag.
+ bool soKeepAlive = Boolean::parseBoolean(
+ properties.getProperty( "soKeepAlive", "false" ) );
+
+ // Get the socket receive buffer size.
+ int soReceiveBufferSize = Integer::parseInt(
+ properties.getProperty( "soReceiveBufferSize", "-1" ) );
+
+ // Get the socket send buffer size.
+ int soSendBufferSize = Integer::parseInt(
+ properties.getProperty( "soSendBufferSize", "-1" ) );
+
+ // Get the socket TCP_NODELAY flag.
+ bool tcpNoDelay = Boolean::parseBoolean(
+ properties.getProperty( "tcpNoDelay", "true" ) );
+
+ // Get the socket connect timeout in microseconds.
+ socket.setConnectTimeout(
+ Integer::parseInt( properties.getProperty( "soConnectTimeout", "-1" ) ) );
+
+ // Connect the socket.
+ socket.connect( host.c_str(), port );
+
+ // Set the socket options.
+ socket.setSoLinger( soLinger );
+ socket.setKeepAlive( soKeepAlive );
+ socket.setTcpNoDelay( tcpNoDelay );
+
+ if( soReceiveBufferSize > 0 ){
+ socket.setReceiveBufferSize( soReceiveBufferSize );
+ }
+
+ if( soSendBufferSize > 0 ){
+ socket.setSendBufferSize( soSendBufferSize );
+ }
+ }
+ DECAF_CATCH_RETHROW( SocketException )
+ DECAF_CATCH_EXCEPTION_CONVERT( Exception, SocketException )
+ DECAF_CATCHALL_THROW( SocketException )
+}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.h?rev=927638&r1=927637&r2=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/activemq/transport/tcp/TcpTransport.h Thu Mar 25 23:24:38 2010
@@ -137,6 +137,10 @@ namespace tcp{
void initialize( const decaf::net::URI& uri,
const decaf::util::Properties& properties );
+ void configureSocket( decaf::net::Socket& socket,
+ const decaf::net::URI& uri,
+ const decaf::util::Properties& properties );
+
};
}}}
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.cpp?rev=927638&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.cpp (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.cpp Thu Mar 25 23:24:38 2010
@@ -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 "DefaultSocketFactory.h"
+
+#include <decaf/net/Socket.h>
+#include <decaf/net/SocketException.h>
+
+#include <decaf/internal/net/tcp/TcpSocket.h>
+
+#include <memory>
+
+using namespace decaf;
+using namespace decaf::io;
+using namespace decaf::net;
+using namespace decaf::lang;
+using namespace decaf::internal;
+using namespace decaf::internal::net;
+using namespace decaf::internal::net::tcp;
+
+////////////////////////////////////////////////////////////////////////////////
+DefaultSocketFactory::DefaultSocketFactory() : SocketFactory() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+DefaultSocketFactory::~DefaultSocketFactory() {
+}
+
+////////////////////////////////////////////////////////////////////////////////
+Socket* DefaultSocketFactory::createSocket()
+ throw( decaf::io::IOException ) {
+
+ return new TcpSocket();
+}
+
+////////////////////////////////////////////////////////////////////////////////
+Socket* DefaultSocketFactory::createSocket( const std::string& hostname, int port )
+ throw( decaf::io::IOException, decaf::net::UnknownHostException ) {
+
+ try {
+
+ // Ensure something is actually passed in for the URI
+ if( hostname == "" ) {
+ throw SocketException( __FILE__, __LINE__, "uri not provided" );
+ }
+
+ if( port <= 0 ) {
+ throw SocketException( __FILE__, __LINE__, "valid port not provided" );
+ }
+
+ std::auto_ptr<TcpSocket> tcpSocket( new TcpSocket() );
+
+ // Connect the socket.
+ tcpSocket->connect( hostname.c_str(), port );
+
+ return tcpSocket.release();
+ }
+ DECAF_CATCH_RETHROW( IOException )
+ DECAF_CATCH_EXCEPTION_CONVERT( Exception, IOException )
+ DECAF_CATCHALL_THROW( IOException )
+}
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.cpp
------------------------------------------------------------------------------
svn:eol-style = native
Added: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.h?rev=927638&view=auto
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.h (added)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.h Thu Mar 25 23:24:38 2010
@@ -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.
+ */
+
+#ifndef DEFAULTSOCKETFACTORY_H_
+#define DEFAULTSOCKETFACTORY_H_
+
+#include <decaf/util/Config.h>
+
+#include <decaf/net/SocketFactory.h>
+
+namespace decaf {
+namespace internal {
+namespace net {
+namespace tcp {
+
+ /**
+ * SocketFactory implementation that is used to create TCP style Sockets.
+ *
+ * @since 1.0
+ */
+ class DECAF_API DefaultSocketFactory : public decaf::net::SocketFactory {
+ public:
+
+ DefaultSocketFactory();
+
+ virtual ~DefaultSocketFactory();
+
+ /**
+ * {@inheritDoc}
+ */
+ virtual decaf::net::Socket* createSocket()
+ throw( decaf::io::IOException );
+
+ /**
+ * {@inheritDoc}
+ */
+ virtual decaf::net::Socket* createSocket( const std::string& name, int port )
+ throw( decaf::io::IOException, decaf::net::UnknownHostException );
+
+ };
+
+}}}}
+
+#endif /* DEFAULTSOCKETFACTORY_H_ */
Propchange: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/DefaultSocketFactory.h
------------------------------------------------------------------------------
svn:eol-style = native
Copied: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.cpp (from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.cpp)
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.cpp?p2=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.cpp&p1=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.cpp&r1=926290&r2=927638&rev=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.cpp Thu Mar 25 23:24:38 2010
@@ -14,15 +14,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#include <decaf/util/Config.h>
#include "TcpSocket.h"
-#include "SocketInputStream.h"
-#include "SocketOutputStream.h"
-#include "SocketError.h"
+
+#include <decaf/internal/net/tcp/TcpSocketInputStream.h>
+#include <decaf/internal/net/tcp/TcpSocketOutputStream.h>
+
+#include <decaf/net/SocketError.h>
using namespace decaf;
using namespace decaf::internal;
+using namespace decaf::internal::net;
+using namespace decaf::internal::net::tcp;
using namespace decaf::net;
using namespace decaf::io;
using namespace decaf::lang;
@@ -31,22 +34,16 @@ using namespace decaf::lang;
TcpSocket::TcpSocket() throw ( SocketException )
: socketHandle( NULL ),
inputStream( NULL ),
- outputStream( NULL ) {
+ outputStream( NULL ),
+ connectTimeout( -1 ) {
}
////////////////////////////////////////////////////////////////////////////////
TcpSocket::TcpSocket( SocketHandle socketHandle )
- : socketHandle( NULL ),
- inputStream( NULL ),
- outputStream( NULL ) {
-
- try {
- this->socketHandle = socketHandle;
- this->inputStream = new SocketInputStream( socketHandle );
- this->outputStream = new SocketOutputStream( socketHandle );
- }
- DECAF_CATCH_RETHROW( SocketException )
- DECAF_CATCHALL_THROW( SocketException )
+ : socketHandle( socketHandle ),
+ inputStream( new TcpSocketInputStream( socketHandle ) ),
+ outputStream( new TcpSocketOutputStream( socketHandle ) ),
+ connectTimeout( -1 ) {
}
////////////////////////////////////////////////////////////////////////////////
@@ -54,7 +51,7 @@ TcpSocket::~TcpSocket() {
try{
- // No shutdown, just close - dont want blocking destructor.
+ // No shutdown, just close - don't want a blocking destructor.
close();
// Destroy the input stream.
@@ -84,7 +81,7 @@ OutputStream* TcpSocket::getOutputStream
}
////////////////////////////////////////////////////////////////////////////////
-void TcpSocket::connect(const char* host, int port, int timeout) throw ( SocketException ) {
+void TcpSocket::connect( const char* host, int port ) throw ( SocketException ) {
try{
@@ -114,8 +111,8 @@ void TcpSocket::connect(const char* host
// If we have a connection timeout specified, temporarily set the socket to
// non-blocking so that we can timeout the connect operation. We'll restore
// to blocking mode right after we connect.
- apr_socket_opt_set( socketHandle, APR_SO_NONBLOCK, (timeout>0)?1:0 );
- apr_socket_timeout_set( socketHandle, timeout );
+ apr_socket_opt_set( socketHandle, APR_SO_NONBLOCK, (this->connectTimeout > 0 ) ? 1 : 0 );
+ apr_socket_timeout_set( socketHandle, connectTimeout );
// try to Connect to the provided address.
checkResult(apr_socket_connect( socketHandle, socketAddress ));
@@ -125,8 +122,8 @@ void TcpSocket::connect(const char* host
apr_socket_timeout_set( socketHandle, -1 );
// Create an input/output stream for this socket.
- inputStream = new SocketInputStream( socketHandle );
- outputStream = new SocketOutputStream( socketHandle );
+ inputStream = new TcpSocketInputStream( socketHandle );
+ outputStream = new TcpSocketOutputStream( socketHandle );
} catch( SocketException& ex ) {
ex.setMark( __FILE__, __LINE__);
@@ -327,6 +324,16 @@ void TcpSocket::setTcpNoDelay( bool valu
}
////////////////////////////////////////////////////////////////////////////////
+void TcpSocket::setConnectTimeout( int timeout ) throw( decaf::net::SocketException ) {
+ this->connectTimeout = timeout;
+}
+
+////////////////////////////////////////////////////////////////////////////////
+int TcpSocket::getConnectTimeout() const throw( decaf::net::SocketException ) {
+ return this->connectTimeout;
+}
+
+////////////////////////////////////////////////////////////////////////////////
void TcpSocket::checkResult( apr_status_t value ) const throw ( SocketException ) {
if( value != APR_SUCCESS ){
Copied: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.h (from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.h)
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.h?p2=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.h&p1=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.h&r1=926290&r2=927638&rev=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/TcpSocket.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocket.h Thu Mar 25 23:24:38 2010
@@ -14,8 +14,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#ifndef _DECAF_NET_TCPSOCKET_H_
-#define _DECAF_NET_TCPSOCKET_H_
+#ifndef _DECAF_INTERNAL_NET_TCP_TCPSOCKET_H_
+#define _DECAF_INTERNAL_NET_TCP_TCPSOCKET_H_
#include <decaf/net/SocketException.h>
#include <decaf/net/Socket.h>
@@ -24,17 +24,19 @@
#include <decaf/util/Config.h>
#include <decaf/internal/AprPool.h>
-namespace decaf{
-namespace net{
+namespace decaf {
+namespace internal {
+namespace net {
+namespace tcp {
// Forward declarations
- class SocketInputStream;
- class SocketOutputStream;
+ class TcpSocketInputStream;
+ class TcpSocketOutputStream;
/**
* Platform-independent implementation of the socket interface.
*/
- class DECAF_API TcpSocket : public Socket {
+ class DECAF_API TcpSocket : public decaf::net::Socket {
private:
/**
@@ -45,22 +47,27 @@ namespace net{
/**
* The handle for this socket.
*/
- SocketHandle socketHandle;
+ decaf::net::Socket::SocketHandle socketHandle;
/**
* The Address info for this Socket
*/
- SocketAddress socketAddress;
+ decaf::net::Socket::SocketAddress socketAddress;
/**
* The input stream for reading this socket.
*/
- SocketInputStream* inputStream;
+ TcpSocketInputStream* inputStream;
/**
* The output stream for writing to this socket.
*/
- SocketOutputStream* outputStream;
+ TcpSocketOutputStream* outputStream;
+
+ /**
+ * Configured Connect Timeout, -1 means no timeout.
+ */
+ int connectTimeout;
public:
@@ -69,14 +76,14 @@ namespace net{
* @throws SocketException thrown one windows if the static initialization
* call to WSAStartup was not successful.
*/
- TcpSocket() throw ( SocketException );
+ TcpSocket() throw ( decaf::net::SocketException );
/**
* Construct a connected or bound socket based on given
* socket handle.
* @param socketHandle a socket handle to wrap in the object
*/
- TcpSocket( SocketHandle socketHandle );
+ TcpSocket( decaf::net::Socket::SocketHandle socketHandle );
/**
* Releases the socket handle but not gracefully shut down the connection.
@@ -87,7 +94,7 @@ namespace net{
* Gets the handle for the socket.
* @return SocketHabler for this Socket, can be NULL
*/
- SocketHandle getSocketHandle () {
+ decaf::net::Socket::SocketHandle getSocketHandle () {
return socketHandle;
}
@@ -96,21 +103,10 @@ namespace net{
* connected to another destination.
* @param host The host of the server to connect to.
* @param port The port of the server to connect to.
- * @param timeout of socket in microseconds
- * @throws SocketException Thrown if a failure occurred in the connect.
- */
- void connect( const char* host, int port, int timeout ) throw( SocketException );
-
- /**
- * Connects to the specified destination. Closes this socket if
- * connected to another destination.
- * @param host The host of the server to connect to.
- * @param port The port of the server to connect to.
* @throws SocketException Thrown if a failure occurred in the connect.
*/
- virtual void connect( const char* host, int port ) throw( SocketException ) {
- connect(host,port,-1);
- }
+ virtual void connect( const char* host, int port )
+ throw( decaf::net::SocketException );
/**
* Indicates whether or not this socket is connected to a destination.
@@ -137,84 +133,84 @@ namespace net{
* @return The linger time in seconds.
* @throws SocketException if the operation fails.
*/
- virtual int getSoLinger() const throw( SocketException );
+ virtual int getSoLinger() const throw( decaf::net::SocketException );
/**
* Sets the linger time.
* @param linger The linger time in seconds. If 0, linger is off.
* @throws SocketException if the operation fails.
*/
- virtual void setSoLinger( int linger ) throw( SocketException );
+ virtual void setSoLinger( int linger ) throw( decaf::net::SocketException );
/**
* Gets the keep alive flag.
* @return True if keep alive is enabled.
* @throws SocketException if the operation fails.
*/
- virtual bool getKeepAlive() const throw( SocketException );
+ virtual bool getKeepAlive() const throw( decaf::net::SocketException );
/**
* Enables/disables the keep alive flag.
* @param keepAlive If true, enables the flag.
* @throws SocketException if the operation fails.
*/
- virtual void setKeepAlive( bool keepAlive ) throw( SocketException );
+ virtual void setKeepAlive( bool keepAlive ) throw( decaf::net::SocketException );
/**
* Gets the receive buffer size.
* @return the receive buffer size in bytes.
* @throws SocketException if the operation fails.
*/
- virtual int getReceiveBufferSize() const throw( SocketException );
+ virtual int getReceiveBufferSize() const throw( decaf::net::SocketException );
/**
* Sets the recieve buffer size.
* @param size Number of bytes to set the receive buffer to.
* @throws SocketException if the operation fails.
*/
- virtual void setReceiveBufferSize( int size ) throw( SocketException );
+ virtual void setReceiveBufferSize( int size ) throw( decaf::net::SocketException );
/**
* Gets the reuse address flag.
* @return True if the address can be reused.
* @throws SocketException if the operation fails.
*/
- virtual bool getReuseAddress() const throw( SocketException );
+ virtual bool getReuseAddress() const throw( decaf::net::SocketException );
/**
* Sets the reuse address flag.
* @param reuse If true, sets the flag.
* @throws SocketException if the operation fails.
*/
- virtual void setReuseAddress( bool reuse ) throw( SocketException );
+ virtual void setReuseAddress( bool reuse ) throw( decaf::net::SocketException );
/**
* Gets the send buffer size.
* @return the size in bytes of the send buffer.
* @throws SocketException if the operation fails.
*/
- virtual int getSendBufferSize() const throw( SocketException );
+ virtual int getSendBufferSize() const throw( decaf::net::SocketException );
/**
* Sets the send buffer size.
* @param size The number of bytes to set the send buffer to.
* @throws SocketException if the operation fails.
*/
- virtual void setSendBufferSize( int size ) throw( SocketException );
+ virtual void setSendBufferSize( int size ) throw( decaf::net::SocketException );
/**
* Gets the timeout for socket operations.
* @return The timeout in milliseconds for socket operations.
* @throws SocketException Thrown if unable to retrieve the information.
*/
- virtual int getSoTimeout() const throw( SocketException );
+ virtual int getSoTimeout() const throw( decaf::net::SocketException );
/**
* Sets the timeout for socket operations.
* @param timeout The timeout in milliseconds for socket operations.<p>
* @throws SocketException Thrown if unable to set the information.
*/
- virtual void setSoTimeout( int timeout ) throw(SocketException);
+ virtual void setSoTimeout( int timeout ) throw( decaf::net::SocketException );
/**
* Closes this object and deallocates the appropriate resources.
@@ -222,28 +218,49 @@ namespace net{
*/
virtual void close() throw( decaf::io::IOException );
- public:
-
/**
* Gets the Status of the TCP_NODELAY param for this socket as a Bool
* @returns true if TCP_NODELAY is enabled
* @throws Exception
*/
- virtual bool getTcpNoDelay() const throw ( lang::Exception );
+ virtual bool getTcpNoDelay() const throw ( decaf::lang::Exception );
/**
* Sets the Status of the TCP_NODELAY param for this socket as a Bool
* @param value - true if TCP_NODELAY is to be enabled
* @throws Exception
*/
- virtual void setTcpNoDelay( bool value ) throw ( lang::Exception );
+ virtual void setTcpNoDelay( bool value ) throw ( decaf::lang::Exception );
+
+ /**
+ * Gets the set Connect timeout, Socket implementations that support this
+ * setting will throw an SocketException if they cannot connect within the
+ * specified timeout.
+ *
+ * @returns The time in Milliseconds to wait for a connection to be made.
+ *
+ * @throws SocketException if the operation fails.
+ */
+ virtual int getConnectTimeout() const throw( decaf::net::SocketException );
+
+ /**
+ * Sets the set Connect timeout, Socket implementations that support this
+ * setting will throw an SocketException if they cannot connect within the
+ * specified timeout.
+ *
+ * @param timeout
+ * The time in Milliseconds to wait for a connection to be made.
+ *
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setConnectTimeout( int timeout ) throw( decaf::net::SocketException );
protected:
- void checkResult( apr_status_t value ) const throw (SocketException);
+ void checkResult( apr_status_t value ) const throw( decaf::net::SocketException );
};
-}}
+}}}}
-#endif /*_DECAF_NET_TCPSOCKET_H_*/
+#endif /*_DECAF_INTERNAL_NET_TCP_TCPSOCKET_H_*/
Copied: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketInputStream.cpp (from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.cpp)
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketInputStream.cpp?p2=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketInputStream.cpp&p1=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.cpp&r1=926290&r2=927638&rev=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketInputStream.cpp Thu Mar 25 23:24:38 2010
@@ -35,8 +35,9 @@
#include <sys/filio.h>
#endif
-#include <decaf/net/SocketInputStream.h>
+#include <decaf/internal/net/tcp/TcpSocketInputStream.h>
#include <decaf/net/SocketError.h>
+#include <decaf/net/Socket.h>
#include <decaf/io/IOException.h>
#include <decaf/lang/Character.h>
#include <decaf/lang/exceptions/UnsupportedOperationException.h>
@@ -51,32 +52,35 @@ using namespace decaf;
using namespace decaf::net;
using namespace decaf::io;
using namespace decaf::util;
+using namespace decaf::internal;
+using namespace decaf::internal::net;
+using namespace decaf::internal::net::tcp;
using namespace decaf::lang;
using namespace decaf::lang::exceptions;
using namespace std;
////////////////////////////////////////////////////////////////////////////////
-SocketInputStream::SocketInputStream( net::Socket::SocketHandle socket ) : InputStream() {
+TcpSocketInputStream::TcpSocketInputStream( decaf::net::Socket::SocketHandle socket ) : InputStream() {
this->socket = socket;
this->closed = false;
}
////////////////////////////////////////////////////////////////////////////////
-SocketInputStream::~SocketInputStream(){}
+TcpSocketInputStream::~TcpSocketInputStream(){}
////////////////////////////////////////////////////////////////////////////////
-void SocketInputStream::close() throw( decaf::io::IOException ){
+void TcpSocketInputStream::close() throw( decaf::io::IOException ){
this->closed = true;
}
////////////////////////////////////////////////////////////////////////////////
-int SocketInputStream::available() const throw ( io::IOException ){
+int TcpSocketInputStream::available() const throw ( io::IOException ){
// Check for a closed call from socket class, if closed then this read fails.
if( closed ){
throw IOException(
__FILE__, __LINE__,
- "decaf::io::SocketInputStream::available - The stream is closed" );
+ "decaf::io::TcpSocketInputStream::available - The stream is closed" );
}
// Convert to an OS level socket.
@@ -136,13 +140,13 @@ int SocketInputStream::available() const
}
////////////////////////////////////////////////////////////////////////////////
-int SocketInputStream::doReadByte() throw ( IOException ){
+int TcpSocketInputStream::doReadByte() throw ( IOException ){
// Check for a closed call from socket class, if closed then this read fails.
if( closed ){
throw IOException(
__FILE__, __LINE__,
- "decaf::io::SocketInputStream::read - The Stream has been closed" );
+ "decaf::io::TcpSocketInputStream::read - The Stream has been closed" );
}
apr_status_t result = APR_SUCCESS;
@@ -153,14 +157,14 @@ int SocketInputStream::doReadByte() thro
if( ( size != sizeof(c) && !closed ) || result != APR_SUCCESS ){
throw IOException( __FILE__, __LINE__,
- "activemq::io::SocketInputStream::read - failed reading a byte");
+ "activemq::io::TcpSocketInputStream::read - failed reading a byte");
}
return c;
}
////////////////////////////////////////////////////////////////////////////////
-int SocketInputStream::doReadArrayBounded( unsigned char* buffer, int size, int offset, int length )
+int TcpSocketInputStream::doReadArrayBounded( unsigned char* buffer, int size, int offset, int length )
throw ( decaf::io::IOException,
decaf::lang::exceptions::IndexOutOfBoundsException,
decaf::lang::exceptions::NullPointerException ) {
@@ -169,7 +173,7 @@ int SocketInputStream::doReadArrayBounde
if( closed ){
throw IOException(
__FILE__, __LINE__,
- "decaf::io::SocketInputStream::read - The Stream has been closed" );
+ "decaf::io::TcpSocketInputStream::read - The Stream has been closed" );
}
if( length == 0 ) {
@@ -179,7 +183,7 @@ int SocketInputStream::doReadArrayBounde
if( buffer == NULL ) {
throw NullPointerException(
__FILE__, __LINE__,
- "SocketInputStream::read - Buffer passed is Null" );
+ "TcpSocketInputStream::read - Buffer passed is Null" );
}
if( size < 0 ) {
@@ -215,14 +219,14 @@ int SocketInputStream::doReadArrayBounde
if( closed ){
throw IOException(
__FILE__, __LINE__,
- "decaf::io::SocketInputStream::read - The connection is broken" );
+ "decaf::io::TcpSocketInputStream::read - The connection is broken" );
}
// Check for error.
if( result != APR_SUCCESS ){
throw IOException(
__FILE__, __LINE__,
- "decaf::net::SocketInputStream::read - %s",
+ "decaf::net::TcpSocketInputStream::read - %s",
SocketError::getErrorString().c_str() );
}
@@ -230,10 +234,10 @@ int SocketInputStream::doReadArrayBounde
}
////////////////////////////////////////////////////////////////////////////////
-long long SocketInputStream::skip( long long num DECAF_UNUSED )
+long long TcpSocketInputStream::skip( long long num DECAF_UNUSED )
throw ( io::IOException, lang::exceptions::UnsupportedOperationException ) {
throw lang::exceptions::UnsupportedOperationException(
__FILE__, __LINE__,
- "SocketInputStream::skip() method is not supported");
+ "TcpSocketInputStream::skip() method is not supported");
}
Copied: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketInputStream.h (from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.h)
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketInputStream.h?p2=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketInputStream.h&p1=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.h&r1=926290&r2=927638&rev=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketInputStream.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketInputStream.h Thu Mar 25 23:24:38 2010
@@ -15,16 +15,18 @@
* limitations under the License.
*/
-#ifndef _DECAF_NET_SOCKETINPUTSTREAM_H_
-#define _DECAF_NET_SOCKETINPUTSTREAM_H_
+#ifndef _DECAF_INTERNAL_NET_TCP_TCPSOCKETINPUTSTREAM_H_
+#define _DECAF_INTERNAL_NET_TCP_TCPSOCKETINPUTSTREAM_H_
#include <decaf/io/InputStream.h>
#include <decaf/net/Socket.h>
#include <decaf/lang/Exception.h>
#include <decaf/lang/exceptions/NullPointerException.h>
-namespace decaf{
-namespace net{
+namespace decaf {
+namespace internal {
+namespace net {
+namespace tcp {
/**
* Input stream for performing reads on a socket. This class will only
@@ -32,10 +34,10 @@ namespace net{
*
* @since 1.0
*/
- class DECAF_API SocketInputStream : public io::InputStream {
+ class DECAF_API TcpSocketInputStream : public decaf::io::InputStream {
private:
- Socket::SocketHandle socket;
+ decaf::net::Socket::SocketHandle socket;
bool closed;
public:
@@ -44,14 +46,14 @@ namespace net{
* Constructor.
* @param socket the socket handle.
*/
- SocketInputStream( Socket::SocketHandle socket );
+ TcpSocketInputStream( decaf::net::Socket::SocketHandle socket );
- virtual ~SocketInputStream();
+ virtual ~TcpSocketInputStream();
/**
* {@inheritDoc}
*/
- virtual int available() const throw ( io::IOException );
+ virtual int available() const throw ( decaf::io::IOException );
/**
* Close - does nothing. It is the responsibility of the owner
@@ -67,8 +69,8 @@ namespace net{
* {@inheritDoc}
*/
virtual long long skip( long long num )
- throw ( io::IOException,
- lang::exceptions::UnsupportedOperationException );
+ throw ( decaf::io::IOException,
+ decaf::lang::exceptions::UnsupportedOperationException );
protected:
@@ -81,6 +83,6 @@ namespace net{
};
-}}
+}}}}
-#endif /*_DECAF_NET_SOCKETINPUTSTREAM_H_*/
+#endif /*_DECAF_INTERNAL_NET_TCP_TCPSOCKETINPUTSTREAM_H_*/
Copied: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketOutputStream.cpp (from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.cpp)
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketOutputStream.cpp?p2=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketOutputStream.cpp&p1=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.cpp&r1=926290&r2=927638&rev=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketOutputStream.cpp Thu Mar 25 23:24:38 2010
@@ -15,27 +15,31 @@
* limitations under the License.
*/
-#include "SocketOutputStream.h"
-#include <decaf/util/Config.h>
+#include "TcpSocketOutputStream.h"
+
#include <decaf/lang/Character.h>
-#include "SocketError.h"
+
+#include <decaf/net/SocketError.h>
using namespace decaf;
using namespace decaf::net;
using namespace decaf::io;
using namespace decaf::util;
+using namespace decaf::internal;
+using namespace decaf::internal::net;
+using namespace decaf::internal::net::tcp;
using namespace decaf::lang;
using namespace decaf::lang::exceptions;
using namespace std;
////////////////////////////////////////////////////////////////////////////////
-SocketOutputStream::SocketOutputStream( Socket::SocketHandle socket ) {
+TcpSocketOutputStream::TcpSocketOutputStream( decaf::net::Socket::SocketHandle socket ) {
this->socket = socket;
this->closed = false;
}
////////////////////////////////////////////////////////////////////////////////
-SocketOutputStream::~SocketOutputStream() {
+TcpSocketOutputStream::~TcpSocketOutputStream() {
try{
this->close();
}
@@ -44,12 +48,12 @@ SocketOutputStream::~SocketOutputStream(
}
////////////////////////////////////////////////////////////////////////////////
-void SocketOutputStream::close() throw( decaf::io::IOException ) {
+void TcpSocketOutputStream::close() throw( decaf::io::IOException ) {
this->closed = true;
}
////////////////////////////////////////////////////////////////////////////////
-void SocketOutputStream::doWriteByte( unsigned char c ) throw ( IOException ) {
+void TcpSocketOutputStream::doWriteByte( unsigned char c ) throw ( IOException ) {
try{
@@ -61,7 +65,7 @@ void SocketOutputStream::doWriteByte( un
}
////////////////////////////////////////////////////////////////////////////////
-void SocketOutputStream::doWriteArrayBounded( const unsigned char* buffer, int size, int offset, int length )
+void TcpSocketOutputStream::doWriteArrayBounded( const unsigned char* buffer, int size, int offset, int length )
throw ( decaf::io::IOException,
decaf::lang::exceptions::NullPointerException,
decaf::lang::exceptions::IndexOutOfBoundsException ) {
@@ -75,13 +79,13 @@ void SocketOutputStream::doWriteArrayBou
if( buffer == NULL ) {
throw NullPointerException(
__FILE__, __LINE__,
- "SocketOutputStream::write - passed buffer is null" );
+ "TcpSocketOutputStream::write - passed buffer is null" );
}
if( closed ) {
throw IOException(
__FILE__, __LINE__,
- "SocketOutputStream::write - This Stream has been closed." );
+ "TcpSocketOutputStream::write - This Stream has been closed." );
}
if( size < 0 ) {
@@ -112,7 +116,7 @@ void SocketOutputStream::doWriteArrayBou
if( result != APR_SUCCESS || closed ) {
throw IOException(
__FILE__, __LINE__,
- "decaf::net::SocketOutputStream::write - %s",
+ "decaf::net::TcpSocketOutputStream::write - %s",
SocketError::getErrorString().c_str() );
}
Copied: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketOutputStream.h (from r926290, activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.h)
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketOutputStream.h?p2=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketOutputStream.h&p1=activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.h&r1=926290&r2=927638&rev=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketOutputStream.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/internal/net/tcp/TcpSocketOutputStream.h Thu Mar 25 23:24:38 2010
@@ -15,37 +15,39 @@
* limitations under the License.
*/
-#ifndef _DECAF_NET_SOCKETOUTPUTSTREAM_H_
-#define _DECAF_NET_SOCKETOUTPUTSTREAM_H_
+#ifndef _DECAF_INTERNAL_NET_TCP_TCPSOCKETOUTPUTSTREAM_H_
+#define _DECAF_INTERNAL_NET_TCP_TCPSOCKETOUTPUTSTREAM_H_
#include <decaf/io/OutputStream.h>
#include <decaf/net/Socket.h>
-namespace decaf{
-namespace net{
+namespace decaf {
+namespace internal {
+namespace net {
+namespace tcp {
/**
* Output stream for performing write operations on a socket.
*
* @since 1.0
*/
- class DECAF_API SocketOutputStream : public io::OutputStream {
+ class DECAF_API TcpSocketOutputStream : public decaf::io::OutputStream {
private:
- // The socket.
- Socket::SocketHandle socket;
- bool closed;
+ decaf::net::Socket::SocketHandle socket;
+ volatile bool closed;
public:
/**
* Create a new instance of a Socket OutputStream class.
*
- * @param socket the socket handle.
+ * @param socket
+ * The socket handle to use to write out the data.
*/
- SocketOutputStream( Socket::SocketHandle socket );
+ TcpSocketOutputStream( decaf::net::Socket::SocketHandle socket );
- virtual ~SocketOutputStream();
+ virtual ~TcpSocketOutputStream();
virtual void close() throw( decaf::io::IOException );
@@ -60,6 +62,6 @@ namespace net{
};
-}}
+}}}}
-#endif /*_DECAF_NET_SOCKETOUTPUTSTREAM_H_*/
+#endif /*_DECAF_INTERNAL_NET_TCP_TCPSOCKETOUTPUTSTREAM_H_*/
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.cpp?rev=927638&r1=927637&r2=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.cpp Thu Mar 25 23:24:38 2010
@@ -18,6 +18,8 @@
#include "ServerSocket.h"
#include "SocketError.h"
+#include <decaf/internal/net/tcp/TcpSocket.h>
+
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
@@ -28,6 +30,7 @@
using namespace decaf;
using namespace decaf::net;
+using namespace decaf::internal::net::tcp;
////////////////////////////////////////////////////////////////////////////////
ServerSocket::ServerSocket() {
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.h?rev=927638&r1=927637&r2=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/ServerSocket.h Thu Mar 25 23:24:38 2010
@@ -17,7 +17,6 @@
#ifndef _DECAF_NET_SERVERSOCKETIMPL_H_
#define _DECAF_NET_SERVERSOCKETIMPL_H_
-#include <decaf/net/TcpSocket.h>
#include <decaf/net/SocketException.h>
#include <decaf/util/Config.h>
#include <decaf/internal/AprPool.h>
@@ -27,11 +26,12 @@
namespace decaf{
namespace net{
+ class Socket;
+
/**
* A server socket class (for testing purposes).
*/
- class DECAF_API ServerSocket
- {
+ class DECAF_API ServerSocket {
public:
typedef apr_socket_t* SocketHandle;
@@ -61,20 +61,31 @@ namespace net{
/**
* Bind and listen to given IP/dns and port.
- * @param host IP address or host name.
- * @param port TCP port between 1..655535
+ *
+ * @param host
+ * The IP address or host name.
+ * @param port
+ * The TCP port between 1..655535.
+ *
+ * @throws SocketException if an I/O error occurs while binding the socket.
*/
- virtual void bind( const char* host, int port ) throw ( SocketException );
+ virtual void bind( const char* host, int port )
+ throw ( SocketException );
/**
* Bind and listen to given IP/dns and port.
- * @param host IP address or host name.
- * @param port TCP port between 1..655535
- * @param backlog Size of listen backlog.
- */
- virtual void bind( const char* host,
- int port,
- int backlog ) throw ( SocketException );
+ *
+ * @param host
+ * The IP address or host name.
+ * @param port
+ * The TCP port between 1..655535.
+ * @param backlog
+ * The size of listen backlog.
+ *
+ * @throws SocketException if an I/O error occurs while binding the socket.
+ */
+ virtual void bind( const char* host, int port, int backlog )
+ throw ( SocketException );
/**
* Blocks until a client connects to the bound socket.
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.h?rev=927638&r1=927637&r2=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/Socket.h Thu Mar 25 23:24:38 2010
@@ -28,8 +28,7 @@
namespace decaf{
namespace net{
- class DECAF_API Socket : public decaf::io::Closeable
- {
+ class DECAF_API Socket : public decaf::io::Closeable {
public:
/**
@@ -75,6 +74,29 @@ namespace net{
virtual io::OutputStream* getOutputStream() = 0;
/**
+ * Gets the set Connect timeout, Socket implementations that support this
+ * setting will throw an SocketException if they cannot connect within the
+ * specified timeout.
+ *
+ * @returns The time in Milliseconds to wait for a connection to be made.
+ *
+ * @throws SocketException if the operation fails.
+ */
+ virtual int getConnectTimeout() const throw( SocketException ) = 0;
+
+ /**
+ * Sets the set Connect timeout, Socket implementations that support this
+ * setting will throw an SocketException if they cannot connect within the
+ * specified timeout.
+ *
+ * @param timeout
+ * The time in Milliseconds to wait for a connection to be made.
+ *
+ * @throws SocketException if the operation fails.
+ */
+ virtual void setConnectTimeout( int timeout ) throw( SocketException ) = 0;
+
+ /**
* Gets the linger time.
* @return The linger time in seconds.
* @throws SocketException if the operation fails.
@@ -158,6 +180,20 @@ namespace net{
*/
virtual void setSoTimeout( int timeout ) throw( SocketException ) = 0;
+ /**
+ * Gets the Status of the TCP_NODELAY param for this socket as a Bool
+ * @returns true if TCP_NODELAY is enabled
+ * @throws Exception
+ */
+ virtual bool getTcpNoDelay() const throw ( decaf::lang::Exception ) = 0;
+
+ /**
+ * Sets the Status of the TCP_NODELAY param for this socket as a Bool
+ * @param value - true if TCP_NODELAY is to be enabled
+ * @throws Exception
+ */
+ virtual void setTcpNoDelay( bool value ) throw ( decaf::lang::Exception ) = 0;
+
};
}}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketFactory.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketFactory.cpp?rev=927638&r1=927637&r2=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketFactory.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketFactory.cpp Thu Mar 25 23:24:38 2010
@@ -15,122 +15,27 @@
* 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;
+#include <decaf/internal/net/tcp/DefaultSocketFactory.h>
+
using namespace decaf;
-using namespace decaf::util;
+using namespace decaf::io;
using namespace decaf::net;
-using namespace decaf::lang;
-using namespace decaf::lang::exceptions;
+using namespace decaf::internal::net::tcp;
+
+////////////////////////////////////////////////////////////////////////////////
+SocketFactory::SocketFactory() {
+
+}
+
+////////////////////////////////////////////////////////////////////////////////
+Socket* SocketFactory::createSocket() throw( decaf::io::IOException ) {
+
+ throw IOException(
+ __FILE__, __LINE__, "Unconnected Sockets not implemented for this Socket Type." );
+}
////////////////////////////////////////////////////////////////////////////////
-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";
-
- // Get the socket connect timeout in microseconds.
- int connectTimeout = -1;
- dummy = properties.getProperty( "soConnectTimeout", "-1" );
- sscanf( dummy.c_str(), "%d", &connectTimeout );
-
- // 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, connectTimeout );
-
- // 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 )
+SocketFactory* SocketFactory::getDefault() {
+ return new DefaultSocketFactory();
}
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketFactory.h
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketFactory.h?rev=927638&r1=927637&r2=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketFactory.h (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/main/decaf/net/SocketFactory.h Thu Mar 25 23:24:38 2010
@@ -17,52 +17,66 @@
#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>
+#include <decaf/io/IOException.h>
+#include <decaf/net/UnknownHostException.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>
+ * The SocketFactory is used to create Socket objects and can be sub-classed to
+ * provide other types of Sockets or Sockets with varying configurations.
+ *
+ * @see decaf.net.Socket
*
- * @see <code>Socket</code>
+ * @since 1.0
*/
- class DECAF_API SocketFactory
- {
+ class DECAF_API SocketFactory {
+ protected:
+
+ SocketFactory();
+
public:
virtual ~SocketFactory() {}
/**
- * Creates and returns a Socket derived Object based on the values
- * defined in the Properties Object that is passed in.
+ * Creates an unconnected Socket object.
+ *
+ * @return a new Socket object, caller must free this object when done.
+ *
+ * @throws IOException if the Socket cannot be created.
+ */
+ virtual Socket* createSocket() throw( decaf::io::IOException );
+
+ /**
+ * Creates a new Socket object and connects it to the specified remote host and
+ * port using the configuration of this SocketFactory.
+ *
+ * @param host
+ * The host name or IP address to connect the socket to.
+ * @param port
+ * The port on the remote host to connect to.
+ *
+ * @return a new Socket object, caller must free this object when done.
*
- * @param uri
- * the URI for the Socket Connection.
- * @param properties
- * A Properties object that contains configuration details.
+ * @throws IOException if an I/O error occurs while creating the Socket object.
+ * @throws UnknownHostException if the host name is not known.
+ */
+ virtual Socket* createSocket( const std::string& name, int port )
+ throw( decaf::io::IOException, decaf::net::UnknownHostException ) = 0;
+
+ /**
+ * Creates and returns an instance of the environments default SocketFactory
+ * type.
*
* @throws SocketException.
*/
- static Socket* createSocket( const std::string& uri,
- const util::Properties& properties )
- throw ( SocketException );
+ static SocketFactory* getDefault();
};
Modified: activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketFactoryTest.cpp
URL: http://svn.apache.org/viewvc/activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketFactoryTest.cpp?rev=927638&r1=927637&r2=927638&view=diff
==============================================================================
--- activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketFactoryTest.cpp (original)
+++ activemq/activemq-cpp/trunk/activemq-cpp/src/test/decaf/net/SocketFactoryTest.cpp Thu Mar 25 23:24:38 2010
@@ -19,7 +19,8 @@
#include <decaf/util/Properties.h>
#include <decaf/net/SocketFactory.h>
-#include <decaf/net/TcpSocket.h>
+
+#include <memory>
using namespace decaf;
using namespace decaf::net;
@@ -37,16 +38,10 @@ void SocketFactoryTest::test()
Thread::sleep( 500 );
- util::Properties properties;
-
- std::ostringstream ostream;
-
- ostream << "127.0.0.1:" << port;
+ std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
+ std::auto_ptr<Socket> client( factory->createSocket( "127.0.0.1", port ) );
- properties.setProperty("soLinger", "false");
-
- Socket* client = SocketFactory::createSocket(
- ostream.str(), properties );
+ client->setSoLinger( 0 );
synchronized(&serverThread.mutex)
{
@@ -74,12 +69,9 @@ void SocketFactoryTest::test()
serverThread.stop();
serverThread.join();
-
- delete client;
}
catch(lang::Exception ex)
{
- std::cout << "SocketFactoryTest::test - Caught Exception." << std::endl;
ex.printStackTrace();
CPPUNIT_ASSERT( false );
}
@@ -95,21 +87,13 @@ void SocketFactoryTest::testNoDelay()
Thread::sleep( 40 );
- util::Properties properties;
+ std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
+ std::auto_ptr<Socket> client( factory->createSocket( "127.0.0.1", port ) );
- std::ostringstream ostream;
+ client->setSoLinger( 0 );
+ client->setTcpNoDelay( true );
- 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 );
+ CPPUNIT_ASSERT( client->getTcpNoDelay() == true );
synchronized(&serverThread.mutex)
{
@@ -137,8 +121,6 @@ void SocketFactoryTest::testNoDelay()
serverThread.stop();
serverThread.join();
-
- delete client;
}
catch(lang::Exception ex)
{
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=927638&r1=927637&r2=927638&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 Thu Mar 25 23:24:38 2010
@@ -17,7 +17,15 @@
#include "SocketTest.h"
-#include <decaf/net/TcpSocket.h>
+#include <decaf/net/Socket.h>
+#include <decaf/net/SocketFactory.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>
using namespace std;
using namespace decaf;
@@ -26,19 +34,124 @@ using namespace decaf::util;
using namespace decaf::lang;
////////////////////////////////////////////////////////////////////////////////
+namespace {
+
+ class MyServerThread : public lang::Thread{
+ private:
+
+ bool done;
+ int numClients;
+ std::string lastMessage;
+ int port;
+
+ public:
+
+ util::concurrent::Mutex mutex;
+
+ public:
+
+ MyServerThread( int port ) : Thread(), done( false ), numClients( 0 ), lastMessage(), port( port ) {
+ }
+
+ 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{
+
+ if( stream->read( buf, 1000, 0, 1000 ) == -1 ) {
+ done = true;
+ continue;
+ }
+
+ lastMessage = (char*)buf;
+
+ if( strcmp( (char*)buf, "reply" ) == 0 ){
+ io::OutputStream* output = socket->getOutputStream();
+ output->write( (unsigned char*)"hello", (int)strlen("hello"), 0, (int)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 );
+ }
+ }
+
+ };
+}
+
+////////////////////////////////////////////////////////////////////////////////
void SocketTest::testConnect() {
try{
- MyServerThread serverThread;
+ MyServerThread serverThread( port );
serverThread.start();
Thread::sleep( 40 );
- TcpSocket client;
+ std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
+ std::auto_ptr<Socket> client( factory->createSocket() );
- client.connect("127.0.0.1", port);
- client.setSoLinger( false );
+ client->connect( "127.0.0.1", port );
+ client->setSoLinger( false );
synchronized(&serverThread.mutex)
{
@@ -50,7 +163,7 @@ void SocketTest::testConnect() {
CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
- client.close();
+ client->close();
synchronized(&serverThread.mutex)
{
@@ -75,15 +188,16 @@ void SocketTest::testTx() {
try{
- MyServerThread serverThread;
+ MyServerThread serverThread( port );
serverThread.start();
Thread::sleep( 10 );
- TcpSocket client;
+ std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
+ std::auto_ptr<Socket> client( factory->createSocket() );
- client.connect("127.0.0.1", port);
- client.setSoLinger( false );
+ client->connect("127.0.0.1", port);
+ client->setSoLinger( false );
synchronized(&serverThread.mutex)
{
@@ -95,7 +209,7 @@ void SocketTest::testTx() {
CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
- io::OutputStream* stream = client.getOutputStream();
+ io::OutputStream* stream = client->getOutputStream();
std::string msg = "don't reply";
stream->write( (unsigned char*)msg.c_str(), (int)msg.length(), 0, (int)msg.length() );
@@ -104,7 +218,7 @@ void SocketTest::testTx() {
CPPUNIT_ASSERT( serverThread.getLastMessage() == msg );
- client.close();
+ client->close();
synchronized(&serverThread.mutex)
{
@@ -129,15 +243,16 @@ void SocketTest::testTrx() {
try{
- MyServerThread serverThread;
+ MyServerThread serverThread( port );
serverThread.start();
Thread::sleep( 10 );
- TcpSocket client;
+ std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
+ std::auto_ptr<Socket> client( factory->createSocket() );
- client.connect("127.0.0.1", port);
- client.setSoLinger(false);
+ client->connect("127.0.0.1", port);
+ client->setSoLinger(false);
synchronized(&serverThread.mutex)
{
@@ -149,7 +264,7 @@ void SocketTest::testTrx() {
CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
- io::OutputStream* stream = client.getOutputStream();
+ io::OutputStream* stream = client->getOutputStream();
std::string msg = "reply";
stream->write( (unsigned char*)msg.c_str(), (int)msg.length(), 0, (int)msg.length() );
@@ -161,13 +276,13 @@ void SocketTest::testTrx() {
unsigned char buf[500];
memset( buf, 0, 500 );
- io::InputStream* istream = client.getInputStream();
+ io::InputStream* istream = client->getInputStream();
CPPUNIT_ASSERT( istream->available() != 0 );
std::size_t numRead = istream->read( buf, 500, 0, 500 );
CPPUNIT_ASSERT( numRead == 5 );
CPPUNIT_ASSERT( strcmp( (char*)buf, "hello" ) == 0 );
- client.close();
+ client->close();
serverThread.stop();
serverThread.join();
@@ -182,15 +297,16 @@ void SocketTest::testRxFail() {
try{
- MyServerThread serverThread;
+ MyServerThread serverThread( port );
serverThread.start();
Thread::sleep( 10 );
- TcpSocket client;
+ std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
+ std::auto_ptr<Socket> client( factory->createSocket() );
- client.connect("127.0.0.1", port);
- client.setSoLinger( false );
+ client->connect("127.0.0.1", port);
+ client->setSoLinger( false );
synchronized(&serverThread.mutex)
{
@@ -205,7 +321,7 @@ void SocketTest::testRxFail() {
// Give it a chance to get to its read call
Thread::sleep( 100 );
- client.close();
+ client->close();
synchronized(&serverThread.mutex)
{
@@ -230,18 +346,19 @@ void SocketTest::testTrxNoDelay() {
try{
- MyServerThread serverThread;
+ MyServerThread serverThread( port );
serverThread.start();
Thread::sleep( 10 );
- TcpSocket client;
+ std::auto_ptr<SocketFactory> factory( SocketFactory::getDefault() );
+ std::auto_ptr<Socket> client( factory->createSocket() );
- client.connect("127.0.0.1", port);
- client.setSoLinger(false);
- client.setTcpNoDelay(true);
+ client->connect("127.0.0.1", port);
+ client->setSoLinger(false);
+ client->setTcpNoDelay(true);
- CPPUNIT_ASSERT( client.getTcpNoDelay() == true );
+ CPPUNIT_ASSERT( client->getTcpNoDelay() == true );
synchronized(&serverThread.mutex)
{
@@ -253,7 +370,7 @@ void SocketTest::testTrxNoDelay() {
CPPUNIT_ASSERT( serverThread.getNumClients() == 1 );
- io::OutputStream* stream = client.getOutputStream();
+ io::OutputStream* stream = client->getOutputStream();
std::string msg = "reply";
stream->write( (unsigned char*)msg.c_str(), (int)msg.length() );
@@ -265,12 +382,12 @@ void SocketTest::testTrxNoDelay() {
unsigned char buf[500];
memset( buf, 0, 500 );
- io::InputStream* istream = client.getInputStream();
+ io::InputStream* istream = client->getInputStream();
std::size_t numRead = istream->read( buf, 500, 0, 500 );
CPPUNIT_ASSERT( numRead == 5 );
CPPUNIT_ASSERT( strcmp( (char*)buf, "hello" ) == 0 );
- client.close();
+ client->close();
serverThread.stop();
serverThread.join();
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=927638&r1=927637&r2=927638&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 Thu Mar 25 23:24:38 2010
@@ -21,13 +21,6 @@
#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{
@@ -45,107 +38,6 @@ namespace net{
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{
-
- if( stream->read( buf, 1000, 0, 1000 ) == -1 ) {
- done = true;
- continue;
- }
-
- lastMessage = (char*)buf;
-
- if( strcmp( (char*)buf, "reply" ) == 0 ){
- io::OutputStream* output = socket->getOutputStream();
- output->write( (unsigned char*)"hello", (int)strlen("hello"), 0, (int)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() {}